blob: 3ed5868e7c0f680edecb2c6bad30232722f68402 [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"
26
Chris Mason78fae272007-03-25 11:35:08 -040027static int total_trans = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -040028extern struct kmem_cache *btrfs_trans_handle_cachep;
29extern struct kmem_cache *btrfs_transaction_cachep;
30
Chris Mason08607c12007-06-08 15:33:54 -040031static struct workqueue_struct *trans_wq;
32
Chris Mason0f7d52f2007-04-09 10:42:37 -040033#define BTRFS_ROOT_TRANS_TAG 0
Chris Mason6702ed42007-08-07 16:15:09 -040034#define BTRFS_ROOT_DEFRAG_TAG 1
Chris Mason0f7d52f2007-04-09 10:42:37 -040035
Chris Mason79154b12007-03-22 15:59:16 -040036static void put_transaction(struct btrfs_transaction *transaction)
37{
Chris Mason2c90e5d2007-04-02 10:50:19 -040038 WARN_ON(transaction->use_count == 0);
Chris Mason79154b12007-03-22 15:59:16 -040039 transaction->use_count--;
Chris Mason78fae272007-03-25 11:35:08 -040040 if (transaction->use_count == 0) {
41 WARN_ON(total_trans == 0);
42 total_trans--;
Chris Mason8fd17792007-04-19 21:01:03 -040043 list_del_init(&transaction->list);
Chris Mason2c90e5d2007-04-02 10:50:19 -040044 memset(transaction, 0, sizeof(*transaction));
45 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040046 }
Chris Mason79154b12007-03-22 15:59:16 -040047}
48
49static int join_transaction(struct btrfs_root *root)
50{
51 struct btrfs_transaction *cur_trans;
52 cur_trans = root->fs_info->running_transaction;
53 if (!cur_trans) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040054 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
55 GFP_NOFS);
Chris Mason78fae272007-03-25 11:35:08 -040056 total_trans++;
Chris Mason79154b12007-03-22 15:59:16 -040057 BUG_ON(!cur_trans);
Chris Mason0f7d52f2007-04-09 10:42:37 -040058 root->fs_info->generation++;
Chris Mason79154b12007-03-22 15:59:16 -040059 root->fs_info->running_transaction = cur_trans;
Josef Bacik15ee9bc2007-08-10 16:22:09 -040060 cur_trans->num_writers = 1;
61 cur_trans->num_joined = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040062 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040063 init_waitqueue_head(&cur_trans->writer_wait);
64 init_waitqueue_head(&cur_trans->commit_wait);
65 cur_trans->in_commit = 0;
Chris Masond5719762007-03-23 10:01:08 -040066 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040067 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040068 cur_trans->start_time = get_seconds();
Chris Mason8fd17792007-04-19 21:01:03 -040069 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
Chris Masondc17ff82008-01-08 15:46:30 -050070 btrfs_ordered_inode_tree_init(&cur_trans->ordered_inode_tree);
Chris Mason5f39d392007-10-15 16:14:19 -040071 extent_map_tree_init(&cur_trans->dirty_pages,
72 root->fs_info->btree_inode->i_mapping,
73 GFP_NOFS);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040074 } else {
75 cur_trans->num_writers++;
76 cur_trans->num_joined++;
Chris Mason79154b12007-03-22 15:59:16 -040077 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -040078
Chris Mason79154b12007-03-22 15:59:16 -040079 return 0;
80}
81
Chris Mason6702ed42007-08-07 16:15:09 -040082static int record_root_in_trans(struct btrfs_root *root)
83{
84 u64 running_trans_id = root->fs_info->running_transaction->transid;
85 if (root->ref_cows && root->last_trans < running_trans_id) {
86 WARN_ON(root == root->fs_info->extent_root);
87 if (root->root_item.refs != 0) {
88 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
89 (unsigned long)root->root_key.objectid,
90 BTRFS_ROOT_TRANS_TAG);
91 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
92 (unsigned long)root->root_key.objectid,
93 BTRFS_ROOT_DEFRAG_TAG);
94 root->commit_root = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -040095 extent_buffer_get(root->node);
Chris Mason6702ed42007-08-07 16:15:09 -040096 } else {
97 WARN_ON(1);
98 }
99 root->last_trans = running_trans_id;
100 }
101 return 0;
102}
103
Chris Mason79154b12007-03-22 15:59:16 -0400104struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
105 int num_blocks)
106{
Chris Mason2c90e5d2007-04-02 10:50:19 -0400107 struct btrfs_trans_handle *h =
108 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
Chris Mason79154b12007-03-22 15:59:16 -0400109 int ret;
110
111 mutex_lock(&root->fs_info->trans_mutex);
112 ret = join_transaction(root);
113 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400114
Chris Mason6702ed42007-08-07 16:15:09 -0400115 record_root_in_trans(root);
116 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400117 h->transaction = root->fs_info->running_transaction;
118 h->blocks_reserved = num_blocks;
119 h->blocks_used = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400120 h->block_group = NULL;
Chris Mason26b80032007-08-08 20:17:12 -0400121 h->alloc_exclude_nr = 0;
122 h->alloc_exclude_start = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400123 root->fs_info->running_transaction->use_count++;
124 mutex_unlock(&root->fs_info->trans_mutex);
125 return h;
126}
127
128int btrfs_end_transaction(struct btrfs_trans_handle *trans,
129 struct btrfs_root *root)
130{
131 struct btrfs_transaction *cur_trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400132
Chris Mason79154b12007-03-22 15:59:16 -0400133 mutex_lock(&root->fs_info->trans_mutex);
134 cur_trans = root->fs_info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400135 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400136 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400137 cur_trans->num_writers--;
Chris Mason79154b12007-03-22 15:59:16 -0400138 if (waitqueue_active(&cur_trans->writer_wait))
139 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400140 put_transaction(cur_trans);
141 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400142 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400143 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400144 return 0;
145}
146
147
148int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
149 struct btrfs_root *root)
150{
Chris Mason7c4452b2007-04-28 09:29:35 -0400151 int ret;
Chris Mason7c4452b2007-04-28 09:29:35 -0400152 int err;
153 int werr = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400154 struct extent_map_tree *dirty_pages;
Chris Mason7c4452b2007-04-28 09:29:35 -0400155 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400156 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason5f39d392007-10-15 16:14:19 -0400157 u64 start;
158 u64 end;
159 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400160
161 if (!trans || !trans->transaction) {
162 return filemap_write_and_wait(btree_inode->i_mapping);
163 }
164 dirty_pages = &trans->transaction->dirty_pages;
165 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400166 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
167 EXTENT_DIRTY);
168 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400169 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400170 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
171 while(start <= end) {
172 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400173 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -0400174 page = find_lock_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400175 if (!page)
176 continue;
Chris Mason6702ed42007-08-07 16:15:09 -0400177 if (PageWriteback(page)) {
178 if (PageDirty(page))
179 wait_on_page_writeback(page);
180 else {
181 unlock_page(page);
182 page_cache_release(page);
183 continue;
184 }
185 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400186 err = write_one_page(page, 0);
187 if (err)
188 werr = err;
189 page_cache_release(page);
190 }
191 }
192 err = filemap_fdatawait(btree_inode->i_mapping);
193 if (err)
194 werr = err;
195 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400196}
197
198int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
199 struct btrfs_root *root)
200{
201 int ret;
202 u64 old_extent_block;
203 struct btrfs_fs_info *fs_info = root->fs_info;
204 struct btrfs_root *tree_root = fs_info->tree_root;
205 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Mason79154b12007-03-22 15:59:16 -0400206
Chris Mason9078a3e2007-04-26 16:46:15 -0400207 btrfs_write_dirty_block_groups(trans, extent_root);
Chris Mason79154b12007-03-22 15:59:16 -0400208 while(1) {
Chris Masondb945352007-10-15 16:15:53 -0400209 old_extent_block = btrfs_root_bytenr(&extent_root->root_item);
210 if (old_extent_block == extent_root->node->start)
Chris Mason79154b12007-03-22 15:59:16 -0400211 break;
Chris Masondb945352007-10-15 16:15:53 -0400212 btrfs_set_root_bytenr(&extent_root->root_item,
213 extent_root->node->start);
214 btrfs_set_root_level(&extent_root->root_item,
215 btrfs_header_level(extent_root->node));
Chris Mason79154b12007-03-22 15:59:16 -0400216 ret = btrfs_update_root(trans, tree_root,
217 &extent_root->root_key,
218 &extent_root->root_item);
219 BUG_ON(ret);
Chris Mason9078a3e2007-04-26 16:46:15 -0400220 btrfs_write_dirty_block_groups(trans, extent_root);
Chris Mason79154b12007-03-22 15:59:16 -0400221 }
222 return 0;
223}
224
225static int wait_for_commit(struct btrfs_root *root,
226 struct btrfs_transaction *commit)
227{
228 DEFINE_WAIT(wait);
Chris Masonccd467d2007-06-28 15:57:36 -0400229 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400230 while(!commit->commit_done) {
231 prepare_to_wait(&commit->commit_wait, &wait,
232 TASK_UNINTERRUPTIBLE);
233 if (commit->commit_done)
234 break;
235 mutex_unlock(&root->fs_info->trans_mutex);
236 schedule();
237 mutex_lock(&root->fs_info->trans_mutex);
238 }
Chris Masonccd467d2007-06-28 15:57:36 -0400239 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400240 finish_wait(&commit->commit_wait, &wait);
241 return 0;
242}
243
Chris Mason0f7d52f2007-04-09 10:42:37 -0400244struct dirty_root {
245 struct list_head list;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400246 struct btrfs_root *root;
Josef Bacik58176a92007-08-29 15:47:34 -0400247 struct btrfs_root *latest_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400248};
249
Chris Mason5ce14bb2007-09-11 11:15:39 -0400250int btrfs_add_dead_root(struct btrfs_root *root,
251 struct btrfs_root *latest,
252 struct list_head *dead_list)
Chris Mason5eda7b52007-06-22 14:16:25 -0400253{
254 struct dirty_root *dirty;
255
256 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
257 if (!dirty)
258 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400259 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400260 dirty->latest_root = latest;
Chris Mason5eda7b52007-06-22 14:16:25 -0400261 list_add(&dirty->list, dead_list);
262 return 0;
263}
264
Chris Mason35b7e472007-05-02 15:53:43 -0400265static int add_dirty_roots(struct btrfs_trans_handle *trans,
266 struct radix_tree_root *radix,
267 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400268{
269 struct dirty_root *dirty;
270 struct btrfs_root *gang[8];
271 struct btrfs_root *root;
272 int i;
273 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400274 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400275 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400276
Chris Mason0f7d52f2007-04-09 10:42:37 -0400277 while(1) {
278 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
279 ARRAY_SIZE(gang),
280 BTRFS_ROOT_TRANS_TAG);
281 if (ret == 0)
282 break;
283 for (i = 0; i < ret; i++) {
284 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400285 radix_tree_tag_clear(radix,
286 (unsigned long)root->root_key.objectid,
287 BTRFS_ROOT_TRANS_TAG);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400288 if (root->commit_root == root->node) {
Chris Masondb945352007-10-15 16:15:53 -0400289 WARN_ON(root->node->start !=
290 btrfs_root_bytenr(&root->root_item));
Chris Mason5f39d392007-10-15 16:14:19 -0400291 free_extent_buffer(root->commit_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400292 root->commit_root = NULL;
Josef Bacik58176a92007-08-29 15:47:34 -0400293
294 /* make sure to update the root on disk
295 * so we get any updates to the block used
296 * counts
297 */
298 err = btrfs_update_root(trans,
299 root->fs_info->tree_root,
300 &root->root_key,
301 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400302 continue;
303 }
304 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
305 BUG_ON(!dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400306 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
307 BUG_ON(!dirty->root);
308
309 memset(&root->root_item.drop_progress, 0,
310 sizeof(struct btrfs_disk_key));
311 root->root_item.drop_level = 0;
312
313 memcpy(dirty->root, root, sizeof(*root));
314 dirty->root->node = root->commit_root;
Josef Bacik58176a92007-08-29 15:47:34 -0400315 dirty->latest_root = root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400316 root->commit_root = NULL;
Chris Mason5eda7b52007-06-22 14:16:25 -0400317
Chris Mason0f7d52f2007-04-09 10:42:37 -0400318 root->root_key.offset = root->fs_info->generation;
Chris Masondb945352007-10-15 16:15:53 -0400319 btrfs_set_root_bytenr(&root->root_item,
320 root->node->start);
321 btrfs_set_root_level(&root->root_item,
322 btrfs_header_level(root->node));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400323 err = btrfs_insert_root(trans, root->fs_info->tree_root,
324 &root->root_key,
325 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400326 if (err)
327 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400328
329 refs = btrfs_root_refs(&dirty->root->root_item);
330 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400331 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400332 &dirty->root->root_key,
333 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400334
335 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400336 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400337 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400338 } else {
339 WARN_ON(1);
340 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400341 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400342 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400343 }
344 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400345 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400346}
347
Chris Masone9d0b132007-08-10 14:06:19 -0400348int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
349{
350 struct btrfs_fs_info *info = root->fs_info;
351 int ret;
352 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400353 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400354
355 if (root->defrag_running)
356 return 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400357 trans = btrfs_start_transaction(root, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400358 while (1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400359 root->defrag_running = 1;
360 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400361 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400362 btrfs_end_transaction(trans, root);
363 mutex_unlock(&info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400364 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400365 cond_resched();
366
367 mutex_lock(&info->fs_mutex);
368 trans = btrfs_start_transaction(root, 1);
369 if (ret != -EAGAIN)
370 break;
371 }
372 root->defrag_running = 0;
373 radix_tree_tag_clear(&info->fs_roots_radix,
374 (unsigned long)root->root_key.objectid,
375 BTRFS_ROOT_DEFRAG_TAG);
376 btrfs_end_transaction(trans, root);
377 return 0;
378}
379
Chris Mason6702ed42007-08-07 16:15:09 -0400380int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
381{
382 struct btrfs_root *gang[1];
383 struct btrfs_root *root;
Chris Mason6702ed42007-08-07 16:15:09 -0400384 int i;
385 int ret;
386 int err = 0;
387 u64 last = 0;
388
Chris Mason6702ed42007-08-07 16:15:09 -0400389 while(1) {
390 ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix,
391 (void **)gang, last,
392 ARRAY_SIZE(gang),
393 BTRFS_ROOT_DEFRAG_TAG);
394 if (ret == 0)
395 break;
396 for (i = 0; i < ret; i++) {
397 root = gang[i];
398 last = root->root_key.objectid + 1;
Chris Masonf510cfe2007-10-15 16:14:48 -0400399 btrfs_defrag_root(root, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400400 }
401 }
Chris Mason6b800532007-10-15 16:17:34 -0400402 btrfs_defrag_root(info->extent_root, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400403 return err;
404}
405
Chris Mason35b7e472007-05-02 15:53:43 -0400406static int drop_dirty_roots(struct btrfs_root *tree_root,
407 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400408{
409 struct dirty_root *dirty;
410 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400411 unsigned long nr;
Chris Masondb945352007-10-15 16:15:53 -0400412 u64 num_bytes;
413 u64 bytes_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400414 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400415 int err;
416
Chris Mason0f7d52f2007-04-09 10:42:37 -0400417 while(!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400418 struct btrfs_root *root;
419
Chris Masonfacda1e2007-06-08 18:11:48 -0400420 mutex_lock(&tree_root->fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400421 dirty = list_entry(list->next, struct dirty_root, list);
422 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400423
Chris Masondb945352007-10-15 16:15:53 -0400424 num_bytes = btrfs_root_used(&dirty->root->root_item);
Josef Bacik58176a92007-08-29 15:47:34 -0400425 root = dirty->latest_root;
426
Chris Mason9f3a7422007-08-07 15:52:19 -0400427 while(1) {
428 trans = btrfs_start_transaction(tree_root, 1);
429 ret = btrfs_drop_snapshot(trans, dirty->root);
430 if (ret != -EAGAIN) {
431 break;
432 }
Josef Bacik58176a92007-08-29 15:47:34 -0400433
Chris Mason9f3a7422007-08-07 15:52:19 -0400434 err = btrfs_update_root(trans,
435 tree_root,
436 &dirty->root->root_key,
437 &dirty->root->root_item);
438 if (err)
439 ret = err;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400440 nr = trans->blocks_used;
Chris Mason9f3a7422007-08-07 15:52:19 -0400441 ret = btrfs_end_transaction(trans, tree_root);
442 BUG_ON(ret);
Chris Masonf4468e92007-08-08 10:08:58 -0400443 mutex_unlock(&tree_root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400444 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400445 cond_resched();
Chris Masonf4468e92007-08-08 10:08:58 -0400446 mutex_lock(&tree_root->fs_info->fs_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400447 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400448 BUG_ON(ret);
Josef Bacik58176a92007-08-29 15:47:34 -0400449
Chris Masondb945352007-10-15 16:15:53 -0400450 num_bytes -= btrfs_root_used(&dirty->root->root_item);
451 bytes_used = btrfs_root_used(&root->root_item);
452 if (num_bytes) {
Josef Bacik58176a92007-08-29 15:47:34 -0400453 record_root_in_trans(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400454 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400455 bytes_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400456 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400457 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400458 if (ret) {
459 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400460 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400461 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400462 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400463 ret = btrfs_end_transaction(trans, tree_root);
464 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400465
Chris Masonf510cfe2007-10-15 16:14:48 -0400466 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400467 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400468 kfree(dirty);
Chris Masonfacda1e2007-06-08 18:11:48 -0400469 mutex_unlock(&tree_root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400470
471 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400472 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400473 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400474 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400475}
476
Chris Masondc17ff82008-01-08 15:46:30 -0500477int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans,
478 struct btrfs_root *root)
479{
480 struct btrfs_transaction *cur_trans = trans->transaction;
481 struct inode *inode;
482 u64 root_objectid = 0;
483 u64 objectid = 0;
484 u64 transid = trans->transid;
485 int ret;
486
487printk("write ordered trans %Lu\n", transid);
488 while(1) {
489 ret = btrfs_find_first_ordered_inode(
490 &cur_trans->ordered_inode_tree,
491 &root_objectid, &objectid);
492 if (!ret)
493 break;
494
495 mutex_unlock(&root->fs_info->trans_mutex);
496 mutex_unlock(&root->fs_info->fs_mutex);
497 inode = btrfs_ilookup(root->fs_info->sb, objectid,
498 root_objectid);
499 if (inode) {
500 if (S_ISREG(inode->i_mode))
501 filemap_fdatawrite(inode->i_mapping);
502 iput(inode);
503 }
504 mutex_lock(&root->fs_info->fs_mutex);
505 mutex_lock(&root->fs_info->trans_mutex);
506 }
507 while(1) {
508 root_objectid = 0;
509 objectid = 0;
510 ret = btrfs_find_del_first_ordered_inode(
511 &cur_trans->ordered_inode_tree,
512 &root_objectid, &objectid);
513 if (!ret)
514 break;
515 mutex_unlock(&root->fs_info->trans_mutex);
516 mutex_unlock(&root->fs_info->fs_mutex);
517 inode = btrfs_ilookup(root->fs_info->sb, objectid,
518 root_objectid);
519 if (inode) {
520 if (S_ISREG(inode->i_mode))
521 filemap_write_and_wait(inode->i_mapping);
522 iput(inode);
523 }
524 mutex_lock(&root->fs_info->fs_mutex);
525 mutex_lock(&root->fs_info->trans_mutex);
526 }
527printk("done write ordered trans %Lu\n", transid);
528 return 0;
529}
530
Chris Mason79154b12007-03-22 15:59:16 -0400531int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
532 struct btrfs_root *root)
533{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400534 unsigned long joined = 0;
535 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400536 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400537 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400538 struct list_head dirty_fs_roots;
Chris Mason4313b392008-01-03 09:08:48 -0500539 struct extent_map_tree *pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400540 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400541 int ret;
Chris Mason79154b12007-03-22 15:59:16 -0400542
Chris Mason0f7d52f2007-04-09 10:42:37 -0400543 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Masond6e4a422007-04-06 15:37:36 -0400544
Chris Mason79154b12007-03-22 15:59:16 -0400545 mutex_lock(&root->fs_info->trans_mutex);
546 if (trans->transaction->in_commit) {
547 cur_trans = trans->transaction;
548 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400549 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400550 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400551
552 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400553 ret = wait_for_commit(root, cur_trans);
554 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400555
556 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400557 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400558 mutex_unlock(&root->fs_info->trans_mutex);
559
Chris Masonccd467d2007-06-28 15:57:36 -0400560 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400561 return 0;
562 }
Chris Mason4313b392008-01-03 09:08:48 -0500563
564 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
565 if (!pinned_copy)
566 return -ENOMEM;
567
568 extent_map_tree_init(pinned_copy,
569 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
570
Chris Mason2c90e5d2007-04-02 10:50:19 -0400571 trans->transaction->in_commit = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400572 cur_trans = trans->transaction;
573 if (cur_trans->list.prev != &root->fs_info->trans_list) {
574 prev_trans = list_entry(cur_trans->list.prev,
575 struct btrfs_transaction, list);
576 if (!prev_trans->commit_done) {
577 prev_trans->use_count++;
578 mutex_unlock(&root->fs_info->fs_mutex);
579 mutex_unlock(&root->fs_info->trans_mutex);
580
581 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400582
583 mutex_lock(&root->fs_info->fs_mutex);
584 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400585 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400586 }
587 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400588
589 do {
590 joined = cur_trans->num_joined;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400591 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400592 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -0400593 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400594
595 if (cur_trans->num_writers > 1)
596 timeout = MAX_SCHEDULE_TIMEOUT;
597 else
598 timeout = 1;
599
Chris Masonccd467d2007-06-28 15:57:36 -0400600 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400601 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400602
603 schedule_timeout(timeout);
604
Chris Masonccd467d2007-06-28 15:57:36 -0400605 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400606 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400607 finish_wait(&cur_trans->writer_wait, &wait);
Chris Masondc17ff82008-01-08 15:46:30 -0500608 ret = btrfs_write_ordered_inodes(trans, root);
609
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400610 } while (cur_trans->num_writers > 1 ||
611 (cur_trans->num_joined != joined));
612
Chris Mason2c90e5d2007-04-02 10:50:19 -0400613 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -0500614
Chris Mason54aa1f42007-06-22 14:16:25 -0400615 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
616 &dirty_fs_roots);
617 BUG_ON(ret);
618
Chris Mason79154b12007-03-22 15:59:16 -0400619 ret = btrfs_commit_tree_roots(trans, root);
620 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -0400621
Chris Mason78fae272007-03-25 11:35:08 -0400622 cur_trans = root->fs_info->running_transaction;
623 root->fs_info->running_transaction = NULL;
Chris Mason4b52dff2007-06-26 10:06:50 -0400624 btrfs_set_super_generation(&root->fs_info->super_copy,
625 cur_trans->transid);
626 btrfs_set_super_root(&root->fs_info->super_copy,
Chris Masondb945352007-10-15 16:15:53 -0400627 root->fs_info->tree_root->node->start);
628 btrfs_set_super_root_level(&root->fs_info->super_copy,
629 btrfs_header_level(root->fs_info->tree_root->node));
Chris Mason5f39d392007-10-15 16:14:19 -0400630
631 write_extent_buffer(root->fs_info->sb_buffer,
632 &root->fs_info->super_copy, 0,
633 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -0400634
Chris Mason4313b392008-01-03 09:08:48 -0500635 btrfs_copy_pinned(root, pinned_copy);
Chris Masonccd467d2007-06-28 15:57:36 -0400636
Chris Mason78fae272007-03-25 11:35:08 -0400637 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason8fd17792007-04-19 21:01:03 -0400638 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400639 ret = btrfs_write_and_wait_transaction(trans, root);
640 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400641 write_ctree_super(trans, root);
Chris Mason4313b392008-01-03 09:08:48 -0500642
Chris Mason8fd17792007-04-19 21:01:03 -0400643 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason4313b392008-01-03 09:08:48 -0500644 btrfs_finish_extent_commit(trans, root, pinned_copy);
Chris Mason78fae272007-03-25 11:35:08 -0400645 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4313b392008-01-03 09:08:48 -0500646
647 kfree(pinned_copy);
648
Chris Mason2c90e5d2007-04-02 10:50:19 -0400649 cur_trans->commit_done = 1;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400650 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400651 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400652 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400653 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -0400654
Chris Masonfacda1e2007-06-08 18:11:48 -0400655 if (root->fs_info->closing)
656 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
657 else
658 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Josef Bacik58176a92007-08-29 15:47:34 -0400659
Chris Mason78fae272007-03-25 11:35:08 -0400660 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400661 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400662
Chris Masonfacda1e2007-06-08 18:11:48 -0400663 if (root->fs_info->closing) {
664 mutex_unlock(&root->fs_info->fs_mutex);
665 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
666 mutex_lock(&root->fs_info->fs_mutex);
667 }
Chris Mason79154b12007-03-22 15:59:16 -0400668 return ret;
669}
670
Chris Masone9d0b132007-08-10 14:06:19 -0400671int btrfs_clean_old_snapshots(struct btrfs_root *root)
672{
673 struct list_head dirty_roots;
674 INIT_LIST_HEAD(&dirty_roots);
675
676 mutex_lock(&root->fs_info->trans_mutex);
677 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
678 mutex_unlock(&root->fs_info->trans_mutex);
679
680 if (!list_empty(&dirty_roots)) {
681 drop_dirty_roots(root, &dirty_roots);
682 }
683 return 0;
684}
Chris Mason6da6aba2007-12-18 16:15:09 -0500685#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
686void btrfs_transaction_cleaner(void *p)
687#else
Chris Mason08607c12007-06-08 15:33:54 -0400688void btrfs_transaction_cleaner(struct work_struct *work)
Chris Mason6da6aba2007-12-18 16:15:09 -0500689#endif
Chris Mason08607c12007-06-08 15:33:54 -0400690{
Chris Mason6da6aba2007-12-18 16:15:09 -0500691#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
692 struct btrfs_fs_info *fs_info = p;
693#else
Chris Mason08607c12007-06-08 15:33:54 -0400694 struct btrfs_fs_info *fs_info = container_of(work,
695 struct btrfs_fs_info,
696 trans_work.work);
697
Chris Mason6da6aba2007-12-18 16:15:09 -0500698#endif
Chris Mason08607c12007-06-08 15:33:54 -0400699 struct btrfs_root *root = fs_info->tree_root;
700 struct btrfs_transaction *cur;
701 struct btrfs_trans_handle *trans;
702 unsigned long now;
703 unsigned long delay = HZ * 30;
704 int ret;
705
Chris Mason08607c12007-06-08 15:33:54 -0400706 mutex_lock(&root->fs_info->fs_mutex);
707 mutex_lock(&root->fs_info->trans_mutex);
708 cur = root->fs_info->running_transaction;
709 if (!cur) {
710 mutex_unlock(&root->fs_info->trans_mutex);
711 goto out;
712 }
713 now = get_seconds();
714 if (now < cur->start_time || now - cur->start_time < 30) {
715 mutex_unlock(&root->fs_info->trans_mutex);
716 delay = HZ * 5;
717 goto out;
718 }
719 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason6702ed42007-08-07 16:15:09 -0400720 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason08607c12007-06-08 15:33:54 -0400721 trans = btrfs_start_transaction(root, 1);
722 ret = btrfs_commit_transaction(trans, root);
723out:
724 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone9d0b132007-08-10 14:06:19 -0400725 btrfs_clean_old_snapshots(root);
Chris Mason08607c12007-06-08 15:33:54 -0400726 btrfs_transaction_queue_work(root, delay);
727}
728
729void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
730{
731 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
732}
733
734void btrfs_transaction_flush_work(struct btrfs_root *root)
735{
736 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
737 flush_workqueue(trans_wq);
738}
739
740void __init btrfs_init_transaction_sys(void)
741{
742 trans_wq = create_workqueue("btrfs");
743}
744
Christian Hesse17636e02007-12-11 09:25:06 -0500745void btrfs_exit_transaction_sys(void)
Chris Mason08607c12007-06-08 15:33:54 -0400746{
747 destroy_workqueue(trans_wq);
748}
749