xref: /openbmc/linux/fs/btrfs/transaction.c (revision d352ac68)
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 
49d352ac68SChris Mason /*
50d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
51d352ac68SChris Mason  */
5280b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root)
5379154b1bSChris Mason {
5479154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
5579154b1bSChris Mason 	cur_trans = root->fs_info->running_transaction;
5679154b1bSChris Mason 	if (!cur_trans) {
572c90e5d6SChris Mason 		cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
582c90e5d6SChris Mason 					     GFP_NOFS);
5978fae27eSChris Mason 		total_trans++;
6079154b1bSChris Mason 		BUG_ON(!cur_trans);
610f7d52f4SChris Mason 		root->fs_info->generation++;
62e18e4809SChris Mason 		root->fs_info->last_alloc = 0;
634529ba49SChris Mason 		root->fs_info->last_data_alloc = 0;
64e02119d5SChris Mason 		root->fs_info->last_log_alloc = 0;
6515ee9bc7SJosef Bacik 		cur_trans->num_writers = 1;
6615ee9bc7SJosef Bacik 		cur_trans->num_joined = 0;
670f7d52f4SChris Mason 		cur_trans->transid = root->fs_info->generation;
6879154b1bSChris Mason 		init_waitqueue_head(&cur_trans->writer_wait);
6979154b1bSChris Mason 		init_waitqueue_head(&cur_trans->commit_wait);
7079154b1bSChris Mason 		cur_trans->in_commit = 0;
71f9295749SChris Mason 		cur_trans->blocked = 0;
72d5719762SChris Mason 		cur_trans->use_count = 1;
7379154b1bSChris Mason 		cur_trans->commit_done = 0;
7408607c1bSChris Mason 		cur_trans->start_time = get_seconds();
753063d29fSChris Mason 		INIT_LIST_HEAD(&cur_trans->pending_snapshots);
768fd17795SChris Mason 		list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
77d1310b2eSChris Mason 		extent_io_tree_init(&cur_trans->dirty_pages,
785f39d397SChris Mason 				     root->fs_info->btree_inode->i_mapping,
795f39d397SChris Mason 				     GFP_NOFS);
8048ec2cf8SChris Mason 		spin_lock(&root->fs_info->new_trans_lock);
8148ec2cf8SChris Mason 		root->fs_info->running_transaction = cur_trans;
8248ec2cf8SChris Mason 		spin_unlock(&root->fs_info->new_trans_lock);
8315ee9bc7SJosef Bacik 	} else {
8479154b1bSChris Mason 		cur_trans->num_writers++;
8515ee9bc7SJosef Bacik 		cur_trans->num_joined++;
8615ee9bc7SJosef Bacik 	}
8715ee9bc7SJosef Bacik 
8879154b1bSChris Mason 	return 0;
8979154b1bSChris Mason }
9079154b1bSChris Mason 
91d352ac68SChris Mason /*
92d352ac68SChris Mason  * this does all the record keeping required to make sure that a
93d352ac68SChris Mason  * reference counted root is properly recorded in a given transaction.
94d352ac68SChris Mason  * This is required to make sure the old root from before we joined the transaction
95d352ac68SChris Mason  * is deleted when the transaction commits
96d352ac68SChris Mason  */
97e02119d5SChris Mason noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
986702ed49SChris Mason {
99f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
1006702ed49SChris Mason 	u64 running_trans_id = root->fs_info->running_transaction->transid;
1016702ed49SChris Mason 	if (root->ref_cows && root->last_trans < running_trans_id) {
1026702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
1036702ed49SChris Mason 		if (root->root_item.refs != 0) {
1046702ed49SChris Mason 			radix_tree_tag_set(&root->fs_info->fs_roots_radix,
1056702ed49SChris Mason 				   (unsigned long)root->root_key.objectid,
1066702ed49SChris Mason 				   BTRFS_ROOT_TRANS_TAG);
10731153d81SYan Zheng 
10831153d81SYan Zheng 			dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
10931153d81SYan Zheng 			BUG_ON(!dirty);
11031153d81SYan Zheng 			dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
11131153d81SYan Zheng 			BUG_ON(!dirty->root);
11231153d81SYan Zheng 			dirty->latest_root = root;
11331153d81SYan Zheng 			INIT_LIST_HEAD(&dirty->list);
11431153d81SYan Zheng 
115925baeddSChris Mason 			root->commit_root = btrfs_root_node(root);
11631153d81SYan Zheng 
11731153d81SYan Zheng 			memcpy(dirty->root, root, sizeof(*root));
11831153d81SYan Zheng 			spin_lock_init(&dirty->root->node_lock);
119bcc63abbSYan 			spin_lock_init(&dirty->root->list_lock);
12031153d81SYan Zheng 			mutex_init(&dirty->root->objectid_mutex);
1215b21f2edSZheng Yan 			mutex_init(&dirty->root->log_mutex);
122bcc63abbSYan 			INIT_LIST_HEAD(&dirty->root->dead_list);
12331153d81SYan Zheng 			dirty->root->node = root->commit_root;
12431153d81SYan Zheng 			dirty->root->commit_root = NULL;
125bcc63abbSYan 
126bcc63abbSYan 			spin_lock(&root->list_lock);
127bcc63abbSYan 			list_add(&dirty->root->dead_list, &root->dead_list);
128bcc63abbSYan 			spin_unlock(&root->list_lock);
129bcc63abbSYan 
130bcc63abbSYan 			root->dirty_root = dirty;
1316702ed49SChris Mason 		} else {
1326702ed49SChris Mason 			WARN_ON(1);
1336702ed49SChris Mason 		}
1346702ed49SChris Mason 		root->last_trans = running_trans_id;
1356702ed49SChris Mason 	}
1366702ed49SChris Mason 	return 0;
1376702ed49SChris Mason }
1386702ed49SChris Mason 
139d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
140d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
141d352ac68SChris Mason  * transaction might not be fully on disk.
142d352ac68SChris Mason  */
14337d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
14479154b1bSChris Mason {
145f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
14679154b1bSChris Mason 
147f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
14837d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
149f9295749SChris Mason 		DEFINE_WAIT(wait);
150f9295749SChris Mason 		cur_trans->use_count++;
151f9295749SChris Mason 		while(1) {
152f9295749SChris Mason 			prepare_to_wait(&root->fs_info->transaction_wait, &wait,
153f9295749SChris Mason 					TASK_UNINTERRUPTIBLE);
154f9295749SChris Mason 			if (cur_trans->blocked) {
155f9295749SChris Mason 				mutex_unlock(&root->fs_info->trans_mutex);
156f9295749SChris Mason 				schedule();
157f9295749SChris Mason 				mutex_lock(&root->fs_info->trans_mutex);
158f9295749SChris Mason 				finish_wait(&root->fs_info->transaction_wait,
159f9295749SChris Mason 					    &wait);
160f9295749SChris Mason 			} else {
161f9295749SChris Mason 				finish_wait(&root->fs_info->transaction_wait,
162f9295749SChris Mason 					    &wait);
163f9295749SChris Mason 				break;
164f9295749SChris Mason 			}
165f9295749SChris Mason 		}
166f9295749SChris Mason 		put_transaction(cur_trans);
167f9295749SChris Mason 	}
16837d1aeeeSChris Mason }
16937d1aeeeSChris Mason 
170e02119d5SChris Mason static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
1719ca9ee09SSage Weil 					     int num_blocks, int wait)
17237d1aeeeSChris Mason {
17337d1aeeeSChris Mason 	struct btrfs_trans_handle *h =
17437d1aeeeSChris Mason 		kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
17537d1aeeeSChris Mason 	int ret;
17637d1aeeeSChris Mason 
17737d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
1784bef0848SChris Mason 	if (!root->fs_info->log_root_recovering &&
1794bef0848SChris Mason 	    ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
18037d1aeeeSChris Mason 		wait_current_trans(root);
18179154b1bSChris Mason 	ret = join_transaction(root);
18279154b1bSChris Mason 	BUG_ON(ret);
1830f7d52f4SChris Mason 
184e02119d5SChris Mason 	btrfs_record_root_in_trans(root);
1856702ed49SChris Mason 	h->transid = root->fs_info->running_transaction->transid;
18679154b1bSChris Mason 	h->transaction = root->fs_info->running_transaction;
18779154b1bSChris Mason 	h->blocks_reserved = num_blocks;
18879154b1bSChris Mason 	h->blocks_used = 0;
18931f3c99bSChris Mason 	h->block_group = NULL;
19026b8003fSChris Mason 	h->alloc_exclude_nr = 0;
19126b8003fSChris Mason 	h->alloc_exclude_start = 0;
19279154b1bSChris Mason 	root->fs_info->running_transaction->use_count++;
19379154b1bSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
19479154b1bSChris Mason 	return h;
19579154b1bSChris Mason }
19679154b1bSChris Mason 
197f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
198f9295749SChris Mason 						   int num_blocks)
199f9295749SChris Mason {
2009ca9ee09SSage Weil 	return start_transaction(root, num_blocks, 1);
201f9295749SChris Mason }
202f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
203f9295749SChris Mason 						   int num_blocks)
204f9295749SChris Mason {
2059ca9ee09SSage Weil 	return start_transaction(root, num_blocks, 0);
206f9295749SChris Mason }
207f9295749SChris Mason 
2089ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
2099ca9ee09SSage Weil 							 int num_blocks)
2109ca9ee09SSage Weil {
2119ca9ee09SSage Weil 	return start_transaction(r, num_blocks, 2);
2129ca9ee09SSage Weil }
2139ca9ee09SSage Weil 
214d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
21589ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root,
21689ce8a63SChris Mason 				    struct btrfs_transaction *commit)
21789ce8a63SChris Mason {
21889ce8a63SChris Mason 	DEFINE_WAIT(wait);
21989ce8a63SChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
22089ce8a63SChris Mason 	while(!commit->commit_done) {
22189ce8a63SChris Mason 		prepare_to_wait(&commit->commit_wait, &wait,
22289ce8a63SChris Mason 				TASK_UNINTERRUPTIBLE);
22389ce8a63SChris Mason 		if (commit->commit_done)
22489ce8a63SChris Mason 			break;
22589ce8a63SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
22689ce8a63SChris Mason 		schedule();
22789ce8a63SChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
22889ce8a63SChris Mason 	}
22989ce8a63SChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
23089ce8a63SChris Mason 	finish_wait(&commit->commit_wait, &wait);
23189ce8a63SChris Mason 	return 0;
23289ce8a63SChris Mason }
23389ce8a63SChris Mason 
234d352ac68SChris Mason /*
235d352ac68SChris Mason  * rate limit against the drop_snapshot code.  This helps to slow down new operations
236d352ac68SChris Mason  * if the drop_snapshot code isn't able to keep up.
237d352ac68SChris Mason  */
23837d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root)
239ab78c84dSChris Mason {
240ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
2412dd3e67bSChris Mason 	int harder_count = 0;
242ab78c84dSChris Mason 
2432dd3e67bSChris Mason harder:
244ab78c84dSChris Mason 	if (atomic_read(&info->throttles)) {
245ab78c84dSChris Mason 		DEFINE_WAIT(wait);
246ab78c84dSChris Mason 		int thr;
247ab78c84dSChris Mason 		thr = atomic_read(&info->throttle_gen);
248ab78c84dSChris Mason 
249ab78c84dSChris Mason 		do {
250ab78c84dSChris Mason 			prepare_to_wait(&info->transaction_throttle,
251ab78c84dSChris Mason 					&wait, TASK_UNINTERRUPTIBLE);
252ab78c84dSChris Mason 			if (!atomic_read(&info->throttles)) {
253ab78c84dSChris Mason 				finish_wait(&info->transaction_throttle, &wait);
254ab78c84dSChris Mason 				break;
255ab78c84dSChris Mason 			}
256ab78c84dSChris Mason 			schedule();
257ab78c84dSChris Mason 			finish_wait(&info->transaction_throttle, &wait);
258ab78c84dSChris Mason 		} while (thr == atomic_read(&info->throttle_gen));
2592dd3e67bSChris Mason 		harder_count++;
2602dd3e67bSChris Mason 
2612dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
2622dd3e67bSChris Mason 		    harder_count < 2)
2632dd3e67bSChris Mason 			goto harder;
2642dd3e67bSChris Mason 
2652dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
2662dd3e67bSChris Mason 		    harder_count < 10)
2672dd3e67bSChris Mason 			goto harder;
2682dd3e67bSChris Mason 
2692dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
2702dd3e67bSChris Mason 		    harder_count < 20)
2712dd3e67bSChris Mason 			goto harder;
272ab78c84dSChris Mason 	}
273ab78c84dSChris Mason }
274ab78c84dSChris Mason 
27537d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
27637d1aeeeSChris Mason {
27737d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
2789ca9ee09SSage Weil 	if (!root->fs_info->open_ioctl_trans)
27937d1aeeeSChris Mason 		wait_current_trans(root);
28037d1aeeeSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
28137d1aeeeSChris Mason 
28237d1aeeeSChris Mason 	throttle_on_drops(root);
28337d1aeeeSChris Mason }
28437d1aeeeSChris Mason 
28589ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
28689ce8a63SChris Mason 			  struct btrfs_root *root, int throttle)
28779154b1bSChris Mason {
28879154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
289ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
290d6e4a428SChris Mason 
291ab78c84dSChris Mason 	mutex_lock(&info->trans_mutex);
292ab78c84dSChris Mason 	cur_trans = info->running_transaction;
293ccd467d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
294d5719762SChris Mason 	WARN_ON(cur_trans->num_writers < 1);
295ccd467d6SChris Mason 	cur_trans->num_writers--;
29689ce8a63SChris Mason 
29779154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
29879154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
29979154b1bSChris Mason 	put_transaction(cur_trans);
300ab78c84dSChris Mason 	mutex_unlock(&info->trans_mutex);
301d6025579SChris Mason 	memset(trans, 0, sizeof(*trans));
3022c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
303ab78c84dSChris Mason 
304ab78c84dSChris Mason 	if (throttle)
30537d1aeeeSChris Mason 		throttle_on_drops(root);
306ab78c84dSChris Mason 
30779154b1bSChris Mason 	return 0;
30879154b1bSChris Mason }
30979154b1bSChris Mason 
31089ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
31189ce8a63SChris Mason 			  struct btrfs_root *root)
31289ce8a63SChris Mason {
31389ce8a63SChris Mason 	return __btrfs_end_transaction(trans, root, 0);
31489ce8a63SChris Mason }
31589ce8a63SChris Mason 
31689ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
31789ce8a63SChris Mason 				   struct btrfs_root *root)
31889ce8a63SChris Mason {
31989ce8a63SChris Mason 	return __btrfs_end_transaction(trans, root, 1);
32089ce8a63SChris Mason }
32189ce8a63SChris Mason 
322d352ac68SChris Mason /*
323d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
324d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
325d352ac68SChris Mason  * those extents are on disk for transaction or log commit
326d352ac68SChris Mason  */
327d0c803c4SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
328d0c803c4SChris Mason 					struct extent_io_tree *dirty_pages)
32979154b1bSChris Mason {
3307c4452b9SChris Mason 	int ret;
331777e6bd7SChris Mason 	int err = 0;
3327c4452b9SChris Mason 	int werr = 0;
3337c4452b9SChris Mason 	struct page *page;
3347c4452b9SChris Mason 	struct inode *btree_inode = root->fs_info->btree_inode;
335777e6bd7SChris Mason 	u64 start = 0;
3365f39d397SChris Mason 	u64 end;
3375f39d397SChris Mason 	unsigned long index;
3387c4452b9SChris Mason 
3397c4452b9SChris Mason 	while(1) {
340777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, start, &start, &end,
3415f39d397SChris Mason 					    EXTENT_DIRTY);
3425f39d397SChris Mason 		if (ret)
3437c4452b9SChris Mason 			break;
3445f39d397SChris Mason 		while(start <= end) {
345777e6bd7SChris Mason 			cond_resched();
346777e6bd7SChris Mason 
3475f39d397SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
34835ebb934SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
3494bef0848SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
3507c4452b9SChris Mason 			if (!page)
3517c4452b9SChris Mason 				continue;
3524bef0848SChris Mason 
3534bef0848SChris Mason 			btree_lock_page_hook(page);
3544bef0848SChris Mason 			if (!page->mapping) {
3554bef0848SChris Mason 				unlock_page(page);
3564bef0848SChris Mason 				page_cache_release(page);
3574bef0848SChris Mason 				continue;
3584bef0848SChris Mason 			}
3594bef0848SChris Mason 
3606702ed49SChris Mason 			if (PageWriteback(page)) {
3616702ed49SChris Mason 				if (PageDirty(page))
3626702ed49SChris Mason 					wait_on_page_writeback(page);
3636702ed49SChris Mason 				else {
3646702ed49SChris Mason 					unlock_page(page);
3656702ed49SChris Mason 					page_cache_release(page);
3666702ed49SChris Mason 					continue;
3676702ed49SChris Mason 				}
3686702ed49SChris Mason 			}
3697c4452b9SChris Mason 			err = write_one_page(page, 0);
3707c4452b9SChris Mason 			if (err)
3717c4452b9SChris Mason 				werr = err;
3727c4452b9SChris Mason 			page_cache_release(page);
3737c4452b9SChris Mason 		}
3747c4452b9SChris Mason 	}
375777e6bd7SChris Mason 	while(1) {
376777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
377777e6bd7SChris Mason 					    EXTENT_DIRTY);
378777e6bd7SChris Mason 		if (ret)
379777e6bd7SChris Mason 			break;
380777e6bd7SChris Mason 
381777e6bd7SChris Mason 		clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
382777e6bd7SChris Mason 		while(start <= end) {
383777e6bd7SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
384777e6bd7SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
385777e6bd7SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
386777e6bd7SChris Mason 			if (!page)
387777e6bd7SChris Mason 				continue;
388777e6bd7SChris Mason 			if (PageDirty(page)) {
3894bef0848SChris Mason 				btree_lock_page_hook(page);
3904bef0848SChris Mason 				wait_on_page_writeback(page);
391777e6bd7SChris Mason 				err = write_one_page(page, 0);
392777e6bd7SChris Mason 				if (err)
393777e6bd7SChris Mason 					werr = err;
394777e6bd7SChris Mason 			}
395777e6bd7SChris Mason 			wait_on_page_writeback(page);
396777e6bd7SChris Mason 			page_cache_release(page);
397777e6bd7SChris Mason 			cond_resched();
398777e6bd7SChris Mason 		}
399777e6bd7SChris Mason 	}
4007c4452b9SChris Mason 	if (err)
4017c4452b9SChris Mason 		werr = err;
4027c4452b9SChris Mason 	return werr;
40379154b1bSChris Mason }
40479154b1bSChris Mason 
405d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
406d0c803c4SChris Mason 				     struct btrfs_root *root)
407d0c803c4SChris Mason {
408d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
409d0c803c4SChris Mason 		struct inode *btree_inode;
410d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
411d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
412d0c803c4SChris Mason 	}
413d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
414d0c803c4SChris Mason 					   &trans->transaction->dirty_pages);
415d0c803c4SChris Mason }
416d0c803c4SChris Mason 
417d352ac68SChris Mason /*
418d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
419d352ac68SChris Mason  *
420d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
421d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
422d352ac68SChris Mason  * allocation tree.
423d352ac68SChris Mason  *
424d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
425d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
426d352ac68SChris Mason  */
4270b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
42879154b1bSChris Mason 			       struct btrfs_root *root)
42979154b1bSChris Mason {
43079154b1bSChris Mason 	int ret;
4310b86a832SChris Mason 	u64 old_root_bytenr;
4320b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
43379154b1bSChris Mason 
4340b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
43579154b1bSChris Mason 	while(1) {
4360b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
4370b86a832SChris Mason 		if (old_root_bytenr == root->node->start)
43879154b1bSChris Mason 			break;
4390b86a832SChris Mason 		btrfs_set_root_bytenr(&root->root_item,
4400b86a832SChris Mason 				       root->node->start);
4410b86a832SChris Mason 		btrfs_set_root_level(&root->root_item,
4420b86a832SChris Mason 				     btrfs_header_level(root->node));
44379154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
4440b86a832SChris Mason 					&root->root_key,
4450b86a832SChris Mason 					&root->root_item);
44679154b1bSChris Mason 		BUG_ON(ret);
4470b86a832SChris Mason 		btrfs_write_dirty_block_groups(trans, root);
4480b86a832SChris Mason 	}
4490b86a832SChris Mason 	return 0;
4500b86a832SChris Mason }
4510b86a832SChris Mason 
452d352ac68SChris Mason /*
453d352ac68SChris Mason  * update all the cowonly tree roots on disk
454d352ac68SChris Mason  */
4550b86a832SChris Mason int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
4560b86a832SChris Mason 			    struct btrfs_root *root)
4570b86a832SChris Mason {
4580b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
4590b86a832SChris Mason 	struct list_head *next;
4600b86a832SChris Mason 
4610b86a832SChris Mason 	while(!list_empty(&fs_info->dirty_cowonly_roots)) {
4620b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
4630b86a832SChris Mason 		list_del_init(next);
4640b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
4650b86a832SChris Mason 		update_cowonly_root(trans, root);
46679154b1bSChris Mason 	}
46779154b1bSChris Mason 	return 0;
46879154b1bSChris Mason }
46979154b1bSChris Mason 
470d352ac68SChris Mason /*
471d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
472d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
473d352ac68SChris Mason  * be deleted
474d352ac68SChris Mason  */
475b48652c1SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
4765eda7b5eSChris Mason {
477f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
4785eda7b5eSChris Mason 
4795eda7b5eSChris Mason 	dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
4805eda7b5eSChris Mason 	if (!dirty)
4815eda7b5eSChris Mason 		return -ENOMEM;
4825eda7b5eSChris Mason 	dirty->root = root;
4835ce14bbcSChris Mason 	dirty->latest_root = latest;
484b48652c1SYan Zheng 
485b48652c1SYan Zheng 	mutex_lock(&root->fs_info->trans_mutex);
486b48652c1SYan Zheng 	list_add(&dirty->list, &latest->fs_info->dead_roots);
487b48652c1SYan Zheng 	mutex_unlock(&root->fs_info->trans_mutex);
4885eda7b5eSChris Mason 	return 0;
4895eda7b5eSChris Mason }
4905eda7b5eSChris Mason 
491d352ac68SChris Mason /*
492d352ac68SChris Mason  * at transaction commit time we need to schedule the old roots for
493d352ac68SChris Mason  * deletion via btrfs_drop_snapshot.  This runs through all the
494d352ac68SChris Mason  * reference counted roots that were modified in the current
495d352ac68SChris Mason  * transaction and puts them into the drop list
496d352ac68SChris Mason  */
49780b6794dSChris Mason static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
49835b7e476SChris Mason 				    struct radix_tree_root *radix,
49935b7e476SChris Mason 				    struct list_head *list)
5000f7d52f4SChris Mason {
501f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
5020f7d52f4SChris Mason 	struct btrfs_root *gang[8];
5030f7d52f4SChris Mason 	struct btrfs_root *root;
5040f7d52f4SChris Mason 	int i;
5050f7d52f4SChris Mason 	int ret;
50654aa1f4dSChris Mason 	int err = 0;
5075eda7b5eSChris Mason 	u32 refs;
50854aa1f4dSChris Mason 
5090f7d52f4SChris Mason 	while(1) {
5100f7d52f4SChris Mason 		ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
5110f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
5120f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
5130f7d52f4SChris Mason 		if (ret == 0)
5140f7d52f4SChris Mason 			break;
5150f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
5160f7d52f4SChris Mason 			root = gang[i];
5172619ba1fSChris Mason 			radix_tree_tag_clear(radix,
5182619ba1fSChris Mason 				     (unsigned long)root->root_key.objectid,
5190f7d52f4SChris Mason 				     BTRFS_ROOT_TRANS_TAG);
52031153d81SYan Zheng 
52131153d81SYan Zheng 			BUG_ON(!root->ref_tree);
522017e5369SChris Mason 			dirty = root->dirty_root;
52331153d81SYan Zheng 
524e02119d5SChris Mason 			btrfs_free_log(trans, root);
5251a40e23bSZheng Yan 			btrfs_free_reloc_root(root);
526e02119d5SChris Mason 
5270f7d52f4SChris Mason 			if (root->commit_root == root->node) {
528db94535dSChris Mason 				WARN_ON(root->node->start !=
529db94535dSChris Mason 					btrfs_root_bytenr(&root->root_item));
53031153d81SYan Zheng 
5315f39d397SChris Mason 				free_extent_buffer(root->commit_root);
5320f7d52f4SChris Mason 				root->commit_root = NULL;
5337ea394f1SYan Zheng 				root->dirty_root = NULL;
53431153d81SYan Zheng 
535bcc63abbSYan 				spin_lock(&root->list_lock);
536bcc63abbSYan 				list_del_init(&dirty->root->dead_list);
537bcc63abbSYan 				spin_unlock(&root->list_lock);
538bcc63abbSYan 
53931153d81SYan Zheng 				kfree(dirty->root);
54031153d81SYan Zheng 				kfree(dirty);
54158176a96SJosef Bacik 
54258176a96SJosef Bacik 				/* make sure to update the root on disk
54358176a96SJosef Bacik 				 * so we get any updates to the block used
54458176a96SJosef Bacik 				 * counts
54558176a96SJosef Bacik 				 */
54658176a96SJosef Bacik 				err = btrfs_update_root(trans,
54758176a96SJosef Bacik 						root->fs_info->tree_root,
54858176a96SJosef Bacik 						&root->root_key,
54958176a96SJosef Bacik 						&root->root_item);
5500f7d52f4SChris Mason 				continue;
5510f7d52f4SChris Mason 			}
5529f3a7427SChris Mason 
5539f3a7427SChris Mason 			memset(&root->root_item.drop_progress, 0,
5549f3a7427SChris Mason 			       sizeof(struct btrfs_disk_key));
5559f3a7427SChris Mason 			root->root_item.drop_level = 0;
5560f7d52f4SChris Mason 			root->commit_root = NULL;
5577ea394f1SYan Zheng 			root->dirty_root = NULL;
5580f7d52f4SChris Mason 			root->root_key.offset = root->fs_info->generation;
559db94535dSChris Mason 			btrfs_set_root_bytenr(&root->root_item,
560db94535dSChris Mason 					      root->node->start);
561db94535dSChris Mason 			btrfs_set_root_level(&root->root_item,
562db94535dSChris Mason 					     btrfs_header_level(root->node));
5630f7d52f4SChris Mason 			err = btrfs_insert_root(trans, root->fs_info->tree_root,
5640f7d52f4SChris Mason 						&root->root_key,
5650f7d52f4SChris Mason 						&root->root_item);
56654aa1f4dSChris Mason 			if (err)
56754aa1f4dSChris Mason 				break;
5689f3a7427SChris Mason 
5699f3a7427SChris Mason 			refs = btrfs_root_refs(&dirty->root->root_item);
5709f3a7427SChris Mason 			btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
5715eda7b5eSChris Mason 			err = btrfs_update_root(trans, root->fs_info->tree_root,
5729f3a7427SChris Mason 						&dirty->root->root_key,
5739f3a7427SChris Mason 						&dirty->root->root_item);
5745eda7b5eSChris Mason 
5755eda7b5eSChris Mason 			BUG_ON(err);
5769f3a7427SChris Mason 			if (refs == 1) {
5770f7d52f4SChris Mason 				list_add(&dirty->list, list);
5789f3a7427SChris Mason 			} else {
5799f3a7427SChris Mason 				WARN_ON(1);
58031153d81SYan Zheng 				free_extent_buffer(dirty->root->node);
5819f3a7427SChris Mason 				kfree(dirty->root);
5825eda7b5eSChris Mason 				kfree(dirty);
5830f7d52f4SChris Mason 			}
5840f7d52f4SChris Mason 		}
5859f3a7427SChris Mason 	}
58654aa1f4dSChris Mason 	return err;
5870f7d52f4SChris Mason }
5880f7d52f4SChris Mason 
589d352ac68SChris Mason /*
590d352ac68SChris Mason  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
591d352ac68SChris Mason  * otherwise every leaf in the btree is read and defragged.
592d352ac68SChris Mason  */
593e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
594e9d0b13bSChris Mason {
595e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
596e9d0b13bSChris Mason 	int ret;
597e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
598d3c2fdcfSChris Mason 	unsigned long nr;
599e9d0b13bSChris Mason 
600a2135011SChris Mason 	smp_mb();
601e9d0b13bSChris Mason 	if (root->defrag_running)
602e9d0b13bSChris Mason 		return 0;
603e9d0b13bSChris Mason 	trans = btrfs_start_transaction(root, 1);
6046b80053dSChris Mason 	while (1) {
605e9d0b13bSChris Mason 		root->defrag_running = 1;
606e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
607d3c2fdcfSChris Mason 		nr = trans->blocks_used;
608e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
609d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(info->tree_root, nr);
610e9d0b13bSChris Mason 		cond_resched();
611e9d0b13bSChris Mason 
612e9d0b13bSChris Mason 		trans = btrfs_start_transaction(root, 1);
6133f157a2fSChris Mason 		if (root->fs_info->closing || ret != -EAGAIN)
614e9d0b13bSChris Mason 			break;
615e9d0b13bSChris Mason 	}
616e9d0b13bSChris Mason 	root->defrag_running = 0;
617a2135011SChris Mason 	smp_mb();
618e9d0b13bSChris Mason 	btrfs_end_transaction(trans, root);
619e9d0b13bSChris Mason 	return 0;
620e9d0b13bSChris Mason }
621e9d0b13bSChris Mason 
622d352ac68SChris Mason /*
623d352ac68SChris Mason  * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
624d352ac68SChris Mason  * all of them
625d352ac68SChris Mason  */
62680b6794dSChris Mason static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
62735b7e476SChris Mason 				     struct list_head *list)
6280f7d52f4SChris Mason {
629f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
6300f7d52f4SChris Mason 	struct btrfs_trans_handle *trans;
631d3c2fdcfSChris Mason 	unsigned long nr;
632db94535dSChris Mason 	u64 num_bytes;
633db94535dSChris Mason 	u64 bytes_used;
634bcc63abbSYan 	u64 max_useless;
63554aa1f4dSChris Mason 	int ret = 0;
6369f3a7427SChris Mason 	int err;
6379f3a7427SChris Mason 
6380f7d52f4SChris Mason 	while(!list_empty(list)) {
63958176a96SJosef Bacik 		struct btrfs_root *root;
64058176a96SJosef Bacik 
641f321e491SYan Zheng 		dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
6420f7d52f4SChris Mason 		list_del_init(&dirty->list);
6435eda7b5eSChris Mason 
644db94535dSChris Mason 		num_bytes = btrfs_root_used(&dirty->root->root_item);
64558176a96SJosef Bacik 		root = dirty->latest_root;
646a2135011SChris Mason 		atomic_inc(&root->fs_info->throttles);
64758176a96SJosef Bacik 
6489f3a7427SChris Mason 		while(1) {
6490f7d52f4SChris Mason 			trans = btrfs_start_transaction(tree_root, 1);
6505b21f2edSZheng Yan 			mutex_lock(&root->fs_info->drop_mutex);
6519f3a7427SChris Mason 			ret = btrfs_drop_snapshot(trans, dirty->root);
6529f3a7427SChris Mason 			if (ret != -EAGAIN) {
6539f3a7427SChris Mason 				break;
6549f3a7427SChris Mason 			}
6555b21f2edSZheng Yan 			mutex_unlock(&root->fs_info->drop_mutex);
65658176a96SJosef Bacik 
6579f3a7427SChris Mason 			err = btrfs_update_root(trans,
6589f3a7427SChris Mason 					tree_root,
6599f3a7427SChris Mason 					&dirty->root->root_key,
6609f3a7427SChris Mason 					&dirty->root->root_item);
6619f3a7427SChris Mason 			if (err)
6629f3a7427SChris Mason 				ret = err;
663d3c2fdcfSChris Mason 			nr = trans->blocks_used;
664017e5369SChris Mason 			ret = btrfs_end_transaction(trans, tree_root);
6650f7d52f4SChris Mason 			BUG_ON(ret);
666a2135011SChris Mason 
667d3c2fdcfSChris Mason 			btrfs_btree_balance_dirty(tree_root, nr);
6684dc11904SChris Mason 			cond_resched();
6699f3a7427SChris Mason 		}
6709f3a7427SChris Mason 		BUG_ON(ret);
671a2135011SChris Mason 		atomic_dec(&root->fs_info->throttles);
672017e5369SChris Mason 		wake_up(&root->fs_info->transaction_throttle);
67358176a96SJosef Bacik 
674a2135011SChris Mason 		mutex_lock(&root->fs_info->alloc_mutex);
675db94535dSChris Mason 		num_bytes -= btrfs_root_used(&dirty->root->root_item);
676db94535dSChris Mason 		bytes_used = btrfs_root_used(&root->root_item);
677db94535dSChris Mason 		if (num_bytes) {
678e02119d5SChris Mason 			btrfs_record_root_in_trans(root);
6795f39d397SChris Mason 			btrfs_set_root_used(&root->root_item,
680db94535dSChris Mason 					    bytes_used - num_bytes);
68158176a96SJosef Bacik 		}
682a2135011SChris Mason 		mutex_unlock(&root->fs_info->alloc_mutex);
683a2135011SChris Mason 
6849f3a7427SChris Mason 		ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
68558176a96SJosef Bacik 		if (ret) {
68658176a96SJosef Bacik 			BUG();
68754aa1f4dSChris Mason 			break;
68858176a96SJosef Bacik 		}
689a2135011SChris Mason 		mutex_unlock(&root->fs_info->drop_mutex);
690a2135011SChris Mason 
691bcc63abbSYan 		spin_lock(&root->list_lock);
692bcc63abbSYan 		list_del_init(&dirty->root->dead_list);
693bcc63abbSYan 		if (!list_empty(&root->dead_list)) {
694bcc63abbSYan 			struct btrfs_root *oldest;
695bcc63abbSYan 			oldest = list_entry(root->dead_list.prev,
696bcc63abbSYan 					    struct btrfs_root, dead_list);
697bcc63abbSYan 			max_useless = oldest->root_key.offset - 1;
698bcc63abbSYan 		} else {
699bcc63abbSYan 			max_useless = root->root_key.offset - 1;
700bcc63abbSYan 		}
701bcc63abbSYan 		spin_unlock(&root->list_lock);
702bcc63abbSYan 
703d3c2fdcfSChris Mason 		nr = trans->blocks_used;
7040f7d52f4SChris Mason 		ret = btrfs_end_transaction(trans, tree_root);
7050f7d52f4SChris Mason 		BUG_ON(ret);
7065eda7b5eSChris Mason 
707e4657689SZheng Yan 		ret = btrfs_remove_leaf_refs(root, max_useless, 0);
708bcc63abbSYan 		BUG_ON(ret);
709bcc63abbSYan 
710f510cfecSChris Mason 		free_extent_buffer(dirty->root->node);
7115eda7b5eSChris Mason 		kfree(dirty->root);
7120f7d52f4SChris Mason 		kfree(dirty);
713d3c2fdcfSChris Mason 
714d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(tree_root, nr);
7154dc11904SChris Mason 		cond_resched();
7160f7d52f4SChris Mason 	}
71754aa1f4dSChris Mason 	return ret;
7180f7d52f4SChris Mason }
7190f7d52f4SChris Mason 
720d352ac68SChris Mason /*
721d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
722d352ac68SChris Mason  * transaction commit.  This does the actual creation
723d352ac68SChris Mason  */
72480b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
7253063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
7263063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
7273063d29fSChris Mason {
7283063d29fSChris Mason 	struct btrfs_key key;
72980b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
7303063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
7313063d29fSChris Mason 	struct btrfs_root *root = pending->root;
7323063d29fSChris Mason 	struct extent_buffer *tmp;
733925baeddSChris Mason 	struct extent_buffer *old;
7343063d29fSChris Mason 	int ret;
7353b96362cSSven Wegener 	int namelen;
7363063d29fSChris Mason 	u64 objectid;
7373063d29fSChris Mason 
73880b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
73980b6794dSChris Mason 	if (!new_root_item) {
74080b6794dSChris Mason 		ret = -ENOMEM;
74180b6794dSChris Mason 		goto fail;
74280b6794dSChris Mason 	}
7433063d29fSChris Mason 	ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
7443063d29fSChris Mason 	if (ret)
7453063d29fSChris Mason 		goto fail;
7463063d29fSChris Mason 
74780b6794dSChris Mason 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
7483063d29fSChris Mason 
7493063d29fSChris Mason 	key.objectid = objectid;
7505b21f2edSZheng Yan 	key.offset = trans->transid;
7513063d29fSChris Mason 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
7523063d29fSChris Mason 
753925baeddSChris Mason 	old = btrfs_lock_root_node(root);
75465b51a00SChris Mason 	btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
7553063d29fSChris Mason 
756925baeddSChris Mason 	btrfs_copy_root(trans, root, old, &tmp, objectid);
757925baeddSChris Mason 	btrfs_tree_unlock(old);
758925baeddSChris Mason 	free_extent_buffer(old);
7593063d29fSChris Mason 
76080b6794dSChris Mason 	btrfs_set_root_bytenr(new_root_item, tmp->start);
76180b6794dSChris Mason 	btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
7623063d29fSChris Mason 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
76380b6794dSChris Mason 				new_root_item);
764925baeddSChris Mason 	btrfs_tree_unlock(tmp);
7653063d29fSChris Mason 	free_extent_buffer(tmp);
7663063d29fSChris Mason 	if (ret)
7673063d29fSChris Mason 		goto fail;
7683063d29fSChris Mason 
7693063d29fSChris Mason 	/*
7703063d29fSChris Mason 	 * insert the directory item
7713063d29fSChris Mason 	 */
7723063d29fSChris Mason 	key.offset = (u64)-1;
7733b96362cSSven Wegener 	namelen = strlen(pending->name);
7743063d29fSChris Mason 	ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
7753b96362cSSven Wegener 				    pending->name, namelen,
7763063d29fSChris Mason 				    root->fs_info->sb->s_root->d_inode->i_ino,
777aec7477bSJosef Bacik 				    &key, BTRFS_FT_DIR, 0);
7783063d29fSChris Mason 
7793063d29fSChris Mason 	if (ret)
7803063d29fSChris Mason 		goto fail;
7813063d29fSChris Mason 
7823063d29fSChris Mason 	ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
7833063d29fSChris Mason 			     pending->name, strlen(pending->name), objectid,
784aec7477bSJosef Bacik 			     root->fs_info->sb->s_root->d_inode->i_ino, 0);
7853b96362cSSven Wegener 
7863b96362cSSven Wegener 	/* Invalidate existing dcache entry for new snapshot. */
7873b96362cSSven Wegener 	btrfs_invalidate_dcache_root(root, pending->name, namelen);
7883b96362cSSven Wegener 
7893063d29fSChris Mason fail:
79080b6794dSChris Mason 	kfree(new_root_item);
7913063d29fSChris Mason 	return ret;
7923063d29fSChris Mason }
7933063d29fSChris Mason 
794d352ac68SChris Mason /*
795d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
796d352ac68SChris Mason  */
79780b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
7983063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
7993063d29fSChris Mason {
8003063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
8013063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
8023063d29fSChris Mason 	int ret;
8033063d29fSChris Mason 
8043063d29fSChris Mason 	while(!list_empty(head)) {
8053063d29fSChris Mason 		pending = list_entry(head->next,
8063063d29fSChris Mason 				     struct btrfs_pending_snapshot, list);
8073063d29fSChris Mason 		ret = create_pending_snapshot(trans, fs_info, pending);
8083063d29fSChris Mason 		BUG_ON(ret);
8093063d29fSChris Mason 		list_del(&pending->list);
8103063d29fSChris Mason 		kfree(pending->name);
8113063d29fSChris Mason 		kfree(pending);
8123063d29fSChris Mason 	}
813dc17ff8fSChris Mason 	return 0;
814dc17ff8fSChris Mason }
815dc17ff8fSChris Mason 
81679154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
81779154b1bSChris Mason 			     struct btrfs_root *root)
81879154b1bSChris Mason {
81915ee9bc7SJosef Bacik 	unsigned long joined = 0;
82015ee9bc7SJosef Bacik 	unsigned long timeout = 1;
82179154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
8228fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
8230b86a832SChris Mason 	struct btrfs_root *chunk_root = root->fs_info->chunk_root;
8240f7d52f4SChris Mason 	struct list_head dirty_fs_roots;
825d1310b2eSChris Mason 	struct extent_io_tree *pinned_copy;
82679154b1bSChris Mason 	DEFINE_WAIT(wait);
82715ee9bc7SJosef Bacik 	int ret;
82879154b1bSChris Mason 
8290f7d52f4SChris Mason 	INIT_LIST_HEAD(&dirty_fs_roots);
83079154b1bSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
83179154b1bSChris Mason 	if (trans->transaction->in_commit) {
83279154b1bSChris Mason 		cur_trans = trans->transaction;
83379154b1bSChris Mason 		trans->transaction->use_count++;
834ccd467d6SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
83579154b1bSChris Mason 		btrfs_end_transaction(trans, root);
836ccd467d6SChris Mason 
83779154b1bSChris Mason 		ret = wait_for_commit(root, cur_trans);
83879154b1bSChris Mason 		BUG_ON(ret);
83915ee9bc7SJosef Bacik 
84015ee9bc7SJosef Bacik 		mutex_lock(&root->fs_info->trans_mutex);
84179154b1bSChris Mason 		put_transaction(cur_trans);
84215ee9bc7SJosef Bacik 		mutex_unlock(&root->fs_info->trans_mutex);
84315ee9bc7SJosef Bacik 
84479154b1bSChris Mason 		return 0;
84579154b1bSChris Mason 	}
8464313b399SChris Mason 
8474313b399SChris Mason 	pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
8484313b399SChris Mason 	if (!pinned_copy)
8494313b399SChris Mason 		return -ENOMEM;
8504313b399SChris Mason 
851d1310b2eSChris Mason 	extent_io_tree_init(pinned_copy,
8524313b399SChris Mason 			     root->fs_info->btree_inode->i_mapping, GFP_NOFS);
8534313b399SChris Mason 
8542c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
855f9295749SChris Mason 	trans->transaction->blocked = 1;
856ccd467d6SChris Mason 	cur_trans = trans->transaction;
857ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
858ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
859ccd467d6SChris Mason 					struct btrfs_transaction, list);
860ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
861ccd467d6SChris Mason 			prev_trans->use_count++;
862ccd467d6SChris Mason 			mutex_unlock(&root->fs_info->trans_mutex);
863ccd467d6SChris Mason 
864ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
865ccd467d6SChris Mason 
866ccd467d6SChris Mason 			mutex_lock(&root->fs_info->trans_mutex);
86715ee9bc7SJosef Bacik 			put_transaction(prev_trans);
868ccd467d6SChris Mason 		}
869ccd467d6SChris Mason 	}
87015ee9bc7SJosef Bacik 
87115ee9bc7SJosef Bacik 	do {
8727ea394f1SYan Zheng 		int snap_pending = 0;
87315ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
8747ea394f1SYan Zheng 		if (!list_empty(&trans->transaction->pending_snapshots))
8757ea394f1SYan Zheng 			snap_pending = 1;
8767ea394f1SYan Zheng 
8772c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
87815ee9bc7SJosef Bacik 		prepare_to_wait(&cur_trans->writer_wait, &wait,
87979154b1bSChris Mason 				TASK_UNINTERRUPTIBLE);
88015ee9bc7SJosef Bacik 
88115ee9bc7SJosef Bacik 		if (cur_trans->num_writers > 1)
88215ee9bc7SJosef Bacik 			timeout = MAX_SCHEDULE_TIMEOUT;
88315ee9bc7SJosef Bacik 		else
88415ee9bc7SJosef Bacik 			timeout = 1;
88515ee9bc7SJosef Bacik 
88679154b1bSChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
88715ee9bc7SJosef Bacik 
8887ea394f1SYan Zheng 		if (snap_pending) {
8897ea394f1SYan Zheng 			ret = btrfs_wait_ordered_extents(root, 1);
8907ea394f1SYan Zheng 			BUG_ON(ret);
8917ea394f1SYan Zheng 		}
8927ea394f1SYan Zheng 
89315ee9bc7SJosef Bacik 		schedule_timeout(timeout);
89415ee9bc7SJosef Bacik 
89579154b1bSChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
89615ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
89715ee9bc7SJosef Bacik 	} while (cur_trans->num_writers > 1 ||
89815ee9bc7SJosef Bacik 		 (cur_trans->num_joined != joined));
89915ee9bc7SJosef Bacik 
9003063d29fSChris Mason 	ret = create_pending_snapshots(trans, root->fs_info);
9013063d29fSChris Mason 	BUG_ON(ret);
9023063d29fSChris Mason 
9032c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
904dc17ff8fSChris Mason 
905e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
906e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
907e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
908e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
909e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
910e02119d5SChris Mason 	 * of the trees.
911e02119d5SChris Mason 	 *
912e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
913e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
914e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
915e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
916e02119d5SChris Mason 	 * with the tree-log code.
917e02119d5SChris Mason 	 */
918e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
9191a40e23bSZheng Yan 	/*
9201a40e23bSZheng Yan 	 * keep tree reloc code from adding new reloc trees
9211a40e23bSZheng Yan 	 */
9221a40e23bSZheng Yan 	mutex_lock(&root->fs_info->tree_reloc_mutex);
9231a40e23bSZheng Yan 
924e02119d5SChris Mason 
92554aa1f4dSChris Mason 	ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
92654aa1f4dSChris Mason 			      &dirty_fs_roots);
92754aa1f4dSChris Mason 	BUG_ON(ret);
92854aa1f4dSChris Mason 
929e02119d5SChris Mason 	/* add_dirty_roots gets rid of all the tree log roots, it is now
930e02119d5SChris Mason 	 * safe to free the root of tree log roots
931e02119d5SChris Mason 	 */
932e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
933e02119d5SChris Mason 
9341a40e23bSZheng Yan 	btrfs_free_reloc_mappings(root);
9351a40e23bSZheng Yan 
93679154b1bSChris Mason 	ret = btrfs_commit_tree_roots(trans, root);
93779154b1bSChris Mason 	BUG_ON(ret);
93854aa1f4dSChris Mason 
93978fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
940cee36a03SChris Mason 	spin_lock(&root->fs_info->new_trans_lock);
94178fae27eSChris Mason 	root->fs_info->running_transaction = NULL;
942cee36a03SChris Mason 	spin_unlock(&root->fs_info->new_trans_lock);
9434b52dff6SChris Mason 	btrfs_set_super_generation(&root->fs_info->super_copy,
9444b52dff6SChris Mason 				   cur_trans->transid);
9454b52dff6SChris Mason 	btrfs_set_super_root(&root->fs_info->super_copy,
946db94535dSChris Mason 			     root->fs_info->tree_root->node->start);
947db94535dSChris Mason 	btrfs_set_super_root_level(&root->fs_info->super_copy,
948db94535dSChris Mason 			   btrfs_header_level(root->fs_info->tree_root->node));
9495f39d397SChris Mason 
9500b86a832SChris Mason 	btrfs_set_super_chunk_root(&root->fs_info->super_copy,
9510b86a832SChris Mason 				   chunk_root->node->start);
9520b86a832SChris Mason 	btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
9530b86a832SChris Mason 					 btrfs_header_level(chunk_root->node));
954e02119d5SChris Mason 
955e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
956e02119d5SChris Mason 		btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
957e02119d5SChris Mason 		btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
958e02119d5SChris Mason 	}
959e02119d5SChris Mason 
960a061fc8dSChris Mason 	memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
9614b52dff6SChris Mason 	       sizeof(root->fs_info->super_copy));
962ccd467d6SChris Mason 
9634313b399SChris Mason 	btrfs_copy_pinned(root, pinned_copy);
964ccd467d6SChris Mason 
965f9295749SChris Mason 	trans->transaction->blocked = 0;
966e6dcd2dcSChris Mason 	wake_up(&root->fs_info->transaction_throttle);
967f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
968e6dcd2dcSChris Mason 
96978fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
97079154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
97179154b1bSChris Mason 	BUG_ON(ret);
97279154b1bSChris Mason 	write_ctree_super(trans, root);
9734313b399SChris Mason 
974e02119d5SChris Mason 	/*
975e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
976e02119d5SChris Mason 	 * to go about their business
977e02119d5SChris Mason 	 */
978e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
979e02119d5SChris Mason 
9804313b399SChris Mason 	btrfs_finish_extent_commit(trans, root, pinned_copy);
9814313b399SChris Mason 	kfree(pinned_copy);
9824313b399SChris Mason 
9831a40e23bSZheng Yan 	btrfs_drop_dead_reloc_roots(root);
9841a40e23bSZheng Yan 	mutex_unlock(&root->fs_info->tree_reloc_mutex);
9851a40e23bSZheng Yan 
9861a40e23bSZheng Yan 	mutex_lock(&root->fs_info->trans_mutex);
9871a40e23bSZheng Yan 
9882c90e5d6SChris Mason 	cur_trans->commit_done = 1;
98915ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
9902c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
99179154b1bSChris Mason 	put_transaction(cur_trans);
99278fae27eSChris Mason 	put_transaction(cur_trans);
99358176a96SJosef Bacik 
994bcc63abbSYan 	list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
995facda1e7SChris Mason 	if (root->fs_info->closing)
996facda1e7SChris Mason 		list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
99758176a96SJosef Bacik 
99878fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
9992c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
100079154b1bSChris Mason 
1001facda1e7SChris Mason 	if (root->fs_info->closing) {
10020f7d52f4SChris Mason 		drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
1003facda1e7SChris Mason 	}
100479154b1bSChris Mason 	return ret;
100579154b1bSChris Mason }
100679154b1bSChris Mason 
1007d352ac68SChris Mason /*
1008d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1009d352ac68SChris Mason  */
1010e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1011e9d0b13bSChris Mason {
1012e9d0b13bSChris Mason 	struct list_head dirty_roots;
1013e9d0b13bSChris Mason 	INIT_LIST_HEAD(&dirty_roots);
1014a74a4b97SChris Mason again:
1015e9d0b13bSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
1016e9d0b13bSChris Mason 	list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
1017e9d0b13bSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
1018e9d0b13bSChris Mason 
1019e9d0b13bSChris Mason 	if (!list_empty(&dirty_roots)) {
1020e9d0b13bSChris Mason 		drop_dirty_roots(root, &dirty_roots);
1021a74a4b97SChris Mason 		goto again;
1022e9d0b13bSChris Mason 	}
1023e9d0b13bSChris Mason 	return 0;
1024e9d0b13bSChris Mason }
1025