blob: 86a5acc19ce7fca22f28174f3885343214856c46 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason79154b12007-03-22 15:59:16 -040019#include <linux/fs.h>
Chris Mason34088782007-06-12 11:36:58 -040020#include <linux/sched.h>
Chris Masond3c2fdcf2007-09-17 10:58:06 -040021#include <linux/writeback.h>
Chris Mason5f39d392007-10-15 16:14:19 -040022#include <linux/pagemap.h>
Chris Mason79154b12007-03-22 15:59:16 -040023#include "ctree.h"
24#include "disk-io.h"
25#include "transaction.h"
Chris Mason925baed2008-06-25 16:01:30 -040026#include "locking.h"
Chris Mason79154b12007-03-22 15:59:16 -040027
Chris Mason78fae272007-03-25 11:35:08 -040028static int total_trans = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -040029extern struct kmem_cache *btrfs_trans_handle_cachep;
30extern struct kmem_cache *btrfs_transaction_cachep;
31
Chris Mason0f7d52f2007-04-09 10:42:37 -040032#define BTRFS_ROOT_TRANS_TAG 0
33
Chris Mason80b67942008-02-01 16:35:04 -050034static noinline void put_transaction(struct btrfs_transaction *transaction)
Chris Mason79154b12007-03-22 15:59:16 -040035{
Chris Mason2c90e5d2007-04-02 10:50:19 -040036 WARN_ON(transaction->use_count == 0);
Chris Mason79154b12007-03-22 15:59:16 -040037 transaction->use_count--;
Chris Mason78fae272007-03-25 11:35:08 -040038 if (transaction->use_count == 0) {
39 WARN_ON(total_trans == 0);
40 total_trans--;
Chris Mason8fd17792007-04-19 21:01:03 -040041 list_del_init(&transaction->list);
Chris Mason2c90e5d2007-04-02 10:50:19 -040042 memset(transaction, 0, sizeof(*transaction));
43 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040044 }
Chris Mason79154b12007-03-22 15:59:16 -040045}
46
Chris Mason80b67942008-02-01 16:35:04 -050047static noinline int join_transaction(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -040048{
49 struct btrfs_transaction *cur_trans;
50 cur_trans = root->fs_info->running_transaction;
51 if (!cur_trans) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040052 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
53 GFP_NOFS);
Chris Mason78fae272007-03-25 11:35:08 -040054 total_trans++;
Chris Mason79154b12007-03-22 15:59:16 -040055 BUG_ON(!cur_trans);
Chris Mason0f7d52f2007-04-09 10:42:37 -040056 root->fs_info->generation++;
Chris Masone18e4802008-01-18 10:54:22 -050057 root->fs_info->last_alloc = 0;
Chris Mason4529ba42008-01-31 16:45:07 -050058 root->fs_info->last_data_alloc = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -040059 cur_trans->num_writers = 1;
60 cur_trans->num_joined = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040061 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040062 init_waitqueue_head(&cur_trans->writer_wait);
63 init_waitqueue_head(&cur_trans->commit_wait);
64 cur_trans->in_commit = 0;
Chris Masond5719762007-03-23 10:01:08 -040065 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040066 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040067 cur_trans->start_time = get_seconds();
Chris Mason3063d292008-01-08 15:46:30 -050068 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
Chris Mason8fd17792007-04-19 21:01:03 -040069 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
Chris Masond1310b22008-01-24 16:13:08 -050070 extent_io_tree_init(&cur_trans->dirty_pages,
Chris Mason5f39d392007-10-15 16:14:19 -040071 root->fs_info->btree_inode->i_mapping,
72 GFP_NOFS);
Chris Mason48ec2cf2008-06-09 09:35:50 -040073 spin_lock(&root->fs_info->new_trans_lock);
74 root->fs_info->running_transaction = cur_trans;
75 spin_unlock(&root->fs_info->new_trans_lock);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040076 } else {
77 cur_trans->num_writers++;
78 cur_trans->num_joined++;
Chris Mason79154b12007-03-22 15:59:16 -040079 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -040080
Chris Mason79154b12007-03-22 15:59:16 -040081 return 0;
82}
83
Chris Mason80b67942008-02-01 16:35:04 -050084static noinline int record_root_in_trans(struct btrfs_root *root)
Chris Mason6702ed42007-08-07 16:15:09 -040085{
86 u64 running_trans_id = root->fs_info->running_transaction->transid;
87 if (root->ref_cows && root->last_trans < running_trans_id) {
88 WARN_ON(root == root->fs_info->extent_root);
89 if (root->root_item.refs != 0) {
90 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
91 (unsigned long)root->root_key.objectid,
92 BTRFS_ROOT_TRANS_TAG);
Chris Mason925baed2008-06-25 16:01:30 -040093 root->commit_root = btrfs_root_node(root);
Chris Mason6702ed42007-08-07 16:15:09 -040094 } else {
95 WARN_ON(1);
96 }
97 root->last_trans = running_trans_id;
98 }
99 return 0;
100}
101
Chris Mason79154b12007-03-22 15:59:16 -0400102struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
103 int num_blocks)
104{
Chris Mason2c90e5d2007-04-02 10:50:19 -0400105 struct btrfs_trans_handle *h =
106 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
Chris Mason79154b12007-03-22 15:59:16 -0400107 int ret;
108
109 mutex_lock(&root->fs_info->trans_mutex);
110 ret = join_transaction(root);
111 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400112
Chris Mason6702ed42007-08-07 16:15:09 -0400113 record_root_in_trans(root);
114 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400115 h->transaction = root->fs_info->running_transaction;
116 h->blocks_reserved = num_blocks;
117 h->blocks_used = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400118 h->block_group = NULL;
Chris Mason26b80032007-08-08 20:17:12 -0400119 h->alloc_exclude_nr = 0;
120 h->alloc_exclude_start = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400121 root->fs_info->running_transaction->use_count++;
122 mutex_unlock(&root->fs_info->trans_mutex);
123 return h;
124}
125
Chris Mason89ce8a62008-06-25 16:01:31 -0400126static noinline int wait_for_commit(struct btrfs_root *root,
127 struct btrfs_transaction *commit)
128{
129 DEFINE_WAIT(wait);
130 mutex_lock(&root->fs_info->trans_mutex);
131 while(!commit->commit_done) {
132 prepare_to_wait(&commit->commit_wait, &wait,
133 TASK_UNINTERRUPTIBLE);
134 if (commit->commit_done)
135 break;
136 mutex_unlock(&root->fs_info->trans_mutex);
137 schedule();
138 mutex_lock(&root->fs_info->trans_mutex);
139 }
140 mutex_unlock(&root->fs_info->trans_mutex);
141 finish_wait(&commit->commit_wait, &wait);
142 return 0;
143}
144
145static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
146 struct btrfs_root *root, int throttle)
Chris Mason79154b12007-03-22 15:59:16 -0400147{
148 struct btrfs_transaction *cur_trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400149
Chris Mason79154b12007-03-22 15:59:16 -0400150 mutex_lock(&root->fs_info->trans_mutex);
151 cur_trans = root->fs_info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400152 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400153 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400154 cur_trans->num_writers--;
Chris Mason89ce8a62008-06-25 16:01:31 -0400155
Chris Mason79154b12007-03-22 15:59:16 -0400156 if (waitqueue_active(&cur_trans->writer_wait))
157 wake_up(&cur_trans->writer_wait);
Chris Mason89ce8a62008-06-25 16:01:31 -0400158
159 if (cur_trans->in_commit && throttle) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400160 DEFINE_WAIT(wait);
Chris Mason89ce8a62008-06-25 16:01:31 -0400161 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400162 prepare_to_wait(&root->fs_info->transaction_throttle, &wait,
163 TASK_UNINTERRUPTIBLE);
164 schedule();
165 finish_wait(&root->fs_info->transaction_throttle, &wait);
Chris Mason89ce8a62008-06-25 16:01:31 -0400166 mutex_lock(&root->fs_info->trans_mutex);
167 }
168
Chris Mason79154b12007-03-22 15:59:16 -0400169 put_transaction(cur_trans);
170 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400171 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400172 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400173 return 0;
174}
175
Chris Mason89ce8a62008-06-25 16:01:31 -0400176int btrfs_end_transaction(struct btrfs_trans_handle *trans,
177 struct btrfs_root *root)
178{
179 return __btrfs_end_transaction(trans, root, 0);
180}
181
182int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
183 struct btrfs_root *root)
184{
185 return __btrfs_end_transaction(trans, root, 1);
186}
187
Chris Mason79154b12007-03-22 15:59:16 -0400188
189int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
190 struct btrfs_root *root)
191{
Chris Mason7c4452b2007-04-28 09:29:35 -0400192 int ret;
Chris Mason7c4452b2007-04-28 09:29:35 -0400193 int err;
194 int werr = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500195 struct extent_io_tree *dirty_pages;
Chris Mason7c4452b2007-04-28 09:29:35 -0400196 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400197 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason5f39d392007-10-15 16:14:19 -0400198 u64 start;
199 u64 end;
200 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400201
202 if (!trans || !trans->transaction) {
203 return filemap_write_and_wait(btree_inode->i_mapping);
204 }
205 dirty_pages = &trans->transaction->dirty_pages;
206 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400207 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
208 EXTENT_DIRTY);
209 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400210 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400211 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
212 while(start <= end) {
213 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400214 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -0400215 page = find_lock_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400216 if (!page)
217 continue;
Chris Mason6702ed42007-08-07 16:15:09 -0400218 if (PageWriteback(page)) {
219 if (PageDirty(page))
220 wait_on_page_writeback(page);
221 else {
222 unlock_page(page);
223 page_cache_release(page);
224 continue;
225 }
226 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400227 err = write_one_page(page, 0);
228 if (err)
229 werr = err;
230 page_cache_release(page);
231 }
232 }
233 err = filemap_fdatawait(btree_inode->i_mapping);
234 if (err)
235 werr = err;
236 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400237}
238
Chris Mason0b86a832008-03-24 15:01:56 -0400239static int update_cowonly_root(struct btrfs_trans_handle *trans,
240 struct btrfs_root *root)
241{
242 int ret;
243 u64 old_root_bytenr;
244 struct btrfs_root *tree_root = root->fs_info->tree_root;
245
246 btrfs_write_dirty_block_groups(trans, root);
247 while(1) {
248 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
249 if (old_root_bytenr == root->node->start)
250 break;
251 btrfs_set_root_bytenr(&root->root_item,
252 root->node->start);
253 btrfs_set_root_level(&root->root_item,
254 btrfs_header_level(root->node));
255 ret = btrfs_update_root(trans, tree_root,
256 &root->root_key,
257 &root->root_item);
258 BUG_ON(ret);
259 btrfs_write_dirty_block_groups(trans, root);
260 }
261 return 0;
262}
263
Chris Mason79154b12007-03-22 15:59:16 -0400264int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
265 struct btrfs_root *root)
266{
Chris Mason79154b12007-03-22 15:59:16 -0400267 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400268 struct list_head *next;
Chris Mason79154b12007-03-22 15:59:16 -0400269
Chris Mason0b86a832008-03-24 15:01:56 -0400270 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
271 next = fs_info->dirty_cowonly_roots.next;
272 list_del_init(next);
273 root = list_entry(next, struct btrfs_root, dirty_list);
274 update_cowonly_root(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400275 }
276 return 0;
277}
278
Chris Mason0f7d52f2007-04-09 10:42:37 -0400279struct dirty_root {
280 struct list_head list;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400281 struct btrfs_root *root;
Josef Bacik58176a92007-08-29 15:47:34 -0400282 struct btrfs_root *latest_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400283};
284
Chris Mason5ce14bb2007-09-11 11:15:39 -0400285int btrfs_add_dead_root(struct btrfs_root *root,
286 struct btrfs_root *latest,
287 struct list_head *dead_list)
Chris Mason5eda7b52007-06-22 14:16:25 -0400288{
289 struct dirty_root *dirty;
290
291 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
292 if (!dirty)
293 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400294 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400295 dirty->latest_root = latest;
Chris Mason5eda7b52007-06-22 14:16:25 -0400296 list_add(&dirty->list, dead_list);
297 return 0;
298}
299
Chris Mason80b67942008-02-01 16:35:04 -0500300static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
301 struct radix_tree_root *radix,
302 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400303{
304 struct dirty_root *dirty;
305 struct btrfs_root *gang[8];
306 struct btrfs_root *root;
307 int i;
308 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400309 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400310 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400311
Chris Mason0f7d52f2007-04-09 10:42:37 -0400312 while(1) {
313 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
314 ARRAY_SIZE(gang),
315 BTRFS_ROOT_TRANS_TAG);
316 if (ret == 0)
317 break;
318 for (i = 0; i < ret; i++) {
319 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400320 radix_tree_tag_clear(radix,
321 (unsigned long)root->root_key.objectid,
322 BTRFS_ROOT_TRANS_TAG);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400323 if (root->commit_root == root->node) {
Chris Masondb945352007-10-15 16:15:53 -0400324 WARN_ON(root->node->start !=
325 btrfs_root_bytenr(&root->root_item));
Chris Mason5f39d392007-10-15 16:14:19 -0400326 free_extent_buffer(root->commit_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400327 root->commit_root = NULL;
Josef Bacik58176a92007-08-29 15:47:34 -0400328
329 /* make sure to update the root on disk
330 * so we get any updates to the block used
331 * counts
332 */
333 err = btrfs_update_root(trans,
334 root->fs_info->tree_root,
335 &root->root_key,
336 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400337 continue;
338 }
339 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
340 BUG_ON(!dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400341 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
342 BUG_ON(!dirty->root);
343
344 memset(&root->root_item.drop_progress, 0,
345 sizeof(struct btrfs_disk_key));
346 root->root_item.drop_level = 0;
347
348 memcpy(dirty->root, root, sizeof(*root));
349 dirty->root->node = root->commit_root;
Josef Bacik58176a92007-08-29 15:47:34 -0400350 dirty->latest_root = root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400351 root->commit_root = NULL;
Chris Mason5eda7b52007-06-22 14:16:25 -0400352
Chris Mason0f7d52f2007-04-09 10:42:37 -0400353 root->root_key.offset = root->fs_info->generation;
Chris Masondb945352007-10-15 16:15:53 -0400354 btrfs_set_root_bytenr(&root->root_item,
355 root->node->start);
356 btrfs_set_root_level(&root->root_item,
357 btrfs_header_level(root->node));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400358 err = btrfs_insert_root(trans, root->fs_info->tree_root,
359 &root->root_key,
360 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400361 if (err)
362 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400363
364 refs = btrfs_root_refs(&dirty->root->root_item);
365 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400366 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400367 &dirty->root->root_key,
368 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400369
370 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400371 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400372 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400373 } else {
374 WARN_ON(1);
375 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400376 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400377 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400378 }
379 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400380 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400381}
382
Chris Masone9d0b132007-08-10 14:06:19 -0400383int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
384{
385 struct btrfs_fs_info *info = root->fs_info;
386 int ret;
387 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400388 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400389
Chris Masona2135012008-06-25 16:01:30 -0400390 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400391 if (root->defrag_running)
392 return 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400393 trans = btrfs_start_transaction(root, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400394 while (1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400395 root->defrag_running = 1;
396 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400397 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400398 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400399 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400400 cond_resched();
401
Chris Masone9d0b132007-08-10 14:06:19 -0400402 trans = btrfs_start_transaction(root, 1);
Chris Mason3f157a22008-06-25 16:01:31 -0400403 if (root->fs_info->closing || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400404 break;
405 }
406 root->defrag_running = 0;
Chris Masona2135012008-06-25 16:01:30 -0400407 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400408 btrfs_end_transaction(trans, root);
409 return 0;
410}
411
Chris Mason80b67942008-02-01 16:35:04 -0500412static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
413 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400414{
415 struct dirty_root *dirty;
416 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400417 unsigned long nr;
Chris Masondb945352007-10-15 16:15:53 -0400418 u64 num_bytes;
419 u64 bytes_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400420 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400421 int err;
422
Chris Mason0f7d52f2007-04-09 10:42:37 -0400423 while(!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400424 struct btrfs_root *root;
425
Chris Mason0f7d52f2007-04-09 10:42:37 -0400426 dirty = list_entry(list->next, struct dirty_root, list);
427 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400428
Chris Masondb945352007-10-15 16:15:53 -0400429 num_bytes = btrfs_root_used(&dirty->root->root_item);
Josef Bacik58176a92007-08-29 15:47:34 -0400430 root = dirty->latest_root;
Chris Masona2135012008-06-25 16:01:30 -0400431 atomic_inc(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400432
Chris Masona2135012008-06-25 16:01:30 -0400433 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400434 while(1) {
435 trans = btrfs_start_transaction(tree_root, 1);
436 ret = btrfs_drop_snapshot(trans, dirty->root);
437 if (ret != -EAGAIN) {
438 break;
439 }
Josef Bacik58176a92007-08-29 15:47:34 -0400440
Chris Mason9f3a7422007-08-07 15:52:19 -0400441 err = btrfs_update_root(trans,
442 tree_root,
443 &dirty->root->root_key,
444 &dirty->root->root_item);
445 if (err)
446 ret = err;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400447 nr = trans->blocks_used;
Chris Mason1b1e2132008-06-25 16:01:31 -0400448 ret = btrfs_end_transaction_throttle(trans, tree_root);
Chris Mason9f3a7422007-08-07 15:52:19 -0400449 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400450
451 mutex_unlock(&root->fs_info->drop_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400452 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400453 cond_resched();
Chris Masona2135012008-06-25 16:01:30 -0400454 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400455 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400456 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400457 atomic_dec(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400458
Chris Masona2135012008-06-25 16:01:30 -0400459 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masondb945352007-10-15 16:15:53 -0400460 num_bytes -= btrfs_root_used(&dirty->root->root_item);
461 bytes_used = btrfs_root_used(&root->root_item);
462 if (num_bytes) {
Josef Bacik58176a92007-08-29 15:47:34 -0400463 record_root_in_trans(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400464 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400465 bytes_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400466 }
Chris Masona2135012008-06-25 16:01:30 -0400467 mutex_unlock(&root->fs_info->alloc_mutex);
468
Chris Mason9f3a7422007-08-07 15:52:19 -0400469 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400470 if (ret) {
471 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400472 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400473 }
Chris Masona2135012008-06-25 16:01:30 -0400474 mutex_unlock(&root->fs_info->drop_mutex);
475
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400476 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400477 ret = btrfs_end_transaction(trans, tree_root);
478 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400479
Chris Masonf510cfe2007-10-15 16:14:48 -0400480 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400481 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400482 kfree(dirty);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400483
484 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400485 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400486 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400487 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400488}
489
Chris Mason80b67942008-02-01 16:35:04 -0500490static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500491 struct btrfs_fs_info *fs_info,
492 struct btrfs_pending_snapshot *pending)
493{
494 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500495 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500496 struct btrfs_root *tree_root = fs_info->tree_root;
497 struct btrfs_root *root = pending->root;
498 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400499 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500500 int ret;
Sven Wegener3b963622008-06-09 21:57:42 -0400501 int namelen;
Chris Mason3063d292008-01-08 15:46:30 -0500502 u64 objectid;
503
Chris Mason80b67942008-02-01 16:35:04 -0500504 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
505 if (!new_root_item) {
506 ret = -ENOMEM;
507 goto fail;
508 }
Chris Mason3063d292008-01-08 15:46:30 -0500509 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
510 if (ret)
511 goto fail;
512
Chris Mason80b67942008-02-01 16:35:04 -0500513 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Chris Mason3063d292008-01-08 15:46:30 -0500514
515 key.objectid = objectid;
516 key.offset = 1;
517 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
518
Chris Mason925baed2008-06-25 16:01:30 -0400519 old = btrfs_lock_root_node(root);
520 btrfs_cow_block(trans, root, old, NULL, 0, &old);
Chris Mason3063d292008-01-08 15:46:30 -0500521
Chris Mason925baed2008-06-25 16:01:30 -0400522 btrfs_copy_root(trans, root, old, &tmp, objectid);
523 btrfs_tree_unlock(old);
524 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -0500525
Chris Mason80b67942008-02-01 16:35:04 -0500526 btrfs_set_root_bytenr(new_root_item, tmp->start);
527 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
Chris Mason3063d292008-01-08 15:46:30 -0500528 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
Chris Mason80b67942008-02-01 16:35:04 -0500529 new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -0400530 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -0500531 free_extent_buffer(tmp);
532 if (ret)
533 goto fail;
534
535 /*
536 * insert the directory item
537 */
538 key.offset = (u64)-1;
Sven Wegener3b963622008-06-09 21:57:42 -0400539 namelen = strlen(pending->name);
Chris Mason3063d292008-01-08 15:46:30 -0500540 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
Sven Wegener3b963622008-06-09 21:57:42 -0400541 pending->name, namelen,
Chris Mason3063d292008-01-08 15:46:30 -0500542 root->fs_info->sb->s_root->d_inode->i_ino,
543 &key, BTRFS_FT_DIR);
544
545 if (ret)
546 goto fail;
547
548 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
549 pending->name, strlen(pending->name), objectid,
550 root->fs_info->sb->s_root->d_inode->i_ino);
Sven Wegener3b963622008-06-09 21:57:42 -0400551
552 /* Invalidate existing dcache entry for new snapshot. */
553 btrfs_invalidate_dcache_root(root, pending->name, namelen);
554
Chris Mason3063d292008-01-08 15:46:30 -0500555fail:
Chris Mason80b67942008-02-01 16:35:04 -0500556 kfree(new_root_item);
Chris Mason3063d292008-01-08 15:46:30 -0500557 return ret;
558}
559
Chris Mason80b67942008-02-01 16:35:04 -0500560static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
561 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -0500562{
563 struct btrfs_pending_snapshot *pending;
564 struct list_head *head = &trans->transaction->pending_snapshots;
565 int ret;
566
567 while(!list_empty(head)) {
568 pending = list_entry(head->next,
569 struct btrfs_pending_snapshot, list);
570 ret = create_pending_snapshot(trans, fs_info, pending);
571 BUG_ON(ret);
572 list_del(&pending->list);
573 kfree(pending->name);
574 kfree(pending);
575 }
Chris Masondc17ff82008-01-08 15:46:30 -0500576 return 0;
577}
578
Chris Mason79154b12007-03-22 15:59:16 -0400579int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
580 struct btrfs_root *root)
581{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400582 unsigned long joined = 0;
583 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400584 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400585 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400586 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400587 struct list_head dirty_fs_roots;
Chris Masond1310b22008-01-24 16:13:08 -0500588 struct extent_io_tree *pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400589 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400590 int ret;
Chris Mason79154b12007-03-22 15:59:16 -0400591
Chris Mason0f7d52f2007-04-09 10:42:37 -0400592 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Masond6e4a422007-04-06 15:37:36 -0400593
Chris Mason79154b12007-03-22 15:59:16 -0400594 mutex_lock(&root->fs_info->trans_mutex);
595 if (trans->transaction->in_commit) {
596 cur_trans = trans->transaction;
597 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400598 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400599 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400600
Chris Mason79154b12007-03-22 15:59:16 -0400601 ret = wait_for_commit(root, cur_trans);
602 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400603
604 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400605 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400606 mutex_unlock(&root->fs_info->trans_mutex);
607
Chris Mason79154b12007-03-22 15:59:16 -0400608 return 0;
609 }
Chris Mason4313b392008-01-03 09:08:48 -0500610
611 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
612 if (!pinned_copy)
613 return -ENOMEM;
614
Chris Masond1310b22008-01-24 16:13:08 -0500615 extent_io_tree_init(pinned_copy,
Chris Mason4313b392008-01-03 09:08:48 -0500616 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
617
Chris Masone6dcd2d2008-07-17 12:53:50 -0400618printk("commit trans %Lu\n", trans->transid);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400619 trans->transaction->in_commit = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400620 cur_trans = trans->transaction;
621 if (cur_trans->list.prev != &root->fs_info->trans_list) {
622 prev_trans = list_entry(cur_trans->list.prev,
623 struct btrfs_transaction, list);
624 if (!prev_trans->commit_done) {
625 prev_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400626 mutex_unlock(&root->fs_info->trans_mutex);
627
628 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400629
Chris Masonccd467d2007-06-28 15:57:36 -0400630 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400631 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400632 }
633 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400634
635 do {
636 joined = cur_trans->num_joined;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400637 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400638 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -0400639 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400640
641 if (cur_trans->num_writers > 1)
642 timeout = MAX_SCHEDULE_TIMEOUT;
643 else
644 timeout = 1;
645
Chris Mason79154b12007-03-22 15:59:16 -0400646 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400647
648 schedule_timeout(timeout);
649
Chris Mason79154b12007-03-22 15:59:16 -0400650 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400651 finish_wait(&cur_trans->writer_wait, &wait);
652 } while (cur_trans->num_writers > 1 ||
653 (cur_trans->num_joined != joined));
654
Chris Mason3063d292008-01-08 15:46:30 -0500655 ret = create_pending_snapshots(trans, root->fs_info);
656 BUG_ON(ret);
657
Chris Mason2c90e5d2007-04-02 10:50:19 -0400658 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -0500659
Chris Mason54aa1f42007-06-22 14:16:25 -0400660 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
661 &dirty_fs_roots);
662 BUG_ON(ret);
663
Chris Mason79154b12007-03-22 15:59:16 -0400664 ret = btrfs_commit_tree_roots(trans, root);
665 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -0400666
Chris Mason78fae272007-03-25 11:35:08 -0400667 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -0500668 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -0400669 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -0500670 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason4b52dff2007-06-26 10:06:50 -0400671 btrfs_set_super_generation(&root->fs_info->super_copy,
672 cur_trans->transid);
673 btrfs_set_super_root(&root->fs_info->super_copy,
Chris Masondb945352007-10-15 16:15:53 -0400674 root->fs_info->tree_root->node->start);
675 btrfs_set_super_root_level(&root->fs_info->super_copy,
676 btrfs_header_level(root->fs_info->tree_root->node));
Chris Mason5f39d392007-10-15 16:14:19 -0400677
Chris Mason0b86a832008-03-24 15:01:56 -0400678 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
679 chunk_root->node->start);
680 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
681 btrfs_header_level(chunk_root->node));
Chris Masona061fc82008-05-07 11:43:44 -0400682 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
683 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -0400684
Chris Mason4313b392008-01-03 09:08:48 -0500685 btrfs_copy_pinned(root, pinned_copy);
Chris Masonccd467d2007-06-28 15:57:36 -0400686
Chris Masone6dcd2d2008-07-17 12:53:50 -0400687 wake_up(&root->fs_info->transaction_throttle);
688
Chris Mason78fae272007-03-25 11:35:08 -0400689 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400690 ret = btrfs_write_and_wait_transaction(trans, root);
691 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400692 write_ctree_super(trans, root);
Chris Mason4313b392008-01-03 09:08:48 -0500693
Chris Mason4313b392008-01-03 09:08:48 -0500694 btrfs_finish_extent_commit(trans, root, pinned_copy);
Chris Mason78fae272007-03-25 11:35:08 -0400695 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4313b392008-01-03 09:08:48 -0500696
697 kfree(pinned_copy);
698
Chris Mason2c90e5d2007-04-02 10:50:19 -0400699 cur_trans->commit_done = 1;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400700 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400701 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400702 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400703 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -0400704
Chris Masonfacda1e2007-06-08 18:11:48 -0400705 if (root->fs_info->closing)
706 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
707 else
708 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Josef Bacik58176a92007-08-29 15:47:34 -0400709
Chris Mason78fae272007-03-25 11:35:08 -0400710 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400711printk("done commit trans %Lu\n", trans->transid);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400712 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400713
Chris Masonfacda1e2007-06-08 18:11:48 -0400714 if (root->fs_info->closing) {
Chris Masonfacda1e2007-06-08 18:11:48 -0400715 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -0400716 }
Chris Mason79154b12007-03-22 15:59:16 -0400717 return ret;
718}
719
Chris Masone9d0b132007-08-10 14:06:19 -0400720int btrfs_clean_old_snapshots(struct btrfs_root *root)
721{
722 struct list_head dirty_roots;
723 INIT_LIST_HEAD(&dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -0400724again:
Chris Masone9d0b132007-08-10 14:06:19 -0400725 mutex_lock(&root->fs_info->trans_mutex);
726 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
727 mutex_unlock(&root->fs_info->trans_mutex);
728
729 if (!list_empty(&dirty_roots)) {
730 drop_dirty_roots(root, &dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -0400731 goto again;
Chris Masone9d0b132007-08-10 14:06:19 -0400732 }
733 return 0;
734}
Chris Mason08607c12007-06-08 15:33:54 -0400735