xref: /openbmc/linux/fs/btrfs/transaction.c (revision 52c26179)
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 
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) {
418fd17795SChris Mason 		list_del_init(&transaction->list);
422c90e5d6SChris Mason 		memset(transaction, 0, sizeof(*transaction));
432c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
4479154b1bSChris Mason 	}
4578fae27eSChris Mason }
4679154b1bSChris Mason 
47d352ac68SChris Mason /*
48d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
49d352ac68SChris Mason  */
5080b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root)
5179154b1bSChris Mason {
5279154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
5379154b1bSChris Mason 	cur_trans = root->fs_info->running_transaction;
5479154b1bSChris Mason 	if (!cur_trans) {
552c90e5d6SChris Mason 		cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
562c90e5d6SChris Mason 					     GFP_NOFS);
5779154b1bSChris Mason 		BUG_ON(!cur_trans);
580f7d52f4SChris Mason 		root->fs_info->generation++;
59e18e4809SChris Mason 		root->fs_info->last_alloc = 0;
604529ba49SChris Mason 		root->fs_info->last_data_alloc = 0;
6115ee9bc7SJosef Bacik 		cur_trans->num_writers = 1;
6215ee9bc7SJosef Bacik 		cur_trans->num_joined = 0;
630f7d52f4SChris Mason 		cur_trans->transid = root->fs_info->generation;
6479154b1bSChris Mason 		init_waitqueue_head(&cur_trans->writer_wait);
6579154b1bSChris Mason 		init_waitqueue_head(&cur_trans->commit_wait);
6679154b1bSChris Mason 		cur_trans->in_commit = 0;
67f9295749SChris Mason 		cur_trans->blocked = 0;
68d5719762SChris Mason 		cur_trans->use_count = 1;
6979154b1bSChris Mason 		cur_trans->commit_done = 0;
7008607c1bSChris Mason 		cur_trans->start_time = get_seconds();
713063d29fSChris Mason 		INIT_LIST_HEAD(&cur_trans->pending_snapshots);
728fd17795SChris Mason 		list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
73d1310b2eSChris Mason 		extent_io_tree_init(&cur_trans->dirty_pages,
745f39d397SChris Mason 				     root->fs_info->btree_inode->i_mapping,
755f39d397SChris Mason 				     GFP_NOFS);
7648ec2cf8SChris Mason 		spin_lock(&root->fs_info->new_trans_lock);
7748ec2cf8SChris Mason 		root->fs_info->running_transaction = cur_trans;
7848ec2cf8SChris Mason 		spin_unlock(&root->fs_info->new_trans_lock);
7915ee9bc7SJosef Bacik 	} else {
8079154b1bSChris Mason 		cur_trans->num_writers++;
8115ee9bc7SJosef Bacik 		cur_trans->num_joined++;
8215ee9bc7SJosef Bacik 	}
8315ee9bc7SJosef Bacik 
8479154b1bSChris Mason 	return 0;
8579154b1bSChris Mason }
8679154b1bSChris Mason 
87d352ac68SChris Mason /*
88d352ac68SChris Mason  * this does all the record keeping required to make sure that a
89d352ac68SChris Mason  * reference counted root is properly recorded in a given transaction.
90d352ac68SChris Mason  * This is required to make sure the old root from before we joined the transaction
91d352ac68SChris Mason  * is deleted when the transaction commits
92d352ac68SChris Mason  */
93e02119d5SChris Mason noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
946702ed49SChris Mason {
95f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
966702ed49SChris Mason 	u64 running_trans_id = root->fs_info->running_transaction->transid;
976702ed49SChris Mason 	if (root->ref_cows && root->last_trans < running_trans_id) {
986702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
996702ed49SChris Mason 		if (root->root_item.refs != 0) {
1006702ed49SChris Mason 			radix_tree_tag_set(&root->fs_info->fs_roots_radix,
1016702ed49SChris Mason 				   (unsigned long)root->root_key.objectid,
1026702ed49SChris Mason 				   BTRFS_ROOT_TRANS_TAG);
10331153d81SYan Zheng 
10431153d81SYan Zheng 			dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
10531153d81SYan Zheng 			BUG_ON(!dirty);
10631153d81SYan Zheng 			dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
10731153d81SYan Zheng 			BUG_ON(!dirty->root);
10831153d81SYan Zheng 			dirty->latest_root = root;
10931153d81SYan Zheng 			INIT_LIST_HEAD(&dirty->list);
11031153d81SYan Zheng 
111925baeddSChris Mason 			root->commit_root = btrfs_root_node(root);
11231153d81SYan Zheng 
11331153d81SYan Zheng 			memcpy(dirty->root, root, sizeof(*root));
11431153d81SYan Zheng 			spin_lock_init(&dirty->root->node_lock);
115bcc63abbSYan 			spin_lock_init(&dirty->root->list_lock);
11631153d81SYan Zheng 			mutex_init(&dirty->root->objectid_mutex);
1175b21f2edSZheng Yan 			mutex_init(&dirty->root->log_mutex);
118bcc63abbSYan 			INIT_LIST_HEAD(&dirty->root->dead_list);
11931153d81SYan Zheng 			dirty->root->node = root->commit_root;
12031153d81SYan Zheng 			dirty->root->commit_root = NULL;
121bcc63abbSYan 
122bcc63abbSYan 			spin_lock(&root->list_lock);
123bcc63abbSYan 			list_add(&dirty->root->dead_list, &root->dead_list);
124bcc63abbSYan 			spin_unlock(&root->list_lock);
125bcc63abbSYan 
126bcc63abbSYan 			root->dirty_root = dirty;
1276702ed49SChris Mason 		} else {
1286702ed49SChris Mason 			WARN_ON(1);
1296702ed49SChris Mason 		}
1306702ed49SChris Mason 		root->last_trans = running_trans_id;
1316702ed49SChris Mason 	}
1326702ed49SChris Mason 	return 0;
1336702ed49SChris Mason }
1346702ed49SChris Mason 
135d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
136d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
137d352ac68SChris Mason  * transaction might not be fully on disk.
138d352ac68SChris Mason  */
13937d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
14079154b1bSChris Mason {
141f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
14279154b1bSChris Mason 
143f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
14437d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
145f9295749SChris Mason 		DEFINE_WAIT(wait);
146f9295749SChris Mason 		cur_trans->use_count++;
147f9295749SChris Mason 		while(1) {
148f9295749SChris Mason 			prepare_to_wait(&root->fs_info->transaction_wait, &wait,
149f9295749SChris Mason 					TASK_UNINTERRUPTIBLE);
150f9295749SChris Mason 			if (cur_trans->blocked) {
151f9295749SChris Mason 				mutex_unlock(&root->fs_info->trans_mutex);
152f9295749SChris Mason 				schedule();
153f9295749SChris Mason 				mutex_lock(&root->fs_info->trans_mutex);
154f9295749SChris Mason 				finish_wait(&root->fs_info->transaction_wait,
155f9295749SChris Mason 					    &wait);
156f9295749SChris Mason 			} else {
157f9295749SChris Mason 				finish_wait(&root->fs_info->transaction_wait,
158f9295749SChris Mason 					    &wait);
159f9295749SChris Mason 				break;
160f9295749SChris Mason 			}
161f9295749SChris Mason 		}
162f9295749SChris Mason 		put_transaction(cur_trans);
163f9295749SChris Mason 	}
16437d1aeeeSChris Mason }
16537d1aeeeSChris Mason 
166e02119d5SChris Mason static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
1679ca9ee09SSage Weil 					     int num_blocks, int wait)
16837d1aeeeSChris Mason {
16937d1aeeeSChris Mason 	struct btrfs_trans_handle *h =
17037d1aeeeSChris Mason 		kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
17137d1aeeeSChris Mason 	int ret;
17237d1aeeeSChris Mason 
17337d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
1744bef0848SChris Mason 	if (!root->fs_info->log_root_recovering &&
1754bef0848SChris Mason 	    ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
17637d1aeeeSChris Mason 		wait_current_trans(root);
17779154b1bSChris Mason 	ret = join_transaction(root);
17879154b1bSChris Mason 	BUG_ON(ret);
1790f7d52f4SChris Mason 
180e02119d5SChris Mason 	btrfs_record_root_in_trans(root);
1816702ed49SChris Mason 	h->transid = root->fs_info->running_transaction->transid;
18279154b1bSChris Mason 	h->transaction = root->fs_info->running_transaction;
18379154b1bSChris Mason 	h->blocks_reserved = num_blocks;
18479154b1bSChris Mason 	h->blocks_used = 0;
185d2fb3437SYan Zheng 	h->block_group = 0;
18626b8003fSChris Mason 	h->alloc_exclude_nr = 0;
18726b8003fSChris Mason 	h->alloc_exclude_start = 0;
18879154b1bSChris Mason 	root->fs_info->running_transaction->use_count++;
18979154b1bSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
19079154b1bSChris Mason 	return h;
19179154b1bSChris Mason }
19279154b1bSChris Mason 
193f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
194f9295749SChris Mason 						   int num_blocks)
195f9295749SChris Mason {
1969ca9ee09SSage Weil 	return start_transaction(root, num_blocks, 1);
197f9295749SChris Mason }
198f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
199f9295749SChris Mason 						   int num_blocks)
200f9295749SChris Mason {
2019ca9ee09SSage Weil 	return start_transaction(root, num_blocks, 0);
202f9295749SChris Mason }
203f9295749SChris Mason 
2049ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
2059ca9ee09SSage Weil 							 int num_blocks)
2069ca9ee09SSage Weil {
2079ca9ee09SSage Weil 	return start_transaction(r, num_blocks, 2);
2089ca9ee09SSage Weil }
2099ca9ee09SSage Weil 
210d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
21189ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root,
21289ce8a63SChris Mason 				    struct btrfs_transaction *commit)
21389ce8a63SChris Mason {
21489ce8a63SChris Mason 	DEFINE_WAIT(wait);
21589ce8a63SChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
21689ce8a63SChris Mason 	while(!commit->commit_done) {
21789ce8a63SChris Mason 		prepare_to_wait(&commit->commit_wait, &wait,
21889ce8a63SChris Mason 				TASK_UNINTERRUPTIBLE);
21989ce8a63SChris Mason 		if (commit->commit_done)
22089ce8a63SChris Mason 			break;
22189ce8a63SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
22289ce8a63SChris Mason 		schedule();
22389ce8a63SChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
22489ce8a63SChris Mason 	}
22589ce8a63SChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
22689ce8a63SChris Mason 	finish_wait(&commit->commit_wait, &wait);
22789ce8a63SChris Mason 	return 0;
22889ce8a63SChris Mason }
22989ce8a63SChris Mason 
230d352ac68SChris Mason /*
231d352ac68SChris Mason  * rate limit against the drop_snapshot code.  This helps to slow down new operations
232d352ac68SChris Mason  * if the drop_snapshot code isn't able to keep up.
233d352ac68SChris Mason  */
23437d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root)
235ab78c84dSChris Mason {
236ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
2372dd3e67bSChris Mason 	int harder_count = 0;
238ab78c84dSChris Mason 
2392dd3e67bSChris Mason harder:
240ab78c84dSChris Mason 	if (atomic_read(&info->throttles)) {
241ab78c84dSChris Mason 		DEFINE_WAIT(wait);
242ab78c84dSChris Mason 		int thr;
243ab78c84dSChris Mason 		thr = atomic_read(&info->throttle_gen);
244ab78c84dSChris Mason 
245ab78c84dSChris Mason 		do {
246ab78c84dSChris Mason 			prepare_to_wait(&info->transaction_throttle,
247ab78c84dSChris Mason 					&wait, TASK_UNINTERRUPTIBLE);
248ab78c84dSChris Mason 			if (!atomic_read(&info->throttles)) {
249ab78c84dSChris Mason 				finish_wait(&info->transaction_throttle, &wait);
250ab78c84dSChris Mason 				break;
251ab78c84dSChris Mason 			}
252ab78c84dSChris Mason 			schedule();
253ab78c84dSChris Mason 			finish_wait(&info->transaction_throttle, &wait);
254ab78c84dSChris Mason 		} while (thr == atomic_read(&info->throttle_gen));
2552dd3e67bSChris Mason 		harder_count++;
2562dd3e67bSChris Mason 
2572dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
2582dd3e67bSChris Mason 		    harder_count < 2)
2592dd3e67bSChris Mason 			goto harder;
2602dd3e67bSChris Mason 
2612dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
2622dd3e67bSChris Mason 		    harder_count < 10)
2632dd3e67bSChris Mason 			goto harder;
2642dd3e67bSChris Mason 
2652dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
2662dd3e67bSChris Mason 		    harder_count < 20)
2672dd3e67bSChris Mason 			goto harder;
268ab78c84dSChris Mason 	}
269ab78c84dSChris Mason }
270ab78c84dSChris Mason 
27137d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
27237d1aeeeSChris Mason {
27337d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
2749ca9ee09SSage Weil 	if (!root->fs_info->open_ioctl_trans)
27537d1aeeeSChris Mason 		wait_current_trans(root);
27637d1aeeeSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
27737d1aeeeSChris Mason 
27837d1aeeeSChris Mason 	throttle_on_drops(root);
27937d1aeeeSChris Mason }
28037d1aeeeSChris Mason 
28189ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
28289ce8a63SChris Mason 			  struct btrfs_root *root, int throttle)
28379154b1bSChris Mason {
28479154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
285ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
286d6e4a428SChris Mason 
287ab78c84dSChris Mason 	mutex_lock(&info->trans_mutex);
288ab78c84dSChris Mason 	cur_trans = info->running_transaction;
289ccd467d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
290d5719762SChris Mason 	WARN_ON(cur_trans->num_writers < 1);
291ccd467d6SChris Mason 	cur_trans->num_writers--;
29289ce8a63SChris Mason 
29379154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
29479154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
29579154b1bSChris Mason 	put_transaction(cur_trans);
296ab78c84dSChris Mason 	mutex_unlock(&info->trans_mutex);
297d6025579SChris Mason 	memset(trans, 0, sizeof(*trans));
2982c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
299ab78c84dSChris Mason 
300ab78c84dSChris Mason 	if (throttle)
30137d1aeeeSChris Mason 		throttle_on_drops(root);
302ab78c84dSChris Mason 
30379154b1bSChris Mason 	return 0;
30479154b1bSChris Mason }
30579154b1bSChris Mason 
30689ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
30789ce8a63SChris Mason 			  struct btrfs_root *root)
30889ce8a63SChris Mason {
30989ce8a63SChris Mason 	return __btrfs_end_transaction(trans, root, 0);
31089ce8a63SChris Mason }
31189ce8a63SChris Mason 
31289ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
31389ce8a63SChris Mason 				   struct btrfs_root *root)
31489ce8a63SChris Mason {
31589ce8a63SChris Mason 	return __btrfs_end_transaction(trans, root, 1);
31689ce8a63SChris Mason }
31789ce8a63SChris Mason 
318d352ac68SChris Mason /*
319d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
320d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
321d352ac68SChris Mason  * those extents are on disk for transaction or log commit
322d352ac68SChris Mason  */
323d0c803c4SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
324d0c803c4SChris Mason 					struct extent_io_tree *dirty_pages)
32579154b1bSChris Mason {
3267c4452b9SChris Mason 	int ret;
327777e6bd7SChris Mason 	int err = 0;
3287c4452b9SChris Mason 	int werr = 0;
3297c4452b9SChris Mason 	struct page *page;
3307c4452b9SChris Mason 	struct inode *btree_inode = root->fs_info->btree_inode;
331777e6bd7SChris Mason 	u64 start = 0;
3325f39d397SChris Mason 	u64 end;
3335f39d397SChris Mason 	unsigned long index;
3347c4452b9SChris Mason 
3357c4452b9SChris Mason 	while(1) {
336777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, start, &start, &end,
3375f39d397SChris Mason 					    EXTENT_DIRTY);
3385f39d397SChris Mason 		if (ret)
3397c4452b9SChris Mason 			break;
3405f39d397SChris Mason 		while(start <= end) {
341777e6bd7SChris Mason 			cond_resched();
342777e6bd7SChris Mason 
3435f39d397SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
34435ebb934SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
3454bef0848SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
3467c4452b9SChris Mason 			if (!page)
3477c4452b9SChris Mason 				continue;
3484bef0848SChris Mason 
3494bef0848SChris Mason 			btree_lock_page_hook(page);
3504bef0848SChris Mason 			if (!page->mapping) {
3514bef0848SChris Mason 				unlock_page(page);
3524bef0848SChris Mason 				page_cache_release(page);
3534bef0848SChris Mason 				continue;
3544bef0848SChris Mason 			}
3554bef0848SChris Mason 
3566702ed49SChris Mason 			if (PageWriteback(page)) {
3576702ed49SChris Mason 				if (PageDirty(page))
3586702ed49SChris Mason 					wait_on_page_writeback(page);
3596702ed49SChris Mason 				else {
3606702ed49SChris Mason 					unlock_page(page);
3616702ed49SChris Mason 					page_cache_release(page);
3626702ed49SChris Mason 					continue;
3636702ed49SChris Mason 				}
3646702ed49SChris Mason 			}
3657c4452b9SChris Mason 			err = write_one_page(page, 0);
3667c4452b9SChris Mason 			if (err)
3677c4452b9SChris Mason 				werr = err;
3687c4452b9SChris Mason 			page_cache_release(page);
3697c4452b9SChris Mason 		}
3707c4452b9SChris Mason 	}
371777e6bd7SChris Mason 	while(1) {
372777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
373777e6bd7SChris Mason 					    EXTENT_DIRTY);
374777e6bd7SChris Mason 		if (ret)
375777e6bd7SChris Mason 			break;
376777e6bd7SChris Mason 
377777e6bd7SChris Mason 		clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
378777e6bd7SChris Mason 		while(start <= end) {
379777e6bd7SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
380777e6bd7SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
381777e6bd7SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
382777e6bd7SChris Mason 			if (!page)
383777e6bd7SChris Mason 				continue;
384777e6bd7SChris Mason 			if (PageDirty(page)) {
3854bef0848SChris Mason 				btree_lock_page_hook(page);
3864bef0848SChris Mason 				wait_on_page_writeback(page);
387777e6bd7SChris Mason 				err = write_one_page(page, 0);
388777e6bd7SChris Mason 				if (err)
389777e6bd7SChris Mason 					werr = err;
390777e6bd7SChris Mason 			}
391777e6bd7SChris Mason 			wait_on_page_writeback(page);
392777e6bd7SChris Mason 			page_cache_release(page);
393777e6bd7SChris Mason 			cond_resched();
394777e6bd7SChris Mason 		}
395777e6bd7SChris Mason 	}
3967c4452b9SChris Mason 	if (err)
3977c4452b9SChris Mason 		werr = err;
3987c4452b9SChris Mason 	return werr;
39979154b1bSChris Mason }
40079154b1bSChris Mason 
401d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
402d0c803c4SChris Mason 				     struct btrfs_root *root)
403d0c803c4SChris Mason {
404d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
405d0c803c4SChris Mason 		struct inode *btree_inode;
406d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
407d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
408d0c803c4SChris Mason 	}
409d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
410d0c803c4SChris Mason 					   &trans->transaction->dirty_pages);
411d0c803c4SChris Mason }
412d0c803c4SChris Mason 
413d352ac68SChris Mason /*
414d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
415d352ac68SChris Mason  *
416d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
417d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
418d352ac68SChris Mason  * allocation tree.
419d352ac68SChris Mason  *
420d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
421d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
422d352ac68SChris Mason  */
4230b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
42479154b1bSChris Mason 			       struct btrfs_root *root)
42579154b1bSChris Mason {
42679154b1bSChris Mason 	int ret;
4270b86a832SChris Mason 	u64 old_root_bytenr;
4280b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
42979154b1bSChris Mason 
43087ef2bb4SChris Mason 	btrfs_extent_post_op(trans, root);
4310b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
43287ef2bb4SChris Mason 	btrfs_extent_post_op(trans, root);
43387ef2bb4SChris Mason 
43479154b1bSChris Mason 	while(1) {
4350b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
4360b86a832SChris Mason 		if (old_root_bytenr == root->node->start)
43779154b1bSChris Mason 			break;
4380b86a832SChris Mason 		btrfs_set_root_bytenr(&root->root_item,
4390b86a832SChris Mason 				       root->node->start);
4400b86a832SChris Mason 		btrfs_set_root_level(&root->root_item,
4410b86a832SChris Mason 				     btrfs_header_level(root->node));
44284234f3aSYan Zheng 		btrfs_set_root_generation(&root->root_item, trans->transid);
44387ef2bb4SChris Mason 
44487ef2bb4SChris Mason 		btrfs_extent_post_op(trans, root);
44587ef2bb4SChris Mason 
44679154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
4470b86a832SChris Mason 					&root->root_key,
4480b86a832SChris Mason 					&root->root_item);
44979154b1bSChris Mason 		BUG_ON(ret);
4500b86a832SChris Mason 		btrfs_write_dirty_block_groups(trans, root);
45187ef2bb4SChris Mason 		btrfs_extent_post_op(trans, root);
4520b86a832SChris Mason 	}
4530b86a832SChris Mason 	return 0;
4540b86a832SChris Mason }
4550b86a832SChris Mason 
456d352ac68SChris Mason /*
457d352ac68SChris Mason  * update all the cowonly tree roots on disk
458d352ac68SChris Mason  */
4590b86a832SChris Mason int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
4600b86a832SChris Mason 			    struct btrfs_root *root)
4610b86a832SChris Mason {
4620b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
4630b86a832SChris Mason 	struct list_head *next;
46484234f3aSYan Zheng 	struct extent_buffer *eb;
46584234f3aSYan Zheng 
46687ef2bb4SChris Mason 	btrfs_extent_post_op(trans, fs_info->tree_root);
46787ef2bb4SChris Mason 
46884234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
46984234f3aSYan Zheng 	btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb, 0);
47084234f3aSYan Zheng 	btrfs_tree_unlock(eb);
47184234f3aSYan Zheng 	free_extent_buffer(eb);
4720b86a832SChris Mason 
47387ef2bb4SChris Mason 	btrfs_extent_post_op(trans, fs_info->tree_root);
47487ef2bb4SChris Mason 
4750b86a832SChris Mason 	while(!list_empty(&fs_info->dirty_cowonly_roots)) {
4760b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
4770b86a832SChris Mason 		list_del_init(next);
4780b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
47987ef2bb4SChris Mason 
4800b86a832SChris Mason 		update_cowonly_root(trans, root);
48179154b1bSChris Mason 	}
48279154b1bSChris Mason 	return 0;
48379154b1bSChris Mason }
48479154b1bSChris Mason 
485d352ac68SChris Mason /*
486d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
487d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
488d352ac68SChris Mason  * be deleted
489d352ac68SChris Mason  */
490b48652c1SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
4915eda7b5eSChris Mason {
492f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
4935eda7b5eSChris Mason 
4945eda7b5eSChris Mason 	dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
4955eda7b5eSChris Mason 	if (!dirty)
4965eda7b5eSChris Mason 		return -ENOMEM;
4975eda7b5eSChris Mason 	dirty->root = root;
4985ce14bbcSChris Mason 	dirty->latest_root = latest;
499b48652c1SYan Zheng 
500b48652c1SYan Zheng 	mutex_lock(&root->fs_info->trans_mutex);
501b48652c1SYan Zheng 	list_add(&dirty->list, &latest->fs_info->dead_roots);
502b48652c1SYan Zheng 	mutex_unlock(&root->fs_info->trans_mutex);
5035eda7b5eSChris Mason 	return 0;
5045eda7b5eSChris Mason }
5055eda7b5eSChris Mason 
506d352ac68SChris Mason /*
507d352ac68SChris Mason  * at transaction commit time we need to schedule the old roots for
508d352ac68SChris Mason  * deletion via btrfs_drop_snapshot.  This runs through all the
509d352ac68SChris Mason  * reference counted roots that were modified in the current
510d352ac68SChris Mason  * transaction and puts them into the drop list
511d352ac68SChris Mason  */
51280b6794dSChris Mason static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
51335b7e476SChris Mason 				    struct radix_tree_root *radix,
51435b7e476SChris Mason 				    struct list_head *list)
5150f7d52f4SChris Mason {
516f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
5170f7d52f4SChris Mason 	struct btrfs_root *gang[8];
5180f7d52f4SChris Mason 	struct btrfs_root *root;
5190f7d52f4SChris Mason 	int i;
5200f7d52f4SChris Mason 	int ret;
52154aa1f4dSChris Mason 	int err = 0;
5225eda7b5eSChris Mason 	u32 refs;
52354aa1f4dSChris Mason 
5240f7d52f4SChris Mason 	while(1) {
5250f7d52f4SChris Mason 		ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
5260f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
5270f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
5280f7d52f4SChris Mason 		if (ret == 0)
5290f7d52f4SChris Mason 			break;
5300f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
5310f7d52f4SChris Mason 			root = gang[i];
5322619ba1fSChris Mason 			radix_tree_tag_clear(radix,
5332619ba1fSChris Mason 				     (unsigned long)root->root_key.objectid,
5340f7d52f4SChris Mason 				     BTRFS_ROOT_TRANS_TAG);
53531153d81SYan Zheng 
53631153d81SYan Zheng 			BUG_ON(!root->ref_tree);
537017e5369SChris Mason 			dirty = root->dirty_root;
53831153d81SYan Zheng 
539e02119d5SChris Mason 			btrfs_free_log(trans, root);
540f82d02d9SYan Zheng 			btrfs_free_reloc_root(trans, root);
541e02119d5SChris Mason 
5420f7d52f4SChris Mason 			if (root->commit_root == root->node) {
543db94535dSChris Mason 				WARN_ON(root->node->start !=
544db94535dSChris Mason 					btrfs_root_bytenr(&root->root_item));
54531153d81SYan Zheng 
5465f39d397SChris Mason 				free_extent_buffer(root->commit_root);
5470f7d52f4SChris Mason 				root->commit_root = NULL;
5487ea394f1SYan Zheng 				root->dirty_root = NULL;
54931153d81SYan Zheng 
550bcc63abbSYan 				spin_lock(&root->list_lock);
551bcc63abbSYan 				list_del_init(&dirty->root->dead_list);
552bcc63abbSYan 				spin_unlock(&root->list_lock);
553bcc63abbSYan 
55431153d81SYan Zheng 				kfree(dirty->root);
55531153d81SYan Zheng 				kfree(dirty);
55658176a96SJosef Bacik 
55758176a96SJosef Bacik 				/* make sure to update the root on disk
55858176a96SJosef Bacik 				 * so we get any updates to the block used
55958176a96SJosef Bacik 				 * counts
56058176a96SJosef Bacik 				 */
56158176a96SJosef Bacik 				err = btrfs_update_root(trans,
56258176a96SJosef Bacik 						root->fs_info->tree_root,
56358176a96SJosef Bacik 						&root->root_key,
56458176a96SJosef Bacik 						&root->root_item);
5650f7d52f4SChris Mason 				continue;
5660f7d52f4SChris Mason 			}
5679f3a7427SChris Mason 
5689f3a7427SChris Mason 			memset(&root->root_item.drop_progress, 0,
5699f3a7427SChris Mason 			       sizeof(struct btrfs_disk_key));
5709f3a7427SChris Mason 			root->root_item.drop_level = 0;
5710f7d52f4SChris Mason 			root->commit_root = NULL;
5727ea394f1SYan Zheng 			root->dirty_root = NULL;
5730f7d52f4SChris Mason 			root->root_key.offset = root->fs_info->generation;
574db94535dSChris Mason 			btrfs_set_root_bytenr(&root->root_item,
575db94535dSChris Mason 					      root->node->start);
576db94535dSChris Mason 			btrfs_set_root_level(&root->root_item,
577db94535dSChris Mason 					     btrfs_header_level(root->node));
57884234f3aSYan Zheng 			btrfs_set_root_generation(&root->root_item,
57984234f3aSYan Zheng 						  root->root_key.offset);
58084234f3aSYan Zheng 
5810f7d52f4SChris Mason 			err = btrfs_insert_root(trans, root->fs_info->tree_root,
5820f7d52f4SChris Mason 						&root->root_key,
5830f7d52f4SChris Mason 						&root->root_item);
58454aa1f4dSChris Mason 			if (err)
58554aa1f4dSChris Mason 				break;
5869f3a7427SChris Mason 
5879f3a7427SChris Mason 			refs = btrfs_root_refs(&dirty->root->root_item);
5889f3a7427SChris Mason 			btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
5895eda7b5eSChris Mason 			err = btrfs_update_root(trans, root->fs_info->tree_root,
5909f3a7427SChris Mason 						&dirty->root->root_key,
5919f3a7427SChris Mason 						&dirty->root->root_item);
5925eda7b5eSChris Mason 
5935eda7b5eSChris Mason 			BUG_ON(err);
5949f3a7427SChris Mason 			if (refs == 1) {
5950f7d52f4SChris Mason 				list_add(&dirty->list, list);
5969f3a7427SChris Mason 			} else {
5979f3a7427SChris Mason 				WARN_ON(1);
59831153d81SYan Zheng 				free_extent_buffer(dirty->root->node);
5999f3a7427SChris Mason 				kfree(dirty->root);
6005eda7b5eSChris Mason 				kfree(dirty);
6010f7d52f4SChris Mason 			}
6020f7d52f4SChris Mason 		}
6039f3a7427SChris Mason 	}
60454aa1f4dSChris Mason 	return err;
6050f7d52f4SChris Mason }
6060f7d52f4SChris Mason 
607d352ac68SChris Mason /*
608d352ac68SChris Mason  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
609d352ac68SChris Mason  * otherwise every leaf in the btree is read and defragged.
610d352ac68SChris Mason  */
611e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
612e9d0b13bSChris Mason {
613e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
614e9d0b13bSChris Mason 	int ret;
615e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
616d3c2fdcfSChris Mason 	unsigned long nr;
617e9d0b13bSChris Mason 
618a2135011SChris Mason 	smp_mb();
619e9d0b13bSChris Mason 	if (root->defrag_running)
620e9d0b13bSChris Mason 		return 0;
621e9d0b13bSChris Mason 	trans = btrfs_start_transaction(root, 1);
6226b80053dSChris Mason 	while (1) {
623e9d0b13bSChris Mason 		root->defrag_running = 1;
624e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
625d3c2fdcfSChris Mason 		nr = trans->blocks_used;
626e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
627d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(info->tree_root, nr);
628e9d0b13bSChris Mason 		cond_resched();
629e9d0b13bSChris Mason 
630e9d0b13bSChris Mason 		trans = btrfs_start_transaction(root, 1);
6313f157a2fSChris Mason 		if (root->fs_info->closing || ret != -EAGAIN)
632e9d0b13bSChris Mason 			break;
633e9d0b13bSChris Mason 	}
634e9d0b13bSChris Mason 	root->defrag_running = 0;
635a2135011SChris Mason 	smp_mb();
636e9d0b13bSChris Mason 	btrfs_end_transaction(trans, root);
637e9d0b13bSChris Mason 	return 0;
638e9d0b13bSChris Mason }
639e9d0b13bSChris Mason 
640d352ac68SChris Mason /*
641d352ac68SChris Mason  * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
642d352ac68SChris Mason  * all of them
643d352ac68SChris Mason  */
64480b6794dSChris Mason static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
64535b7e476SChris Mason 				     struct list_head *list)
6460f7d52f4SChris Mason {
647f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
6480f7d52f4SChris Mason 	struct btrfs_trans_handle *trans;
649d3c2fdcfSChris Mason 	unsigned long nr;
650db94535dSChris Mason 	u64 num_bytes;
651db94535dSChris Mason 	u64 bytes_used;
652bcc63abbSYan 	u64 max_useless;
65354aa1f4dSChris Mason 	int ret = 0;
6549f3a7427SChris Mason 	int err;
6559f3a7427SChris Mason 
6560f7d52f4SChris Mason 	while(!list_empty(list)) {
65758176a96SJosef Bacik 		struct btrfs_root *root;
65858176a96SJosef Bacik 
659f321e491SYan Zheng 		dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
6600f7d52f4SChris Mason 		list_del_init(&dirty->list);
6615eda7b5eSChris Mason 
662db94535dSChris Mason 		num_bytes = btrfs_root_used(&dirty->root->root_item);
66358176a96SJosef Bacik 		root = dirty->latest_root;
664a2135011SChris Mason 		atomic_inc(&root->fs_info->throttles);
66558176a96SJosef Bacik 
6669f3a7427SChris Mason 		while(1) {
6670f7d52f4SChris Mason 			trans = btrfs_start_transaction(tree_root, 1);
6685b21f2edSZheng Yan 			mutex_lock(&root->fs_info->drop_mutex);
6699f3a7427SChris Mason 			ret = btrfs_drop_snapshot(trans, dirty->root);
6709f3a7427SChris Mason 			if (ret != -EAGAIN) {
6719f3a7427SChris Mason 				break;
6729f3a7427SChris Mason 			}
6735b21f2edSZheng Yan 			mutex_unlock(&root->fs_info->drop_mutex);
67458176a96SJosef Bacik 
6759f3a7427SChris Mason 			err = btrfs_update_root(trans,
6769f3a7427SChris Mason 					tree_root,
6779f3a7427SChris Mason 					&dirty->root->root_key,
6789f3a7427SChris Mason 					&dirty->root->root_item);
6799f3a7427SChris Mason 			if (err)
6809f3a7427SChris Mason 				ret = err;
681d3c2fdcfSChris Mason 			nr = trans->blocks_used;
682017e5369SChris Mason 			ret = btrfs_end_transaction(trans, tree_root);
6830f7d52f4SChris Mason 			BUG_ON(ret);
684a2135011SChris Mason 
685d3c2fdcfSChris Mason 			btrfs_btree_balance_dirty(tree_root, nr);
6864dc11904SChris Mason 			cond_resched();
6879f3a7427SChris Mason 		}
6889f3a7427SChris Mason 		BUG_ON(ret);
689a2135011SChris Mason 		atomic_dec(&root->fs_info->throttles);
690017e5369SChris Mason 		wake_up(&root->fs_info->transaction_throttle);
69158176a96SJosef Bacik 
692db94535dSChris Mason 		num_bytes -= btrfs_root_used(&dirty->root->root_item);
693db94535dSChris Mason 		bytes_used = btrfs_root_used(&root->root_item);
694db94535dSChris Mason 		if (num_bytes) {
695e02119d5SChris Mason 			btrfs_record_root_in_trans(root);
6965f39d397SChris Mason 			btrfs_set_root_used(&root->root_item,
697db94535dSChris Mason 					    bytes_used - num_bytes);
69858176a96SJosef Bacik 		}
699a2135011SChris Mason 
7009f3a7427SChris Mason 		ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
70158176a96SJosef Bacik 		if (ret) {
70258176a96SJosef Bacik 			BUG();
70354aa1f4dSChris Mason 			break;
70458176a96SJosef Bacik 		}
705a2135011SChris Mason 		mutex_unlock(&root->fs_info->drop_mutex);
706a2135011SChris Mason 
707bcc63abbSYan 		spin_lock(&root->list_lock);
708bcc63abbSYan 		list_del_init(&dirty->root->dead_list);
709bcc63abbSYan 		if (!list_empty(&root->dead_list)) {
710bcc63abbSYan 			struct btrfs_root *oldest;
711bcc63abbSYan 			oldest = list_entry(root->dead_list.prev,
712bcc63abbSYan 					    struct btrfs_root, dead_list);
713bcc63abbSYan 			max_useless = oldest->root_key.offset - 1;
714bcc63abbSYan 		} else {
715bcc63abbSYan 			max_useless = root->root_key.offset - 1;
716bcc63abbSYan 		}
717bcc63abbSYan 		spin_unlock(&root->list_lock);
718bcc63abbSYan 
719d3c2fdcfSChris Mason 		nr = trans->blocks_used;
7200f7d52f4SChris Mason 		ret = btrfs_end_transaction(trans, tree_root);
7210f7d52f4SChris Mason 		BUG_ON(ret);
7225eda7b5eSChris Mason 
723e4657689SZheng Yan 		ret = btrfs_remove_leaf_refs(root, max_useless, 0);
724bcc63abbSYan 		BUG_ON(ret);
725bcc63abbSYan 
726f510cfecSChris Mason 		free_extent_buffer(dirty->root->node);
7275eda7b5eSChris Mason 		kfree(dirty->root);
7280f7d52f4SChris Mason 		kfree(dirty);
729d3c2fdcfSChris Mason 
730d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(tree_root, nr);
7314dc11904SChris Mason 		cond_resched();
7320f7d52f4SChris Mason 	}
73354aa1f4dSChris Mason 	return ret;
7340f7d52f4SChris Mason }
7350f7d52f4SChris Mason 
736d352ac68SChris Mason /*
737d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
738d352ac68SChris Mason  * transaction commit.  This does the actual creation
739d352ac68SChris Mason  */
74080b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
7413063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
7423063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
7433063d29fSChris Mason {
7443063d29fSChris Mason 	struct btrfs_key key;
74580b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
7463063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
7473063d29fSChris Mason 	struct btrfs_root *root = pending->root;
7483063d29fSChris Mason 	struct extent_buffer *tmp;
749925baeddSChris Mason 	struct extent_buffer *old;
7503063d29fSChris Mason 	int ret;
7513063d29fSChris Mason 	u64 objectid;
7523063d29fSChris Mason 
75380b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
75480b6794dSChris Mason 	if (!new_root_item) {
75580b6794dSChris Mason 		ret = -ENOMEM;
75680b6794dSChris Mason 		goto fail;
75780b6794dSChris Mason 	}
7583063d29fSChris Mason 	ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
7593063d29fSChris Mason 	if (ret)
7603063d29fSChris Mason 		goto fail;
7613063d29fSChris Mason 
76280ff3856SYan Zheng 	btrfs_record_root_in_trans(root);
76380ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
76480b6794dSChris Mason 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
7653063d29fSChris Mason 
7663063d29fSChris Mason 	key.objectid = objectid;
7675b21f2edSZheng Yan 	key.offset = trans->transid;
7683063d29fSChris Mason 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
7693063d29fSChris Mason 
770925baeddSChris Mason 	old = btrfs_lock_root_node(root);
77165b51a00SChris Mason 	btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
7723063d29fSChris Mason 
773925baeddSChris Mason 	btrfs_copy_root(trans, root, old, &tmp, objectid);
774925baeddSChris Mason 	btrfs_tree_unlock(old);
775925baeddSChris Mason 	free_extent_buffer(old);
7763063d29fSChris Mason 
77780b6794dSChris Mason 	btrfs_set_root_bytenr(new_root_item, tmp->start);
77880b6794dSChris Mason 	btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
77984234f3aSYan Zheng 	btrfs_set_root_generation(new_root_item, trans->transid);
7803063d29fSChris Mason 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
78180b6794dSChris Mason 				new_root_item);
782925baeddSChris Mason 	btrfs_tree_unlock(tmp);
7833063d29fSChris Mason 	free_extent_buffer(tmp);
7843063d29fSChris Mason 	if (ret)
7853063d29fSChris Mason 		goto fail;
7863063d29fSChris Mason 
7873de4586cSChris Mason 	key.offset = (u64)-1;
7883de4586cSChris Mason 	memcpy(&pending->root_key, &key, sizeof(key));
7893de4586cSChris Mason fail:
7903de4586cSChris Mason 	kfree(new_root_item);
7913de4586cSChris Mason 	return ret;
7923de4586cSChris Mason }
7933de4586cSChris Mason 
7943de4586cSChris Mason static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info,
7953de4586cSChris Mason 				   struct btrfs_pending_snapshot *pending)
7963de4586cSChris Mason {
7973de4586cSChris Mason 	int ret;
7983de4586cSChris Mason 	int namelen;
7993de4586cSChris Mason 	u64 index = 0;
8003de4586cSChris Mason 	struct btrfs_trans_handle *trans;
8013de4586cSChris Mason 	struct inode *parent_inode;
8023de4586cSChris Mason 	struct inode *inode;
8030660b5afSChris Mason 	struct btrfs_root *parent_root;
8043de4586cSChris Mason 
8053394e160SChris Mason 	parent_inode = pending->dentry->d_parent->d_inode;
8060660b5afSChris Mason 	parent_root = BTRFS_I(parent_inode)->root;
8070660b5afSChris Mason 	trans = btrfs_start_transaction(parent_root, 1);
8083de4586cSChris Mason 
8093063d29fSChris Mason 	/*
8103063d29fSChris Mason 	 * insert the directory item
8113063d29fSChris Mason 	 */
8123b96362cSSven Wegener 	namelen = strlen(pending->name);
8133de4586cSChris Mason 	ret = btrfs_set_inode_index(parent_inode, &index);
8140660b5afSChris Mason 	ret = btrfs_insert_dir_item(trans, parent_root,
8153b96362cSSven Wegener 			    pending->name, namelen,
8163de4586cSChris Mason 			    parent_inode->i_ino,
8173de4586cSChris Mason 			    &pending->root_key, BTRFS_FT_DIR, index);
8183063d29fSChris Mason 
8193063d29fSChris Mason 	if (ret)
8203063d29fSChris Mason 		goto fail;
8210660b5afSChris Mason 
82252c26179SYan Zheng 	btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2);
82352c26179SYan Zheng 	ret = btrfs_update_inode(trans, parent_root, parent_inode);
82452c26179SYan Zheng 	BUG_ON(ret);
82552c26179SYan Zheng 
8260660b5afSChris Mason 	/* add the backref first */
8270660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
8280660b5afSChris Mason 				 pending->root_key.objectid,
8290660b5afSChris Mason 				 BTRFS_ROOT_BACKREF_KEY,
8300660b5afSChris Mason 				 parent_root->root_key.objectid,
8310660b5afSChris Mason 				 parent_inode->i_ino, index, pending->name,
8320660b5afSChris Mason 				 namelen);
8330660b5afSChris Mason 
8340660b5afSChris Mason 	BUG_ON(ret);
8350660b5afSChris Mason 
8360660b5afSChris Mason 	/* now add the forward ref */
8370660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
8380660b5afSChris Mason 				 parent_root->root_key.objectid,
8390660b5afSChris Mason 				 BTRFS_ROOT_REF_KEY,
8400660b5afSChris Mason 				 pending->root_key.objectid,
8410660b5afSChris Mason 				 parent_inode->i_ino, index, pending->name,
8420660b5afSChris Mason 				 namelen);
8430660b5afSChris Mason 
8443de4586cSChris Mason 	inode = btrfs_lookup_dentry(parent_inode, pending->dentry);
8453de4586cSChris Mason 	d_instantiate(pending->dentry, inode);
8463063d29fSChris Mason fail:
8473de4586cSChris Mason 	btrfs_end_transaction(trans, fs_info->fs_root);
8483063d29fSChris Mason 	return ret;
8493063d29fSChris Mason }
8503063d29fSChris Mason 
851d352ac68SChris Mason /*
852d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
853d352ac68SChris Mason  */
85480b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
8553063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
8563063d29fSChris Mason {
8573063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
8583063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
8593de4586cSChris Mason 	struct list_head *cur;
8603de4586cSChris Mason 	int ret;
8613de4586cSChris Mason 
8623de4586cSChris Mason 	list_for_each(cur, head) {
8633de4586cSChris Mason 		pending = list_entry(cur, struct btrfs_pending_snapshot, list);
8643de4586cSChris Mason 		ret = create_pending_snapshot(trans, fs_info, pending);
8653de4586cSChris Mason 		BUG_ON(ret);
8663de4586cSChris Mason 	}
8673de4586cSChris Mason 	return 0;
8683de4586cSChris Mason }
8693de4586cSChris Mason 
8703de4586cSChris Mason static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans,
8713de4586cSChris Mason 					     struct btrfs_fs_info *fs_info)
8723de4586cSChris Mason {
8733de4586cSChris Mason 	struct btrfs_pending_snapshot *pending;
8743de4586cSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
8753063d29fSChris Mason 	int ret;
8763063d29fSChris Mason 
8773063d29fSChris Mason 	while(!list_empty(head)) {
8783063d29fSChris Mason 		pending = list_entry(head->next,
8793063d29fSChris Mason 				     struct btrfs_pending_snapshot, list);
8803de4586cSChris Mason 		ret = finish_pending_snapshot(fs_info, pending);
8813063d29fSChris Mason 		BUG_ON(ret);
8823063d29fSChris Mason 		list_del(&pending->list);
8833063d29fSChris Mason 		kfree(pending->name);
8843063d29fSChris Mason 		kfree(pending);
8853063d29fSChris Mason 	}
886dc17ff8fSChris Mason 	return 0;
887dc17ff8fSChris Mason }
888dc17ff8fSChris Mason 
88979154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
89079154b1bSChris Mason 			     struct btrfs_root *root)
89179154b1bSChris Mason {
89215ee9bc7SJosef Bacik 	unsigned long joined = 0;
89315ee9bc7SJosef Bacik 	unsigned long timeout = 1;
89479154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
8958fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
8960b86a832SChris Mason 	struct btrfs_root *chunk_root = root->fs_info->chunk_root;
8970f7d52f4SChris Mason 	struct list_head dirty_fs_roots;
898d1310b2eSChris Mason 	struct extent_io_tree *pinned_copy;
89979154b1bSChris Mason 	DEFINE_WAIT(wait);
90015ee9bc7SJosef Bacik 	int ret;
90179154b1bSChris Mason 
9020f7d52f4SChris Mason 	INIT_LIST_HEAD(&dirty_fs_roots);
90379154b1bSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
90479154b1bSChris Mason 	if (trans->transaction->in_commit) {
90579154b1bSChris Mason 		cur_trans = trans->transaction;
90679154b1bSChris Mason 		trans->transaction->use_count++;
907ccd467d6SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
90879154b1bSChris Mason 		btrfs_end_transaction(trans, root);
909ccd467d6SChris Mason 
91079154b1bSChris Mason 		ret = wait_for_commit(root, cur_trans);
91179154b1bSChris Mason 		BUG_ON(ret);
91215ee9bc7SJosef Bacik 
91315ee9bc7SJosef Bacik 		mutex_lock(&root->fs_info->trans_mutex);
91479154b1bSChris Mason 		put_transaction(cur_trans);
91515ee9bc7SJosef Bacik 		mutex_unlock(&root->fs_info->trans_mutex);
91615ee9bc7SJosef Bacik 
91779154b1bSChris Mason 		return 0;
91879154b1bSChris Mason 	}
9194313b399SChris Mason 
9204313b399SChris Mason 	pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
9214313b399SChris Mason 	if (!pinned_copy)
9224313b399SChris Mason 		return -ENOMEM;
9234313b399SChris Mason 
924d1310b2eSChris Mason 	extent_io_tree_init(pinned_copy,
9254313b399SChris Mason 			     root->fs_info->btree_inode->i_mapping, GFP_NOFS);
9264313b399SChris Mason 
9272c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
928f9295749SChris Mason 	trans->transaction->blocked = 1;
929ccd467d6SChris Mason 	cur_trans = trans->transaction;
930ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
931ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
932ccd467d6SChris Mason 					struct btrfs_transaction, list);
933ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
934ccd467d6SChris Mason 			prev_trans->use_count++;
935ccd467d6SChris Mason 			mutex_unlock(&root->fs_info->trans_mutex);
936ccd467d6SChris Mason 
937ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
938ccd467d6SChris Mason 
939ccd467d6SChris Mason 			mutex_lock(&root->fs_info->trans_mutex);
94015ee9bc7SJosef Bacik 			put_transaction(prev_trans);
941ccd467d6SChris Mason 		}
942ccd467d6SChris Mason 	}
94315ee9bc7SJosef Bacik 
94415ee9bc7SJosef Bacik 	do {
9457ea394f1SYan Zheng 		int snap_pending = 0;
94615ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
9477ea394f1SYan Zheng 		if (!list_empty(&trans->transaction->pending_snapshots))
9487ea394f1SYan Zheng 			snap_pending = 1;
9497ea394f1SYan Zheng 
9502c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
95115ee9bc7SJosef Bacik 		prepare_to_wait(&cur_trans->writer_wait, &wait,
95279154b1bSChris Mason 				TASK_UNINTERRUPTIBLE);
95315ee9bc7SJosef Bacik 
95415ee9bc7SJosef Bacik 		if (cur_trans->num_writers > 1)
95515ee9bc7SJosef Bacik 			timeout = MAX_SCHEDULE_TIMEOUT;
95615ee9bc7SJosef Bacik 		else
95715ee9bc7SJosef Bacik 			timeout = 1;
95815ee9bc7SJosef Bacik 
95979154b1bSChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
96015ee9bc7SJosef Bacik 
9617ea394f1SYan Zheng 		if (snap_pending) {
9627ea394f1SYan Zheng 			ret = btrfs_wait_ordered_extents(root, 1);
9637ea394f1SYan Zheng 			BUG_ON(ret);
9647ea394f1SYan Zheng 		}
9657ea394f1SYan Zheng 
96615ee9bc7SJosef Bacik 		schedule_timeout(timeout);
96715ee9bc7SJosef Bacik 
96879154b1bSChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
96915ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
97015ee9bc7SJosef Bacik 	} while (cur_trans->num_writers > 1 ||
97115ee9bc7SJosef Bacik 		 (cur_trans->num_joined != joined));
97215ee9bc7SJosef Bacik 
9733063d29fSChris Mason 	ret = create_pending_snapshots(trans, root->fs_info);
9743063d29fSChris Mason 	BUG_ON(ret);
9753063d29fSChris Mason 
9762c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
977dc17ff8fSChris Mason 
978e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
979e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
980e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
981e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
982e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
983e02119d5SChris Mason 	 * of the trees.
984e02119d5SChris Mason 	 *
985e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
986e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
987e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
988e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
989e02119d5SChris Mason 	 * with the tree-log code.
990e02119d5SChris Mason 	 */
991e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
9921a40e23bSZheng Yan 	/*
9931a40e23bSZheng Yan 	 * keep tree reloc code from adding new reloc trees
9941a40e23bSZheng Yan 	 */
9951a40e23bSZheng Yan 	mutex_lock(&root->fs_info->tree_reloc_mutex);
9961a40e23bSZheng Yan 
997e02119d5SChris Mason 
99854aa1f4dSChris Mason 	ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
99954aa1f4dSChris Mason 			      &dirty_fs_roots);
100054aa1f4dSChris Mason 	BUG_ON(ret);
100154aa1f4dSChris Mason 
1002e02119d5SChris Mason 	/* add_dirty_roots gets rid of all the tree log roots, it is now
1003e02119d5SChris Mason 	 * safe to free the root of tree log roots
1004e02119d5SChris Mason 	 */
1005e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1006e02119d5SChris Mason 
100779154b1bSChris Mason 	ret = btrfs_commit_tree_roots(trans, root);
100879154b1bSChris Mason 	BUG_ON(ret);
100954aa1f4dSChris Mason 
101078fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
1011cee36a03SChris Mason 	spin_lock(&root->fs_info->new_trans_lock);
101278fae27eSChris Mason 	root->fs_info->running_transaction = NULL;
1013cee36a03SChris Mason 	spin_unlock(&root->fs_info->new_trans_lock);
10144b52dff6SChris Mason 	btrfs_set_super_generation(&root->fs_info->super_copy,
10154b52dff6SChris Mason 				   cur_trans->transid);
10164b52dff6SChris Mason 	btrfs_set_super_root(&root->fs_info->super_copy,
1017db94535dSChris Mason 			     root->fs_info->tree_root->node->start);
1018db94535dSChris Mason 	btrfs_set_super_root_level(&root->fs_info->super_copy,
1019db94535dSChris Mason 			   btrfs_header_level(root->fs_info->tree_root->node));
10205f39d397SChris Mason 
10210b86a832SChris Mason 	btrfs_set_super_chunk_root(&root->fs_info->super_copy,
10220b86a832SChris Mason 				   chunk_root->node->start);
10230b86a832SChris Mason 	btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
10240b86a832SChris Mason 					 btrfs_header_level(chunk_root->node));
102584234f3aSYan Zheng 	btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
102684234f3aSYan Zheng 				btrfs_header_generation(chunk_root->node));
1027e02119d5SChris Mason 
1028e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
1029e02119d5SChris Mason 		btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1030e02119d5SChris Mason 		btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1031e02119d5SChris Mason 	}
1032e02119d5SChris Mason 
1033a061fc8dSChris Mason 	memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
10344b52dff6SChris Mason 	       sizeof(root->fs_info->super_copy));
1035ccd467d6SChris Mason 
10364313b399SChris Mason 	btrfs_copy_pinned(root, pinned_copy);
1037ccd467d6SChris Mason 
1038f9295749SChris Mason 	trans->transaction->blocked = 0;
1039e6dcd2dcSChris Mason 	wake_up(&root->fs_info->transaction_throttle);
1040f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1041e6dcd2dcSChris Mason 
104278fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
104379154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
104479154b1bSChris Mason 	BUG_ON(ret);
1045a512bbf8SYan Zheng 	write_ctree_super(trans, root, 0);
10464313b399SChris Mason 
1047e02119d5SChris Mason 	/*
1048e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1049e02119d5SChris Mason 	 * to go about their business
1050e02119d5SChris Mason 	 */
1051e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1052e02119d5SChris Mason 
10534313b399SChris Mason 	btrfs_finish_extent_commit(trans, root, pinned_copy);
10544313b399SChris Mason 	kfree(pinned_copy);
10554313b399SChris Mason 
10561a40e23bSZheng Yan 	btrfs_drop_dead_reloc_roots(root);
10571a40e23bSZheng Yan 	mutex_unlock(&root->fs_info->tree_reloc_mutex);
10581a40e23bSZheng Yan 
10593de4586cSChris Mason 	/* do the directory inserts of any pending snapshot creations */
10603de4586cSChris Mason 	finish_pending_snapshots(trans, root->fs_info);
10613de4586cSChris Mason 
10621a40e23bSZheng Yan 	mutex_lock(&root->fs_info->trans_mutex);
10631a40e23bSZheng Yan 
10642c90e5d6SChris Mason 	cur_trans->commit_done = 1;
106515ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
10662c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
10673de4586cSChris Mason 
106879154b1bSChris Mason 	put_transaction(cur_trans);
106978fae27eSChris Mason 	put_transaction(cur_trans);
107058176a96SJosef Bacik 
1071bcc63abbSYan 	list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
1072facda1e7SChris Mason 	if (root->fs_info->closing)
1073facda1e7SChris Mason 		list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
107458176a96SJosef Bacik 
107578fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
10763de4586cSChris Mason 
10772c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
107879154b1bSChris Mason 
1079facda1e7SChris Mason 	if (root->fs_info->closing) {
10800f7d52f4SChris Mason 		drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
1081facda1e7SChris Mason 	}
108279154b1bSChris Mason 	return ret;
108379154b1bSChris Mason }
108479154b1bSChris Mason 
1085d352ac68SChris Mason /*
1086d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1087d352ac68SChris Mason  */
1088e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1089e9d0b13bSChris Mason {
1090e9d0b13bSChris Mason 	struct list_head dirty_roots;
1091e9d0b13bSChris Mason 	INIT_LIST_HEAD(&dirty_roots);
1092a74a4b97SChris Mason again:
1093e9d0b13bSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
1094e9d0b13bSChris Mason 	list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
1095e9d0b13bSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
1096e9d0b13bSChris Mason 
1097e9d0b13bSChris Mason 	if (!list_empty(&dirty_roots)) {
1098e9d0b13bSChris Mason 		drop_dirty_roots(root, &dirty_roots);
1099a74a4b97SChris Mason 		goto again;
1100e9d0b13bSChris Mason 	}
1101e9d0b13bSChris Mason 	return 0;
1102e9d0b13bSChris Mason }
1103