blob: 56ab1f5ea11bb0cb2917de61fc238548b81d94e6 [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>
Chris Mason34088782007-06-12 11:36:58 -040020#include <linux/sched.h>
Chris Masond3c2fdcf2007-09-17 10:58:06 -040021#include <linux/writeback.h>
Chris Mason5f39d392007-10-15 16:14:19 -040022#include <linux/pagemap.h>
Chris Mason5f2cc082008-11-07 18:22:45 -050023#include <linux/blkdev.h>
Chris Mason79154b12007-03-22 15:59:16 -040024#include "ctree.h"
25#include "disk-io.h"
26#include "transaction.h"
Chris Mason925baed2008-06-25 16:01:30 -040027#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040028#include "ref-cache.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{
Chris Mason2c90e5d2007-04-02 10:50:19 -040035 WARN_ON(transaction->use_count == 0);
Chris Mason79154b12007-03-22 15:59:16 -040036 transaction->use_count--;
Chris Mason78fae272007-03-25 11:35:08 -040037 if (transaction->use_count == 0) {
Chris Mason8fd17792007-04-19 21:01:03 -040038 list_del_init(&transaction->list);
Chris Mason2c90e5d2007-04-02 10:50:19 -040039 memset(transaction, 0, sizeof(*transaction));
40 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040041 }
Chris Mason79154b12007-03-22 15:59:16 -040042}
43
Chris Masond352ac62008-09-29 15:18:18 -040044/*
45 * either allocate a new transaction or hop into the existing one
46 */
Chris Mason80b67942008-02-01 16:35:04 -050047static noinline int join_transaction(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -040048{
49 struct btrfs_transaction *cur_trans;
50 cur_trans = root->fs_info->running_transaction;
51 if (!cur_trans) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040052 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
53 GFP_NOFS);
Chris Mason79154b12007-03-22 15:59:16 -040054 BUG_ON(!cur_trans);
Chris Mason0f7d52f2007-04-09 10:42:37 -040055 root->fs_info->generation++;
Chris Masone18e4802008-01-18 10:54:22 -050056 root->fs_info->last_alloc = 0;
Chris Mason4529ba42008-01-31 16:45:07 -050057 root->fs_info->last_data_alloc = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -040058 cur_trans->num_writers = 1;
59 cur_trans->num_joined = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040060 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040061 init_waitqueue_head(&cur_trans->writer_wait);
62 init_waitqueue_head(&cur_trans->commit_wait);
63 cur_trans->in_commit = 0;
Chris Masonf9295742008-07-17 12:54:14 -040064 cur_trans->blocked = 0;
Chris Masond5719762007-03-23 10:01:08 -040065 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040066 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040067 cur_trans->start_time = get_seconds();
Chris Mason3063d292008-01-08 15:46:30 -050068 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
Chris Mason8fd17792007-04-19 21:01:03 -040069 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
Chris Masond1310b22008-01-24 16:13:08 -050070 extent_io_tree_init(&cur_trans->dirty_pages,
Chris Mason5f39d392007-10-15 16:14:19 -040071 root->fs_info->btree_inode->i_mapping,
72 GFP_NOFS);
Chris Mason48ec2cf2008-06-09 09:35:50 -040073 spin_lock(&root->fs_info->new_trans_lock);
74 root->fs_info->running_transaction = cur_trans;
75 spin_unlock(&root->fs_info->new_trans_lock);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040076 } else {
77 cur_trans->num_writers++;
78 cur_trans->num_joined++;
Chris Mason79154b12007-03-22 15:59:16 -040079 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -040080
Chris Mason79154b12007-03-22 15:59:16 -040081 return 0;
82}
83
Chris Masond352ac62008-09-29 15:18:18 -040084/*
Chris Masond3977122009-01-05 21:25:51 -050085 * this does all the record keeping required to make sure that a reference
86 * counted root is properly recorded in a given transaction. This is required
87 * to make sure the old root from before we joined the transaction is deleted
88 * when the transaction commits
Chris Masond352ac62008-09-29 15:18:18 -040089 */
Chris Masone02119d2008-09-05 16:13:11 -040090noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
Chris Mason6702ed42007-08-07 16:15:09 -040091{
Yan Zhengf321e492008-07-30 09:26:11 -040092 struct btrfs_dirty_root *dirty;
Chris Mason6702ed42007-08-07 16:15:09 -040093 u64 running_trans_id = root->fs_info->running_transaction->transid;
94 if (root->ref_cows && root->last_trans < running_trans_id) {
95 WARN_ON(root == root->fs_info->extent_root);
96 if (root->root_item.refs != 0) {
97 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
98 (unsigned long)root->root_key.objectid,
99 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400100
101 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
102 BUG_ON(!dirty);
103 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
104 BUG_ON(!dirty->root);
Yan Zheng31153d82008-07-28 15:32:19 -0400105 dirty->latest_root = root;
106 INIT_LIST_HEAD(&dirty->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400107
Chris Mason925baed2008-06-25 16:01:30 -0400108 root->commit_root = btrfs_root_node(root);
Yan Zheng31153d82008-07-28 15:32:19 -0400109
110 memcpy(dirty->root, root, sizeof(*root));
111 spin_lock_init(&dirty->root->node_lock);
Yanbcc63ab2008-07-30 16:29:20 -0400112 spin_lock_init(&dirty->root->list_lock);
Yan Zheng31153d82008-07-28 15:32:19 -0400113 mutex_init(&dirty->root->objectid_mutex);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400114 mutex_init(&dirty->root->log_mutex);
Yanbcc63ab2008-07-30 16:29:20 -0400115 INIT_LIST_HEAD(&dirty->root->dead_list);
Yan Zheng31153d82008-07-28 15:32:19 -0400116 dirty->root->node = root->commit_root;
117 dirty->root->commit_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400118
119 spin_lock(&root->list_lock);
120 list_add(&dirty->root->dead_list, &root->dead_list);
121 spin_unlock(&root->list_lock);
122
123 root->dirty_root = dirty;
Chris Mason6702ed42007-08-07 16:15:09 -0400124 } else {
125 WARN_ON(1);
126 }
127 root->last_trans = running_trans_id;
128 }
129 return 0;
130}
131
Chris Masond352ac62008-09-29 15:18:18 -0400132/* wait for commit against the current transaction to become unblocked
133 * when this is done, it is safe to start a new transaction, but the current
134 * transaction might not be fully on disk.
135 */
Chris Mason37d1aee2008-07-31 10:48:37 -0400136static void wait_current_trans(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -0400137{
Chris Masonf9295742008-07-17 12:54:14 -0400138 struct btrfs_transaction *cur_trans;
Chris Mason79154b12007-03-22 15:59:16 -0400139
Chris Masonf9295742008-07-17 12:54:14 -0400140 cur_trans = root->fs_info->running_transaction;
Chris Mason37d1aee2008-07-31 10:48:37 -0400141 if (cur_trans && cur_trans->blocked) {
Chris Masonf9295742008-07-17 12:54:14 -0400142 DEFINE_WAIT(wait);
143 cur_trans->use_count++;
Chris Masond3977122009-01-05 21:25:51 -0500144 while (1) {
Chris Masonf9295742008-07-17 12:54:14 -0400145 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
146 TASK_UNINTERRUPTIBLE);
147 if (cur_trans->blocked) {
148 mutex_unlock(&root->fs_info->trans_mutex);
149 schedule();
150 mutex_lock(&root->fs_info->trans_mutex);
151 finish_wait(&root->fs_info->transaction_wait,
152 &wait);
153 } else {
154 finish_wait(&root->fs_info->transaction_wait,
155 &wait);
156 break;
157 }
158 }
159 put_transaction(cur_trans);
160 }
Chris Mason37d1aee2008-07-31 10:48:37 -0400161}
162
Chris Masone02119d2008-09-05 16:13:11 -0400163static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
Sage Weil9ca9ee02008-08-04 10:41:27 -0400164 int num_blocks, int wait)
Chris Mason37d1aee2008-07-31 10:48:37 -0400165{
166 struct btrfs_trans_handle *h =
167 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
168 int ret;
169
170 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4bef0842008-09-08 11:18:08 -0400171 if (!root->fs_info->log_root_recovering &&
172 ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
Chris Mason37d1aee2008-07-31 10:48:37 -0400173 wait_current_trans(root);
Chris Mason79154b12007-03-22 15:59:16 -0400174 ret = join_transaction(root);
175 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400176
Chris Masone02119d2008-09-05 16:13:11 -0400177 btrfs_record_root_in_trans(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400178 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400179 h->transaction = root->fs_info->running_transaction;
180 h->blocks_reserved = num_blocks;
181 h->blocks_used = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500182 h->block_group = 0;
Chris Mason26b80032007-08-08 20:17:12 -0400183 h->alloc_exclude_nr = 0;
184 h->alloc_exclude_start = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400185 root->fs_info->running_transaction->use_count++;
186 mutex_unlock(&root->fs_info->trans_mutex);
187 return h;
188}
189
Chris Masonf9295742008-07-17 12:54:14 -0400190struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
191 int num_blocks)
192{
Sage Weil9ca9ee02008-08-04 10:41:27 -0400193 return start_transaction(root, num_blocks, 1);
Chris Masonf9295742008-07-17 12:54:14 -0400194}
195struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
196 int num_blocks)
197{
Sage Weil9ca9ee02008-08-04 10:41:27 -0400198 return start_transaction(root, num_blocks, 0);
Chris Masonf9295742008-07-17 12:54:14 -0400199}
200
Sage Weil9ca9ee02008-08-04 10:41:27 -0400201struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
202 int num_blocks)
203{
204 return start_transaction(r, num_blocks, 2);
205}
206
Chris Masond352ac62008-09-29 15:18:18 -0400207/* wait for a transaction commit to be fully complete */
Chris Mason89ce8a62008-06-25 16:01:31 -0400208static noinline int wait_for_commit(struct btrfs_root *root,
209 struct btrfs_transaction *commit)
210{
211 DEFINE_WAIT(wait);
212 mutex_lock(&root->fs_info->trans_mutex);
Chris Masond3977122009-01-05 21:25:51 -0500213 while (!commit->commit_done) {
Chris Mason89ce8a62008-06-25 16:01:31 -0400214 prepare_to_wait(&commit->commit_wait, &wait,
215 TASK_UNINTERRUPTIBLE);
216 if (commit->commit_done)
217 break;
218 mutex_unlock(&root->fs_info->trans_mutex);
219 schedule();
220 mutex_lock(&root->fs_info->trans_mutex);
221 }
222 mutex_unlock(&root->fs_info->trans_mutex);
223 finish_wait(&commit->commit_wait, &wait);
224 return 0;
225}
226
Chris Masond352ac62008-09-29 15:18:18 -0400227/*
Chris Masond3977122009-01-05 21:25:51 -0500228 * rate limit against the drop_snapshot code. This helps to slow down new
229 * operations if the drop_snapshot code isn't able to keep up.
Chris Masond352ac62008-09-29 15:18:18 -0400230 */
Chris Mason37d1aee2008-07-31 10:48:37 -0400231static void throttle_on_drops(struct btrfs_root *root)
Chris Masonab78c842008-07-29 16:15:18 -0400232{
233 struct btrfs_fs_info *info = root->fs_info;
Chris Mason2dd3e672008-08-04 08:20:15 -0400234 int harder_count = 0;
Chris Masonab78c842008-07-29 16:15:18 -0400235
Chris Mason2dd3e672008-08-04 08:20:15 -0400236harder:
Chris Masonab78c842008-07-29 16:15:18 -0400237 if (atomic_read(&info->throttles)) {
238 DEFINE_WAIT(wait);
239 int thr;
Chris Masonab78c842008-07-29 16:15:18 -0400240 thr = atomic_read(&info->throttle_gen);
241
242 do {
243 prepare_to_wait(&info->transaction_throttle,
244 &wait, TASK_UNINTERRUPTIBLE);
245 if (!atomic_read(&info->throttles)) {
246 finish_wait(&info->transaction_throttle, &wait);
247 break;
248 }
249 schedule();
250 finish_wait(&info->transaction_throttle, &wait);
251 } while (thr == atomic_read(&info->throttle_gen));
Chris Mason2dd3e672008-08-04 08:20:15 -0400252 harder_count++;
253
254 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
255 harder_count < 2)
256 goto harder;
257
258 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
259 harder_count < 10)
260 goto harder;
261
262 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
263 harder_count < 20)
264 goto harder;
Chris Masonab78c842008-07-29 16:15:18 -0400265 }
266}
267
Chris Mason37d1aee2008-07-31 10:48:37 -0400268void btrfs_throttle(struct btrfs_root *root)
269{
270 mutex_lock(&root->fs_info->trans_mutex);
Sage Weil9ca9ee02008-08-04 10:41:27 -0400271 if (!root->fs_info->open_ioctl_trans)
272 wait_current_trans(root);
Chris Mason37d1aee2008-07-31 10:48:37 -0400273 mutex_unlock(&root->fs_info->trans_mutex);
274
275 throttle_on_drops(root);
276}
277
Chris Mason89ce8a62008-06-25 16:01:31 -0400278static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
279 struct btrfs_root *root, int throttle)
Chris Mason79154b12007-03-22 15:59:16 -0400280{
281 struct btrfs_transaction *cur_trans;
Chris Masonab78c842008-07-29 16:15:18 -0400282 struct btrfs_fs_info *info = root->fs_info;
Chris Masond6e4a422007-04-06 15:37:36 -0400283
Chris Masonab78c842008-07-29 16:15:18 -0400284 mutex_lock(&info->trans_mutex);
285 cur_trans = info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400286 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400287 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400288 cur_trans->num_writers--;
Chris Mason89ce8a62008-06-25 16:01:31 -0400289
Chris Mason79154b12007-03-22 15:59:16 -0400290 if (waitqueue_active(&cur_trans->writer_wait))
291 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400292 put_transaction(cur_trans);
Chris Masonab78c842008-07-29 16:15:18 -0400293 mutex_unlock(&info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400294 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400295 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Masonab78c842008-07-29 16:15:18 -0400296
297 if (throttle)
Chris Mason37d1aee2008-07-31 10:48:37 -0400298 throttle_on_drops(root);
Chris Masonab78c842008-07-29 16:15:18 -0400299
Chris Mason79154b12007-03-22 15:59:16 -0400300 return 0;
301}
302
Chris Mason89ce8a62008-06-25 16:01:31 -0400303int btrfs_end_transaction(struct btrfs_trans_handle *trans,
304 struct btrfs_root *root)
305{
306 return __btrfs_end_transaction(trans, root, 0);
307}
308
309int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
310 struct btrfs_root *root)
311{
312 return __btrfs_end_transaction(trans, root, 1);
313}
314
Chris Masond352ac62008-09-29 15:18:18 -0400315/*
316 * when btree blocks are allocated, they have some corresponding bits set for
317 * them in one of two extent_io trees. This is used to make sure all of
318 * those extents are on disk for transaction or log commit
319 */
Chris Masond0c803c2008-09-11 16:17:57 -0400320int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
321 struct extent_io_tree *dirty_pages)
Chris Mason79154b12007-03-22 15:59:16 -0400322{
Chris Mason7c4452b2007-04-28 09:29:35 -0400323 int ret;
Chris Mason777e6bd2008-08-15 15:34:15 -0400324 int err = 0;
Chris Mason7c4452b2007-04-28 09:29:35 -0400325 int werr = 0;
326 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400327 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason777e6bd2008-08-15 15:34:15 -0400328 u64 start = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400329 u64 end;
330 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400331
Chris Masond3977122009-01-05 21:25:51 -0500332 while (1) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400333 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
Chris Mason5f39d392007-10-15 16:14:19 -0400334 EXTENT_DIRTY);
335 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400336 break;
Chris Masond3977122009-01-05 21:25:51 -0500337 while (start <= end) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400338 cond_resched();
339
Chris Mason5f39d392007-10-15 16:14:19 -0400340 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400341 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason4bef0842008-09-08 11:18:08 -0400342 page = find_get_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400343 if (!page)
344 continue;
Chris Mason4bef0842008-09-08 11:18:08 -0400345
346 btree_lock_page_hook(page);
347 if (!page->mapping) {
348 unlock_page(page);
349 page_cache_release(page);
350 continue;
351 }
352
Chris Mason6702ed42007-08-07 16:15:09 -0400353 if (PageWriteback(page)) {
354 if (PageDirty(page))
355 wait_on_page_writeback(page);
356 else {
357 unlock_page(page);
358 page_cache_release(page);
359 continue;
360 }
361 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400362 err = write_one_page(page, 0);
363 if (err)
364 werr = err;
365 page_cache_release(page);
366 }
367 }
Chris Masond3977122009-01-05 21:25:51 -0500368 while (1) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400369 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
370 EXTENT_DIRTY);
371 if (ret)
372 break;
373
374 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500375 while (start <= end) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400376 index = start >> PAGE_CACHE_SHIFT;
377 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
378 page = find_get_page(btree_inode->i_mapping, index);
379 if (!page)
380 continue;
381 if (PageDirty(page)) {
Chris Mason4bef0842008-09-08 11:18:08 -0400382 btree_lock_page_hook(page);
383 wait_on_page_writeback(page);
Chris Mason777e6bd2008-08-15 15:34:15 -0400384 err = write_one_page(page, 0);
385 if (err)
386 werr = err;
387 }
Chris Mason105d9312008-11-18 12:13:12 -0500388 wait_on_page_writeback(page);
Chris Mason777e6bd2008-08-15 15:34:15 -0400389 page_cache_release(page);
390 cond_resched();
391 }
392 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400393 if (err)
394 werr = err;
395 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400396}
397
Chris Masond0c803c2008-09-11 16:17:57 -0400398int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
399 struct btrfs_root *root)
400{
401 if (!trans || !trans->transaction) {
402 struct inode *btree_inode;
403 btree_inode = root->fs_info->btree_inode;
404 return filemap_write_and_wait(btree_inode->i_mapping);
405 }
406 return btrfs_write_and_wait_marked_extents(root,
407 &trans->transaction->dirty_pages);
408}
409
Chris Masond352ac62008-09-29 15:18:18 -0400410/*
411 * this is used to update the root pointer in the tree of tree roots.
412 *
413 * But, in the case of the extent allocation tree, updating the root
414 * pointer may allocate blocks which may change the root of the extent
415 * allocation tree.
416 *
417 * So, this loops and repeats and makes sure the cowonly root didn't
418 * change while the root pointer was being updated in the metadata.
419 */
Chris Mason0b86a832008-03-24 15:01:56 -0400420static int update_cowonly_root(struct btrfs_trans_handle *trans,
421 struct btrfs_root *root)
422{
423 int ret;
424 u64 old_root_bytenr;
425 struct btrfs_root *tree_root = root->fs_info->tree_root;
426
Chris Mason87ef2bb2008-10-30 11:23:27 -0400427 btrfs_extent_post_op(trans, root);
Chris Mason0b86a832008-03-24 15:01:56 -0400428 btrfs_write_dirty_block_groups(trans, root);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400429 btrfs_extent_post_op(trans, root);
430
Chris Masond3977122009-01-05 21:25:51 -0500431 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -0400432 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
433 if (old_root_bytenr == root->node->start)
434 break;
435 btrfs_set_root_bytenr(&root->root_item,
436 root->node->start);
437 btrfs_set_root_level(&root->root_item,
438 btrfs_header_level(root->node));
Yan Zheng84234f32008-10-29 14:49:05 -0400439 btrfs_set_root_generation(&root->root_item, trans->transid);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400440
441 btrfs_extent_post_op(trans, root);
442
Chris Mason0b86a832008-03-24 15:01:56 -0400443 ret = btrfs_update_root(trans, tree_root,
444 &root->root_key,
445 &root->root_item);
446 BUG_ON(ret);
447 btrfs_write_dirty_block_groups(trans, root);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400448 btrfs_extent_post_op(trans, root);
Chris Mason0b86a832008-03-24 15:01:56 -0400449 }
450 return 0;
451}
452
Chris Masond352ac62008-09-29 15:18:18 -0400453/*
454 * update all the cowonly tree roots on disk
455 */
Chris Mason79154b12007-03-22 15:59:16 -0400456int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
457 struct btrfs_root *root)
458{
Chris Mason79154b12007-03-22 15:59:16 -0400459 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400460 struct list_head *next;
Yan Zheng84234f32008-10-29 14:49:05 -0400461 struct extent_buffer *eb;
462
Chris Mason87ef2bb2008-10-30 11:23:27 -0400463 btrfs_extent_post_op(trans, fs_info->tree_root);
464
Yan Zheng84234f32008-10-29 14:49:05 -0400465 eb = btrfs_lock_root_node(fs_info->tree_root);
466 btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb, 0);
467 btrfs_tree_unlock(eb);
468 free_extent_buffer(eb);
Chris Mason79154b12007-03-22 15:59:16 -0400469
Chris Mason87ef2bb2008-10-30 11:23:27 -0400470 btrfs_extent_post_op(trans, fs_info->tree_root);
471
Chris Masond3977122009-01-05 21:25:51 -0500472 while (!list_empty(&fs_info->dirty_cowonly_roots)) {
Chris Mason0b86a832008-03-24 15:01:56 -0400473 next = fs_info->dirty_cowonly_roots.next;
474 list_del_init(next);
475 root = list_entry(next, struct btrfs_root, dirty_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400476
Chris Mason0b86a832008-03-24 15:01:56 -0400477 update_cowonly_root(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400478 }
479 return 0;
480}
481
Chris Masond352ac62008-09-29 15:18:18 -0400482/*
483 * dead roots are old snapshots that need to be deleted. This allocates
484 * a dirty root struct and adds it into the list of dead roots that need to
485 * be deleted
486 */
Yan Zhengb48652c2008-08-04 23:23:47 -0400487int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
Chris Mason5eda7b52007-06-22 14:16:25 -0400488{
Yan Zhengf321e492008-07-30 09:26:11 -0400489 struct btrfs_dirty_root *dirty;
Chris Mason5eda7b52007-06-22 14:16:25 -0400490
491 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
492 if (!dirty)
493 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400494 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400495 dirty->latest_root = latest;
Yan Zhengb48652c2008-08-04 23:23:47 -0400496
497 mutex_lock(&root->fs_info->trans_mutex);
498 list_add(&dirty->list, &latest->fs_info->dead_roots);
499 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason5eda7b52007-06-22 14:16:25 -0400500 return 0;
501}
502
Chris Masond352ac62008-09-29 15:18:18 -0400503/*
504 * at transaction commit time we need to schedule the old roots for
505 * deletion via btrfs_drop_snapshot. This runs through all the
506 * reference counted roots that were modified in the current
507 * transaction and puts them into the drop list
508 */
Chris Mason80b67942008-02-01 16:35:04 -0500509static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
510 struct radix_tree_root *radix,
511 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400512{
Yan Zhengf321e492008-07-30 09:26:11 -0400513 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400514 struct btrfs_root *gang[8];
515 struct btrfs_root *root;
516 int i;
517 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400518 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400519 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400520
Chris Masond3977122009-01-05 21:25:51 -0500521 while (1) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400522 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
523 ARRAY_SIZE(gang),
524 BTRFS_ROOT_TRANS_TAG);
525 if (ret == 0)
526 break;
527 for (i = 0; i < ret; i++) {
528 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400529 radix_tree_tag_clear(radix,
530 (unsigned long)root->root_key.objectid,
531 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400532
533 BUG_ON(!root->ref_tree);
Chris Mason017e5362008-07-28 15:32:51 -0400534 dirty = root->dirty_root;
Yan Zheng31153d82008-07-28 15:32:19 -0400535
Chris Masone02119d2008-09-05 16:13:11 -0400536 btrfs_free_log(trans, root);
Yan Zhengf82d02d2008-10-29 14:49:05 -0400537 btrfs_free_reloc_root(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -0400538
Chris Mason0f7d52f2007-04-09 10:42:37 -0400539 if (root->commit_root == root->node) {
Chris Masondb945352007-10-15 16:15:53 -0400540 WARN_ON(root->node->start !=
541 btrfs_root_bytenr(&root->root_item));
Yan Zheng31153d82008-07-28 15:32:19 -0400542
Chris Mason5f39d392007-10-15 16:14:19 -0400543 free_extent_buffer(root->commit_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400544 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400545 root->dirty_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400546
547 spin_lock(&root->list_lock);
548 list_del_init(&dirty->root->dead_list);
549 spin_unlock(&root->list_lock);
550
Yan Zheng31153d82008-07-28 15:32:19 -0400551 kfree(dirty->root);
552 kfree(dirty);
Josef Bacik58176a92007-08-29 15:47:34 -0400553
554 /* make sure to update the root on disk
555 * so we get any updates to the block used
556 * counts
557 */
558 err = btrfs_update_root(trans,
559 root->fs_info->tree_root,
560 &root->root_key,
561 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400562 continue;
563 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400564
565 memset(&root->root_item.drop_progress, 0,
566 sizeof(struct btrfs_disk_key));
567 root->root_item.drop_level = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400568 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400569 root->dirty_root = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400570 root->root_key.offset = root->fs_info->generation;
Chris Masondb945352007-10-15 16:15:53 -0400571 btrfs_set_root_bytenr(&root->root_item,
572 root->node->start);
573 btrfs_set_root_level(&root->root_item,
574 btrfs_header_level(root->node));
Yan Zheng84234f32008-10-29 14:49:05 -0400575 btrfs_set_root_generation(&root->root_item,
576 root->root_key.offset);
577
Chris Mason0f7d52f2007-04-09 10:42:37 -0400578 err = btrfs_insert_root(trans, root->fs_info->tree_root,
579 &root->root_key,
580 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400581 if (err)
582 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400583
584 refs = btrfs_root_refs(&dirty->root->root_item);
585 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400586 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400587 &dirty->root->root_key,
588 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400589
590 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400591 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400592 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400593 } else {
594 WARN_ON(1);
Yan Zheng31153d82008-07-28 15:32:19 -0400595 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400596 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400597 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400598 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400599 }
600 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400601 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400602}
603
Chris Masond352ac62008-09-29 15:18:18 -0400604/*
605 * defrag a given btree. If cacheonly == 1, this won't read from the disk,
606 * otherwise every leaf in the btree is read and defragged.
607 */
Chris Masone9d0b132007-08-10 14:06:19 -0400608int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
609{
610 struct btrfs_fs_info *info = root->fs_info;
611 int ret;
612 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400613 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400614
Chris Masona2135012008-06-25 16:01:30 -0400615 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400616 if (root->defrag_running)
617 return 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400618 trans = btrfs_start_transaction(root, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400619 while (1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400620 root->defrag_running = 1;
621 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400622 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400623 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400624 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400625 cond_resched();
626
Chris Masone9d0b132007-08-10 14:06:19 -0400627 trans = btrfs_start_transaction(root, 1);
Chris Mason3f157a22008-06-25 16:01:31 -0400628 if (root->fs_info->closing || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400629 break;
630 }
631 root->defrag_running = 0;
Chris Masona2135012008-06-25 16:01:30 -0400632 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400633 btrfs_end_transaction(trans, root);
634 return 0;
635}
636
Chris Masond352ac62008-09-29 15:18:18 -0400637/*
638 * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
639 * all of them
640 */
Chris Mason80b67942008-02-01 16:35:04 -0500641static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
642 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400643{
Yan Zhengf321e492008-07-30 09:26:11 -0400644 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400645 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400646 unsigned long nr;
Chris Masondb945352007-10-15 16:15:53 -0400647 u64 num_bytes;
648 u64 bytes_used;
Yanbcc63ab2008-07-30 16:29:20 -0400649 u64 max_useless;
Chris Mason54aa1f42007-06-22 14:16:25 -0400650 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400651 int err;
652
Chris Masond3977122009-01-05 21:25:51 -0500653 while (!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400654 struct btrfs_root *root;
655
Yan Zhengf321e492008-07-30 09:26:11 -0400656 dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400657 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400658
Chris Masondb945352007-10-15 16:15:53 -0400659 num_bytes = btrfs_root_used(&dirty->root->root_item);
Josef Bacik58176a92007-08-29 15:47:34 -0400660 root = dirty->latest_root;
Chris Masona2135012008-06-25 16:01:30 -0400661 atomic_inc(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400662
Chris Masond3977122009-01-05 21:25:51 -0500663 while (1) {
Chris Mason9f3a7422007-08-07 15:52:19 -0400664 trans = btrfs_start_transaction(tree_root, 1);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400665 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400666 ret = btrfs_drop_snapshot(trans, dirty->root);
Chris Masond3977122009-01-05 21:25:51 -0500667 if (ret != -EAGAIN)
Chris Mason9f3a7422007-08-07 15:52:19 -0400668 break;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400669 mutex_unlock(&root->fs_info->drop_mutex);
Josef Bacik58176a92007-08-29 15:47:34 -0400670
Chris Mason9f3a7422007-08-07 15:52:19 -0400671 err = btrfs_update_root(trans,
672 tree_root,
673 &dirty->root->root_key,
674 &dirty->root->root_item);
675 if (err)
676 ret = err;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400677 nr = trans->blocks_used;
Chris Mason017e5362008-07-28 15:32:51 -0400678 ret = btrfs_end_transaction(trans, tree_root);
Chris Mason9f3a7422007-08-07 15:52:19 -0400679 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400680
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400681 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400682 cond_resched();
Chris Mason9f3a7422007-08-07 15:52:19 -0400683 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400684 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400685 atomic_dec(&root->fs_info->throttles);
Chris Mason017e5362008-07-28 15:32:51 -0400686 wake_up(&root->fs_info->transaction_throttle);
Josef Bacik58176a92007-08-29 15:47:34 -0400687
Chris Masondb945352007-10-15 16:15:53 -0400688 num_bytes -= btrfs_root_used(&dirty->root->root_item);
689 bytes_used = btrfs_root_used(&root->root_item);
690 if (num_bytes) {
Chris Masone02119d2008-09-05 16:13:11 -0400691 btrfs_record_root_in_trans(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400692 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400693 bytes_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400694 }
Chris Masona2135012008-06-25 16:01:30 -0400695
Chris Mason9f3a7422007-08-07 15:52:19 -0400696 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400697 if (ret) {
698 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400699 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400700 }
Chris Masona2135012008-06-25 16:01:30 -0400701 mutex_unlock(&root->fs_info->drop_mutex);
702
Yanbcc63ab2008-07-30 16:29:20 -0400703 spin_lock(&root->list_lock);
704 list_del_init(&dirty->root->dead_list);
705 if (!list_empty(&root->dead_list)) {
706 struct btrfs_root *oldest;
707 oldest = list_entry(root->dead_list.prev,
708 struct btrfs_root, dead_list);
709 max_useless = oldest->root_key.offset - 1;
710 } else {
711 max_useless = root->root_key.offset - 1;
712 }
713 spin_unlock(&root->list_lock);
714
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400715 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400716 ret = btrfs_end_transaction(trans, tree_root);
717 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400718
Zheng Yane4657682008-09-26 10:04:53 -0400719 ret = btrfs_remove_leaf_refs(root, max_useless, 0);
Yanbcc63ab2008-07-30 16:29:20 -0400720 BUG_ON(ret);
721
Chris Masonf510cfe2007-10-15 16:14:48 -0400722 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400723 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400724 kfree(dirty);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400725
726 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400727 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400728 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400729 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400730}
731
Chris Masond352ac62008-09-29 15:18:18 -0400732/*
733 * new snapshots need to be created at a very specific time in the
734 * transaction commit. This does the actual creation
735 */
Chris Mason80b67942008-02-01 16:35:04 -0500736static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500737 struct btrfs_fs_info *fs_info,
738 struct btrfs_pending_snapshot *pending)
739{
740 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500741 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500742 struct btrfs_root *tree_root = fs_info->tree_root;
743 struct btrfs_root *root = pending->root;
744 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400745 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500746 int ret;
747 u64 objectid;
748
Chris Mason80b67942008-02-01 16:35:04 -0500749 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
750 if (!new_root_item) {
751 ret = -ENOMEM;
752 goto fail;
753 }
Chris Mason3063d292008-01-08 15:46:30 -0500754 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
755 if (ret)
756 goto fail;
757
Yan Zheng80ff3852008-10-30 14:20:02 -0400758 btrfs_record_root_in_trans(root);
759 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
Chris Mason80b67942008-02-01 16:35:04 -0500760 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Chris Mason3063d292008-01-08 15:46:30 -0500761
762 key.objectid = objectid;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400763 key.offset = trans->transid;
Chris Mason3063d292008-01-08 15:46:30 -0500764 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
765
Chris Mason925baed2008-06-25 16:01:30 -0400766 old = btrfs_lock_root_node(root);
Chris Mason65b51a02008-08-01 15:11:20 -0400767 btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
Chris Mason3063d292008-01-08 15:46:30 -0500768
Chris Mason925baed2008-06-25 16:01:30 -0400769 btrfs_copy_root(trans, root, old, &tmp, objectid);
770 btrfs_tree_unlock(old);
771 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -0500772
Chris Mason80b67942008-02-01 16:35:04 -0500773 btrfs_set_root_bytenr(new_root_item, tmp->start);
774 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
Yan Zheng84234f32008-10-29 14:49:05 -0400775 btrfs_set_root_generation(new_root_item, trans->transid);
Chris Mason3063d292008-01-08 15:46:30 -0500776 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
Chris Mason80b67942008-02-01 16:35:04 -0500777 new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -0400778 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -0500779 free_extent_buffer(tmp);
780 if (ret)
781 goto fail;
782
Chris Mason3de45862008-11-17 21:02:50 -0500783 key.offset = (u64)-1;
784 memcpy(&pending->root_key, &key, sizeof(key));
785fail:
786 kfree(new_root_item);
787 return ret;
788}
789
790static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info,
791 struct btrfs_pending_snapshot *pending)
792{
793 int ret;
794 int namelen;
795 u64 index = 0;
796 struct btrfs_trans_handle *trans;
797 struct inode *parent_inode;
798 struct inode *inode;
Chris Mason0660b5a2008-11-17 20:37:39 -0500799 struct btrfs_root *parent_root;
Chris Mason3de45862008-11-17 21:02:50 -0500800
Chris Mason3394e162008-11-17 20:42:26 -0500801 parent_inode = pending->dentry->d_parent->d_inode;
Chris Mason0660b5a2008-11-17 20:37:39 -0500802 parent_root = BTRFS_I(parent_inode)->root;
803 trans = btrfs_start_transaction(parent_root, 1);
Chris Mason3de45862008-11-17 21:02:50 -0500804
Chris Mason3063d292008-01-08 15:46:30 -0500805 /*
806 * insert the directory item
807 */
Sven Wegener3b963622008-06-09 21:57:42 -0400808 namelen = strlen(pending->name);
Chris Mason3de45862008-11-17 21:02:50 -0500809 ret = btrfs_set_inode_index(parent_inode, &index);
Chris Mason0660b5a2008-11-17 20:37:39 -0500810 ret = btrfs_insert_dir_item(trans, parent_root,
Chris Mason3de45862008-11-17 21:02:50 -0500811 pending->name, namelen,
812 parent_inode->i_ino,
813 &pending->root_key, BTRFS_FT_DIR, index);
Chris Mason3063d292008-01-08 15:46:30 -0500814
815 if (ret)
816 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500817
Yan Zheng52c26172009-01-05 15:43:43 -0500818 btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2);
819 ret = btrfs_update_inode(trans, parent_root, parent_inode);
820 BUG_ON(ret);
821
Chris Mason0660b5a2008-11-17 20:37:39 -0500822 /* add the backref first */
823 ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
824 pending->root_key.objectid,
825 BTRFS_ROOT_BACKREF_KEY,
826 parent_root->root_key.objectid,
827 parent_inode->i_ino, index, pending->name,
828 namelen);
829
830 BUG_ON(ret);
831
832 /* now add the forward ref */
833 ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
834 parent_root->root_key.objectid,
835 BTRFS_ROOT_REF_KEY,
836 pending->root_key.objectid,
837 parent_inode->i_ino, index, pending->name,
838 namelen);
839
Chris Mason3de45862008-11-17 21:02:50 -0500840 inode = btrfs_lookup_dentry(parent_inode, pending->dentry);
841 d_instantiate(pending->dentry, inode);
Chris Mason3063d292008-01-08 15:46:30 -0500842fail:
Chris Mason3de45862008-11-17 21:02:50 -0500843 btrfs_end_transaction(trans, fs_info->fs_root);
Chris Mason3063d292008-01-08 15:46:30 -0500844 return ret;
845}
846
Chris Masond352ac62008-09-29 15:18:18 -0400847/*
848 * create all the snapshots we've scheduled for creation
849 */
Chris Mason80b67942008-02-01 16:35:04 -0500850static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
851 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -0500852{
853 struct btrfs_pending_snapshot *pending;
854 struct list_head *head = &trans->transaction->pending_snapshots;
Chris Mason3de45862008-11-17 21:02:50 -0500855 struct list_head *cur;
856 int ret;
857
858 list_for_each(cur, head) {
859 pending = list_entry(cur, struct btrfs_pending_snapshot, list);
860 ret = create_pending_snapshot(trans, fs_info, pending);
861 BUG_ON(ret);
862 }
863 return 0;
864}
865
866static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans,
867 struct btrfs_fs_info *fs_info)
868{
869 struct btrfs_pending_snapshot *pending;
870 struct list_head *head = &trans->transaction->pending_snapshots;
Chris Mason3063d292008-01-08 15:46:30 -0500871 int ret;
872
Chris Masond3977122009-01-05 21:25:51 -0500873 while (!list_empty(head)) {
Chris Mason3063d292008-01-08 15:46:30 -0500874 pending = list_entry(head->next,
875 struct btrfs_pending_snapshot, list);
Chris Mason3de45862008-11-17 21:02:50 -0500876 ret = finish_pending_snapshot(fs_info, pending);
Chris Mason3063d292008-01-08 15:46:30 -0500877 BUG_ON(ret);
878 list_del(&pending->list);
879 kfree(pending->name);
880 kfree(pending);
881 }
Chris Masondc17ff82008-01-08 15:46:30 -0500882 return 0;
883}
884
Chris Mason79154b12007-03-22 15:59:16 -0400885int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
886 struct btrfs_root *root)
887{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400888 unsigned long joined = 0;
889 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400890 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400891 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400892 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400893 struct list_head dirty_fs_roots;
Chris Masond1310b22008-01-24 16:13:08 -0500894 struct extent_io_tree *pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400895 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400896 int ret;
Chris Mason79154b12007-03-22 15:59:16 -0400897
Chris Mason0f7d52f2007-04-09 10:42:37 -0400898 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Mason79154b12007-03-22 15:59:16 -0400899 mutex_lock(&root->fs_info->trans_mutex);
900 if (trans->transaction->in_commit) {
901 cur_trans = trans->transaction;
902 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400903 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400904 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400905
Chris Mason79154b12007-03-22 15:59:16 -0400906 ret = wait_for_commit(root, cur_trans);
907 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400908
909 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400910 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400911 mutex_unlock(&root->fs_info->trans_mutex);
912
Chris Mason79154b12007-03-22 15:59:16 -0400913 return 0;
914 }
Chris Mason4313b392008-01-03 09:08:48 -0500915
916 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
917 if (!pinned_copy)
918 return -ENOMEM;
919
Chris Masond1310b22008-01-24 16:13:08 -0500920 extent_io_tree_init(pinned_copy,
Chris Mason4313b392008-01-03 09:08:48 -0500921 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
922
Chris Mason2c90e5d2007-04-02 10:50:19 -0400923 trans->transaction->in_commit = 1;
Chris Masonf9295742008-07-17 12:54:14 -0400924 trans->transaction->blocked = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400925 cur_trans = trans->transaction;
926 if (cur_trans->list.prev != &root->fs_info->trans_list) {
927 prev_trans = list_entry(cur_trans->list.prev,
928 struct btrfs_transaction, list);
929 if (!prev_trans->commit_done) {
930 prev_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400931 mutex_unlock(&root->fs_info->trans_mutex);
932
933 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400934
Chris Masonccd467d2007-06-28 15:57:36 -0400935 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400936 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400937 }
938 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400939
940 do {
Yan Zheng7ea394f2008-08-05 13:05:02 -0400941 int snap_pending = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400942 joined = cur_trans->num_joined;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400943 if (!list_empty(&trans->transaction->pending_snapshots))
944 snap_pending = 1;
945
Chris Mason2c90e5d2007-04-02 10:50:19 -0400946 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400947 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -0400948 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400949
950 if (cur_trans->num_writers > 1)
951 timeout = MAX_SCHEDULE_TIMEOUT;
952 else
953 timeout = 1;
954
Chris Mason79154b12007-03-22 15:59:16 -0400955 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400956
Yan Zheng7ea394f2008-08-05 13:05:02 -0400957 if (snap_pending) {
958 ret = btrfs_wait_ordered_extents(root, 1);
959 BUG_ON(ret);
960 }
961
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400962 schedule_timeout(timeout);
963
Chris Mason79154b12007-03-22 15:59:16 -0400964 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400965 finish_wait(&cur_trans->writer_wait, &wait);
966 } while (cur_trans->num_writers > 1 ||
967 (cur_trans->num_joined != joined));
968
Chris Mason3063d292008-01-08 15:46:30 -0500969 ret = create_pending_snapshots(trans, root->fs_info);
970 BUG_ON(ret);
971
Chris Mason2c90e5d2007-04-02 10:50:19 -0400972 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -0500973
Chris Masone02119d2008-09-05 16:13:11 -0400974 /* btrfs_commit_tree_roots is responsible for getting the
975 * various roots consistent with each other. Every pointer
976 * in the tree of tree roots has to point to the most up to date
977 * root for every subvolume and other tree. So, we have to keep
978 * the tree logging code from jumping in and changing any
979 * of the trees.
980 *
981 * At this point in the commit, there can't be any tree-log
982 * writers, but a little lower down we drop the trans mutex
983 * and let new people in. By holding the tree_log_mutex
984 * from now until after the super is written, we avoid races
985 * with the tree-log code.
986 */
987 mutex_lock(&root->fs_info->tree_log_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -0400988 /*
989 * keep tree reloc code from adding new reloc trees
990 */
991 mutex_lock(&root->fs_info->tree_reloc_mutex);
992
Chris Masone02119d2008-09-05 16:13:11 -0400993
Chris Mason54aa1f42007-06-22 14:16:25 -0400994 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
995 &dirty_fs_roots);
996 BUG_ON(ret);
997
Chris Masone02119d2008-09-05 16:13:11 -0400998 /* add_dirty_roots gets rid of all the tree log roots, it is now
999 * safe to free the root of tree log roots
1000 */
1001 btrfs_free_log_root_tree(trans, root->fs_info);
1002
Chris Mason79154b12007-03-22 15:59:16 -04001003 ret = btrfs_commit_tree_roots(trans, root);
1004 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -04001005
Chris Mason78fae272007-03-25 11:35:08 -04001006 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -05001007 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -04001008 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -05001009 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason4b52dff2007-06-26 10:06:50 -04001010 btrfs_set_super_generation(&root->fs_info->super_copy,
1011 cur_trans->transid);
1012 btrfs_set_super_root(&root->fs_info->super_copy,
Chris Masondb945352007-10-15 16:15:53 -04001013 root->fs_info->tree_root->node->start);
1014 btrfs_set_super_root_level(&root->fs_info->super_copy,
1015 btrfs_header_level(root->fs_info->tree_root->node));
Chris Mason5f39d392007-10-15 16:14:19 -04001016
Chris Mason0b86a832008-03-24 15:01:56 -04001017 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
1018 chunk_root->node->start);
1019 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
1020 btrfs_header_level(chunk_root->node));
Yan Zheng84234f32008-10-29 14:49:05 -04001021 btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
1022 btrfs_header_generation(chunk_root->node));
Chris Masone02119d2008-09-05 16:13:11 -04001023
1024 if (!root->fs_info->log_root_recovering) {
1025 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1026 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1027 }
1028
Chris Masona061fc82008-05-07 11:43:44 -04001029 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
1030 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -04001031
Chris Mason4313b392008-01-03 09:08:48 -05001032 btrfs_copy_pinned(root, pinned_copy);
Chris Masonccd467d2007-06-28 15:57:36 -04001033
Chris Masonf9295742008-07-17 12:54:14 -04001034 trans->transaction->blocked = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001035 wake_up(&root->fs_info->transaction_throttle);
Chris Masonf9295742008-07-17 12:54:14 -04001036 wake_up(&root->fs_info->transaction_wait);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001037
Chris Mason78fae272007-03-25 11:35:08 -04001038 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -04001039 ret = btrfs_write_and_wait_transaction(trans, root);
1040 BUG_ON(ret);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001041 write_ctree_super(trans, root, 0);
Chris Mason4313b392008-01-03 09:08:48 -05001042
Chris Masone02119d2008-09-05 16:13:11 -04001043 /*
1044 * the super is written, we can safely allow the tree-loggers
1045 * to go about their business
1046 */
1047 mutex_unlock(&root->fs_info->tree_log_mutex);
1048
Chris Mason4313b392008-01-03 09:08:48 -05001049 btrfs_finish_extent_commit(trans, root, pinned_copy);
Chris Mason4313b392008-01-03 09:08:48 -05001050 kfree(pinned_copy);
1051
Zheng Yan1a40e232008-09-26 10:09:34 -04001052 btrfs_drop_dead_reloc_roots(root);
1053 mutex_unlock(&root->fs_info->tree_reloc_mutex);
1054
Chris Mason3de45862008-11-17 21:02:50 -05001055 /* do the directory inserts of any pending snapshot creations */
1056 finish_pending_snapshots(trans, root->fs_info);
1057
Zheng Yan1a40e232008-09-26 10:09:34 -04001058 mutex_lock(&root->fs_info->trans_mutex);
1059
Chris Mason2c90e5d2007-04-02 10:50:19 -04001060 cur_trans->commit_done = 1;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001061 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001062 wake_up(&cur_trans->commit_wait);
Chris Mason3de45862008-11-17 21:02:50 -05001063
Chris Mason79154b12007-03-22 15:59:16 -04001064 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -04001065 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -04001066
Yanbcc63ab2008-07-30 16:29:20 -04001067 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -04001068 if (root->fs_info->closing)
1069 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
Josef Bacik58176a92007-08-29 15:47:34 -04001070
Chris Mason78fae272007-03-25 11:35:08 -04001071 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason3de45862008-11-17 21:02:50 -05001072
Chris Mason2c90e5d2007-04-02 10:50:19 -04001073 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -04001074
Chris Masond3977122009-01-05 21:25:51 -05001075 if (root->fs_info->closing)
Chris Masonfacda1e2007-06-08 18:11:48 -04001076 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Mason79154b12007-03-22 15:59:16 -04001077 return ret;
1078}
1079
Chris Masond352ac62008-09-29 15:18:18 -04001080/*
1081 * interface function to delete all the snapshots we have scheduled for deletion
1082 */
Chris Masone9d0b132007-08-10 14:06:19 -04001083int btrfs_clean_old_snapshots(struct btrfs_root *root)
1084{
1085 struct list_head dirty_roots;
1086 INIT_LIST_HEAD(&dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -04001087again:
Chris Masone9d0b132007-08-10 14:06:19 -04001088 mutex_lock(&root->fs_info->trans_mutex);
1089 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
1090 mutex_unlock(&root->fs_info->trans_mutex);
1091
1092 if (!list_empty(&dirty_roots)) {
1093 drop_dirty_roots(root, &dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -04001094 goto again;
Chris Masone9d0b132007-08-10 14:06:19 -04001095 }
1096 return 0;
1097}