blob: b9301a5e4608506b2f6701eabca7d847a67bffb1 [file] [log] [blame]
Chris Masone20d96d2007-03-22 12:13:20 -04001#include <linux/module.h>
2#include <linux/fs.h>
Chris Masond98237b2007-03-28 13:57:48 -04003#include <linux/blkdev.h>
Chris Mason87cbda52007-03-28 19:44:27 -04004#include <linux/crypto.h>
5#include <linux/scatterlist.h>
Chris Mason22b0ebd2007-03-30 08:47:31 -04006#include <linux/swap.h>
Chris Masoneb60cea2007-02-02 09:18:22 -05007#include "ctree.h"
8#include "disk-io.h"
Chris Masone089f052007-03-16 16:20:31 -04009#include "transaction.h"
Chris Masoneb60cea2007-02-02 09:18:22 -050010
Chris Masone20d96d2007-03-22 12:13:20 -040011static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050012{
Chris Masone20d96d2007-03-22 12:13:20 -040013 struct btrfs_node *node = btrfs_buffer_node(buf);
Chris Masond98237b2007-03-28 13:57:48 -040014 if (buf->b_blocknr != btrfs_header_blocknr(&node->header)) {
Chris Mason9a8dd152007-02-23 08:38:36 -050015 BUG();
Chris Masond98237b2007-03-28 13:57:48 -040016 }
Chris Masone20d96d2007-03-22 12:13:20 -040017 if (root->node && btrfs_header_parentid(&node->header) !=
Chris Masondf2ce342007-03-23 11:00:45 -040018 btrfs_header_parentid(btrfs_buffer_header(root->node))) {
Chris Mason7f5c1512007-03-23 15:56:19 -040019 BUG();
Chris Masondf2ce342007-03-23 11:00:45 -040020 }
Chris Mason9a8dd152007-02-23 08:38:36 -050021 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050022}
23
Chris Masond98237b2007-03-28 13:57:48 -040024struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050025{
Chris Masond98237b2007-03-28 13:57:48 -040026 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
27 int blockbits = root->fs_info->sb->s_blocksize_bits;
28 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
29 struct page *page;
30 struct buffer_head *bh;
31 struct buffer_head *head;
32 struct buffer_head *ret = NULL;
33
Chris Mason2c90e5d2007-04-02 10:50:19 -040034
Chris Masond98237b2007-03-28 13:57:48 -040035 page = find_lock_page(mapping, index);
36 if (!page)
37 return NULL;
38
39 if (!page_has_buffers(page))
40 goto out_unlock;
41
42 head = page_buffers(page);
43 bh = head;
44 do {
45 if (buffer_mapped(bh) && bh->b_blocknr == blocknr) {
46 ret = bh;
47 get_bh(bh);
48 goto out_unlock;
49 }
50 bh = bh->b_this_page;
51 } while (bh != head);
52out_unlock:
53 unlock_page(page);
Chris Masond6025572007-03-30 14:27:56 -040054 if (ret) {
Chris Mason22b0ebd2007-03-30 08:47:31 -040055 touch_buffer(ret);
Chris Masond6025572007-03-30 14:27:56 -040056 }
Chris Masond98237b2007-03-28 13:57:48 -040057 page_cache_release(page);
58 return ret;
Chris Masoned2ff2c2007-03-01 18:59:40 -050059}
60
Chris Masond98237b2007-03-28 13:57:48 -040061struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
62 u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050063{
Chris Masond98237b2007-03-28 13:57:48 -040064 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
65 int blockbits = root->fs_info->sb->s_blocksize_bits;
66 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
67 struct page *page;
68 struct buffer_head *bh;
69 struct buffer_head *head;
70 struct buffer_head *ret = NULL;
71 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
Chris Mason22b0ebd2007-03-30 08:47:31 -040072
Chris Masond98237b2007-03-28 13:57:48 -040073 page = grab_cache_page(mapping, index);
74 if (!page)
75 return NULL;
76
Chris Masond98237b2007-03-28 13:57:48 -040077 if (!page_has_buffers(page))
78 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
79 head = page_buffers(page);
80 bh = head;
81 do {
82 if (!buffer_mapped(bh)) {
83 bh->b_bdev = root->fs_info->sb->s_bdev;
84 bh->b_blocknr = first_block;
85 set_buffer_mapped(bh);
86 }
87 if (bh->b_blocknr == blocknr) {
88 ret = bh;
89 get_bh(bh);
90 goto out_unlock;
91 }
92 bh = bh->b_this_page;
93 first_block++;
94 } while (bh != head);
95out_unlock:
96 unlock_page(page);
Chris Mason22b0ebd2007-03-30 08:47:31 -040097 if (ret)
98 touch_buffer(ret);
Chris Masond98237b2007-03-28 13:57:48 -040099 page_cache_release(page);
100 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400101}
Chris Mason123abc82007-03-14 14:14:43 -0400102
Chris Masond98237b2007-03-28 13:57:48 -0400103static sector_t max_block(struct block_device *bdev)
104{
105 sector_t retval = ~((sector_t)0);
106 loff_t sz = i_size_read(bdev->bd_inode);
107
108 if (sz) {
109 unsigned int size = block_size(bdev);
110 unsigned int sizebits = blksize_bits(size);
111 retval = (sz >> sizebits);
112 }
113 return retval;
114}
115
116static int btree_get_block(struct inode *inode, sector_t iblock,
117 struct buffer_head *bh, int create)
118{
119 if (iblock >= max_block(inode->i_sb->s_bdev)) {
120 if (create)
121 return -EIO;
122
123 /*
124 * for reads, we're just trying to fill a partial page.
125 * return a hole, they will have to call get_block again
126 * before they can fill it, and they will get -EIO at that
127 * time
128 */
129 return 0;
130 }
131 bh->b_bdev = inode->i_sb->s_bdev;
132 bh->b_blocknr = iblock;
133 set_buffer_mapped(bh);
134 return 0;
135}
136
Chris Masonf254e522007-03-29 15:15:27 -0400137int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
138 char *result)
Chris Mason87cbda52007-03-28 19:44:27 -0400139{
Chris Mason87cbda52007-03-28 19:44:27 -0400140 struct scatterlist sg;
141 struct crypto_hash *tfm = root->fs_info->hash_tfm;
142 struct hash_desc desc;
143 int ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400144
145 desc.tfm = tfm;
146 desc.flags = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400147 sg_init_one(&sg, data, len);
Chris Mason87cbda52007-03-28 19:44:27 -0400148 spin_lock(&root->fs_info->hash_lock);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400149 ret = crypto_hash_digest(&desc, &sg, 1, result);
Chris Mason87cbda52007-03-28 19:44:27 -0400150 spin_unlock(&root->fs_info->hash_lock);
151 if (ret) {
152 printk("sha256 digest failed\n");
153 }
Chris Masonf254e522007-03-29 15:15:27 -0400154 return ret;
155}
156static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
157 int verify)
158{
159 char result[BTRFS_CSUM_SIZE];
160 int ret;
161 struct btrfs_node *node;
162
163 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
164 bh->b_size - BTRFS_CSUM_SIZE, result);
165 if (ret)
166 return ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400167 if (verify) {
Chris Masonf254e522007-03-29 15:15:27 -0400168 if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) {
169 printk("checksum verify failed on %lu\n",
170 bh->b_blocknr);
171 return 1;
172 }
173 } else {
174 node = btrfs_buffer_node(bh);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400175 memcpy(node->header.csum, result, BTRFS_CSUM_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -0400176 }
Chris Mason87cbda52007-03-28 19:44:27 -0400177 return 0;
178}
179
Chris Masond98237b2007-03-28 13:57:48 -0400180static int btree_writepage(struct page *page, struct writeback_control *wbc)
181{
Chris Mason87cbda52007-03-28 19:44:27 -0400182 struct buffer_head *bh;
183 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
184 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400185 if (!page_has_buffers(page)) {
186 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
187 (1 << BH_Dirty)|(1 << BH_Uptodate));
188 }
189 head = page_buffers(page);
190 bh = head;
191 do {
192 if (buffer_dirty(bh))
193 csum_tree_block(root, bh, 0);
194 bh = bh->b_this_page;
195 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400196 return block_write_full_page(page, btree_get_block, wbc);
197}
198
199static int btree_readpage(struct file * file, struct page * page)
200{
201 return block_read_full_page(page, btree_get_block);
202}
203
204static struct address_space_operations btree_aops = {
205 .readpage = btree_readpage,
206 .writepage = btree_writepage,
207 .sync_page = block_sync_page,
208};
209
Chris Masone20d96d2007-03-22 12:13:20 -0400210struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
211{
Chris Masond98237b2007-03-28 13:57:48 -0400212 struct buffer_head *bh = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400213
Chris Masond98237b2007-03-28 13:57:48 -0400214 bh = btrfs_find_create_tree_block(root, blocknr);
215 if (!bh)
216 return bh;
Chris Mason9d642722007-04-03 11:43:19 -0400217 if (buffer_uptodate(bh))
218 goto uptodate;
Chris Masond98237b2007-03-28 13:57:48 -0400219 lock_buffer(bh);
220 if (!buffer_uptodate(bh)) {
221 get_bh(bh);
222 bh->b_end_io = end_buffer_read_sync;
223 submit_bh(READ, bh);
224 wait_on_buffer(bh);
225 if (!buffer_uptodate(bh))
226 goto fail;
Chris Mason87cbda52007-03-28 19:44:27 -0400227 csum_tree_block(root, bh, 1);
Chris Masond98237b2007-03-28 13:57:48 -0400228 } else {
229 unlock_buffer(bh);
230 }
Chris Mason9d642722007-04-03 11:43:19 -0400231uptodate:
Chris Masond98237b2007-03-28 13:57:48 -0400232 if (check_tree_block(root, bh))
Chris Masoncfaa7292007-02-21 17:04:57 -0500233 BUG();
Chris Masond98237b2007-03-28 13:57:48 -0400234 return bh;
235fail:
236 brelse(bh);
237 return NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500238}
239
Chris Masone089f052007-03-16 16:20:31 -0400240int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400241 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500242{
Chris Masond6025572007-03-30 14:27:56 -0400243 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400244 mark_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500245 return 0;
246}
247
Chris Masone089f052007-03-16 16:20:31 -0400248int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400249 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500250{
Chris Masond6025572007-03-30 14:27:56 -0400251 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400252 clear_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500253 return 0;
254}
255
Chris Mason2c90e5d2007-04-02 10:50:19 -0400256static int __setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400257 struct btrfs_root *root,
258 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400259 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500260{
Chris Masoncfaa7292007-02-21 17:04:57 -0500261 root->node = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500262 root->commit_root = NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400263 root->blocksize = blocksize;
Chris Mason123abc82007-03-14 14:14:43 -0400264 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400265 root->fs_info = fs_info;
Chris Mason3768f362007-03-13 16:47:54 -0400266 memset(&root->root_key, 0, sizeof(root->root_key));
267 memset(&root->root_item, 0, sizeof(root->root_item));
268 return 0;
269}
270
Chris Mason2c90e5d2007-04-02 10:50:19 -0400271static int find_and_setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400272 struct btrfs_root *tree_root,
273 struct btrfs_fs_info *fs_info,
274 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400275 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400276{
277 int ret;
278
Chris Mason2c90e5d2007-04-02 10:50:19 -0400279 __setup_root(blocksize, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400280 ret = btrfs_find_last_root(tree_root, objectid,
281 &root->root_item, &root->root_key);
282 BUG_ON(ret);
283
284 root->node = read_tree_block(root,
285 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400286 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500287 return 0;
288}
289
Chris Mason2c90e5d2007-04-02 10:50:19 -0400290struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500291{
Chris Masone20d96d2007-03-22 12:13:20 -0400292 struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
293 GFP_NOFS);
294 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
295 GFP_NOFS);
296 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
297 GFP_NOFS);
298 struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
299 GFP_NOFS);
300 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
301 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500302 int ret;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400303 struct btrfs_super_block *disk_super;
Chris Masoneb60cea2007-02-02 09:18:22 -0500304
Chris Mason8ef97622007-03-26 10:15:30 -0400305 init_bit_radix(&fs_info->pinned_radix);
306 init_bit_radix(&fs_info->pending_del_radix);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400307 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400308 fs_info->running_transaction = NULL;
309 fs_info->fs_root = root;
310 fs_info->tree_root = tree_root;
311 fs_info->extent_root = extent_root;
312 fs_info->inode_root = inode_root;
313 fs_info->last_inode_alloc = 0;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400314 fs_info->highest_inode = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400315 fs_info->sb = sb;
Chris Masond98237b2007-03-28 13:57:48 -0400316 fs_info->btree_inode = new_inode(sb);
317 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400318 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400319 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
320 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason22b0ebd2007-03-30 08:47:31 -0400321 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400322 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason87cbda52007-03-28 19:44:27 -0400323 fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
Chris Mason30ae8462007-03-29 09:59:15 -0400324 spin_lock_init(&fs_info->hash_lock);
Chris Mason30ae8462007-03-29 09:59:15 -0400325 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
Chris Mason87cbda52007-03-28 19:44:27 -0400326 printk("failed to allocate sha256 hash\n");
327 return NULL;
328 }
Chris Mason79154b12007-03-22 15:59:16 -0400329 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400330 mutex_init(&fs_info->fs_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400331 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
332 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
Chris Mason3768f362007-03-13 16:47:54 -0400333
Chris Mason2c90e5d2007-04-02 10:50:19 -0400334 __setup_root(sb->s_blocksize, tree_root,
335 fs_info, BTRFS_ROOT_TREE_OBJECTID);
336 fs_info->sb_buffer = read_tree_block(tree_root,
337 BTRFS_SUPER_INFO_OFFSET /
338 sb->s_blocksize);
Chris Masond98237b2007-03-28 13:57:48 -0400339
Chris Mason87cbda52007-03-28 19:44:27 -0400340 if (!fs_info->sb_buffer) {
Chris Masond98237b2007-03-28 13:57:48 -0400341 return NULL;
Chris Mason87cbda52007-03-28 19:44:27 -0400342 }
Chris Masond98237b2007-03-28 13:57:48 -0400343 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400344 if (!btrfs_super_root(disk_super)) {
345 return NULL;
346 }
Chris Masond98237b2007-03-28 13:57:48 -0400347 fs_info->disk_super = disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400348 tree_root->node = read_tree_block(tree_root,
349 btrfs_super_root(disk_super));
Chris Mason3768f362007-03-13 16:47:54 -0400350 BUG_ON(!tree_root->node);
351
Chris Mason2c90e5d2007-04-02 10:50:19 -0400352 mutex_lock(&fs_info->fs_mutex);
353 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400354 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400355 BUG_ON(ret);
356
Chris Mason2c90e5d2007-04-02 10:50:19 -0400357 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400358 BTRFS_INODE_MAP_OBJECTID, inode_root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400359 BUG_ON(ret);
360
Chris Mason2c90e5d2007-04-02 10:50:19 -0400361 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400362 BTRFS_FS_TREE_OBJECTID, root);
Chris Mason3768f362007-03-13 16:47:54 -0400363 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500364 root->commit_root = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400365 get_bh(root->node);
Chris Mason3768f362007-03-13 16:47:54 -0400366 root->ref_cows = 1;
Chris Mason293ffd52007-03-20 15:57:25 -0400367 root->fs_info->generation = root->root_key.offset + 1;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400368 ret = btrfs_find_highest_inode(root, &root->fs_info->last_inode_alloc);
369 if (ret == 0)
370 fs_info->highest_inode = fs_info->last_inode_alloc;
Chris Masond6e4a422007-04-06 15:37:36 -0400371 memset(&fs_info->kobj, 0, sizeof(fs_info->kobj));
372 kobj_set_kset_s(fs_info, btrfs_subsys);
373 kobject_set_name(&fs_info->kobj, "%s", sb->s_id);
374 kobject_register(&fs_info->kobj);
Chris Mason5be6f7f2007-04-05 13:35:25 -0400375 mutex_unlock(&fs_info->fs_mutex);
Chris Masoneb60cea2007-02-02 09:18:22 -0500376 return root;
377}
378
Chris Masone089f052007-03-16 16:20:31 -0400379int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400380 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500381{
Chris Masond5719762007-03-23 10:01:08 -0400382 struct buffer_head *bh = root->fs_info->sb_buffer;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400383
Chris Masond5719762007-03-23 10:01:08 -0400384 btrfs_set_super_root(root->fs_info->disk_super,
385 root->fs_info->tree_root->node->b_blocknr);
386 lock_buffer(bh);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400387 WARN_ON(atomic_read(&bh->b_count) < 1);
Chris Masond5719762007-03-23 10:01:08 -0400388 clear_buffer_dirty(bh);
Chris Mason87cbda52007-03-28 19:44:27 -0400389 csum_tree_block(root, bh, 0);
Chris Masond5719762007-03-23 10:01:08 -0400390 bh->b_end_io = end_buffer_write_sync;
391 get_bh(bh);
392 submit_bh(WRITE, bh);
393 wait_on_buffer(bh);
394 if (!buffer_uptodate(bh)) {
395 WARN_ON(1);
396 return -EIO;
Chris Masoncfaa7292007-02-21 17:04:57 -0500397 }
398 return 0;
399}
400
Chris Masone20d96d2007-03-22 12:13:20 -0400401int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500402{
Chris Mason3768f362007-03-13 16:47:54 -0400403 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400404 struct btrfs_trans_handle *trans;
405
Chris Mason2c90e5d2007-04-02 10:50:19 -0400406 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400407 trans = btrfs_start_transaction(root, 1);
408 btrfs_commit_transaction(trans, root);
409 /* run commit again to drop the original snapshot */
410 trans = btrfs_start_transaction(root, 1);
411 btrfs_commit_transaction(trans, root);
412 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400413 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400414 write_ctree_super(NULL, root);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400415 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500416
Chris Masoneb60cea2007-02-02 09:18:22 -0500417 if (root->node)
Chris Mason234b63a2007-03-13 10:46:10 -0400418 btrfs_block_release(root, root->node);
Chris Mason9f5fae22007-03-20 14:38:32 -0400419 if (root->fs_info->extent_root->node)
420 btrfs_block_release(root->fs_info->extent_root,
421 root->fs_info->extent_root->node);
422 if (root->fs_info->inode_root->node)
423 btrfs_block_release(root->fs_info->inode_root,
424 root->fs_info->inode_root->node);
425 if (root->fs_info->tree_root->node)
426 btrfs_block_release(root->fs_info->tree_root,
427 root->fs_info->tree_root->node);
Chris Mason234b63a2007-03-13 10:46:10 -0400428 btrfs_block_release(root, root->commit_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400429 btrfs_block_release(root, root->fs_info->sb_buffer);
Chris Mason87cbda52007-03-28 19:44:27 -0400430 crypto_free_hash(root->fs_info->hash_tfm);
Chris Mason7cfcc172007-04-02 14:53:59 -0400431 truncate_inode_pages(root->fs_info->btree_inode->i_mapping, 0);
432 iput(root->fs_info->btree_inode);
Chris Masone20d96d2007-03-22 12:13:20 -0400433 kfree(root->fs_info->extent_root);
434 kfree(root->fs_info->inode_root);
435 kfree(root->fs_info->tree_root);
Chris Masond6e4a422007-04-06 15:37:36 -0400436 kobject_unregister(&root->fs_info->kobj);
Chris Masone20d96d2007-03-22 12:13:20 -0400437 kfree(root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500438 return 0;
439}
440
Chris Masone20d96d2007-03-22 12:13:20 -0400441void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500442{
Chris Mason7cfcc172007-04-02 14:53:59 -0400443 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500444}
445