xref: /openbmc/linux/fs/btrfs/transaction.c (revision 4bef0848)
16cbd5570SChris Mason /*
26cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
1979154b1bSChris Mason #include <linux/fs.h>
2034088780SChris Mason #include <linux/sched.h>
21d3c2fdcfSChris Mason #include <linux/writeback.h>
225f39d397SChris Mason #include <linux/pagemap.h>
2379154b1bSChris Mason #include "ctree.h"
2479154b1bSChris Mason #include "disk-io.h"
2579154b1bSChris Mason #include "transaction.h"
26925baeddSChris Mason #include "locking.h"
2731153d81SYan Zheng #include "ref-cache.h"
28e02119d5SChris Mason #include "tree-log.h"
2979154b1bSChris Mason 
3078fae27eSChris Mason static int total_trans = 0;
312c90e5d6SChris Mason extern struct kmem_cache *btrfs_trans_handle_cachep;
322c90e5d6SChris Mason extern struct kmem_cache *btrfs_transaction_cachep;
332c90e5d6SChris Mason 
340f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0
350f7d52f4SChris Mason 
3680b6794dSChris Mason static noinline void put_transaction(struct btrfs_transaction *transaction)
3779154b1bSChris Mason {
382c90e5d6SChris Mason 	WARN_ON(transaction->use_count == 0);
3979154b1bSChris Mason 	transaction->use_count--;
4078fae27eSChris Mason 	if (transaction->use_count == 0) {
4178fae27eSChris Mason 		WARN_ON(total_trans == 0);
4278fae27eSChris Mason 		total_trans--;
438fd17795SChris Mason 		list_del_init(&transaction->list);
442c90e5d6SChris Mason 		memset(transaction, 0, sizeof(*transaction));
452c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
4679154b1bSChris Mason 	}
4778fae27eSChris Mason }
4879154b1bSChris Mason 
4980b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root)
5079154b1bSChris Mason {
5179154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
5279154b1bSChris Mason 	cur_trans = root->fs_info->running_transaction;
5379154b1bSChris Mason 	if (!cur_trans) {
542c90e5d6SChris Mason 		cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
552c90e5d6SChris Mason 					     GFP_NOFS);
5678fae27eSChris Mason 		total_trans++;
5779154b1bSChris Mason 		BUG_ON(!cur_trans);
580f7d52f4SChris Mason 		root->fs_info->generation++;
59e18e4809SChris Mason 		root->fs_info->last_alloc = 0;
604529ba49SChris Mason 		root->fs_info->last_data_alloc = 0;
61e02119d5SChris Mason 		root->fs_info->last_log_alloc = 0;
6215ee9bc7SJosef Bacik 		cur_trans->num_writers = 1;
6315ee9bc7SJosef Bacik 		cur_trans->num_joined = 0;
640f7d52f4SChris Mason 		cur_trans->transid = root->fs_info->generation;
6579154b1bSChris Mason 		init_waitqueue_head(&cur_trans->writer_wait);
6679154b1bSChris Mason 		init_waitqueue_head(&cur_trans->commit_wait);
6779154b1bSChris Mason 		cur_trans->in_commit = 0;
68f9295749SChris Mason 		cur_trans->blocked = 0;
69d5719762SChris Mason 		cur_trans->use_count = 1;
7079154b1bSChris Mason 		cur_trans->commit_done = 0;
7108607c1bSChris Mason 		cur_trans->start_time = get_seconds();
723063d29fSChris Mason 		INIT_LIST_HEAD(&cur_trans->pending_snapshots);
738fd17795SChris Mason 		list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
74d1310b2eSChris Mason 		extent_io_tree_init(&cur_trans->dirty_pages,
755f39d397SChris Mason 				     root->fs_info->btree_inode->i_mapping,
765f39d397SChris Mason 				     GFP_NOFS);
7748ec2cf8SChris Mason 		spin_lock(&root->fs_info->new_trans_lock);
7848ec2cf8SChris Mason 		root->fs_info->running_transaction = cur_trans;
7948ec2cf8SChris Mason 		spin_unlock(&root->fs_info->new_trans_lock);
8015ee9bc7SJosef Bacik 	} else {
8179154b1bSChris Mason 		cur_trans->num_writers++;
8215ee9bc7SJosef Bacik 		cur_trans->num_joined++;
8315ee9bc7SJosef Bacik 	}
8415ee9bc7SJosef Bacik 
8579154b1bSChris Mason 	return 0;
8679154b1bSChris Mason }
8779154b1bSChris Mason 
88e02119d5SChris Mason noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
896702ed49SChris Mason {
90f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
916702ed49SChris Mason 	u64 running_trans_id = root->fs_info->running_transaction->transid;
926702ed49SChris Mason 	if (root->ref_cows && root->last_trans < running_trans_id) {
936702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
946702ed49SChris Mason 		if (root->root_item.refs != 0) {
956702ed49SChris Mason 			radix_tree_tag_set(&root->fs_info->fs_roots_radix,
966702ed49SChris Mason 				   (unsigned long)root->root_key.objectid,
976702ed49SChris Mason 				   BTRFS_ROOT_TRANS_TAG);
9831153d81SYan Zheng 
9931153d81SYan Zheng 			dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
10031153d81SYan Zheng 			BUG_ON(!dirty);
10131153d81SYan Zheng 			dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
10231153d81SYan Zheng 			BUG_ON(!dirty->root);
10331153d81SYan Zheng 			dirty->latest_root = root;
10431153d81SYan Zheng 			INIT_LIST_HEAD(&dirty->list);
10531153d81SYan Zheng 
106925baeddSChris Mason 			root->commit_root = btrfs_root_node(root);
10731153d81SYan Zheng 
10831153d81SYan Zheng 			memcpy(dirty->root, root, sizeof(*root));
10931153d81SYan Zheng 			spin_lock_init(&dirty->root->node_lock);
110bcc63abbSYan 			spin_lock_init(&dirty->root->list_lock);
11131153d81SYan Zheng 			mutex_init(&dirty->root->objectid_mutex);
112bcc63abbSYan 			INIT_LIST_HEAD(&dirty->root->dead_list);
11331153d81SYan Zheng 			dirty->root->node = root->commit_root;
11431153d81SYan Zheng 			dirty->root->commit_root = NULL;
115bcc63abbSYan 
116bcc63abbSYan 			spin_lock(&root->list_lock);
117bcc63abbSYan 			list_add(&dirty->root->dead_list, &root->dead_list);
118bcc63abbSYan 			spin_unlock(&root->list_lock);
119bcc63abbSYan 
120bcc63abbSYan 			root->dirty_root = dirty;
1216702ed49SChris Mason 		} else {
1226702ed49SChris Mason 			WARN_ON(1);
1236702ed49SChris Mason 		}
1246702ed49SChris Mason 		root->last_trans = running_trans_id;
1256702ed49SChris Mason 	}
1266702ed49SChris Mason 	return 0;
1276702ed49SChris Mason }
1286702ed49SChris Mason 
12937d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
13079154b1bSChris Mason {
131f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
13279154b1bSChris Mason 
133f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
13437d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
135f9295749SChris Mason 		DEFINE_WAIT(wait);
136f9295749SChris Mason 		cur_trans->use_count++;
137f9295749SChris Mason 		while(1) {
138f9295749SChris Mason 			prepare_to_wait(&root->fs_info->transaction_wait, &wait,
139f9295749SChris Mason 					TASK_UNINTERRUPTIBLE);
140f9295749SChris Mason 			if (cur_trans->blocked) {
141f9295749SChris Mason 				mutex_unlock(&root->fs_info->trans_mutex);
142f9295749SChris Mason 				schedule();
143f9295749SChris Mason 				mutex_lock(&root->fs_info->trans_mutex);
144f9295749SChris Mason 				finish_wait(&root->fs_info->transaction_wait,
145f9295749SChris Mason 					    &wait);
146f9295749SChris Mason 			} else {
147f9295749SChris Mason 				finish_wait(&root->fs_info->transaction_wait,
148f9295749SChris Mason 					    &wait);
149f9295749SChris Mason 				break;
150f9295749SChris Mason 			}
151f9295749SChris Mason 		}
152f9295749SChris Mason 		put_transaction(cur_trans);
153f9295749SChris Mason 	}
15437d1aeeeSChris Mason }
15537d1aeeeSChris Mason 
156e02119d5SChris Mason static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
1579ca9ee09SSage Weil 					     int num_blocks, int wait)
15837d1aeeeSChris Mason {
15937d1aeeeSChris Mason 	struct btrfs_trans_handle *h =
16037d1aeeeSChris Mason 		kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
16137d1aeeeSChris Mason 	int ret;
16237d1aeeeSChris Mason 
16337d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
1644bef0848SChris Mason 	if (!root->fs_info->log_root_recovering &&
1654bef0848SChris Mason 	    ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
16637d1aeeeSChris Mason 		wait_current_trans(root);
16779154b1bSChris Mason 	ret = join_transaction(root);
16879154b1bSChris Mason 	BUG_ON(ret);
1690f7d52f4SChris Mason 
170e02119d5SChris Mason 	btrfs_record_root_in_trans(root);
1716702ed49SChris Mason 	h->transid = root->fs_info->running_transaction->transid;
17279154b1bSChris Mason 	h->transaction = root->fs_info->running_transaction;
17379154b1bSChris Mason 	h->blocks_reserved = num_blocks;
17479154b1bSChris Mason 	h->blocks_used = 0;
17531f3c99bSChris Mason 	h->block_group = NULL;
17626b8003fSChris Mason 	h->alloc_exclude_nr = 0;
17726b8003fSChris Mason 	h->alloc_exclude_start = 0;
17879154b1bSChris Mason 	root->fs_info->running_transaction->use_count++;
17979154b1bSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
18079154b1bSChris Mason 	return h;
18179154b1bSChris Mason }
18279154b1bSChris Mason 
183f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
184f9295749SChris Mason 						   int num_blocks)
185f9295749SChris Mason {
1869ca9ee09SSage Weil 	return start_transaction(root, num_blocks, 1);
187f9295749SChris Mason }
188f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
189f9295749SChris Mason 						   int num_blocks)
190f9295749SChris Mason {
1919ca9ee09SSage Weil 	return start_transaction(root, num_blocks, 0);
192f9295749SChris Mason }
193f9295749SChris Mason 
1949ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
1959ca9ee09SSage Weil 							 int num_blocks)
1969ca9ee09SSage Weil {
1979ca9ee09SSage Weil 	return start_transaction(r, num_blocks, 2);
1989ca9ee09SSage Weil }
1999ca9ee09SSage Weil 
2009ca9ee09SSage Weil 
20189ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root,
20289ce8a63SChris Mason 				    struct btrfs_transaction *commit)
20389ce8a63SChris Mason {
20489ce8a63SChris Mason 	DEFINE_WAIT(wait);
20589ce8a63SChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
20689ce8a63SChris Mason 	while(!commit->commit_done) {
20789ce8a63SChris Mason 		prepare_to_wait(&commit->commit_wait, &wait,
20889ce8a63SChris Mason 				TASK_UNINTERRUPTIBLE);
20989ce8a63SChris Mason 		if (commit->commit_done)
21089ce8a63SChris Mason 			break;
21189ce8a63SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
21289ce8a63SChris Mason 		schedule();
21389ce8a63SChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
21489ce8a63SChris Mason 	}
21589ce8a63SChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
21689ce8a63SChris Mason 	finish_wait(&commit->commit_wait, &wait);
21789ce8a63SChris Mason 	return 0;
21889ce8a63SChris Mason }
21989ce8a63SChris Mason 
22037d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root)
221ab78c84dSChris Mason {
222ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
2232dd3e67bSChris Mason 	int harder_count = 0;
224ab78c84dSChris Mason 
2252dd3e67bSChris Mason harder:
226ab78c84dSChris Mason 	if (atomic_read(&info->throttles)) {
227ab78c84dSChris Mason 		DEFINE_WAIT(wait);
228ab78c84dSChris Mason 		int thr;
229ab78c84dSChris Mason 		thr = atomic_read(&info->throttle_gen);
230ab78c84dSChris Mason 
231ab78c84dSChris Mason 		do {
232ab78c84dSChris Mason 			prepare_to_wait(&info->transaction_throttle,
233ab78c84dSChris Mason 					&wait, TASK_UNINTERRUPTIBLE);
234ab78c84dSChris Mason 			if (!atomic_read(&info->throttles)) {
235ab78c84dSChris Mason 				finish_wait(&info->transaction_throttle, &wait);
236ab78c84dSChris Mason 				break;
237ab78c84dSChris Mason 			}
238ab78c84dSChris Mason 			schedule();
239ab78c84dSChris Mason 			finish_wait(&info->transaction_throttle, &wait);
240ab78c84dSChris Mason 		} while (thr == atomic_read(&info->throttle_gen));
2412dd3e67bSChris Mason 		harder_count++;
2422dd3e67bSChris Mason 
2432dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
2442dd3e67bSChris Mason 		    harder_count < 2)
2452dd3e67bSChris Mason 			goto harder;
2462dd3e67bSChris Mason 
2472dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
2482dd3e67bSChris Mason 		    harder_count < 10)
2492dd3e67bSChris Mason 			goto harder;
2502dd3e67bSChris Mason 
2512dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
2522dd3e67bSChris Mason 		    harder_count < 20)
2532dd3e67bSChris Mason 			goto harder;
254ab78c84dSChris Mason 	}
255ab78c84dSChris Mason }
256ab78c84dSChris Mason 
25737d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
25837d1aeeeSChris Mason {
25937d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
2609ca9ee09SSage Weil 	if (!root->fs_info->open_ioctl_trans)
26137d1aeeeSChris Mason 		wait_current_trans(root);
26237d1aeeeSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
26337d1aeeeSChris Mason 
26437d1aeeeSChris Mason 	throttle_on_drops(root);
26537d1aeeeSChris Mason }
26637d1aeeeSChris Mason 
26789ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
26889ce8a63SChris Mason 			  struct btrfs_root *root, int throttle)
26979154b1bSChris Mason {
27079154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
271ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
272d6e4a428SChris Mason 
273ab78c84dSChris Mason 	mutex_lock(&info->trans_mutex);
274ab78c84dSChris Mason 	cur_trans = info->running_transaction;
275ccd467d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
276d5719762SChris Mason 	WARN_ON(cur_trans->num_writers < 1);
277ccd467d6SChris Mason 	cur_trans->num_writers--;
27889ce8a63SChris Mason 
27979154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
28079154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
28179154b1bSChris Mason 	put_transaction(cur_trans);
282ab78c84dSChris Mason 	mutex_unlock(&info->trans_mutex);
283d6025579SChris Mason 	memset(trans, 0, sizeof(*trans));
2842c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
285ab78c84dSChris Mason 
286ab78c84dSChris Mason 	if (throttle)
28737d1aeeeSChris Mason 		throttle_on_drops(root);
288ab78c84dSChris Mason 
28979154b1bSChris Mason 	return 0;
29079154b1bSChris Mason }
29179154b1bSChris Mason 
29289ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
29389ce8a63SChris Mason 			  struct btrfs_root *root)
29489ce8a63SChris Mason {
29589ce8a63SChris Mason 	return __btrfs_end_transaction(trans, root, 0);
29689ce8a63SChris Mason }
29789ce8a63SChris Mason 
29889ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
29989ce8a63SChris Mason 				   struct btrfs_root *root)
30089ce8a63SChris Mason {
30189ce8a63SChris Mason 	return __btrfs_end_transaction(trans, root, 1);
30289ce8a63SChris Mason }
30389ce8a63SChris Mason 
30479154b1bSChris Mason 
30579154b1bSChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
30679154b1bSChris Mason 				     struct btrfs_root *root)
30779154b1bSChris Mason {
3087c4452b9SChris Mason 	int ret;
309777e6bd7SChris Mason 	int err = 0;
3107c4452b9SChris Mason 	int werr = 0;
311d1310b2eSChris Mason 	struct extent_io_tree *dirty_pages;
3127c4452b9SChris Mason 	struct page *page;
3137c4452b9SChris Mason 	struct inode *btree_inode = root->fs_info->btree_inode;
314777e6bd7SChris Mason 	u64 start = 0;
3155f39d397SChris Mason 	u64 end;
3165f39d397SChris Mason 	unsigned long index;
3177c4452b9SChris Mason 
3187c4452b9SChris Mason 	if (!trans || !trans->transaction) {
3197c4452b9SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
3207c4452b9SChris Mason 	}
3217c4452b9SChris Mason 	dirty_pages = &trans->transaction->dirty_pages;
3227c4452b9SChris Mason 	while(1) {
323777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, start, &start, &end,
3245f39d397SChris Mason 					    EXTENT_DIRTY);
3255f39d397SChris Mason 		if (ret)
3267c4452b9SChris Mason 			break;
3275f39d397SChris Mason 		while(start <= end) {
328777e6bd7SChris Mason 			cond_resched();
329777e6bd7SChris Mason 
3305f39d397SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
33135ebb934SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
3324bef0848SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
3337c4452b9SChris Mason 			if (!page)
3347c4452b9SChris Mason 				continue;
3354bef0848SChris Mason 
3364bef0848SChris Mason 			btree_lock_page_hook(page);
3374bef0848SChris Mason 			if (!page->mapping) {
3384bef0848SChris Mason 				unlock_page(page);
3394bef0848SChris Mason 				page_cache_release(page);
3404bef0848SChris Mason 				continue;
3414bef0848SChris Mason 			}
3424bef0848SChris Mason 
3436702ed49SChris Mason 			if (PageWriteback(page)) {
3446702ed49SChris Mason 				if (PageDirty(page))
3456702ed49SChris Mason 					wait_on_page_writeback(page);
3466702ed49SChris Mason 				else {
3476702ed49SChris Mason 					unlock_page(page);
3486702ed49SChris Mason 					page_cache_release(page);
3496702ed49SChris Mason 					continue;
3506702ed49SChris Mason 				}
3516702ed49SChris Mason 			}
3527c4452b9SChris Mason 			err = write_one_page(page, 0);
3537c4452b9SChris Mason 			if (err)
3547c4452b9SChris Mason 				werr = err;
3557c4452b9SChris Mason 			page_cache_release(page);
3567c4452b9SChris Mason 		}
3577c4452b9SChris Mason 	}
358777e6bd7SChris Mason 	while(1) {
359777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
360777e6bd7SChris Mason 					    EXTENT_DIRTY);
361777e6bd7SChris Mason 		if (ret)
362777e6bd7SChris Mason 			break;
363777e6bd7SChris Mason 
364777e6bd7SChris Mason 		clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
365777e6bd7SChris Mason 		while(start <= end) {
366777e6bd7SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
367777e6bd7SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
368777e6bd7SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
369777e6bd7SChris Mason 			if (!page)
370777e6bd7SChris Mason 				continue;
371777e6bd7SChris Mason 			if (PageDirty(page)) {
3724bef0848SChris Mason 				btree_lock_page_hook(page);
3734bef0848SChris Mason 				wait_on_page_writeback(page);
374777e6bd7SChris Mason 				err = write_one_page(page, 0);
375777e6bd7SChris Mason 				if (err)
376777e6bd7SChris Mason 					werr = err;
377777e6bd7SChris Mason 			}
378777e6bd7SChris Mason 			wait_on_page_writeback(page);
379777e6bd7SChris Mason 			page_cache_release(page);
380777e6bd7SChris Mason 			cond_resched();
381777e6bd7SChris Mason 		}
382777e6bd7SChris Mason 	}
3837c4452b9SChris Mason 	if (err)
3847c4452b9SChris Mason 		werr = err;
3857c4452b9SChris Mason 	return werr;
38679154b1bSChris Mason }
38779154b1bSChris Mason 
3880b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
38979154b1bSChris Mason 			       struct btrfs_root *root)
39079154b1bSChris Mason {
39179154b1bSChris Mason 	int ret;
3920b86a832SChris Mason 	u64 old_root_bytenr;
3930b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
39479154b1bSChris Mason 
3950b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
39679154b1bSChris Mason 	while(1) {
3970b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
3980b86a832SChris Mason 		if (old_root_bytenr == root->node->start)
39979154b1bSChris Mason 			break;
4000b86a832SChris Mason 		btrfs_set_root_bytenr(&root->root_item,
4010b86a832SChris Mason 				       root->node->start);
4020b86a832SChris Mason 		btrfs_set_root_level(&root->root_item,
4030b86a832SChris Mason 				     btrfs_header_level(root->node));
40479154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
4050b86a832SChris Mason 					&root->root_key,
4060b86a832SChris Mason 					&root->root_item);
40779154b1bSChris Mason 		BUG_ON(ret);
4080b86a832SChris Mason 		btrfs_write_dirty_block_groups(trans, root);
4090b86a832SChris Mason 	}
4100b86a832SChris Mason 	return 0;
4110b86a832SChris Mason }
4120b86a832SChris Mason 
4130b86a832SChris Mason int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
4140b86a832SChris Mason 			    struct btrfs_root *root)
4150b86a832SChris Mason {
4160b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
4170b86a832SChris Mason 	struct list_head *next;
4180b86a832SChris Mason 
4190b86a832SChris Mason 	while(!list_empty(&fs_info->dirty_cowonly_roots)) {
4200b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
4210b86a832SChris Mason 		list_del_init(next);
4220b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
4230b86a832SChris Mason 		update_cowonly_root(trans, root);
42479154b1bSChris Mason 	}
42579154b1bSChris Mason 	return 0;
42679154b1bSChris Mason }
42779154b1bSChris Mason 
428b48652c1SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
4295eda7b5eSChris Mason {
430f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
4315eda7b5eSChris Mason 
4325eda7b5eSChris Mason 	dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
4335eda7b5eSChris Mason 	if (!dirty)
4345eda7b5eSChris Mason 		return -ENOMEM;
4355eda7b5eSChris Mason 	dirty->root = root;
4365ce14bbcSChris Mason 	dirty->latest_root = latest;
437b48652c1SYan Zheng 
438b48652c1SYan Zheng 	mutex_lock(&root->fs_info->trans_mutex);
439b48652c1SYan Zheng 	list_add(&dirty->list, &latest->fs_info->dead_roots);
440b48652c1SYan Zheng 	mutex_unlock(&root->fs_info->trans_mutex);
4415eda7b5eSChris Mason 	return 0;
4425eda7b5eSChris Mason }
4435eda7b5eSChris Mason 
44480b6794dSChris Mason static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
44535b7e476SChris Mason 				    struct radix_tree_root *radix,
44635b7e476SChris Mason 				    struct list_head *list)
4470f7d52f4SChris Mason {
448f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
4490f7d52f4SChris Mason 	struct btrfs_root *gang[8];
4500f7d52f4SChris Mason 	struct btrfs_root *root;
4510f7d52f4SChris Mason 	int i;
4520f7d52f4SChris Mason 	int ret;
45354aa1f4dSChris Mason 	int err = 0;
4545eda7b5eSChris Mason 	u32 refs;
45554aa1f4dSChris Mason 
4560f7d52f4SChris Mason 	while(1) {
4570f7d52f4SChris Mason 		ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
4580f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
4590f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
4600f7d52f4SChris Mason 		if (ret == 0)
4610f7d52f4SChris Mason 			break;
4620f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
4630f7d52f4SChris Mason 			root = gang[i];
4642619ba1fSChris Mason 			radix_tree_tag_clear(radix,
4652619ba1fSChris Mason 				     (unsigned long)root->root_key.objectid,
4660f7d52f4SChris Mason 				     BTRFS_ROOT_TRANS_TAG);
46731153d81SYan Zheng 
46831153d81SYan Zheng 			BUG_ON(!root->ref_tree);
469017e5369SChris Mason 			dirty = root->dirty_root;
47031153d81SYan Zheng 
471e02119d5SChris Mason 			btrfs_free_log(trans, root);
472e02119d5SChris Mason 
4730f7d52f4SChris Mason 			if (root->commit_root == root->node) {
474db94535dSChris Mason 				WARN_ON(root->node->start !=
475db94535dSChris Mason 					btrfs_root_bytenr(&root->root_item));
47631153d81SYan Zheng 
4775f39d397SChris Mason 				free_extent_buffer(root->commit_root);
4780f7d52f4SChris Mason 				root->commit_root = NULL;
4797ea394f1SYan Zheng 				root->dirty_root = NULL;
48031153d81SYan Zheng 
481bcc63abbSYan 				spin_lock(&root->list_lock);
482bcc63abbSYan 				list_del_init(&dirty->root->dead_list);
483bcc63abbSYan 				spin_unlock(&root->list_lock);
484bcc63abbSYan 
48531153d81SYan Zheng 				kfree(dirty->root);
48631153d81SYan Zheng 				kfree(dirty);
48758176a96SJosef Bacik 
48858176a96SJosef Bacik 				/* make sure to update the root on disk
48958176a96SJosef Bacik 				 * so we get any updates to the block used
49058176a96SJosef Bacik 				 * counts
49158176a96SJosef Bacik 				 */
49258176a96SJosef Bacik 				err = btrfs_update_root(trans,
49358176a96SJosef Bacik 						root->fs_info->tree_root,
49458176a96SJosef Bacik 						&root->root_key,
49558176a96SJosef Bacik 						&root->root_item);
4960f7d52f4SChris Mason 				continue;
4970f7d52f4SChris Mason 			}
4989f3a7427SChris Mason 
4999f3a7427SChris Mason 			memset(&root->root_item.drop_progress, 0,
5009f3a7427SChris Mason 			       sizeof(struct btrfs_disk_key));
5019f3a7427SChris Mason 			root->root_item.drop_level = 0;
5020f7d52f4SChris Mason 			root->commit_root = NULL;
5037ea394f1SYan Zheng 			root->dirty_root = NULL;
5040f7d52f4SChris Mason 			root->root_key.offset = root->fs_info->generation;
505db94535dSChris Mason 			btrfs_set_root_bytenr(&root->root_item,
506db94535dSChris Mason 					      root->node->start);
507db94535dSChris Mason 			btrfs_set_root_level(&root->root_item,
508db94535dSChris Mason 					     btrfs_header_level(root->node));
5090f7d52f4SChris Mason 			err = btrfs_insert_root(trans, root->fs_info->tree_root,
5100f7d52f4SChris Mason 						&root->root_key,
5110f7d52f4SChris Mason 						&root->root_item);
51254aa1f4dSChris Mason 			if (err)
51354aa1f4dSChris Mason 				break;
5149f3a7427SChris Mason 
5159f3a7427SChris Mason 			refs = btrfs_root_refs(&dirty->root->root_item);
5169f3a7427SChris Mason 			btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
5175eda7b5eSChris Mason 			err = btrfs_update_root(trans, root->fs_info->tree_root,
5189f3a7427SChris Mason 						&dirty->root->root_key,
5199f3a7427SChris Mason 						&dirty->root->root_item);
5205eda7b5eSChris Mason 
5215eda7b5eSChris Mason 			BUG_ON(err);
5229f3a7427SChris Mason 			if (refs == 1) {
5230f7d52f4SChris Mason 				list_add(&dirty->list, list);
5249f3a7427SChris Mason 			} else {
5259f3a7427SChris Mason 				WARN_ON(1);
52631153d81SYan Zheng 				free_extent_buffer(dirty->root->node);
5279f3a7427SChris Mason 				kfree(dirty->root);
5285eda7b5eSChris Mason 				kfree(dirty);
5290f7d52f4SChris Mason 			}
5300f7d52f4SChris Mason 		}
5319f3a7427SChris Mason 	}
53254aa1f4dSChris Mason 	return err;
5330f7d52f4SChris Mason }
5340f7d52f4SChris Mason 
535e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
536e9d0b13bSChris Mason {
537e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
538e9d0b13bSChris Mason 	int ret;
539e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
540d3c2fdcfSChris Mason 	unsigned long nr;
541e9d0b13bSChris Mason 
542a2135011SChris Mason 	smp_mb();
543e9d0b13bSChris Mason 	if (root->defrag_running)
544e9d0b13bSChris Mason 		return 0;
545e9d0b13bSChris Mason 	trans = btrfs_start_transaction(root, 1);
5466b80053dSChris Mason 	while (1) {
547e9d0b13bSChris Mason 		root->defrag_running = 1;
548e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
549d3c2fdcfSChris Mason 		nr = trans->blocks_used;
550e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
551d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(info->tree_root, nr);
552e9d0b13bSChris Mason 		cond_resched();
553e9d0b13bSChris Mason 
554e9d0b13bSChris Mason 		trans = btrfs_start_transaction(root, 1);
5553f157a2fSChris Mason 		if (root->fs_info->closing || ret != -EAGAIN)
556e9d0b13bSChris Mason 			break;
557e9d0b13bSChris Mason 	}
558e9d0b13bSChris Mason 	root->defrag_running = 0;
559a2135011SChris Mason 	smp_mb();
560e9d0b13bSChris Mason 	btrfs_end_transaction(trans, root);
561e9d0b13bSChris Mason 	return 0;
562e9d0b13bSChris Mason }
563e9d0b13bSChris Mason 
56480b6794dSChris Mason static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
56535b7e476SChris Mason 				     struct list_head *list)
5660f7d52f4SChris Mason {
567f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
5680f7d52f4SChris Mason 	struct btrfs_trans_handle *trans;
569d3c2fdcfSChris Mason 	unsigned long nr;
570db94535dSChris Mason 	u64 num_bytes;
571db94535dSChris Mason 	u64 bytes_used;
572bcc63abbSYan 	u64 max_useless;
57354aa1f4dSChris Mason 	int ret = 0;
5749f3a7427SChris Mason 	int err;
5759f3a7427SChris Mason 
5760f7d52f4SChris Mason 	while(!list_empty(list)) {
57758176a96SJosef Bacik 		struct btrfs_root *root;
57858176a96SJosef Bacik 
579f321e491SYan Zheng 		dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
5800f7d52f4SChris Mason 		list_del_init(&dirty->list);
5815eda7b5eSChris Mason 
582db94535dSChris Mason 		num_bytes = btrfs_root_used(&dirty->root->root_item);
58358176a96SJosef Bacik 		root = dirty->latest_root;
584a2135011SChris Mason 		atomic_inc(&root->fs_info->throttles);
58558176a96SJosef Bacik 
586a2135011SChris Mason 		mutex_lock(&root->fs_info->drop_mutex);
5879f3a7427SChris Mason 		while(1) {
5880f7d52f4SChris Mason 			trans = btrfs_start_transaction(tree_root, 1);
5899f3a7427SChris Mason 			ret = btrfs_drop_snapshot(trans, dirty->root);
5909f3a7427SChris Mason 			if (ret != -EAGAIN) {
5919f3a7427SChris Mason 				break;
5929f3a7427SChris Mason 			}
59358176a96SJosef Bacik 
5949f3a7427SChris Mason 			err = btrfs_update_root(trans,
5959f3a7427SChris Mason 					tree_root,
5969f3a7427SChris Mason 					&dirty->root->root_key,
5979f3a7427SChris Mason 					&dirty->root->root_item);
5989f3a7427SChris Mason 			if (err)
5999f3a7427SChris Mason 				ret = err;
600d3c2fdcfSChris Mason 			nr = trans->blocks_used;
601017e5369SChris Mason 			ret = btrfs_end_transaction(trans, tree_root);
6020f7d52f4SChris Mason 			BUG_ON(ret);
603a2135011SChris Mason 
604a2135011SChris Mason 			mutex_unlock(&root->fs_info->drop_mutex);
605d3c2fdcfSChris Mason 			btrfs_btree_balance_dirty(tree_root, nr);
6064dc11904SChris Mason 			cond_resched();
607a2135011SChris Mason 			mutex_lock(&root->fs_info->drop_mutex);
6089f3a7427SChris Mason 		}
6099f3a7427SChris Mason 		BUG_ON(ret);
610a2135011SChris Mason 		atomic_dec(&root->fs_info->throttles);
611017e5369SChris Mason 		wake_up(&root->fs_info->transaction_throttle);
61258176a96SJosef Bacik 
613a2135011SChris Mason 		mutex_lock(&root->fs_info->alloc_mutex);
614db94535dSChris Mason 		num_bytes -= btrfs_root_used(&dirty->root->root_item);
615db94535dSChris Mason 		bytes_used = btrfs_root_used(&root->root_item);
616db94535dSChris Mason 		if (num_bytes) {
617e02119d5SChris Mason 			btrfs_record_root_in_trans(root);
6185f39d397SChris Mason 			btrfs_set_root_used(&root->root_item,
619db94535dSChris Mason 					    bytes_used - num_bytes);
62058176a96SJosef Bacik 		}
621a2135011SChris Mason 		mutex_unlock(&root->fs_info->alloc_mutex);
622a2135011SChris Mason 
6239f3a7427SChris Mason 		ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
62458176a96SJosef Bacik 		if (ret) {
62558176a96SJosef Bacik 			BUG();
62654aa1f4dSChris Mason 			break;
62758176a96SJosef Bacik 		}
628a2135011SChris Mason 		mutex_unlock(&root->fs_info->drop_mutex);
629a2135011SChris Mason 
630bcc63abbSYan 		spin_lock(&root->list_lock);
631bcc63abbSYan 		list_del_init(&dirty->root->dead_list);
632bcc63abbSYan 		if (!list_empty(&root->dead_list)) {
633bcc63abbSYan 			struct btrfs_root *oldest;
634bcc63abbSYan 			oldest = list_entry(root->dead_list.prev,
635bcc63abbSYan 					    struct btrfs_root, dead_list);
636bcc63abbSYan 			max_useless = oldest->root_key.offset - 1;
637bcc63abbSYan 		} else {
638bcc63abbSYan 			max_useless = root->root_key.offset - 1;
639bcc63abbSYan 		}
640bcc63abbSYan 		spin_unlock(&root->list_lock);
641bcc63abbSYan 
642d3c2fdcfSChris Mason 		nr = trans->blocks_used;
6430f7d52f4SChris Mason 		ret = btrfs_end_transaction(trans, tree_root);
6440f7d52f4SChris Mason 		BUG_ON(ret);
6455eda7b5eSChris Mason 
646bcc63abbSYan 		ret = btrfs_remove_leaf_refs(root, max_useless);
647bcc63abbSYan 		BUG_ON(ret);
648bcc63abbSYan 
649f510cfecSChris Mason 		free_extent_buffer(dirty->root->node);
6505eda7b5eSChris Mason 		kfree(dirty->root);
6510f7d52f4SChris Mason 		kfree(dirty);
652d3c2fdcfSChris Mason 
653d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(tree_root, nr);
6544dc11904SChris Mason 		cond_resched();
6550f7d52f4SChris Mason 	}
65654aa1f4dSChris Mason 	return ret;
6570f7d52f4SChris Mason }
6580f7d52f4SChris Mason 
65980b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
6603063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
6613063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
6623063d29fSChris Mason {
6633063d29fSChris Mason 	struct btrfs_key key;
66480b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
6653063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
6663063d29fSChris Mason 	struct btrfs_root *root = pending->root;
6673063d29fSChris Mason 	struct extent_buffer *tmp;
668925baeddSChris Mason 	struct extent_buffer *old;
6693063d29fSChris Mason 	int ret;
6703b96362cSSven Wegener 	int namelen;
6713063d29fSChris Mason 	u64 objectid;
6723063d29fSChris Mason 
67380b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
67480b6794dSChris Mason 	if (!new_root_item) {
67580b6794dSChris Mason 		ret = -ENOMEM;
67680b6794dSChris Mason 		goto fail;
67780b6794dSChris Mason 	}
6783063d29fSChris Mason 	ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
6793063d29fSChris Mason 	if (ret)
6803063d29fSChris Mason 		goto fail;
6813063d29fSChris Mason 
68280b6794dSChris Mason 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
6833063d29fSChris Mason 
6843063d29fSChris Mason 	key.objectid = objectid;
6853063d29fSChris Mason 	key.offset = 1;
6863063d29fSChris Mason 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
6873063d29fSChris Mason 
688925baeddSChris Mason 	old = btrfs_lock_root_node(root);
68965b51a00SChris Mason 	btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
6903063d29fSChris Mason 
691925baeddSChris Mason 	btrfs_copy_root(trans, root, old, &tmp, objectid);
692925baeddSChris Mason 	btrfs_tree_unlock(old);
693925baeddSChris Mason 	free_extent_buffer(old);
6943063d29fSChris Mason 
69580b6794dSChris Mason 	btrfs_set_root_bytenr(new_root_item, tmp->start);
69680b6794dSChris Mason 	btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
6973063d29fSChris Mason 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
69880b6794dSChris Mason 				new_root_item);
699925baeddSChris Mason 	btrfs_tree_unlock(tmp);
7003063d29fSChris Mason 	free_extent_buffer(tmp);
7013063d29fSChris Mason 	if (ret)
7023063d29fSChris Mason 		goto fail;
7033063d29fSChris Mason 
7043063d29fSChris Mason 	/*
7053063d29fSChris Mason 	 * insert the directory item
7063063d29fSChris Mason 	 */
7073063d29fSChris Mason 	key.offset = (u64)-1;
7083b96362cSSven Wegener 	namelen = strlen(pending->name);
7093063d29fSChris Mason 	ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
7103b96362cSSven Wegener 				    pending->name, namelen,
7113063d29fSChris Mason 				    root->fs_info->sb->s_root->d_inode->i_ino,
712aec7477bSJosef Bacik 				    &key, BTRFS_FT_DIR, 0);
7133063d29fSChris Mason 
7143063d29fSChris Mason 	if (ret)
7153063d29fSChris Mason 		goto fail;
7163063d29fSChris Mason 
7173063d29fSChris Mason 	ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
7183063d29fSChris Mason 			     pending->name, strlen(pending->name), objectid,
719aec7477bSJosef Bacik 			     root->fs_info->sb->s_root->d_inode->i_ino, 0);
7203b96362cSSven Wegener 
7213b96362cSSven Wegener 	/* Invalidate existing dcache entry for new snapshot. */
7223b96362cSSven Wegener 	btrfs_invalidate_dcache_root(root, pending->name, namelen);
7233b96362cSSven Wegener 
7243063d29fSChris Mason fail:
72580b6794dSChris Mason 	kfree(new_root_item);
7263063d29fSChris Mason 	return ret;
7273063d29fSChris Mason }
7283063d29fSChris Mason 
72980b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
7303063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
7313063d29fSChris Mason {
7323063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
7333063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
7343063d29fSChris Mason 	int ret;
7353063d29fSChris Mason 
7363063d29fSChris Mason 	while(!list_empty(head)) {
7373063d29fSChris Mason 		pending = list_entry(head->next,
7383063d29fSChris Mason 				     struct btrfs_pending_snapshot, list);
7393063d29fSChris Mason 		ret = create_pending_snapshot(trans, fs_info, pending);
7403063d29fSChris Mason 		BUG_ON(ret);
7413063d29fSChris Mason 		list_del(&pending->list);
7423063d29fSChris Mason 		kfree(pending->name);
7433063d29fSChris Mason 		kfree(pending);
7443063d29fSChris Mason 	}
745dc17ff8fSChris Mason 	return 0;
746dc17ff8fSChris Mason }
747dc17ff8fSChris Mason 
74879154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
74979154b1bSChris Mason 			     struct btrfs_root *root)
75079154b1bSChris Mason {
75115ee9bc7SJosef Bacik 	unsigned long joined = 0;
75215ee9bc7SJosef Bacik 	unsigned long timeout = 1;
75379154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
7548fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
7550b86a832SChris Mason 	struct btrfs_root *chunk_root = root->fs_info->chunk_root;
7560f7d52f4SChris Mason 	struct list_head dirty_fs_roots;
757d1310b2eSChris Mason 	struct extent_io_tree *pinned_copy;
75879154b1bSChris Mason 	DEFINE_WAIT(wait);
75915ee9bc7SJosef Bacik 	int ret;
76079154b1bSChris Mason 
7610f7d52f4SChris Mason 	INIT_LIST_HEAD(&dirty_fs_roots);
76279154b1bSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
76379154b1bSChris Mason 	if (trans->transaction->in_commit) {
76479154b1bSChris Mason 		cur_trans = trans->transaction;
76579154b1bSChris Mason 		trans->transaction->use_count++;
766ccd467d6SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
76779154b1bSChris Mason 		btrfs_end_transaction(trans, root);
768ccd467d6SChris Mason 
76979154b1bSChris Mason 		ret = wait_for_commit(root, cur_trans);
77079154b1bSChris Mason 		BUG_ON(ret);
77115ee9bc7SJosef Bacik 
77215ee9bc7SJosef Bacik 		mutex_lock(&root->fs_info->trans_mutex);
77379154b1bSChris Mason 		put_transaction(cur_trans);
77415ee9bc7SJosef Bacik 		mutex_unlock(&root->fs_info->trans_mutex);
77515ee9bc7SJosef Bacik 
77679154b1bSChris Mason 		return 0;
77779154b1bSChris Mason 	}
7784313b399SChris Mason 
7794313b399SChris Mason 	pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
7804313b399SChris Mason 	if (!pinned_copy)
7814313b399SChris Mason 		return -ENOMEM;
7824313b399SChris Mason 
783d1310b2eSChris Mason 	extent_io_tree_init(pinned_copy,
7844313b399SChris Mason 			     root->fs_info->btree_inode->i_mapping, GFP_NOFS);
7854313b399SChris Mason 
7862c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
787f9295749SChris Mason 	trans->transaction->blocked = 1;
788ccd467d6SChris Mason 	cur_trans = trans->transaction;
789ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
790ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
791ccd467d6SChris Mason 					struct btrfs_transaction, list);
792ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
793ccd467d6SChris Mason 			prev_trans->use_count++;
794ccd467d6SChris Mason 			mutex_unlock(&root->fs_info->trans_mutex);
795ccd467d6SChris Mason 
796ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
797ccd467d6SChris Mason 
798ccd467d6SChris Mason 			mutex_lock(&root->fs_info->trans_mutex);
79915ee9bc7SJosef Bacik 			put_transaction(prev_trans);
800ccd467d6SChris Mason 		}
801ccd467d6SChris Mason 	}
80215ee9bc7SJosef Bacik 
80315ee9bc7SJosef Bacik 	do {
8047ea394f1SYan Zheng 		int snap_pending = 0;
80515ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
8067ea394f1SYan Zheng 		if (!list_empty(&trans->transaction->pending_snapshots))
8077ea394f1SYan Zheng 			snap_pending = 1;
8087ea394f1SYan Zheng 
8092c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
81015ee9bc7SJosef Bacik 		prepare_to_wait(&cur_trans->writer_wait, &wait,
81179154b1bSChris Mason 				TASK_UNINTERRUPTIBLE);
81215ee9bc7SJosef Bacik 
81315ee9bc7SJosef Bacik 		if (cur_trans->num_writers > 1)
81415ee9bc7SJosef Bacik 			timeout = MAX_SCHEDULE_TIMEOUT;
81515ee9bc7SJosef Bacik 		else
81615ee9bc7SJosef Bacik 			timeout = 1;
81715ee9bc7SJosef Bacik 
81879154b1bSChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
81915ee9bc7SJosef Bacik 
8207ea394f1SYan Zheng 		if (snap_pending) {
8217ea394f1SYan Zheng 			ret = btrfs_wait_ordered_extents(root, 1);
8227ea394f1SYan Zheng 			BUG_ON(ret);
8237ea394f1SYan Zheng 		}
8247ea394f1SYan Zheng 
82515ee9bc7SJosef Bacik 		schedule_timeout(timeout);
82615ee9bc7SJosef Bacik 
82779154b1bSChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
82815ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
82915ee9bc7SJosef Bacik 	} while (cur_trans->num_writers > 1 ||
83015ee9bc7SJosef Bacik 		 (cur_trans->num_joined != joined));
83115ee9bc7SJosef Bacik 
8323063d29fSChris Mason 	ret = create_pending_snapshots(trans, root->fs_info);
8333063d29fSChris Mason 	BUG_ON(ret);
8343063d29fSChris Mason 
8352c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
836dc17ff8fSChris Mason 
837e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
838e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
839e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
840e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
841e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
842e02119d5SChris Mason 	 * of the trees.
843e02119d5SChris Mason 	 *
844e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
845e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
846e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
847e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
848e02119d5SChris Mason 	 * with the tree-log code.
849e02119d5SChris Mason 	 */
850e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
851e02119d5SChris Mason 
85254aa1f4dSChris Mason 	ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
85354aa1f4dSChris Mason 			      &dirty_fs_roots);
85454aa1f4dSChris Mason 	BUG_ON(ret);
85554aa1f4dSChris Mason 
856e02119d5SChris Mason 	/* add_dirty_roots gets rid of all the tree log roots, it is now
857e02119d5SChris Mason 	 * safe to free the root of tree log roots
858e02119d5SChris Mason 	 */
859e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
860e02119d5SChris Mason 
86179154b1bSChris Mason 	ret = btrfs_commit_tree_roots(trans, root);
86279154b1bSChris Mason 	BUG_ON(ret);
86354aa1f4dSChris Mason 
86478fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
865cee36a03SChris Mason 	spin_lock(&root->fs_info->new_trans_lock);
86678fae27eSChris Mason 	root->fs_info->running_transaction = NULL;
867cee36a03SChris Mason 	spin_unlock(&root->fs_info->new_trans_lock);
8684b52dff6SChris Mason 	btrfs_set_super_generation(&root->fs_info->super_copy,
8694b52dff6SChris Mason 				   cur_trans->transid);
8704b52dff6SChris Mason 	btrfs_set_super_root(&root->fs_info->super_copy,
871db94535dSChris Mason 			     root->fs_info->tree_root->node->start);
872db94535dSChris Mason 	btrfs_set_super_root_level(&root->fs_info->super_copy,
873db94535dSChris Mason 			   btrfs_header_level(root->fs_info->tree_root->node));
8745f39d397SChris Mason 
8750b86a832SChris Mason 	btrfs_set_super_chunk_root(&root->fs_info->super_copy,
8760b86a832SChris Mason 				   chunk_root->node->start);
8770b86a832SChris Mason 	btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
8780b86a832SChris Mason 					 btrfs_header_level(chunk_root->node));
879e02119d5SChris Mason 
880e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
881e02119d5SChris Mason 		btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
882e02119d5SChris Mason 		btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
883e02119d5SChris Mason 	}
884e02119d5SChris Mason 
885a061fc8dSChris Mason 	memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
8864b52dff6SChris Mason 	       sizeof(root->fs_info->super_copy));
887ccd467d6SChris Mason 
8884313b399SChris Mason 	btrfs_copy_pinned(root, pinned_copy);
889ccd467d6SChris Mason 
890f9295749SChris Mason 	trans->transaction->blocked = 0;
891e6dcd2dcSChris Mason 	wake_up(&root->fs_info->transaction_throttle);
892f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
893e6dcd2dcSChris Mason 
89478fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
89579154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
89679154b1bSChris Mason 	BUG_ON(ret);
89779154b1bSChris Mason 	write_ctree_super(trans, root);
8984313b399SChris Mason 
899e02119d5SChris Mason 	/*
900e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
901e02119d5SChris Mason 	 * to go about their business
902e02119d5SChris Mason 	 */
903e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
904e02119d5SChris Mason 
9054313b399SChris Mason 	btrfs_finish_extent_commit(trans, root, pinned_copy);
90678fae27eSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
9074313b399SChris Mason 
9084313b399SChris Mason 	kfree(pinned_copy);
9094313b399SChris Mason 
9102c90e5d6SChris Mason 	cur_trans->commit_done = 1;
91115ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
9122c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
91379154b1bSChris Mason 	put_transaction(cur_trans);
91478fae27eSChris Mason 	put_transaction(cur_trans);
91558176a96SJosef Bacik 
916bcc63abbSYan 	list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
917facda1e7SChris Mason 	if (root->fs_info->closing)
918facda1e7SChris Mason 		list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
91958176a96SJosef Bacik 
92078fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
9212c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
92279154b1bSChris Mason 
923facda1e7SChris Mason 	if (root->fs_info->closing) {
9240f7d52f4SChris Mason 		drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
925facda1e7SChris Mason 	}
92679154b1bSChris Mason 	return ret;
92779154b1bSChris Mason }
92879154b1bSChris Mason 
929e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
930e9d0b13bSChris Mason {
931e9d0b13bSChris Mason 	struct list_head dirty_roots;
932e9d0b13bSChris Mason 	INIT_LIST_HEAD(&dirty_roots);
933a74a4b97SChris Mason again:
934e9d0b13bSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
935e9d0b13bSChris Mason 	list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
936e9d0b13bSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
937e9d0b13bSChris Mason 
938e9d0b13bSChris Mason 	if (!list_empty(&dirty_roots)) {
939e9d0b13bSChris Mason 		drop_dirty_roots(root, &dirty_roots);
940a74a4b97SChris Mason 		goto again;
941e9d0b13bSChris Mason 	}
942e9d0b13bSChris Mason 	return 0;
943e9d0b13bSChris Mason }
944