xref: /openbmc/linux/fs/btrfs/transaction.c (revision c6e30871)
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>
235f2cc086SChris Mason #include <linux/blkdev.h>
2479154b1bSChris Mason #include "ctree.h"
2579154b1bSChris Mason #include "disk-io.h"
2679154b1bSChris Mason #include "transaction.h"
27925baeddSChris Mason #include "locking.h"
2831153d81SYan Zheng #include "ref-cache.h"
29e02119d5SChris Mason #include "tree-log.h"
3079154b1bSChris Mason 
310f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0
320f7d52f4SChris Mason 
3380b6794dSChris Mason static noinline void put_transaction(struct btrfs_transaction *transaction)
3479154b1bSChris Mason {
352c90e5d6SChris Mason 	WARN_ON(transaction->use_count == 0);
3679154b1bSChris Mason 	transaction->use_count--;
3778fae27eSChris Mason 	if (transaction->use_count == 0) {
388fd17795SChris Mason 		list_del_init(&transaction->list);
392c90e5d6SChris Mason 		memset(transaction, 0, sizeof(*transaction));
402c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
4179154b1bSChris Mason 	}
4278fae27eSChris Mason }
4379154b1bSChris Mason 
44d352ac68SChris Mason /*
45d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
46d352ac68SChris Mason  */
4780b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root)
4879154b1bSChris Mason {
4979154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
5079154b1bSChris Mason 	cur_trans = root->fs_info->running_transaction;
5179154b1bSChris Mason 	if (!cur_trans) {
522c90e5d6SChris Mason 		cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
532c90e5d6SChris Mason 					     GFP_NOFS);
5479154b1bSChris Mason 		BUG_ON(!cur_trans);
550f7d52f4SChris Mason 		root->fs_info->generation++;
56e18e4809SChris Mason 		root->fs_info->last_alloc = 0;
574529ba49SChris Mason 		root->fs_info->last_data_alloc = 0;
5815ee9bc7SJosef Bacik 		cur_trans->num_writers = 1;
5915ee9bc7SJosef Bacik 		cur_trans->num_joined = 0;
600f7d52f4SChris Mason 		cur_trans->transid = root->fs_info->generation;
6179154b1bSChris Mason 		init_waitqueue_head(&cur_trans->writer_wait);
6279154b1bSChris Mason 		init_waitqueue_head(&cur_trans->commit_wait);
6379154b1bSChris Mason 		cur_trans->in_commit = 0;
64f9295749SChris Mason 		cur_trans->blocked = 0;
65d5719762SChris Mason 		cur_trans->use_count = 1;
6679154b1bSChris Mason 		cur_trans->commit_done = 0;
6708607c1bSChris Mason 		cur_trans->start_time = get_seconds();
683063d29fSChris Mason 		INIT_LIST_HEAD(&cur_trans->pending_snapshots);
698fd17795SChris Mason 		list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
70d1310b2eSChris Mason 		extent_io_tree_init(&cur_trans->dirty_pages,
715f39d397SChris Mason 				     root->fs_info->btree_inode->i_mapping,
725f39d397SChris Mason 				     GFP_NOFS);
7348ec2cf8SChris Mason 		spin_lock(&root->fs_info->new_trans_lock);
7448ec2cf8SChris Mason 		root->fs_info->running_transaction = cur_trans;
7548ec2cf8SChris Mason 		spin_unlock(&root->fs_info->new_trans_lock);
7615ee9bc7SJosef Bacik 	} else {
7779154b1bSChris Mason 		cur_trans->num_writers++;
7815ee9bc7SJosef Bacik 		cur_trans->num_joined++;
7915ee9bc7SJosef Bacik 	}
8015ee9bc7SJosef Bacik 
8179154b1bSChris Mason 	return 0;
8279154b1bSChris Mason }
8379154b1bSChris Mason 
84d352ac68SChris Mason /*
85d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
86d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
87d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
88d397712bSChris Mason  * when the transaction commits
89d352ac68SChris Mason  */
90e02119d5SChris Mason noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
916702ed49SChris Mason {
92f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
936702ed49SChris Mason 	u64 running_trans_id = root->fs_info->running_transaction->transid;
946702ed49SChris Mason 	if (root->ref_cows && root->last_trans < running_trans_id) {
956702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
966702ed49SChris Mason 		if (root->root_item.refs != 0) {
976702ed49SChris Mason 			radix_tree_tag_set(&root->fs_info->fs_roots_radix,
986702ed49SChris Mason 				   (unsigned long)root->root_key.objectid,
996702ed49SChris Mason 				   BTRFS_ROOT_TRANS_TAG);
10031153d81SYan Zheng 
10131153d81SYan Zheng 			dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
10231153d81SYan Zheng 			BUG_ON(!dirty);
10331153d81SYan Zheng 			dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
10431153d81SYan Zheng 			BUG_ON(!dirty->root);
10531153d81SYan Zheng 			dirty->latest_root = root;
10631153d81SYan Zheng 			INIT_LIST_HEAD(&dirty->list);
10731153d81SYan Zheng 
108925baeddSChris Mason 			root->commit_root = btrfs_root_node(root);
10931153d81SYan Zheng 
11031153d81SYan Zheng 			memcpy(dirty->root, root, sizeof(*root));
11131153d81SYan Zheng 			spin_lock_init(&dirty->root->node_lock);
112bcc63abbSYan 			spin_lock_init(&dirty->root->list_lock);
11331153d81SYan Zheng 			mutex_init(&dirty->root->objectid_mutex);
1145b21f2edSZheng Yan 			mutex_init(&dirty->root->log_mutex);
115bcc63abbSYan 			INIT_LIST_HEAD(&dirty->root->dead_list);
11631153d81SYan Zheng 			dirty->root->node = root->commit_root;
11731153d81SYan Zheng 			dirty->root->commit_root = NULL;
118bcc63abbSYan 
119bcc63abbSYan 			spin_lock(&root->list_lock);
120bcc63abbSYan 			list_add(&dirty->root->dead_list, &root->dead_list);
121bcc63abbSYan 			spin_unlock(&root->list_lock);
122bcc63abbSYan 
123bcc63abbSYan 			root->dirty_root = dirty;
1246702ed49SChris Mason 		} else {
1256702ed49SChris Mason 			WARN_ON(1);
1266702ed49SChris Mason 		}
1276702ed49SChris Mason 		root->last_trans = running_trans_id;
1286702ed49SChris Mason 	}
1296702ed49SChris Mason 	return 0;
1306702ed49SChris Mason }
1316702ed49SChris Mason 
132d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
133d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
134d352ac68SChris Mason  * transaction might not be fully on disk.
135d352ac68SChris Mason  */
13637d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
13779154b1bSChris Mason {
138f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
13979154b1bSChris Mason 
140f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
14137d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
142f9295749SChris Mason 		DEFINE_WAIT(wait);
143f9295749SChris Mason 		cur_trans->use_count++;
144f9295749SChris Mason 		while (1) {
145f9295749SChris Mason 			prepare_to_wait(&root->fs_info->transaction_wait, &wait,
146f9295749SChris Mason 					TASK_UNINTERRUPTIBLE);
147f9295749SChris Mason 			if (cur_trans->blocked) {
148f9295749SChris Mason 				mutex_unlock(&root->fs_info->trans_mutex);
149f9295749SChris Mason 				schedule();
150f9295749SChris Mason 				mutex_lock(&root->fs_info->trans_mutex);
151f9295749SChris Mason 				finish_wait(&root->fs_info->transaction_wait,
152f9295749SChris Mason 					    &wait);
153f9295749SChris Mason 			} else {
154f9295749SChris Mason 				finish_wait(&root->fs_info->transaction_wait,
155f9295749SChris Mason 					    &wait);
156f9295749SChris Mason 				break;
157f9295749SChris Mason 			}
158f9295749SChris Mason 		}
159f9295749SChris Mason 		put_transaction(cur_trans);
160f9295749SChris Mason 	}
16137d1aeeeSChris Mason }
16237d1aeeeSChris Mason 
163e02119d5SChris Mason static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
1649ca9ee09SSage Weil 					     int num_blocks, int wait)
16537d1aeeeSChris Mason {
16637d1aeeeSChris Mason 	struct btrfs_trans_handle *h =
16737d1aeeeSChris Mason 		kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
16837d1aeeeSChris Mason 	int ret;
16937d1aeeeSChris Mason 
17037d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
1714bef0848SChris Mason 	if (!root->fs_info->log_root_recovering &&
1724bef0848SChris Mason 	    ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
17337d1aeeeSChris Mason 		wait_current_trans(root);
17479154b1bSChris Mason 	ret = join_transaction(root);
17579154b1bSChris Mason 	BUG_ON(ret);
1760f7d52f4SChris Mason 
177e02119d5SChris Mason 	btrfs_record_root_in_trans(root);
1786702ed49SChris Mason 	h->transid = root->fs_info->running_transaction->transid;
17979154b1bSChris Mason 	h->transaction = root->fs_info->running_transaction;
18079154b1bSChris Mason 	h->blocks_reserved = num_blocks;
18179154b1bSChris Mason 	h->blocks_used = 0;
182d2fb3437SYan Zheng 	h->block_group = 0;
18326b8003fSChris Mason 	h->alloc_exclude_nr = 0;
18426b8003fSChris Mason 	h->alloc_exclude_start = 0;
18579154b1bSChris Mason 	root->fs_info->running_transaction->use_count++;
18679154b1bSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
18779154b1bSChris Mason 	return h;
18879154b1bSChris Mason }
18979154b1bSChris Mason 
190f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
191f9295749SChris Mason 						   int num_blocks)
192f9295749SChris Mason {
1939ca9ee09SSage Weil 	return start_transaction(root, num_blocks, 1);
194f9295749SChris Mason }
195f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
196f9295749SChris Mason 						   int num_blocks)
197f9295749SChris Mason {
1989ca9ee09SSage Weil 	return start_transaction(root, num_blocks, 0);
199f9295749SChris Mason }
200f9295749SChris Mason 
2019ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
2029ca9ee09SSage Weil 							 int num_blocks)
2039ca9ee09SSage Weil {
2049ca9ee09SSage Weil 	return start_transaction(r, num_blocks, 2);
2059ca9ee09SSage Weil }
2069ca9ee09SSage Weil 
207d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
20889ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root,
20989ce8a63SChris Mason 				    struct btrfs_transaction *commit)
21089ce8a63SChris Mason {
21189ce8a63SChris Mason 	DEFINE_WAIT(wait);
21289ce8a63SChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
21389ce8a63SChris Mason 	while (!commit->commit_done) {
21489ce8a63SChris Mason 		prepare_to_wait(&commit->commit_wait, &wait,
21589ce8a63SChris Mason 				TASK_UNINTERRUPTIBLE);
21689ce8a63SChris Mason 		if (commit->commit_done)
21789ce8a63SChris Mason 			break;
21889ce8a63SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
21989ce8a63SChris Mason 		schedule();
22089ce8a63SChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
22189ce8a63SChris Mason 	}
22289ce8a63SChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
22389ce8a63SChris Mason 	finish_wait(&commit->commit_wait, &wait);
22489ce8a63SChris Mason 	return 0;
22589ce8a63SChris Mason }
22689ce8a63SChris Mason 
227d352ac68SChris Mason /*
228d397712bSChris Mason  * rate limit against the drop_snapshot code.  This helps to slow down new
229d397712bSChris Mason  * operations if the drop_snapshot code isn't able to keep up.
230d352ac68SChris Mason  */
23137d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root)
232ab78c84dSChris Mason {
233ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
2342dd3e67bSChris Mason 	int harder_count = 0;
235ab78c84dSChris Mason 
2362dd3e67bSChris Mason harder:
237ab78c84dSChris Mason 	if (atomic_read(&info->throttles)) {
238ab78c84dSChris Mason 		DEFINE_WAIT(wait);
239ab78c84dSChris Mason 		int thr;
240ab78c84dSChris Mason 		thr = atomic_read(&info->throttle_gen);
241ab78c84dSChris Mason 
242ab78c84dSChris Mason 		do {
243ab78c84dSChris Mason 			prepare_to_wait(&info->transaction_throttle,
244ab78c84dSChris Mason 					&wait, TASK_UNINTERRUPTIBLE);
245ab78c84dSChris Mason 			if (!atomic_read(&info->throttles)) {
246ab78c84dSChris Mason 				finish_wait(&info->transaction_throttle, &wait);
247ab78c84dSChris Mason 				break;
248ab78c84dSChris Mason 			}
249ab78c84dSChris Mason 			schedule();
250ab78c84dSChris Mason 			finish_wait(&info->transaction_throttle, &wait);
251ab78c84dSChris Mason 		} while (thr == atomic_read(&info->throttle_gen));
2522dd3e67bSChris Mason 		harder_count++;
2532dd3e67bSChris Mason 
2542dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
2552dd3e67bSChris Mason 		    harder_count < 2)
2562dd3e67bSChris Mason 			goto harder;
2572dd3e67bSChris Mason 
2582dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
2592dd3e67bSChris Mason 		    harder_count < 10)
2602dd3e67bSChris Mason 			goto harder;
2612dd3e67bSChris Mason 
2622dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
2632dd3e67bSChris Mason 		    harder_count < 20)
2642dd3e67bSChris Mason 			goto harder;
265ab78c84dSChris Mason 	}
266ab78c84dSChris Mason }
267ab78c84dSChris Mason 
26837d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
26937d1aeeeSChris Mason {
27037d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
2719ca9ee09SSage Weil 	if (!root->fs_info->open_ioctl_trans)
27237d1aeeeSChris Mason 		wait_current_trans(root);
27337d1aeeeSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
27437d1aeeeSChris Mason 
27537d1aeeeSChris Mason 	throttle_on_drops(root);
27637d1aeeeSChris Mason }
27737d1aeeeSChris Mason 
27889ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
27989ce8a63SChris Mason 			  struct btrfs_root *root, int throttle)
28079154b1bSChris Mason {
28179154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
282ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
283d6e4a428SChris Mason 
284ab78c84dSChris Mason 	mutex_lock(&info->trans_mutex);
285ab78c84dSChris Mason 	cur_trans = info->running_transaction;
286ccd467d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
287d5719762SChris Mason 	WARN_ON(cur_trans->num_writers < 1);
288ccd467d6SChris Mason 	cur_trans->num_writers--;
28989ce8a63SChris Mason 
29079154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
29179154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
29279154b1bSChris Mason 	put_transaction(cur_trans);
293ab78c84dSChris Mason 	mutex_unlock(&info->trans_mutex);
294d6025579SChris Mason 	memset(trans, 0, sizeof(*trans));
2952c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
296ab78c84dSChris Mason 
297ab78c84dSChris Mason 	if (throttle)
29837d1aeeeSChris Mason 		throttle_on_drops(root);
299ab78c84dSChris Mason 
30079154b1bSChris Mason 	return 0;
30179154b1bSChris Mason }
30279154b1bSChris Mason 
30389ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
30489ce8a63SChris Mason 			  struct btrfs_root *root)
30589ce8a63SChris Mason {
30689ce8a63SChris Mason 	return __btrfs_end_transaction(trans, root, 0);
30789ce8a63SChris Mason }
30889ce8a63SChris Mason 
30989ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
31089ce8a63SChris Mason 				   struct btrfs_root *root)
31189ce8a63SChris Mason {
31289ce8a63SChris Mason 	return __btrfs_end_transaction(trans, root, 1);
31389ce8a63SChris Mason }
31489ce8a63SChris Mason 
315d352ac68SChris Mason /*
316d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
317d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
318d352ac68SChris Mason  * those extents are on disk for transaction or log commit
319d352ac68SChris Mason  */
320d0c803c4SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
321d0c803c4SChris Mason 					struct extent_io_tree *dirty_pages)
32279154b1bSChris Mason {
3237c4452b9SChris Mason 	int ret;
324777e6bd7SChris Mason 	int err = 0;
3257c4452b9SChris Mason 	int werr = 0;
3267c4452b9SChris Mason 	struct page *page;
3277c4452b9SChris Mason 	struct inode *btree_inode = root->fs_info->btree_inode;
328777e6bd7SChris Mason 	u64 start = 0;
3295f39d397SChris Mason 	u64 end;
3305f39d397SChris Mason 	unsigned long index;
3317c4452b9SChris Mason 
3327c4452b9SChris Mason 	while (1) {
333777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, start, &start, &end,
3345f39d397SChris Mason 					    EXTENT_DIRTY);
3355f39d397SChris Mason 		if (ret)
3367c4452b9SChris Mason 			break;
3375f39d397SChris Mason 		while (start <= end) {
338777e6bd7SChris Mason 			cond_resched();
339777e6bd7SChris Mason 
3405f39d397SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
34135ebb934SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
3424bef0848SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
3437c4452b9SChris Mason 			if (!page)
3447c4452b9SChris Mason 				continue;
3454bef0848SChris Mason 
3464bef0848SChris Mason 			btree_lock_page_hook(page);
3474bef0848SChris Mason 			if (!page->mapping) {
3484bef0848SChris Mason 				unlock_page(page);
3494bef0848SChris Mason 				page_cache_release(page);
3504bef0848SChris Mason 				continue;
3514bef0848SChris Mason 			}
3524bef0848SChris Mason 
3536702ed49SChris Mason 			if (PageWriteback(page)) {
3546702ed49SChris Mason 				if (PageDirty(page))
3556702ed49SChris Mason 					wait_on_page_writeback(page);
3566702ed49SChris Mason 				else {
3576702ed49SChris Mason 					unlock_page(page);
3586702ed49SChris Mason 					page_cache_release(page);
3596702ed49SChris Mason 					continue;
3606702ed49SChris Mason 				}
3616702ed49SChris Mason 			}
3627c4452b9SChris Mason 			err = write_one_page(page, 0);
3637c4452b9SChris Mason 			if (err)
3647c4452b9SChris Mason 				werr = err;
3657c4452b9SChris Mason 			page_cache_release(page);
3667c4452b9SChris Mason 		}
3677c4452b9SChris Mason 	}
368777e6bd7SChris Mason 	while (1) {
369777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
370777e6bd7SChris Mason 					    EXTENT_DIRTY);
371777e6bd7SChris Mason 		if (ret)
372777e6bd7SChris Mason 			break;
373777e6bd7SChris Mason 
374777e6bd7SChris Mason 		clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
375777e6bd7SChris Mason 		while (start <= end) {
376777e6bd7SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
377777e6bd7SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
378777e6bd7SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
379777e6bd7SChris Mason 			if (!page)
380777e6bd7SChris Mason 				continue;
381777e6bd7SChris Mason 			if (PageDirty(page)) {
3824bef0848SChris Mason 				btree_lock_page_hook(page);
3834bef0848SChris Mason 				wait_on_page_writeback(page);
384777e6bd7SChris Mason 				err = write_one_page(page, 0);
385777e6bd7SChris Mason 				if (err)
386777e6bd7SChris Mason 					werr = err;
387777e6bd7SChris Mason 			}
388777e6bd7SChris Mason 			wait_on_page_writeback(page);
389777e6bd7SChris Mason 			page_cache_release(page);
390777e6bd7SChris Mason 			cond_resched();
391777e6bd7SChris Mason 		}
392777e6bd7SChris Mason 	}
3937c4452b9SChris Mason 	if (err)
3947c4452b9SChris Mason 		werr = err;
3957c4452b9SChris Mason 	return werr;
39679154b1bSChris Mason }
39779154b1bSChris Mason 
398d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
399d0c803c4SChris Mason 				     struct btrfs_root *root)
400d0c803c4SChris Mason {
401d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
402d0c803c4SChris Mason 		struct inode *btree_inode;
403d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
404d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
405d0c803c4SChris Mason 	}
406d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
407d0c803c4SChris Mason 					   &trans->transaction->dirty_pages);
408d0c803c4SChris Mason }
409d0c803c4SChris Mason 
410d352ac68SChris Mason /*
411d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
412d352ac68SChris Mason  *
413d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
414d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
415d352ac68SChris Mason  * allocation tree.
416d352ac68SChris Mason  *
417d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
418d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
419d352ac68SChris Mason  */
4200b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
42179154b1bSChris Mason 			       struct btrfs_root *root)
42279154b1bSChris Mason {
42379154b1bSChris Mason 	int ret;
4240b86a832SChris Mason 	u64 old_root_bytenr;
4250b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
42679154b1bSChris Mason 
42787ef2bb4SChris Mason 	btrfs_extent_post_op(trans, root);
4280b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
42987ef2bb4SChris Mason 	btrfs_extent_post_op(trans, root);
43087ef2bb4SChris Mason 
43179154b1bSChris Mason 	while (1) {
4320b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
4330b86a832SChris Mason 		if (old_root_bytenr == root->node->start)
43479154b1bSChris Mason 			break;
4350b86a832SChris Mason 		btrfs_set_root_bytenr(&root->root_item,
4360b86a832SChris Mason 				       root->node->start);
4370b86a832SChris Mason 		btrfs_set_root_level(&root->root_item,
4380b86a832SChris Mason 				     btrfs_header_level(root->node));
43984234f3aSYan Zheng 		btrfs_set_root_generation(&root->root_item, trans->transid);
44087ef2bb4SChris Mason 
44187ef2bb4SChris Mason 		btrfs_extent_post_op(trans, root);
44287ef2bb4SChris Mason 
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);
44887ef2bb4SChris Mason 		btrfs_extent_post_op(trans, root);
4490b86a832SChris Mason 	}
4500b86a832SChris Mason 	return 0;
4510b86a832SChris Mason }
4520b86a832SChris Mason 
453d352ac68SChris Mason /*
454d352ac68SChris Mason  * update all the cowonly tree roots on disk
455d352ac68SChris Mason  */
4560b86a832SChris Mason int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
4570b86a832SChris Mason 			    struct btrfs_root *root)
4580b86a832SChris Mason {
4590b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
4600b86a832SChris Mason 	struct list_head *next;
46184234f3aSYan Zheng 	struct extent_buffer *eb;
46284234f3aSYan Zheng 
46387ef2bb4SChris Mason 	btrfs_extent_post_op(trans, fs_info->tree_root);
46487ef2bb4SChris Mason 
46584234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
46684234f3aSYan Zheng 	btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb, 0);
46784234f3aSYan Zheng 	btrfs_tree_unlock(eb);
46884234f3aSYan Zheng 	free_extent_buffer(eb);
4690b86a832SChris Mason 
47087ef2bb4SChris Mason 	btrfs_extent_post_op(trans, fs_info->tree_root);
47187ef2bb4SChris Mason 
4720b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
4730b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
4740b86a832SChris Mason 		list_del_init(next);
4750b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
47687ef2bb4SChris Mason 
4770b86a832SChris Mason 		update_cowonly_root(trans, root);
47879154b1bSChris Mason 	}
47979154b1bSChris Mason 	return 0;
48079154b1bSChris Mason }
48179154b1bSChris Mason 
482d352ac68SChris Mason /*
483d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
484d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
485d352ac68SChris Mason  * be deleted
486d352ac68SChris Mason  */
487b48652c1SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
4885eda7b5eSChris Mason {
489f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
4905eda7b5eSChris Mason 
4915eda7b5eSChris Mason 	dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
4925eda7b5eSChris Mason 	if (!dirty)
4935eda7b5eSChris Mason 		return -ENOMEM;
4945eda7b5eSChris Mason 	dirty->root = root;
4955ce14bbcSChris Mason 	dirty->latest_root = latest;
496b48652c1SYan Zheng 
497b48652c1SYan Zheng 	mutex_lock(&root->fs_info->trans_mutex);
498b48652c1SYan Zheng 	list_add(&dirty->list, &latest->fs_info->dead_roots);
499b48652c1SYan Zheng 	mutex_unlock(&root->fs_info->trans_mutex);
5005eda7b5eSChris Mason 	return 0;
5015eda7b5eSChris Mason }
5025eda7b5eSChris Mason 
503d352ac68SChris Mason /*
504d352ac68SChris Mason  * at transaction commit time we need to schedule the old roots for
505d352ac68SChris Mason  * deletion via btrfs_drop_snapshot.  This runs through all the
506d352ac68SChris Mason  * reference counted roots that were modified in the current
507d352ac68SChris Mason  * transaction and puts them into the drop list
508d352ac68SChris Mason  */
50980b6794dSChris Mason static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
51035b7e476SChris Mason 				    struct radix_tree_root *radix,
51135b7e476SChris Mason 				    struct list_head *list)
5120f7d52f4SChris Mason {
513f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
5140f7d52f4SChris Mason 	struct btrfs_root *gang[8];
5150f7d52f4SChris Mason 	struct btrfs_root *root;
5160f7d52f4SChris Mason 	int i;
5170f7d52f4SChris Mason 	int ret;
51854aa1f4dSChris Mason 	int err = 0;
5195eda7b5eSChris Mason 	u32 refs;
52054aa1f4dSChris Mason 
5210f7d52f4SChris Mason 	while (1) {
5220f7d52f4SChris Mason 		ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
5230f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
5240f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
5250f7d52f4SChris Mason 		if (ret == 0)
5260f7d52f4SChris Mason 			break;
5270f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
5280f7d52f4SChris Mason 			root = gang[i];
5292619ba1fSChris Mason 			radix_tree_tag_clear(radix,
5302619ba1fSChris Mason 				     (unsigned long)root->root_key.objectid,
5310f7d52f4SChris Mason 				     BTRFS_ROOT_TRANS_TAG);
53231153d81SYan Zheng 
53331153d81SYan Zheng 			BUG_ON(!root->ref_tree);
534017e5369SChris Mason 			dirty = root->dirty_root;
53531153d81SYan Zheng 
536e02119d5SChris Mason 			btrfs_free_log(trans, root);
537f82d02d9SYan Zheng 			btrfs_free_reloc_root(trans, root);
538e02119d5SChris Mason 
5390f7d52f4SChris Mason 			if (root->commit_root == root->node) {
540db94535dSChris Mason 				WARN_ON(root->node->start !=
541db94535dSChris Mason 					btrfs_root_bytenr(&root->root_item));
54231153d81SYan Zheng 
5435f39d397SChris Mason 				free_extent_buffer(root->commit_root);
5440f7d52f4SChris Mason 				root->commit_root = NULL;
5457ea394f1SYan Zheng 				root->dirty_root = NULL;
54631153d81SYan Zheng 
547bcc63abbSYan 				spin_lock(&root->list_lock);
548bcc63abbSYan 				list_del_init(&dirty->root->dead_list);
549bcc63abbSYan 				spin_unlock(&root->list_lock);
550bcc63abbSYan 
55131153d81SYan Zheng 				kfree(dirty->root);
55231153d81SYan Zheng 				kfree(dirty);
55358176a96SJosef Bacik 
55458176a96SJosef Bacik 				/* make sure to update the root on disk
55558176a96SJosef Bacik 				 * so we get any updates to the block used
55658176a96SJosef Bacik 				 * counts
55758176a96SJosef Bacik 				 */
55858176a96SJosef Bacik 				err = btrfs_update_root(trans,
55958176a96SJosef Bacik 						root->fs_info->tree_root,
56058176a96SJosef Bacik 						&root->root_key,
56158176a96SJosef Bacik 						&root->root_item);
5620f7d52f4SChris Mason 				continue;
5630f7d52f4SChris Mason 			}
5649f3a7427SChris Mason 
5659f3a7427SChris Mason 			memset(&root->root_item.drop_progress, 0,
5669f3a7427SChris Mason 			       sizeof(struct btrfs_disk_key));
5679f3a7427SChris Mason 			root->root_item.drop_level = 0;
5680f7d52f4SChris Mason 			root->commit_root = NULL;
5697ea394f1SYan Zheng 			root->dirty_root = NULL;
5700f7d52f4SChris Mason 			root->root_key.offset = root->fs_info->generation;
571db94535dSChris Mason 			btrfs_set_root_bytenr(&root->root_item,
572db94535dSChris Mason 					      root->node->start);
573db94535dSChris Mason 			btrfs_set_root_level(&root->root_item,
574db94535dSChris Mason 					     btrfs_header_level(root->node));
57584234f3aSYan Zheng 			btrfs_set_root_generation(&root->root_item,
57684234f3aSYan Zheng 						  root->root_key.offset);
57784234f3aSYan Zheng 
5780f7d52f4SChris Mason 			err = btrfs_insert_root(trans, root->fs_info->tree_root,
5790f7d52f4SChris Mason 						&root->root_key,
5800f7d52f4SChris Mason 						&root->root_item);
58154aa1f4dSChris Mason 			if (err)
58254aa1f4dSChris Mason 				break;
5839f3a7427SChris Mason 
5849f3a7427SChris Mason 			refs = btrfs_root_refs(&dirty->root->root_item);
5859f3a7427SChris Mason 			btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
5865eda7b5eSChris Mason 			err = btrfs_update_root(trans, root->fs_info->tree_root,
5879f3a7427SChris Mason 						&dirty->root->root_key,
5889f3a7427SChris Mason 						&dirty->root->root_item);
5895eda7b5eSChris Mason 
5905eda7b5eSChris Mason 			BUG_ON(err);
5919f3a7427SChris Mason 			if (refs == 1) {
5920f7d52f4SChris Mason 				list_add(&dirty->list, list);
5939f3a7427SChris Mason 			} else {
5949f3a7427SChris Mason 				WARN_ON(1);
59531153d81SYan Zheng 				free_extent_buffer(dirty->root->node);
5969f3a7427SChris Mason 				kfree(dirty->root);
5975eda7b5eSChris Mason 				kfree(dirty);
5980f7d52f4SChris Mason 			}
5990f7d52f4SChris Mason 		}
6009f3a7427SChris Mason 	}
60154aa1f4dSChris Mason 	return err;
6020f7d52f4SChris Mason }
6030f7d52f4SChris Mason 
604d352ac68SChris Mason /*
605d352ac68SChris Mason  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
606d352ac68SChris Mason  * otherwise every leaf in the btree is read and defragged.
607d352ac68SChris Mason  */
608e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
609e9d0b13bSChris Mason {
610e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
611e9d0b13bSChris Mason 	int ret;
612e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
613d3c2fdcfSChris Mason 	unsigned long nr;
614e9d0b13bSChris Mason 
615a2135011SChris Mason 	smp_mb();
616e9d0b13bSChris Mason 	if (root->defrag_running)
617e9d0b13bSChris Mason 		return 0;
618e9d0b13bSChris Mason 	trans = btrfs_start_transaction(root, 1);
6196b80053dSChris Mason 	while (1) {
620e9d0b13bSChris Mason 		root->defrag_running = 1;
621e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
622d3c2fdcfSChris Mason 		nr = trans->blocks_used;
623e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
624d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(info->tree_root, nr);
625e9d0b13bSChris Mason 		cond_resched();
626e9d0b13bSChris Mason 
627e9d0b13bSChris Mason 		trans = btrfs_start_transaction(root, 1);
6283f157a2fSChris Mason 		if (root->fs_info->closing || ret != -EAGAIN)
629e9d0b13bSChris Mason 			break;
630e9d0b13bSChris Mason 	}
631e9d0b13bSChris Mason 	root->defrag_running = 0;
632a2135011SChris Mason 	smp_mb();
633e9d0b13bSChris Mason 	btrfs_end_transaction(trans, root);
634e9d0b13bSChris Mason 	return 0;
635e9d0b13bSChris Mason }
636e9d0b13bSChris Mason 
637d352ac68SChris Mason /*
638d352ac68SChris Mason  * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
639d352ac68SChris Mason  * all of them
640d352ac68SChris Mason  */
64180b6794dSChris Mason static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
64235b7e476SChris Mason 				     struct list_head *list)
6430f7d52f4SChris Mason {
644f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
6450f7d52f4SChris Mason 	struct btrfs_trans_handle *trans;
646d3c2fdcfSChris Mason 	unsigned long nr;
647db94535dSChris Mason 	u64 num_bytes;
648db94535dSChris Mason 	u64 bytes_used;
649bcc63abbSYan 	u64 max_useless;
65054aa1f4dSChris Mason 	int ret = 0;
6519f3a7427SChris Mason 	int err;
6529f3a7427SChris Mason 
6530f7d52f4SChris Mason 	while (!list_empty(list)) {
65458176a96SJosef Bacik 		struct btrfs_root *root;
65558176a96SJosef Bacik 
656f321e491SYan Zheng 		dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
6570f7d52f4SChris Mason 		list_del_init(&dirty->list);
6585eda7b5eSChris Mason 
659db94535dSChris Mason 		num_bytes = btrfs_root_used(&dirty->root->root_item);
66058176a96SJosef Bacik 		root = dirty->latest_root;
661a2135011SChris Mason 		atomic_inc(&root->fs_info->throttles);
66258176a96SJosef Bacik 
6639f3a7427SChris Mason 		while (1) {
6640f7d52f4SChris Mason 			trans = btrfs_start_transaction(tree_root, 1);
6655b21f2edSZheng Yan 			mutex_lock(&root->fs_info->drop_mutex);
6669f3a7427SChris Mason 			ret = btrfs_drop_snapshot(trans, dirty->root);
667d397712bSChris Mason 			if (ret != -EAGAIN)
6689f3a7427SChris Mason 				break;
6695b21f2edSZheng Yan 			mutex_unlock(&root->fs_info->drop_mutex);
67058176a96SJosef Bacik 
6719f3a7427SChris Mason 			err = btrfs_update_root(trans,
6729f3a7427SChris Mason 					tree_root,
6739f3a7427SChris Mason 					&dirty->root->root_key,
6749f3a7427SChris Mason 					&dirty->root->root_item);
6759f3a7427SChris Mason 			if (err)
6769f3a7427SChris Mason 				ret = err;
677d3c2fdcfSChris Mason 			nr = trans->blocks_used;
678017e5369SChris Mason 			ret = btrfs_end_transaction(trans, tree_root);
6790f7d52f4SChris Mason 			BUG_ON(ret);
680a2135011SChris Mason 
681d3c2fdcfSChris Mason 			btrfs_btree_balance_dirty(tree_root, nr);
6824dc11904SChris Mason 			cond_resched();
6839f3a7427SChris Mason 		}
6849f3a7427SChris Mason 		BUG_ON(ret);
685a2135011SChris Mason 		atomic_dec(&root->fs_info->throttles);
686017e5369SChris Mason 		wake_up(&root->fs_info->transaction_throttle);
68758176a96SJosef Bacik 
688db94535dSChris Mason 		num_bytes -= btrfs_root_used(&dirty->root->root_item);
689db94535dSChris Mason 		bytes_used = btrfs_root_used(&root->root_item);
690db94535dSChris Mason 		if (num_bytes) {
691e02119d5SChris Mason 			btrfs_record_root_in_trans(root);
6925f39d397SChris Mason 			btrfs_set_root_used(&root->root_item,
693db94535dSChris Mason 					    bytes_used - num_bytes);
69458176a96SJosef Bacik 		}
695a2135011SChris Mason 
6969f3a7427SChris Mason 		ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
69758176a96SJosef Bacik 		if (ret) {
69858176a96SJosef Bacik 			BUG();
69954aa1f4dSChris Mason 			break;
70058176a96SJosef Bacik 		}
701a2135011SChris Mason 		mutex_unlock(&root->fs_info->drop_mutex);
702a2135011SChris Mason 
703bcc63abbSYan 		spin_lock(&root->list_lock);
704bcc63abbSYan 		list_del_init(&dirty->root->dead_list);
705bcc63abbSYan 		if (!list_empty(&root->dead_list)) {
706bcc63abbSYan 			struct btrfs_root *oldest;
707bcc63abbSYan 			oldest = list_entry(root->dead_list.prev,
708bcc63abbSYan 					    struct btrfs_root, dead_list);
709bcc63abbSYan 			max_useless = oldest->root_key.offset - 1;
710bcc63abbSYan 		} else {
711bcc63abbSYan 			max_useless = root->root_key.offset - 1;
712bcc63abbSYan 		}
713bcc63abbSYan 		spin_unlock(&root->list_lock);
714bcc63abbSYan 
715d3c2fdcfSChris Mason 		nr = trans->blocks_used;
7160f7d52f4SChris Mason 		ret = btrfs_end_transaction(trans, tree_root);
7170f7d52f4SChris Mason 		BUG_ON(ret);
7185eda7b5eSChris Mason 
719e4657689SZheng Yan 		ret = btrfs_remove_leaf_refs(root, max_useless, 0);
720bcc63abbSYan 		BUG_ON(ret);
721bcc63abbSYan 
722f510cfecSChris Mason 		free_extent_buffer(dirty->root->node);
7235eda7b5eSChris Mason 		kfree(dirty->root);
7240f7d52f4SChris Mason 		kfree(dirty);
725d3c2fdcfSChris Mason 
726d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(tree_root, nr);
7274dc11904SChris Mason 		cond_resched();
7280f7d52f4SChris Mason 	}
72954aa1f4dSChris Mason 	return ret;
7300f7d52f4SChris Mason }
7310f7d52f4SChris Mason 
732d352ac68SChris Mason /*
733d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
734d352ac68SChris Mason  * transaction commit.  This does the actual creation
735d352ac68SChris Mason  */
73680b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
7373063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
7383063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
7393063d29fSChris Mason {
7403063d29fSChris Mason 	struct btrfs_key key;
74180b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
7423063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
7433063d29fSChris Mason 	struct btrfs_root *root = pending->root;
7443063d29fSChris Mason 	struct extent_buffer *tmp;
745925baeddSChris Mason 	struct extent_buffer *old;
7463063d29fSChris Mason 	int ret;
7473063d29fSChris Mason 	u64 objectid;
7483063d29fSChris Mason 
74980b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
75080b6794dSChris Mason 	if (!new_root_item) {
75180b6794dSChris Mason 		ret = -ENOMEM;
75280b6794dSChris Mason 		goto fail;
75380b6794dSChris Mason 	}
7543063d29fSChris Mason 	ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
7553063d29fSChris Mason 	if (ret)
7563063d29fSChris Mason 		goto fail;
7573063d29fSChris Mason 
75880ff3856SYan Zheng 	btrfs_record_root_in_trans(root);
75980ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
76080b6794dSChris Mason 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
7613063d29fSChris Mason 
7623063d29fSChris Mason 	key.objectid = objectid;
7635b21f2edSZheng Yan 	key.offset = trans->transid;
7643063d29fSChris Mason 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
7653063d29fSChris Mason 
766925baeddSChris Mason 	old = btrfs_lock_root_node(root);
76765b51a00SChris Mason 	btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
7683063d29fSChris Mason 
769925baeddSChris Mason 	btrfs_copy_root(trans, root, old, &tmp, objectid);
770925baeddSChris Mason 	btrfs_tree_unlock(old);
771925baeddSChris Mason 	free_extent_buffer(old);
7723063d29fSChris Mason 
77380b6794dSChris Mason 	btrfs_set_root_bytenr(new_root_item, tmp->start);
77480b6794dSChris Mason 	btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
77584234f3aSYan Zheng 	btrfs_set_root_generation(new_root_item, trans->transid);
7763063d29fSChris Mason 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
77780b6794dSChris Mason 				new_root_item);
778925baeddSChris Mason 	btrfs_tree_unlock(tmp);
7793063d29fSChris Mason 	free_extent_buffer(tmp);
7803063d29fSChris Mason 	if (ret)
7813063d29fSChris Mason 		goto fail;
7823063d29fSChris Mason 
7833de4586cSChris Mason 	key.offset = (u64)-1;
7843de4586cSChris Mason 	memcpy(&pending->root_key, &key, sizeof(key));
7853de4586cSChris Mason fail:
7863de4586cSChris Mason 	kfree(new_root_item);
7873de4586cSChris Mason 	return ret;
7883de4586cSChris Mason }
7893de4586cSChris Mason 
7903de4586cSChris Mason static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info,
7913de4586cSChris Mason 				   struct btrfs_pending_snapshot *pending)
7923de4586cSChris Mason {
7933de4586cSChris Mason 	int ret;
7943de4586cSChris Mason 	int namelen;
7953de4586cSChris Mason 	u64 index = 0;
7963de4586cSChris Mason 	struct btrfs_trans_handle *trans;
7973de4586cSChris Mason 	struct inode *parent_inode;
7983de4586cSChris Mason 	struct inode *inode;
7990660b5afSChris Mason 	struct btrfs_root *parent_root;
8003de4586cSChris Mason 
8013394e160SChris Mason 	parent_inode = pending->dentry->d_parent->d_inode;
8020660b5afSChris Mason 	parent_root = BTRFS_I(parent_inode)->root;
803180591bcSYan Zheng 	trans = btrfs_join_transaction(parent_root, 1);
8043de4586cSChris Mason 
8053063d29fSChris Mason 	/*
8063063d29fSChris Mason 	 * insert the directory item
8073063d29fSChris Mason 	 */
8083b96362cSSven Wegener 	namelen = strlen(pending->name);
8093de4586cSChris Mason 	ret = btrfs_set_inode_index(parent_inode, &index);
8100660b5afSChris Mason 	ret = btrfs_insert_dir_item(trans, parent_root,
8113b96362cSSven Wegener 			    pending->name, namelen,
8123de4586cSChris Mason 			    parent_inode->i_ino,
8133de4586cSChris Mason 			    &pending->root_key, BTRFS_FT_DIR, index);
8143063d29fSChris Mason 
8153063d29fSChris Mason 	if (ret)
8163063d29fSChris Mason 		goto fail;
8170660b5afSChris Mason 
81852c26179SYan Zheng 	btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2);
81952c26179SYan Zheng 	ret = btrfs_update_inode(trans, parent_root, parent_inode);
82052c26179SYan Zheng 	BUG_ON(ret);
82152c26179SYan Zheng 
8220660b5afSChris Mason 	/* add the backref first */
8230660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
8240660b5afSChris Mason 				 pending->root_key.objectid,
8250660b5afSChris Mason 				 BTRFS_ROOT_BACKREF_KEY,
8260660b5afSChris Mason 				 parent_root->root_key.objectid,
8270660b5afSChris Mason 				 parent_inode->i_ino, index, pending->name,
8280660b5afSChris Mason 				 namelen);
8290660b5afSChris Mason 
8300660b5afSChris Mason 	BUG_ON(ret);
8310660b5afSChris Mason 
8320660b5afSChris Mason 	/* now add the forward ref */
8330660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
8340660b5afSChris Mason 				 parent_root->root_key.objectid,
8350660b5afSChris Mason 				 BTRFS_ROOT_REF_KEY,
8360660b5afSChris Mason 				 pending->root_key.objectid,
8370660b5afSChris Mason 				 parent_inode->i_ino, index, pending->name,
8380660b5afSChris Mason 				 namelen);
8390660b5afSChris Mason 
8403de4586cSChris Mason 	inode = btrfs_lookup_dentry(parent_inode, pending->dentry);
8413de4586cSChris Mason 	d_instantiate(pending->dentry, inode);
8423063d29fSChris Mason fail:
8433de4586cSChris Mason 	btrfs_end_transaction(trans, fs_info->fs_root);
8443063d29fSChris Mason 	return ret;
8453063d29fSChris Mason }
8463063d29fSChris Mason 
847d352ac68SChris Mason /*
848d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
849d352ac68SChris Mason  */
85080b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
8513063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
8523063d29fSChris Mason {
8533063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
8543063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
8553de4586cSChris Mason 	int ret;
8563de4586cSChris Mason 
857c6e30871SQinghuang Feng 	list_for_each_entry(pending, head, list) {
8583de4586cSChris Mason 		ret = create_pending_snapshot(trans, fs_info, pending);
8593de4586cSChris Mason 		BUG_ON(ret);
8603de4586cSChris Mason 	}
8613de4586cSChris Mason 	return 0;
8623de4586cSChris Mason }
8633de4586cSChris Mason 
8643de4586cSChris Mason static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans,
8653de4586cSChris Mason 					     struct btrfs_fs_info *fs_info)
8663de4586cSChris Mason {
8673de4586cSChris Mason 	struct btrfs_pending_snapshot *pending;
8683de4586cSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
8693063d29fSChris Mason 	int ret;
8703063d29fSChris Mason 
8713063d29fSChris Mason 	while (!list_empty(head)) {
8723063d29fSChris Mason 		pending = list_entry(head->next,
8733063d29fSChris Mason 				     struct btrfs_pending_snapshot, list);
8743de4586cSChris Mason 		ret = finish_pending_snapshot(fs_info, pending);
8753063d29fSChris Mason 		BUG_ON(ret);
8763063d29fSChris Mason 		list_del(&pending->list);
8773063d29fSChris Mason 		kfree(pending->name);
8783063d29fSChris Mason 		kfree(pending);
8793063d29fSChris Mason 	}
880dc17ff8fSChris Mason 	return 0;
881dc17ff8fSChris Mason }
882dc17ff8fSChris Mason 
88379154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
88479154b1bSChris Mason 			     struct btrfs_root *root)
88579154b1bSChris Mason {
88615ee9bc7SJosef Bacik 	unsigned long joined = 0;
88715ee9bc7SJosef Bacik 	unsigned long timeout = 1;
88879154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
8898fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
8900b86a832SChris Mason 	struct btrfs_root *chunk_root = root->fs_info->chunk_root;
8910f7d52f4SChris Mason 	struct list_head dirty_fs_roots;
892d1310b2eSChris Mason 	struct extent_io_tree *pinned_copy;
89379154b1bSChris Mason 	DEFINE_WAIT(wait);
89415ee9bc7SJosef Bacik 	int ret;
89579154b1bSChris Mason 
8960f7d52f4SChris Mason 	INIT_LIST_HEAD(&dirty_fs_roots);
89779154b1bSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
89879154b1bSChris Mason 	if (trans->transaction->in_commit) {
89979154b1bSChris Mason 		cur_trans = trans->transaction;
90079154b1bSChris Mason 		trans->transaction->use_count++;
901ccd467d6SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
90279154b1bSChris Mason 		btrfs_end_transaction(trans, root);
903ccd467d6SChris Mason 
90479154b1bSChris Mason 		ret = wait_for_commit(root, cur_trans);
90579154b1bSChris Mason 		BUG_ON(ret);
90615ee9bc7SJosef Bacik 
90715ee9bc7SJosef Bacik 		mutex_lock(&root->fs_info->trans_mutex);
90879154b1bSChris Mason 		put_transaction(cur_trans);
90915ee9bc7SJosef Bacik 		mutex_unlock(&root->fs_info->trans_mutex);
91015ee9bc7SJosef Bacik 
91179154b1bSChris Mason 		return 0;
91279154b1bSChris Mason 	}
9134313b399SChris Mason 
9144313b399SChris Mason 	pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
9154313b399SChris Mason 	if (!pinned_copy)
9164313b399SChris Mason 		return -ENOMEM;
9174313b399SChris Mason 
918d1310b2eSChris Mason 	extent_io_tree_init(pinned_copy,
9194313b399SChris Mason 			     root->fs_info->btree_inode->i_mapping, GFP_NOFS);
9204313b399SChris Mason 
9212c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
922f9295749SChris Mason 	trans->transaction->blocked = 1;
923ccd467d6SChris Mason 	cur_trans = trans->transaction;
924ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
925ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
926ccd467d6SChris Mason 					struct btrfs_transaction, list);
927ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
928ccd467d6SChris Mason 			prev_trans->use_count++;
929ccd467d6SChris Mason 			mutex_unlock(&root->fs_info->trans_mutex);
930ccd467d6SChris Mason 
931ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
932ccd467d6SChris Mason 
933ccd467d6SChris Mason 			mutex_lock(&root->fs_info->trans_mutex);
93415ee9bc7SJosef Bacik 			put_transaction(prev_trans);
935ccd467d6SChris Mason 		}
936ccd467d6SChris Mason 	}
93715ee9bc7SJosef Bacik 
93815ee9bc7SJosef Bacik 	do {
9397ea394f1SYan Zheng 		int snap_pending = 0;
94015ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
9417ea394f1SYan Zheng 		if (!list_empty(&trans->transaction->pending_snapshots))
9427ea394f1SYan Zheng 			snap_pending = 1;
9437ea394f1SYan Zheng 
9442c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
94515ee9bc7SJosef Bacik 		prepare_to_wait(&cur_trans->writer_wait, &wait,
94679154b1bSChris Mason 				TASK_UNINTERRUPTIBLE);
94715ee9bc7SJosef Bacik 
94815ee9bc7SJosef Bacik 		if (cur_trans->num_writers > 1)
94915ee9bc7SJosef Bacik 			timeout = MAX_SCHEDULE_TIMEOUT;
95015ee9bc7SJosef Bacik 		else
95115ee9bc7SJosef Bacik 			timeout = 1;
95215ee9bc7SJosef Bacik 
95379154b1bSChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
95415ee9bc7SJosef Bacik 
9557ea394f1SYan Zheng 		if (snap_pending) {
9567ea394f1SYan Zheng 			ret = btrfs_wait_ordered_extents(root, 1);
9577ea394f1SYan Zheng 			BUG_ON(ret);
9587ea394f1SYan Zheng 		}
9597ea394f1SYan Zheng 
96015ee9bc7SJosef Bacik 		schedule_timeout(timeout);
96115ee9bc7SJosef Bacik 
96279154b1bSChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
96315ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
96415ee9bc7SJosef Bacik 	} while (cur_trans->num_writers > 1 ||
96515ee9bc7SJosef Bacik 		 (cur_trans->num_joined != joined));
96615ee9bc7SJosef Bacik 
9673063d29fSChris Mason 	ret = create_pending_snapshots(trans, root->fs_info);
9683063d29fSChris Mason 	BUG_ON(ret);
9693063d29fSChris Mason 
9702c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
971dc17ff8fSChris Mason 
972e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
973e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
974e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
975e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
976e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
977e02119d5SChris Mason 	 * of the trees.
978e02119d5SChris Mason 	 *
979e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
980e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
981e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
982e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
983e02119d5SChris Mason 	 * with the tree-log code.
984e02119d5SChris Mason 	 */
985e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
9861a40e23bSZheng Yan 	/*
9871a40e23bSZheng Yan 	 * keep tree reloc code from adding new reloc trees
9881a40e23bSZheng Yan 	 */
9891a40e23bSZheng Yan 	mutex_lock(&root->fs_info->tree_reloc_mutex);
9901a40e23bSZheng Yan 
991e02119d5SChris Mason 
99254aa1f4dSChris Mason 	ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
99354aa1f4dSChris Mason 			      &dirty_fs_roots);
99454aa1f4dSChris Mason 	BUG_ON(ret);
99554aa1f4dSChris Mason 
996e02119d5SChris Mason 	/* add_dirty_roots gets rid of all the tree log roots, it is now
997e02119d5SChris Mason 	 * safe to free the root of tree log roots
998e02119d5SChris Mason 	 */
999e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1000e02119d5SChris Mason 
100179154b1bSChris Mason 	ret = btrfs_commit_tree_roots(trans, root);
100279154b1bSChris Mason 	BUG_ON(ret);
100354aa1f4dSChris Mason 
100478fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
1005cee36a03SChris Mason 	spin_lock(&root->fs_info->new_trans_lock);
100678fae27eSChris Mason 	root->fs_info->running_transaction = NULL;
1007cee36a03SChris Mason 	spin_unlock(&root->fs_info->new_trans_lock);
10084b52dff6SChris Mason 	btrfs_set_super_generation(&root->fs_info->super_copy,
10094b52dff6SChris Mason 				   cur_trans->transid);
10104b52dff6SChris Mason 	btrfs_set_super_root(&root->fs_info->super_copy,
1011db94535dSChris Mason 			     root->fs_info->tree_root->node->start);
1012db94535dSChris Mason 	btrfs_set_super_root_level(&root->fs_info->super_copy,
1013db94535dSChris Mason 			   btrfs_header_level(root->fs_info->tree_root->node));
10145f39d397SChris Mason 
10150b86a832SChris Mason 	btrfs_set_super_chunk_root(&root->fs_info->super_copy,
10160b86a832SChris Mason 				   chunk_root->node->start);
10170b86a832SChris Mason 	btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
10180b86a832SChris Mason 					 btrfs_header_level(chunk_root->node));
101984234f3aSYan Zheng 	btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
102084234f3aSYan Zheng 				btrfs_header_generation(chunk_root->node));
1021e02119d5SChris Mason 
1022e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
1023e02119d5SChris Mason 		btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1024e02119d5SChris Mason 		btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1025e02119d5SChris Mason 	}
1026e02119d5SChris Mason 
1027a061fc8dSChris Mason 	memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
10284b52dff6SChris Mason 	       sizeof(root->fs_info->super_copy));
1029ccd467d6SChris Mason 
10304313b399SChris Mason 	btrfs_copy_pinned(root, pinned_copy);
1031ccd467d6SChris Mason 
1032f9295749SChris Mason 	trans->transaction->blocked = 0;
1033e6dcd2dcSChris Mason 	wake_up(&root->fs_info->transaction_throttle);
1034f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1035e6dcd2dcSChris Mason 
103678fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
103779154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
103879154b1bSChris Mason 	BUG_ON(ret);
1039a512bbf8SYan Zheng 	write_ctree_super(trans, root, 0);
10404313b399SChris Mason 
1041e02119d5SChris Mason 	/*
1042e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1043e02119d5SChris Mason 	 * to go about their business
1044e02119d5SChris Mason 	 */
1045e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1046e02119d5SChris Mason 
10474313b399SChris Mason 	btrfs_finish_extent_commit(trans, root, pinned_copy);
10484313b399SChris Mason 	kfree(pinned_copy);
10494313b399SChris Mason 
10501a40e23bSZheng Yan 	btrfs_drop_dead_reloc_roots(root);
10511a40e23bSZheng Yan 	mutex_unlock(&root->fs_info->tree_reloc_mutex);
10521a40e23bSZheng Yan 
10533de4586cSChris Mason 	/* do the directory inserts of any pending snapshot creations */
10543de4586cSChris Mason 	finish_pending_snapshots(trans, root->fs_info);
10553de4586cSChris Mason 
10561a40e23bSZheng Yan 	mutex_lock(&root->fs_info->trans_mutex);
10571a40e23bSZheng Yan 
10582c90e5d6SChris Mason 	cur_trans->commit_done = 1;
105915ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
10602c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
10613de4586cSChris Mason 
106279154b1bSChris Mason 	put_transaction(cur_trans);
106378fae27eSChris Mason 	put_transaction(cur_trans);
106458176a96SJosef Bacik 
1065bcc63abbSYan 	list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
1066facda1e7SChris Mason 	if (root->fs_info->closing)
1067facda1e7SChris Mason 		list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
106858176a96SJosef Bacik 
106978fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
10703de4586cSChris Mason 
10712c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
107279154b1bSChris Mason 
1073d397712bSChris Mason 	if (root->fs_info->closing)
10740f7d52f4SChris Mason 		drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
107579154b1bSChris Mason 	return ret;
107679154b1bSChris Mason }
107779154b1bSChris Mason 
1078d352ac68SChris Mason /*
1079d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1080d352ac68SChris Mason  */
1081e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1082e9d0b13bSChris Mason {
1083e9d0b13bSChris Mason 	struct list_head dirty_roots;
1084e9d0b13bSChris Mason 	INIT_LIST_HEAD(&dirty_roots);
1085a74a4b97SChris Mason again:
1086e9d0b13bSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
1087e9d0b13bSChris Mason 	list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
1088e9d0b13bSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
1089e9d0b13bSChris Mason 
1090e9d0b13bSChris Mason 	if (!list_empty(&dirty_roots)) {
1091e9d0b13bSChris Mason 		drop_dirty_roots(root, &dirty_roots);
1092a74a4b97SChris Mason 		goto again;
1093e9d0b13bSChris Mason 	}
1094e9d0b13bSChris Mason 	return 0;
1095e9d0b13bSChris Mason }
1096