blob: 98f422d9ab078feae6ffce372abbfd2c2956ded2 [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 Masondc17ff82008-01-08 15:46:30 -050070 btrfs_ordered_inode_tree_init(&cur_trans->ordered_inode_tree);
Chris Masond1310b22008-01-24 16:13:08 -050071 extent_io_tree_init(&cur_trans->dirty_pages,
Chris Mason5f39d392007-10-15 16:14:19 -040072 root->fs_info->btree_inode->i_mapping,
73 GFP_NOFS);
Chris Mason48ec2cf2008-06-09 09:35:50 -040074 spin_lock(&root->fs_info->new_trans_lock);
75 root->fs_info->running_transaction = cur_trans;
76 spin_unlock(&root->fs_info->new_trans_lock);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040077 } else {
78 cur_trans->num_writers++;
79 cur_trans->num_joined++;
Chris Mason79154b12007-03-22 15:59:16 -040080 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -040081
Chris Mason79154b12007-03-22 15:59:16 -040082 return 0;
83}
84
Chris Mason80b67942008-02-01 16:35:04 -050085static noinline int record_root_in_trans(struct btrfs_root *root)
Chris Mason6702ed42007-08-07 16:15:09 -040086{
87 u64 running_trans_id = root->fs_info->running_transaction->transid;
88 if (root->ref_cows && root->last_trans < running_trans_id) {
89 WARN_ON(root == root->fs_info->extent_root);
90 if (root->root_item.refs != 0) {
91 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
92 (unsigned long)root->root_key.objectid,
93 BTRFS_ROOT_TRANS_TAG);
Chris Mason925baed2008-06-25 16:01:30 -040094 root->commit_root = btrfs_root_node(root);
Chris Mason6702ed42007-08-07 16:15:09 -040095 } else {
96 WARN_ON(1);
97 }
98 root->last_trans = running_trans_id;
99 }
100 return 0;
101}
102
Chris Mason79154b12007-03-22 15:59:16 -0400103struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
104 int num_blocks)
105{
Chris Mason2c90e5d2007-04-02 10:50:19 -0400106 struct btrfs_trans_handle *h =
107 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
Chris Mason79154b12007-03-22 15:59:16 -0400108 int ret;
109
110 mutex_lock(&root->fs_info->trans_mutex);
111 ret = join_transaction(root);
112 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400113
Chris Mason6702ed42007-08-07 16:15:09 -0400114 record_root_in_trans(root);
115 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400116 h->transaction = root->fs_info->running_transaction;
117 h->blocks_reserved = num_blocks;
118 h->blocks_used = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400119 h->block_group = NULL;
Chris Mason26b80032007-08-08 20:17:12 -0400120 h->alloc_exclude_nr = 0;
121 h->alloc_exclude_start = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400122 root->fs_info->running_transaction->use_count++;
123 mutex_unlock(&root->fs_info->trans_mutex);
124 return h;
125}
126
Chris Mason89ce8a62008-06-25 16:01:31 -0400127static noinline int wait_for_commit(struct btrfs_root *root,
128 struct btrfs_transaction *commit)
129{
130 DEFINE_WAIT(wait);
131 mutex_lock(&root->fs_info->trans_mutex);
132 while(!commit->commit_done) {
133 prepare_to_wait(&commit->commit_wait, &wait,
134 TASK_UNINTERRUPTIBLE);
135 if (commit->commit_done)
136 break;
137 mutex_unlock(&root->fs_info->trans_mutex);
138 schedule();
139 mutex_lock(&root->fs_info->trans_mutex);
140 }
141 mutex_unlock(&root->fs_info->trans_mutex);
142 finish_wait(&commit->commit_wait, &wait);
143 return 0;
144}
145
146static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
147 struct btrfs_root *root, int throttle)
Chris Mason79154b12007-03-22 15:59:16 -0400148{
149 struct btrfs_transaction *cur_trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400150
Chris Mason79154b12007-03-22 15:59:16 -0400151 mutex_lock(&root->fs_info->trans_mutex);
152 cur_trans = root->fs_info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400153 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400154 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400155 cur_trans->num_writers--;
Chris Mason89ce8a62008-06-25 16:01:31 -0400156
Chris Mason79154b12007-03-22 15:59:16 -0400157 if (waitqueue_active(&cur_trans->writer_wait))
158 wake_up(&cur_trans->writer_wait);
Chris Mason89ce8a62008-06-25 16:01:31 -0400159
160 if (cur_trans->in_commit && throttle) {
161 int ret;
162 mutex_unlock(&root->fs_info->trans_mutex);
163 ret = wait_for_commit(root, cur_trans);
164 BUG_ON(ret);
165 mutex_lock(&root->fs_info->trans_mutex);
166 }
167
Chris Mason79154b12007-03-22 15:59:16 -0400168 put_transaction(cur_trans);
169 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400170 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400171 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400172 return 0;
173}
174
Chris Mason89ce8a62008-06-25 16:01:31 -0400175int btrfs_end_transaction(struct btrfs_trans_handle *trans,
176 struct btrfs_root *root)
177{
178 return __btrfs_end_transaction(trans, root, 0);
179}
180
181int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
182 struct btrfs_root *root)
183{
184 return __btrfs_end_transaction(trans, root, 1);
185}
186
Chris Mason79154b12007-03-22 15:59:16 -0400187
188int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
189 struct btrfs_root *root)
190{
Chris Mason7c4452b2007-04-28 09:29:35 -0400191 int ret;
Chris Mason7c4452b2007-04-28 09:29:35 -0400192 int err;
193 int werr = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500194 struct extent_io_tree *dirty_pages;
Chris Mason7c4452b2007-04-28 09:29:35 -0400195 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400196 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason5f39d392007-10-15 16:14:19 -0400197 u64 start;
198 u64 end;
199 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400200
201 if (!trans || !trans->transaction) {
202 return filemap_write_and_wait(btree_inode->i_mapping);
203 }
204 dirty_pages = &trans->transaction->dirty_pages;
205 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400206 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
207 EXTENT_DIRTY);
208 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400209 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400210 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
211 while(start <= end) {
212 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400213 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -0400214 page = find_lock_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400215 if (!page)
216 continue;
Chris Mason6702ed42007-08-07 16:15:09 -0400217 if (PageWriteback(page)) {
218 if (PageDirty(page))
219 wait_on_page_writeback(page);
220 else {
221 unlock_page(page);
222 page_cache_release(page);
223 continue;
224 }
225 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400226 err = write_one_page(page, 0);
227 if (err)
228 werr = err;
229 page_cache_release(page);
230 }
231 }
232 err = filemap_fdatawait(btree_inode->i_mapping);
233 if (err)
234 werr = err;
235 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400236}
237
Chris Mason0b86a832008-03-24 15:01:56 -0400238static int update_cowonly_root(struct btrfs_trans_handle *trans,
239 struct btrfs_root *root)
240{
241 int ret;
242 u64 old_root_bytenr;
243 struct btrfs_root *tree_root = root->fs_info->tree_root;
244
245 btrfs_write_dirty_block_groups(trans, root);
246 while(1) {
247 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
248 if (old_root_bytenr == root->node->start)
249 break;
250 btrfs_set_root_bytenr(&root->root_item,
251 root->node->start);
252 btrfs_set_root_level(&root->root_item,
253 btrfs_header_level(root->node));
254 ret = btrfs_update_root(trans, tree_root,
255 &root->root_key,
256 &root->root_item);
257 BUG_ON(ret);
258 btrfs_write_dirty_block_groups(trans, root);
259 }
260 return 0;
261}
262
Chris Mason79154b12007-03-22 15:59:16 -0400263int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
264 struct btrfs_root *root)
265{
Chris Mason79154b12007-03-22 15:59:16 -0400266 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400267 struct list_head *next;
Chris Mason79154b12007-03-22 15:59:16 -0400268
Chris Mason0b86a832008-03-24 15:01:56 -0400269 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
270 next = fs_info->dirty_cowonly_roots.next;
271 list_del_init(next);
272 root = list_entry(next, struct btrfs_root, dirty_list);
273 update_cowonly_root(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400274 }
275 return 0;
276}
277
Chris Mason0f7d52f2007-04-09 10:42:37 -0400278struct dirty_root {
279 struct list_head list;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400280 struct btrfs_root *root;
Josef Bacik58176a92007-08-29 15:47:34 -0400281 struct btrfs_root *latest_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400282};
283
Chris Mason5ce14bb2007-09-11 11:15:39 -0400284int btrfs_add_dead_root(struct btrfs_root *root,
285 struct btrfs_root *latest,
286 struct list_head *dead_list)
Chris Mason5eda7b52007-06-22 14:16:25 -0400287{
288 struct dirty_root *dirty;
289
290 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
291 if (!dirty)
292 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400293 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400294 dirty->latest_root = latest;
Chris Mason5eda7b52007-06-22 14:16:25 -0400295 list_add(&dirty->list, dead_list);
296 return 0;
297}
298
Chris Mason80b67942008-02-01 16:35:04 -0500299static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
300 struct radix_tree_root *radix,
301 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400302{
303 struct dirty_root *dirty;
304 struct btrfs_root *gang[8];
305 struct btrfs_root *root;
306 int i;
307 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400308 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400309 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400310
Chris Mason0f7d52f2007-04-09 10:42:37 -0400311 while(1) {
312 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
313 ARRAY_SIZE(gang),
314 BTRFS_ROOT_TRANS_TAG);
315 if (ret == 0)
316 break;
317 for (i = 0; i < ret; i++) {
318 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400319 radix_tree_tag_clear(radix,
320 (unsigned long)root->root_key.objectid,
321 BTRFS_ROOT_TRANS_TAG);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400322 if (root->commit_root == root->node) {
Chris Masondb945352007-10-15 16:15:53 -0400323 WARN_ON(root->node->start !=
324 btrfs_root_bytenr(&root->root_item));
Chris Mason5f39d392007-10-15 16:14:19 -0400325 free_extent_buffer(root->commit_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400326 root->commit_root = NULL;
Josef Bacik58176a92007-08-29 15:47:34 -0400327
328 /* make sure to update the root on disk
329 * so we get any updates to the block used
330 * counts
331 */
332 err = btrfs_update_root(trans,
333 root->fs_info->tree_root,
334 &root->root_key,
335 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400336 continue;
337 }
338 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
339 BUG_ON(!dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400340 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
341 BUG_ON(!dirty->root);
342
343 memset(&root->root_item.drop_progress, 0,
344 sizeof(struct btrfs_disk_key));
345 root->root_item.drop_level = 0;
346
347 memcpy(dirty->root, root, sizeof(*root));
348 dirty->root->node = root->commit_root;
Josef Bacik58176a92007-08-29 15:47:34 -0400349 dirty->latest_root = root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400350 root->commit_root = NULL;
Chris Mason5eda7b52007-06-22 14:16:25 -0400351
Chris Mason0f7d52f2007-04-09 10:42:37 -0400352 root->root_key.offset = root->fs_info->generation;
Chris Masondb945352007-10-15 16:15:53 -0400353 btrfs_set_root_bytenr(&root->root_item,
354 root->node->start);
355 btrfs_set_root_level(&root->root_item,
356 btrfs_header_level(root->node));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400357 err = btrfs_insert_root(trans, root->fs_info->tree_root,
358 &root->root_key,
359 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400360 if (err)
361 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400362
363 refs = btrfs_root_refs(&dirty->root->root_item);
364 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400365 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400366 &dirty->root->root_key,
367 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400368
369 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400370 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400371 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400372 } else {
373 WARN_ON(1);
374 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400375 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400376 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400377 }
378 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400379 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400380}
381
Chris Masone9d0b132007-08-10 14:06:19 -0400382int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
383{
384 struct btrfs_fs_info *info = root->fs_info;
385 int ret;
386 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400387 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400388
Chris Masona2135012008-06-25 16:01:30 -0400389 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400390 if (root->defrag_running)
391 return 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400392 trans = btrfs_start_transaction(root, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400393 while (1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400394 root->defrag_running = 1;
395 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400396 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400397 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400398 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400399 cond_resched();
400
Chris Masone9d0b132007-08-10 14:06:19 -0400401 trans = btrfs_start_transaction(root, 1);
Chris Mason3f157a22008-06-25 16:01:31 -0400402 if (root->fs_info->closing || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400403 break;
404 }
405 root->defrag_running = 0;
Chris Masona2135012008-06-25 16:01:30 -0400406 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400407 btrfs_end_transaction(trans, root);
408 return 0;
409}
410
Chris Mason80b67942008-02-01 16:35:04 -0500411static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
412 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400413{
414 struct dirty_root *dirty;
415 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400416 unsigned long nr;
Chris Masondb945352007-10-15 16:15:53 -0400417 u64 num_bytes;
418 u64 bytes_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400419 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400420 int err;
421
Chris Mason0f7d52f2007-04-09 10:42:37 -0400422 while(!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400423 struct btrfs_root *root;
424
Chris Mason0f7d52f2007-04-09 10:42:37 -0400425 dirty = list_entry(list->next, struct dirty_root, list);
426 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400427
Chris Masondb945352007-10-15 16:15:53 -0400428 num_bytes = btrfs_root_used(&dirty->root->root_item);
Josef Bacik58176a92007-08-29 15:47:34 -0400429 root = dirty->latest_root;
Chris Masona2135012008-06-25 16:01:30 -0400430 atomic_inc(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400431
Chris Masona2135012008-06-25 16:01:30 -0400432 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400433 while(1) {
434 trans = btrfs_start_transaction(tree_root, 1);
435 ret = btrfs_drop_snapshot(trans, dirty->root);
436 if (ret != -EAGAIN) {
437 break;
438 }
Josef Bacik58176a92007-08-29 15:47:34 -0400439
Chris Mason9f3a7422007-08-07 15:52:19 -0400440 err = btrfs_update_root(trans,
441 tree_root,
442 &dirty->root->root_key,
443 &dirty->root->root_item);
444 if (err)
445 ret = err;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400446 nr = trans->blocks_used;
Chris Mason1b1e2132008-06-25 16:01:31 -0400447 ret = btrfs_end_transaction_throttle(trans, tree_root);
Chris Mason9f3a7422007-08-07 15:52:19 -0400448 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400449
450 mutex_unlock(&root->fs_info->drop_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400451 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400452 cond_resched();
Chris Masona2135012008-06-25 16:01:30 -0400453 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400454 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400455 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400456 atomic_dec(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400457
Chris Masona2135012008-06-25 16:01:30 -0400458 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masondb945352007-10-15 16:15:53 -0400459 num_bytes -= btrfs_root_used(&dirty->root->root_item);
460 bytes_used = btrfs_root_used(&root->root_item);
461 if (num_bytes) {
Josef Bacik58176a92007-08-29 15:47:34 -0400462 record_root_in_trans(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400463 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400464 bytes_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400465 }
Chris Masona2135012008-06-25 16:01:30 -0400466 mutex_unlock(&root->fs_info->alloc_mutex);
467
Chris Mason9f3a7422007-08-07 15:52:19 -0400468 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400469 if (ret) {
470 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400471 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400472 }
Chris Masona2135012008-06-25 16:01:30 -0400473 mutex_unlock(&root->fs_info->drop_mutex);
474
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400475 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400476 ret = btrfs_end_transaction(trans, tree_root);
477 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400478
Chris Masonf510cfe2007-10-15 16:14:48 -0400479 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400480 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400481 kfree(dirty);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400482
483 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400484 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400485 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400486 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400487}
488
Chris Masondc17ff82008-01-08 15:46:30 -0500489int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans,
490 struct btrfs_root *root)
491{
492 struct btrfs_transaction *cur_trans = trans->transaction;
493 struct inode *inode;
494 u64 root_objectid = 0;
495 u64 objectid = 0;
Chris Masondc17ff82008-01-08 15:46:30 -0500496 int ret;
497
Chris Masona2135012008-06-25 16:01:30 -0400498 atomic_inc(&root->fs_info->throttles);
Chris Masondc17ff82008-01-08 15:46:30 -0500499 while(1) {
500 ret = btrfs_find_first_ordered_inode(
501 &cur_trans->ordered_inode_tree,
Chris Mason4d5e74b2008-01-16 16:09:22 -0500502 &root_objectid, &objectid, &inode);
Chris Masondc17ff82008-01-08 15:46:30 -0500503 if (!ret)
504 break;
505
506 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason4d5e74b2008-01-16 16:09:22 -0500507
Chris Mason81d7ed22008-04-25 08:51:48 -0400508 if (S_ISREG(inode->i_mode)) {
509 atomic_inc(&BTRFS_I(inode)->ordered_writeback);
Chris Mason4d5e74b2008-01-16 16:09:22 -0500510 filemap_fdatawrite(inode->i_mapping);
Chris Mason81d7ed22008-04-25 08:51:48 -0400511 atomic_dec(&BTRFS_I(inode)->ordered_writeback);
512 }
Chris Mason4d5e74b2008-01-16 16:09:22 -0500513 iput(inode);
514
Chris Masondc17ff82008-01-08 15:46:30 -0500515 mutex_lock(&root->fs_info->trans_mutex);
516 }
517 while(1) {
518 root_objectid = 0;
519 objectid = 0;
520 ret = btrfs_find_del_first_ordered_inode(
521 &cur_trans->ordered_inode_tree,
Chris Mason4d5e74b2008-01-16 16:09:22 -0500522 &root_objectid, &objectid, &inode);
Chris Masondc17ff82008-01-08 15:46:30 -0500523 if (!ret)
524 break;
525 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason4d5e74b2008-01-16 16:09:22 -0500526
Chris Mason81d7ed22008-04-25 08:51:48 -0400527 if (S_ISREG(inode->i_mode)) {
528 atomic_inc(&BTRFS_I(inode)->ordered_writeback);
Chris Mason4d5e74b2008-01-16 16:09:22 -0500529 filemap_write_and_wait(inode->i_mapping);
Chris Mason81d7ed22008-04-25 08:51:48 -0400530 atomic_dec(&BTRFS_I(inode)->ordered_writeback);
531 }
Chris Mason4d5e74b2008-01-16 16:09:22 -0500532 atomic_dec(&inode->i_count);
533 iput(inode);
534
Chris Masondc17ff82008-01-08 15:46:30 -0500535 mutex_lock(&root->fs_info->trans_mutex);
536 }
Chris Masona2135012008-06-25 16:01:30 -0400537 atomic_dec(&root->fs_info->throttles);
Chris Mason3063d292008-01-08 15:46:30 -0500538 return 0;
539}
540
Chris Mason80b67942008-02-01 16:35:04 -0500541static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500542 struct btrfs_fs_info *fs_info,
543 struct btrfs_pending_snapshot *pending)
544{
545 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500546 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500547 struct btrfs_root *tree_root = fs_info->tree_root;
548 struct btrfs_root *root = pending->root;
549 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400550 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500551 int ret;
Sven Wegener3b963622008-06-09 21:57:42 -0400552 int namelen;
Chris Mason3063d292008-01-08 15:46:30 -0500553 u64 objectid;
554
Chris Mason80b67942008-02-01 16:35:04 -0500555 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
556 if (!new_root_item) {
557 ret = -ENOMEM;
558 goto fail;
559 }
Chris Mason3063d292008-01-08 15:46:30 -0500560 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
561 if (ret)
562 goto fail;
563
Chris Mason80b67942008-02-01 16:35:04 -0500564 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Chris Mason3063d292008-01-08 15:46:30 -0500565
566 key.objectid = objectid;
567 key.offset = 1;
568 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
569
Chris Mason925baed2008-06-25 16:01:30 -0400570 old = btrfs_lock_root_node(root);
571 btrfs_cow_block(trans, root, old, NULL, 0, &old);
Chris Mason3063d292008-01-08 15:46:30 -0500572
Chris Mason925baed2008-06-25 16:01:30 -0400573 btrfs_copy_root(trans, root, old, &tmp, objectid);
574 btrfs_tree_unlock(old);
575 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -0500576
Chris Mason80b67942008-02-01 16:35:04 -0500577 btrfs_set_root_bytenr(new_root_item, tmp->start);
578 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
Chris Mason3063d292008-01-08 15:46:30 -0500579 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
Chris Mason80b67942008-02-01 16:35:04 -0500580 new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -0400581 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -0500582 free_extent_buffer(tmp);
583 if (ret)
584 goto fail;
585
586 /*
587 * insert the directory item
588 */
589 key.offset = (u64)-1;
Sven Wegener3b963622008-06-09 21:57:42 -0400590 namelen = strlen(pending->name);
Chris Mason3063d292008-01-08 15:46:30 -0500591 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
Sven Wegener3b963622008-06-09 21:57:42 -0400592 pending->name, namelen,
Chris Mason3063d292008-01-08 15:46:30 -0500593 root->fs_info->sb->s_root->d_inode->i_ino,
594 &key, BTRFS_FT_DIR);
595
596 if (ret)
597 goto fail;
598
599 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
600 pending->name, strlen(pending->name), objectid,
601 root->fs_info->sb->s_root->d_inode->i_ino);
Sven Wegener3b963622008-06-09 21:57:42 -0400602
603 /* Invalidate existing dcache entry for new snapshot. */
604 btrfs_invalidate_dcache_root(root, pending->name, namelen);
605
Chris Mason3063d292008-01-08 15:46:30 -0500606fail:
Chris Mason80b67942008-02-01 16:35:04 -0500607 kfree(new_root_item);
Chris Mason3063d292008-01-08 15:46:30 -0500608 return ret;
609}
610
Chris Mason80b67942008-02-01 16:35:04 -0500611static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
612 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -0500613{
614 struct btrfs_pending_snapshot *pending;
615 struct list_head *head = &trans->transaction->pending_snapshots;
616 int ret;
617
618 while(!list_empty(head)) {
619 pending = list_entry(head->next,
620 struct btrfs_pending_snapshot, list);
621 ret = create_pending_snapshot(trans, fs_info, pending);
622 BUG_ON(ret);
623 list_del(&pending->list);
624 kfree(pending->name);
625 kfree(pending);
626 }
Chris Masondc17ff82008-01-08 15:46:30 -0500627 return 0;
628}
629
Chris Mason79154b12007-03-22 15:59:16 -0400630int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
631 struct btrfs_root *root)
632{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400633 unsigned long joined = 0;
634 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400635 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400636 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400637 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400638 struct list_head dirty_fs_roots;
Chris Masond1310b22008-01-24 16:13:08 -0500639 struct extent_io_tree *pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400640 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400641 int ret;
Chris Mason79154b12007-03-22 15:59:16 -0400642
Chris Mason0f7d52f2007-04-09 10:42:37 -0400643 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Masond6e4a422007-04-06 15:37:36 -0400644
Chris Mason79154b12007-03-22 15:59:16 -0400645 mutex_lock(&root->fs_info->trans_mutex);
646 if (trans->transaction->in_commit) {
647 cur_trans = trans->transaction;
648 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400649 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400650 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400651
Chris Mason79154b12007-03-22 15:59:16 -0400652 ret = wait_for_commit(root, cur_trans);
653 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400654
655 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400656 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400657 mutex_unlock(&root->fs_info->trans_mutex);
658
Chris Mason79154b12007-03-22 15:59:16 -0400659 return 0;
660 }
Chris Mason4313b392008-01-03 09:08:48 -0500661
662 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
663 if (!pinned_copy)
664 return -ENOMEM;
665
Chris Masond1310b22008-01-24 16:13:08 -0500666 extent_io_tree_init(pinned_copy,
Chris Mason4313b392008-01-03 09:08:48 -0500667 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
668
Chris Mason2c90e5d2007-04-02 10:50:19 -0400669 trans->transaction->in_commit = 1;
Chris Mason89ce8a62008-06-25 16:01:31 -0400670printk("trans %Lu in commit\n", trans->transid);
Chris Masonccd467d2007-06-28 15:57:36 -0400671 cur_trans = trans->transaction;
672 if (cur_trans->list.prev != &root->fs_info->trans_list) {
673 prev_trans = list_entry(cur_trans->list.prev,
674 struct btrfs_transaction, list);
675 if (!prev_trans->commit_done) {
676 prev_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400677 mutex_unlock(&root->fs_info->trans_mutex);
678
679 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400680
Chris Masonccd467d2007-06-28 15:57:36 -0400681 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400682 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400683 }
684 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400685
686 do {
687 joined = cur_trans->num_joined;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400688 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400689 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -0400690 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400691
692 if (cur_trans->num_writers > 1)
693 timeout = MAX_SCHEDULE_TIMEOUT;
694 else
695 timeout = 1;
696
Chris Mason79154b12007-03-22 15:59:16 -0400697 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400698
699 schedule_timeout(timeout);
700
Chris Mason79154b12007-03-22 15:59:16 -0400701 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400702 finish_wait(&cur_trans->writer_wait, &wait);
Chris Masondc17ff82008-01-08 15:46:30 -0500703 ret = btrfs_write_ordered_inodes(trans, root);
704
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400705 } while (cur_trans->num_writers > 1 ||
706 (cur_trans->num_joined != joined));
707
Chris Mason3063d292008-01-08 15:46:30 -0500708 ret = create_pending_snapshots(trans, root->fs_info);
709 BUG_ON(ret);
710
Chris Mason2c90e5d2007-04-02 10:50:19 -0400711 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -0500712
Chris Mason54aa1f42007-06-22 14:16:25 -0400713 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
714 &dirty_fs_roots);
715 BUG_ON(ret);
716
Chris Mason79154b12007-03-22 15:59:16 -0400717 ret = btrfs_commit_tree_roots(trans, root);
718 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -0400719
Chris Mason78fae272007-03-25 11:35:08 -0400720 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -0500721 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -0400722 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -0500723 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason4b52dff2007-06-26 10:06:50 -0400724 btrfs_set_super_generation(&root->fs_info->super_copy,
725 cur_trans->transid);
726 btrfs_set_super_root(&root->fs_info->super_copy,
Chris Masondb945352007-10-15 16:15:53 -0400727 root->fs_info->tree_root->node->start);
728 btrfs_set_super_root_level(&root->fs_info->super_copy,
729 btrfs_header_level(root->fs_info->tree_root->node));
Chris Mason5f39d392007-10-15 16:14:19 -0400730
Chris Mason0b86a832008-03-24 15:01:56 -0400731 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
732 chunk_root->node->start);
733 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
734 btrfs_header_level(chunk_root->node));
Chris Masona061fc82008-05-07 11:43:44 -0400735 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
736 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -0400737
Chris Mason4313b392008-01-03 09:08:48 -0500738 btrfs_copy_pinned(root, pinned_copy);
Chris Masonccd467d2007-06-28 15:57:36 -0400739
Chris Mason78fae272007-03-25 11:35:08 -0400740 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400741 ret = btrfs_write_and_wait_transaction(trans, root);
742 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400743 write_ctree_super(trans, root);
Chris Mason4313b392008-01-03 09:08:48 -0500744
Chris Mason4313b392008-01-03 09:08:48 -0500745 btrfs_finish_extent_commit(trans, root, pinned_copy);
Chris Mason78fae272007-03-25 11:35:08 -0400746 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4313b392008-01-03 09:08:48 -0500747
748 kfree(pinned_copy);
749
Chris Mason2c90e5d2007-04-02 10:50:19 -0400750 cur_trans->commit_done = 1;
Chris Mason89ce8a62008-06-25 16:01:31 -0400751printk("trans %Lu done in commit\n", cur_trans->transid);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400752 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400753 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400754 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400755 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -0400756
Chris Masonfacda1e2007-06-08 18:11:48 -0400757 if (root->fs_info->closing)
758 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
759 else
760 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Josef Bacik58176a92007-08-29 15:47:34 -0400761
Chris Mason78fae272007-03-25 11:35:08 -0400762 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400763 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400764
Chris Masonfacda1e2007-06-08 18:11:48 -0400765 if (root->fs_info->closing) {
Chris Masonfacda1e2007-06-08 18:11:48 -0400766 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -0400767 }
Chris Mason79154b12007-03-22 15:59:16 -0400768 return ret;
769}
770
Chris Masone9d0b132007-08-10 14:06:19 -0400771int btrfs_clean_old_snapshots(struct btrfs_root *root)
772{
773 struct list_head dirty_roots;
774 INIT_LIST_HEAD(&dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -0400775again:
Chris Masone9d0b132007-08-10 14:06:19 -0400776 mutex_lock(&root->fs_info->trans_mutex);
777 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
778 mutex_unlock(&root->fs_info->trans_mutex);
779
780 if (!list_empty(&dirty_roots)) {
781 drop_dirty_roots(root, &dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -0400782 goto again;
Chris Masone9d0b132007-08-10 14:06:19 -0400783 }
784 return 0;
785}
Chris Mason08607c12007-06-08 15:33:54 -0400786