blob: 768b0d223e68b71d260d8c0a43eb8f06f8d32fef [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 Masond3c2fdc2007-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 Masonf9295742008-07-17 12:54:14 -040065 cur_trans->blocked = 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 Mason3063d292008-01-08 15:46:30 -050069 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
Chris Mason8fd17792007-04-19 21:01:03 -040070 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
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 Masonf9295742008-07-17 12:54:14 -0400103struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
104 int num_blocks, int join)
Chris Mason79154b12007-03-22 15:59:16 -0400105{
Chris Mason2c90e5d2007-04-02 10:50:19 -0400106 struct btrfs_trans_handle *h =
107 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
Chris Masonf9295742008-07-17 12:54:14 -0400108 struct btrfs_transaction *cur_trans;
Chris Mason79154b12007-03-22 15:59:16 -0400109 int ret;
110
111 mutex_lock(&root->fs_info->trans_mutex);
Chris Masonf9295742008-07-17 12:54:14 -0400112 cur_trans = root->fs_info->running_transaction;
113 if (cur_trans && cur_trans->blocked && !join) {
114 DEFINE_WAIT(wait);
115 cur_trans->use_count++;
116 while(1) {
117 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
118 TASK_UNINTERRUPTIBLE);
119 if (cur_trans->blocked) {
120 mutex_unlock(&root->fs_info->trans_mutex);
121 schedule();
122 mutex_lock(&root->fs_info->trans_mutex);
123 finish_wait(&root->fs_info->transaction_wait,
124 &wait);
125 } else {
126 finish_wait(&root->fs_info->transaction_wait,
127 &wait);
128 break;
129 }
130 }
131 put_transaction(cur_trans);
132 }
Chris Mason79154b12007-03-22 15:59:16 -0400133 ret = join_transaction(root);
134 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400135
Chris Mason6702ed42007-08-07 16:15:09 -0400136 record_root_in_trans(root);
137 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400138 h->transaction = root->fs_info->running_transaction;
139 h->blocks_reserved = num_blocks;
140 h->blocks_used = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400141 h->block_group = NULL;
Chris Mason26b80032007-08-08 20:17:12 -0400142 h->alloc_exclude_nr = 0;
143 h->alloc_exclude_start = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400144 root->fs_info->running_transaction->use_count++;
145 mutex_unlock(&root->fs_info->trans_mutex);
146 return h;
147}
148
Chris Masonf9295742008-07-17 12:54:14 -0400149struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
150 int num_blocks)
151{
152 return start_transaction(root, num_blocks, 0);
153}
154struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
155 int num_blocks)
156{
157 return start_transaction(root, num_blocks, 1);
158}
159
Chris Mason89ce8a62008-06-25 16:01:31 -0400160static noinline int wait_for_commit(struct btrfs_root *root,
161 struct btrfs_transaction *commit)
162{
163 DEFINE_WAIT(wait);
164 mutex_lock(&root->fs_info->trans_mutex);
165 while(!commit->commit_done) {
166 prepare_to_wait(&commit->commit_wait, &wait,
167 TASK_UNINTERRUPTIBLE);
168 if (commit->commit_done)
169 break;
170 mutex_unlock(&root->fs_info->trans_mutex);
171 schedule();
172 mutex_lock(&root->fs_info->trans_mutex);
173 }
174 mutex_unlock(&root->fs_info->trans_mutex);
175 finish_wait(&commit->commit_wait, &wait);
176 return 0;
177}
178
179static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
180 struct btrfs_root *root, int throttle)
Chris Mason79154b12007-03-22 15:59:16 -0400181{
182 struct btrfs_transaction *cur_trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400183
Chris Mason79154b12007-03-22 15:59:16 -0400184 mutex_lock(&root->fs_info->trans_mutex);
185 cur_trans = root->fs_info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400186 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400187 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400188 cur_trans->num_writers--;
Chris Mason89ce8a62008-06-25 16:01:31 -0400189
Chris Mason79154b12007-03-22 15:59:16 -0400190 if (waitqueue_active(&cur_trans->writer_wait))
191 wake_up(&cur_trans->writer_wait);
Chris Mason89ce8a62008-06-25 16:01:31 -0400192
Chris Masonf9295742008-07-17 12:54:14 -0400193 if (0 && cur_trans->in_commit && throttle) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400194 DEFINE_WAIT(wait);
Chris Mason89ce8a62008-06-25 16:01:31 -0400195 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400196 prepare_to_wait(&root->fs_info->transaction_throttle, &wait,
197 TASK_UNINTERRUPTIBLE);
198 schedule();
199 finish_wait(&root->fs_info->transaction_throttle, &wait);
Chris Mason89ce8a62008-06-25 16:01:31 -0400200 mutex_lock(&root->fs_info->trans_mutex);
201 }
202
Chris Mason79154b12007-03-22 15:59:16 -0400203 put_transaction(cur_trans);
204 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400205 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400206 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400207 return 0;
208}
209
Chris Mason89ce8a62008-06-25 16:01:31 -0400210int btrfs_end_transaction(struct btrfs_trans_handle *trans,
211 struct btrfs_root *root)
212{
213 return __btrfs_end_transaction(trans, root, 0);
214}
215
216int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
217 struct btrfs_root *root)
218{
219 return __btrfs_end_transaction(trans, root, 1);
220}
221
Chris Mason79154b12007-03-22 15:59:16 -0400222
223int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
224 struct btrfs_root *root)
225{
Chris Mason7c4452b2007-04-28 09:29:35 -0400226 int ret;
Chris Mason7c4452b2007-04-28 09:29:35 -0400227 int err;
228 int werr = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500229 struct extent_io_tree *dirty_pages;
Chris Mason7c4452b2007-04-28 09:29:35 -0400230 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400231 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason5f39d392007-10-15 16:14:19 -0400232 u64 start;
233 u64 end;
234 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400235
236 if (!trans || !trans->transaction) {
237 return filemap_write_and_wait(btree_inode->i_mapping);
238 }
239 dirty_pages = &trans->transaction->dirty_pages;
240 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400241 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
242 EXTENT_DIRTY);
243 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400244 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400245 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
246 while(start <= end) {
247 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400248 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -0400249 page = find_lock_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400250 if (!page)
251 continue;
Chris Mason6702ed42007-08-07 16:15:09 -0400252 if (PageWriteback(page)) {
253 if (PageDirty(page))
254 wait_on_page_writeback(page);
255 else {
256 unlock_page(page);
257 page_cache_release(page);
258 continue;
259 }
260 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400261 err = write_one_page(page, 0);
262 if (err)
263 werr = err;
264 page_cache_release(page);
265 }
266 }
267 err = filemap_fdatawait(btree_inode->i_mapping);
268 if (err)
269 werr = err;
270 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400271}
272
Chris Mason0b86a832008-03-24 15:01:56 -0400273static int update_cowonly_root(struct btrfs_trans_handle *trans,
274 struct btrfs_root *root)
275{
276 int ret;
277 u64 old_root_bytenr;
278 struct btrfs_root *tree_root = root->fs_info->tree_root;
279
280 btrfs_write_dirty_block_groups(trans, root);
281 while(1) {
282 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
283 if (old_root_bytenr == root->node->start)
284 break;
285 btrfs_set_root_bytenr(&root->root_item,
286 root->node->start);
287 btrfs_set_root_level(&root->root_item,
288 btrfs_header_level(root->node));
289 ret = btrfs_update_root(trans, tree_root,
290 &root->root_key,
291 &root->root_item);
292 BUG_ON(ret);
293 btrfs_write_dirty_block_groups(trans, root);
294 }
295 return 0;
296}
297
Chris Mason79154b12007-03-22 15:59:16 -0400298int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
299 struct btrfs_root *root)
300{
Chris Mason79154b12007-03-22 15:59:16 -0400301 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400302 struct list_head *next;
Chris Mason79154b12007-03-22 15:59:16 -0400303
Chris Mason0b86a832008-03-24 15:01:56 -0400304 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
305 next = fs_info->dirty_cowonly_roots.next;
306 list_del_init(next);
307 root = list_entry(next, struct btrfs_root, dirty_list);
308 update_cowonly_root(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400309 }
310 return 0;
311}
312
Chris Mason0f7d52f2007-04-09 10:42:37 -0400313struct dirty_root {
314 struct list_head list;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400315 struct btrfs_root *root;
Josef Bacik58176a92007-08-29 15:47:34 -0400316 struct btrfs_root *latest_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400317};
318
Chris Mason5ce14bb2007-09-11 11:15:39 -0400319int btrfs_add_dead_root(struct btrfs_root *root,
320 struct btrfs_root *latest,
321 struct list_head *dead_list)
Chris Mason5eda7b52007-06-22 14:16:25 -0400322{
323 struct dirty_root *dirty;
324
325 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
326 if (!dirty)
327 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400328 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400329 dirty->latest_root = latest;
Chris Mason5eda7b52007-06-22 14:16:25 -0400330 list_add(&dirty->list, dead_list);
331 return 0;
332}
333
Chris Mason80b67942008-02-01 16:35:04 -0500334static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
335 struct radix_tree_root *radix,
336 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400337{
338 struct dirty_root *dirty;
339 struct btrfs_root *gang[8];
340 struct btrfs_root *root;
341 int i;
342 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400343 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400344 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400345
Chris Mason0f7d52f2007-04-09 10:42:37 -0400346 while(1) {
347 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
348 ARRAY_SIZE(gang),
349 BTRFS_ROOT_TRANS_TAG);
350 if (ret == 0)
351 break;
352 for (i = 0; i < ret; i++) {
353 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400354 radix_tree_tag_clear(radix,
355 (unsigned long)root->root_key.objectid,
356 BTRFS_ROOT_TRANS_TAG);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400357 if (root->commit_root == root->node) {
Chris Masondb945352007-10-15 16:15:53 -0400358 WARN_ON(root->node->start !=
359 btrfs_root_bytenr(&root->root_item));
Chris Mason5f39d392007-10-15 16:14:19 -0400360 free_extent_buffer(root->commit_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400361 root->commit_root = NULL;
Josef Bacik58176a92007-08-29 15:47:34 -0400362
363 /* make sure to update the root on disk
364 * so we get any updates to the block used
365 * counts
366 */
367 err = btrfs_update_root(trans,
368 root->fs_info->tree_root,
369 &root->root_key,
370 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400371 continue;
372 }
373 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
374 BUG_ON(!dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400375 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
376 BUG_ON(!dirty->root);
377
378 memset(&root->root_item.drop_progress, 0,
379 sizeof(struct btrfs_disk_key));
380 root->root_item.drop_level = 0;
381
382 memcpy(dirty->root, root, sizeof(*root));
383 dirty->root->node = root->commit_root;
Josef Bacik58176a92007-08-29 15:47:34 -0400384 dirty->latest_root = root;
Chris Masoned98b562008-07-22 23:06:42 -0400385 spin_lock_init(&dirty->root->node_lock);
386 mutex_init(&dirty->root->objectid_mutex);
387
Chris Mason0f7d52f2007-04-09 10:42:37 -0400388 root->commit_root = NULL;
Chris Mason5eda7b52007-06-22 14:16:25 -0400389
Chris Mason0f7d52f2007-04-09 10:42:37 -0400390 root->root_key.offset = root->fs_info->generation;
Chris Masondb945352007-10-15 16:15:53 -0400391 btrfs_set_root_bytenr(&root->root_item,
392 root->node->start);
393 btrfs_set_root_level(&root->root_item,
394 btrfs_header_level(root->node));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400395 err = btrfs_insert_root(trans, root->fs_info->tree_root,
396 &root->root_key,
397 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400398 if (err)
399 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400400
401 refs = btrfs_root_refs(&dirty->root->root_item);
402 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400403 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400404 &dirty->root->root_key,
405 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400406
407 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400408 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400409 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400410 } else {
411 WARN_ON(1);
412 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400413 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400414 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400415 }
416 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400417 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400418}
419
Chris Masone9d0b132007-08-10 14:06:19 -0400420int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
421{
422 struct btrfs_fs_info *info = root->fs_info;
423 int ret;
424 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -0400425 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400426
Chris Masona2135012008-06-25 16:01:30 -0400427 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400428 if (root->defrag_running)
429 return 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400430 trans = btrfs_start_transaction(root, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400431 while (1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400432 root->defrag_running = 1;
433 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400434 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400435 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400436 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400437 cond_resched();
438
Chris Masone9d0b132007-08-10 14:06:19 -0400439 trans = btrfs_start_transaction(root, 1);
Chris Mason3f157a22008-06-25 16:01:31 -0400440 if (root->fs_info->closing || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400441 break;
442 }
443 root->defrag_running = 0;
Chris Masona2135012008-06-25 16:01:30 -0400444 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400445 btrfs_end_transaction(trans, root);
446 return 0;
447}
448
Chris Mason80b67942008-02-01 16:35:04 -0500449static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
450 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400451{
452 struct dirty_root *dirty;
453 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -0400454 unsigned long nr;
Chris Masondb945352007-10-15 16:15:53 -0400455 u64 num_bytes;
456 u64 bytes_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400457 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400458 int err;
459
Chris Mason0f7d52f2007-04-09 10:42:37 -0400460 while(!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400461 struct btrfs_root *root;
462
Chris Mason0f7d52f2007-04-09 10:42:37 -0400463 dirty = list_entry(list->next, struct dirty_root, list);
464 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400465
Chris Masondb945352007-10-15 16:15:53 -0400466 num_bytes = btrfs_root_used(&dirty->root->root_item);
Josef Bacik58176a92007-08-29 15:47:34 -0400467 root = dirty->latest_root;
Chris Masona2135012008-06-25 16:01:30 -0400468 atomic_inc(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400469
Chris Masona2135012008-06-25 16:01:30 -0400470 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400471 while(1) {
472 trans = btrfs_start_transaction(tree_root, 1);
473 ret = btrfs_drop_snapshot(trans, dirty->root);
474 if (ret != -EAGAIN) {
475 break;
476 }
Josef Bacik58176a92007-08-29 15:47:34 -0400477
Chris Mason9f3a7422007-08-07 15:52:19 -0400478 err = btrfs_update_root(trans,
479 tree_root,
480 &dirty->root->root_key,
481 &dirty->root->root_item);
482 if (err)
483 ret = err;
Chris Masond3c2fdc2007-09-17 10:58:06 -0400484 nr = trans->blocks_used;
Chris Mason1b1e2132008-06-25 16:01:31 -0400485 ret = btrfs_end_transaction_throttle(trans, tree_root);
Chris Mason9f3a7422007-08-07 15:52:19 -0400486 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400487
488 mutex_unlock(&root->fs_info->drop_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400489 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400490 cond_resched();
Chris Masona2135012008-06-25 16:01:30 -0400491 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400492 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400493 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400494 atomic_dec(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400495
Chris Masona2135012008-06-25 16:01:30 -0400496 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masondb945352007-10-15 16:15:53 -0400497 num_bytes -= btrfs_root_used(&dirty->root->root_item);
498 bytes_used = btrfs_root_used(&root->root_item);
499 if (num_bytes) {
Josef Bacik58176a92007-08-29 15:47:34 -0400500 record_root_in_trans(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400501 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400502 bytes_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400503 }
Chris Masona2135012008-06-25 16:01:30 -0400504 mutex_unlock(&root->fs_info->alloc_mutex);
505
Chris Mason9f3a7422007-08-07 15:52:19 -0400506 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400507 if (ret) {
508 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400509 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400510 }
Chris Masona2135012008-06-25 16:01:30 -0400511 mutex_unlock(&root->fs_info->drop_mutex);
512
Chris Masond3c2fdc2007-09-17 10:58:06 -0400513 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400514 ret = btrfs_end_transaction(trans, tree_root);
515 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400516
Chris Masonf510cfe2007-10-15 16:14:48 -0400517 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400518 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400519 kfree(dirty);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400520
521 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400522 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400523 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400524 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400525}
526
Chris Mason80b67942008-02-01 16:35:04 -0500527static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500528 struct btrfs_fs_info *fs_info,
529 struct btrfs_pending_snapshot *pending)
530{
531 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500532 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500533 struct btrfs_root *tree_root = fs_info->tree_root;
534 struct btrfs_root *root = pending->root;
535 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400536 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500537 int ret;
Sven Wegener3b963622008-06-09 21:57:42 -0400538 int namelen;
Chris Mason3063d292008-01-08 15:46:30 -0500539 u64 objectid;
540
Chris Mason80b67942008-02-01 16:35:04 -0500541 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
542 if (!new_root_item) {
543 ret = -ENOMEM;
544 goto fail;
545 }
Chris Mason3063d292008-01-08 15:46:30 -0500546 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
547 if (ret)
548 goto fail;
549
Chris Mason80b67942008-02-01 16:35:04 -0500550 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Chris Mason3063d292008-01-08 15:46:30 -0500551
552 key.objectid = objectid;
553 key.offset = 1;
554 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
555
Chris Mason925baed2008-06-25 16:01:30 -0400556 old = btrfs_lock_root_node(root);
557 btrfs_cow_block(trans, root, old, NULL, 0, &old);
Chris Mason3063d292008-01-08 15:46:30 -0500558
Chris Mason925baed2008-06-25 16:01:30 -0400559 btrfs_copy_root(trans, root, old, &tmp, objectid);
560 btrfs_tree_unlock(old);
561 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -0500562
Chris Mason80b67942008-02-01 16:35:04 -0500563 btrfs_set_root_bytenr(new_root_item, tmp->start);
564 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
Chris Mason3063d292008-01-08 15:46:30 -0500565 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
Chris Mason80b67942008-02-01 16:35:04 -0500566 new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -0400567 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -0500568 free_extent_buffer(tmp);
569 if (ret)
570 goto fail;
571
572 /*
573 * insert the directory item
574 */
575 key.offset = (u64)-1;
Sven Wegener3b963622008-06-09 21:57:42 -0400576 namelen = strlen(pending->name);
Chris Mason3063d292008-01-08 15:46:30 -0500577 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
Sven Wegener3b963622008-06-09 21:57:42 -0400578 pending->name, namelen,
Chris Mason3063d292008-01-08 15:46:30 -0500579 root->fs_info->sb->s_root->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -0400580 &key, BTRFS_FT_DIR, 0);
Chris Mason3063d292008-01-08 15:46:30 -0500581
582 if (ret)
583 goto fail;
584
585 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
586 pending->name, strlen(pending->name), objectid,
Josef Bacikaec74772008-07-24 12:12:38 -0400587 root->fs_info->sb->s_root->d_inode->i_ino, 0);
Sven Wegener3b963622008-06-09 21:57:42 -0400588
589 /* Invalidate existing dcache entry for new snapshot. */
590 btrfs_invalidate_dcache_root(root, pending->name, namelen);
591
Chris Mason3063d292008-01-08 15:46:30 -0500592fail:
Chris Mason80b67942008-02-01 16:35:04 -0500593 kfree(new_root_item);
Chris Mason3063d292008-01-08 15:46:30 -0500594 return ret;
595}
596
Chris Mason80b67942008-02-01 16:35:04 -0500597static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
598 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -0500599{
600 struct btrfs_pending_snapshot *pending;
601 struct list_head *head = &trans->transaction->pending_snapshots;
602 int ret;
603
604 while(!list_empty(head)) {
605 pending = list_entry(head->next,
606 struct btrfs_pending_snapshot, list);
607 ret = create_pending_snapshot(trans, fs_info, pending);
608 BUG_ON(ret);
609 list_del(&pending->list);
610 kfree(pending->name);
611 kfree(pending);
612 }
Chris Masondc17ff82008-01-08 15:46:30 -0500613 return 0;
614}
615
Chris Mason79154b12007-03-22 15:59:16 -0400616int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
617 struct btrfs_root *root)
618{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400619 unsigned long joined = 0;
620 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400621 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400622 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400623 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400624 struct list_head dirty_fs_roots;
Chris Masond1310b22008-01-24 16:13:08 -0500625 struct extent_io_tree *pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400626 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400627 int ret;
Chris Mason79154b12007-03-22 15:59:16 -0400628
Chris Mason0f7d52f2007-04-09 10:42:37 -0400629 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Masond6e4a422007-04-06 15:37:36 -0400630
Chris Mason79154b12007-03-22 15:59:16 -0400631 mutex_lock(&root->fs_info->trans_mutex);
632 if (trans->transaction->in_commit) {
633 cur_trans = trans->transaction;
634 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400635 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400636 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400637
Chris Mason79154b12007-03-22 15:59:16 -0400638 ret = wait_for_commit(root, cur_trans);
639 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400640
641 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400642 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400643 mutex_unlock(&root->fs_info->trans_mutex);
644
Chris Mason79154b12007-03-22 15:59:16 -0400645 return 0;
646 }
Chris Mason4313b392008-01-03 09:08:48 -0500647
648 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
649 if (!pinned_copy)
650 return -ENOMEM;
651
Chris Masond1310b22008-01-24 16:13:08 -0500652 extent_io_tree_init(pinned_copy,
Chris Mason4313b392008-01-03 09:08:48 -0500653 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
654
Chris Mason2c90e5d2007-04-02 10:50:19 -0400655 trans->transaction->in_commit = 1;
Chris Masonf9295742008-07-17 12:54:14 -0400656 trans->transaction->blocked = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400657 cur_trans = trans->transaction;
658 if (cur_trans->list.prev != &root->fs_info->trans_list) {
659 prev_trans = list_entry(cur_trans->list.prev,
660 struct btrfs_transaction, list);
661 if (!prev_trans->commit_done) {
662 prev_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400663 mutex_unlock(&root->fs_info->trans_mutex);
664
665 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400666
Chris Masonccd467d2007-06-28 15:57:36 -0400667 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400668 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400669 }
670 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400671
672 do {
673 joined = cur_trans->num_joined;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400674 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400675 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -0400676 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400677
678 if (cur_trans->num_writers > 1)
679 timeout = MAX_SCHEDULE_TIMEOUT;
680 else
681 timeout = 1;
682
Chris Mason79154b12007-03-22 15:59:16 -0400683 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400684
685 schedule_timeout(timeout);
686
Chris Mason79154b12007-03-22 15:59:16 -0400687 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400688 finish_wait(&cur_trans->writer_wait, &wait);
689 } while (cur_trans->num_writers > 1 ||
690 (cur_trans->num_joined != joined));
691
Chris Mason3063d292008-01-08 15:46:30 -0500692 ret = create_pending_snapshots(trans, root->fs_info);
693 BUG_ON(ret);
694
Chris Mason2c90e5d2007-04-02 10:50:19 -0400695 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -0500696
Chris Mason54aa1f42007-06-22 14:16:25 -0400697 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
698 &dirty_fs_roots);
699 BUG_ON(ret);
700
Chris Mason79154b12007-03-22 15:59:16 -0400701 ret = btrfs_commit_tree_roots(trans, root);
702 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -0400703
Chris Mason78fae272007-03-25 11:35:08 -0400704 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -0500705 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -0400706 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -0500707 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason4b52dff2007-06-26 10:06:50 -0400708 btrfs_set_super_generation(&root->fs_info->super_copy,
709 cur_trans->transid);
710 btrfs_set_super_root(&root->fs_info->super_copy,
Chris Masondb945352007-10-15 16:15:53 -0400711 root->fs_info->tree_root->node->start);
712 btrfs_set_super_root_level(&root->fs_info->super_copy,
713 btrfs_header_level(root->fs_info->tree_root->node));
Chris Mason5f39d392007-10-15 16:14:19 -0400714
Chris Mason0b86a832008-03-24 15:01:56 -0400715 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
716 chunk_root->node->start);
717 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
718 btrfs_header_level(chunk_root->node));
Chris Masona061fc82008-05-07 11:43:44 -0400719 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
720 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -0400721
Chris Mason4313b392008-01-03 09:08:48 -0500722 btrfs_copy_pinned(root, pinned_copy);
Chris Masonccd467d2007-06-28 15:57:36 -0400723
Chris Masonf9295742008-07-17 12:54:14 -0400724 trans->transaction->blocked = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400725 wake_up(&root->fs_info->transaction_throttle);
Chris Masonf9295742008-07-17 12:54:14 -0400726 wake_up(&root->fs_info->transaction_wait);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400727
Chris Mason78fae272007-03-25 11:35:08 -0400728 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400729 ret = btrfs_write_and_wait_transaction(trans, root);
730 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400731 write_ctree_super(trans, root);
Chris Mason4313b392008-01-03 09:08:48 -0500732
Chris Mason4313b392008-01-03 09:08:48 -0500733 btrfs_finish_extent_commit(trans, root, pinned_copy);
Chris Mason78fae272007-03-25 11:35:08 -0400734 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4313b392008-01-03 09:08:48 -0500735
736 kfree(pinned_copy);
737
Chris Mason2c90e5d2007-04-02 10:50:19 -0400738 cur_trans->commit_done = 1;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400739 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400740 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400741 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400742 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -0400743
Chris Masonfacda1e2007-06-08 18:11:48 -0400744 if (root->fs_info->closing)
745 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
746 else
747 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Josef Bacik58176a92007-08-29 15:47:34 -0400748
Chris Mason78fae272007-03-25 11:35:08 -0400749 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400750 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400751
Chris Masonfacda1e2007-06-08 18:11:48 -0400752 if (root->fs_info->closing) {
Chris Masonfacda1e2007-06-08 18:11:48 -0400753 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -0400754 }
Chris Mason79154b12007-03-22 15:59:16 -0400755 return ret;
756}
757
Chris Masone9d0b132007-08-10 14:06:19 -0400758int btrfs_clean_old_snapshots(struct btrfs_root *root)
759{
760 struct list_head dirty_roots;
761 INIT_LIST_HEAD(&dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -0400762again:
Chris Masone9d0b132007-08-10 14:06:19 -0400763 mutex_lock(&root->fs_info->trans_mutex);
764 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
765 mutex_unlock(&root->fs_info->trans_mutex);
766
767 if (!list_empty(&dirty_roots)) {
768 drop_dirty_roots(root, &dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -0400769 goto again;
Chris Masone9d0b132007-08-10 14:06:19 -0400770 }
771 return 0;
772}
Chris Mason08607c12007-06-08 15:33:54 -0400773