blob: bb133104e2e91213cafddb63263db15b923f18b2 [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 Masond6025572007-03-30 14:27:56 -040011#define PATTERN 0xDEADBEEFUL
12static inline void check_pattern(struct buffer_head *buf)
13{
14 if (buf->b_private != (void *)PATTERN)
15 WARN_ON(1);
16}
17
18static inline void set_pattern(struct buffer_head *buf)
19{
20 buf->b_private = (void *)PATTERN;
21}
Chris Masond98237b2007-03-28 13:57:48 -040022
Chris Masone20d96d2007-03-22 12:13:20 -040023static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050024{
Chris Masone20d96d2007-03-22 12:13:20 -040025 struct btrfs_node *node = btrfs_buffer_node(buf);
Chris Masond98237b2007-03-28 13:57:48 -040026 if (buf->b_blocknr != btrfs_header_blocknr(&node->header)) {
Chris Mason9a8dd152007-02-23 08:38:36 -050027 BUG();
Chris Masond98237b2007-03-28 13:57:48 -040028 }
Chris Masone20d96d2007-03-22 12:13:20 -040029 if (root->node && btrfs_header_parentid(&node->header) !=
Chris Masondf2ce342007-03-23 11:00:45 -040030 btrfs_header_parentid(btrfs_buffer_header(root->node))) {
Chris Mason7f5c1512007-03-23 15:56:19 -040031 BUG();
Chris Masondf2ce342007-03-23 11:00:45 -040032 }
Chris Mason9a8dd152007-02-23 08:38:36 -050033 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050034}
35
Chris Masond98237b2007-03-28 13:57:48 -040036struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050037{
Chris Masond98237b2007-03-28 13:57:48 -040038 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
39 int blockbits = root->fs_info->sb->s_blocksize_bits;
40 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
41 struct page *page;
42 struct buffer_head *bh;
43 struct buffer_head *head;
44 struct buffer_head *ret = NULL;
45
46 page = find_lock_page(mapping, index);
47 if (!page)
48 return NULL;
49
50 if (!page_has_buffers(page))
51 goto out_unlock;
52
53 head = page_buffers(page);
54 bh = head;
55 do {
56 if (buffer_mapped(bh) && bh->b_blocknr == blocknr) {
57 ret = bh;
58 get_bh(bh);
59 goto out_unlock;
60 }
61 bh = bh->b_this_page;
62 } while (bh != head);
63out_unlock:
64 unlock_page(page);
Chris Masond6025572007-03-30 14:27:56 -040065 if (ret) {
Chris Mason22b0ebd2007-03-30 08:47:31 -040066 touch_buffer(ret);
Chris Masond6025572007-03-30 14:27:56 -040067 check_pattern(ret);
68 }
Chris Masond98237b2007-03-28 13:57:48 -040069 page_cache_release(page);
70 return ret;
Chris Masoned2ff2c2007-03-01 18:59:40 -050071}
72
Chris Masond98237b2007-03-28 13:57:48 -040073struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
74 u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050075{
Chris Masond98237b2007-03-28 13:57:48 -040076 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
77 int blockbits = root->fs_info->sb->s_blocksize_bits;
78 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
79 struct page *page;
80 struct buffer_head *bh;
81 struct buffer_head *head;
82 struct buffer_head *ret = NULL;
83 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
Chris Mason22b0ebd2007-03-30 08:47:31 -040084
Chris Masond98237b2007-03-28 13:57:48 -040085 page = grab_cache_page(mapping, index);
86 if (!page)
87 return NULL;
88
Chris Masond98237b2007-03-28 13:57:48 -040089 if (!page_has_buffers(page))
90 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
91 head = page_buffers(page);
92 bh = head;
93 do {
94 if (!buffer_mapped(bh)) {
95 bh->b_bdev = root->fs_info->sb->s_bdev;
96 bh->b_blocknr = first_block;
97 set_buffer_mapped(bh);
Chris Masond6025572007-03-30 14:27:56 -040098 set_pattern(bh);
Chris Masond98237b2007-03-28 13:57:48 -040099 }
100 if (bh->b_blocknr == blocknr) {
101 ret = bh;
102 get_bh(bh);
103 goto out_unlock;
104 }
105 bh = bh->b_this_page;
106 first_block++;
107 } while (bh != head);
108out_unlock:
109 unlock_page(page);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400110 if (ret)
111 touch_buffer(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400112 page_cache_release(page);
113 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400114}
Chris Mason123abc82007-03-14 14:14:43 -0400115
Chris Masond98237b2007-03-28 13:57:48 -0400116static sector_t max_block(struct block_device *bdev)
117{
118 sector_t retval = ~((sector_t)0);
119 loff_t sz = i_size_read(bdev->bd_inode);
120
121 if (sz) {
122 unsigned int size = block_size(bdev);
123 unsigned int sizebits = blksize_bits(size);
124 retval = (sz >> sizebits);
125 }
126 return retval;
127}
128
129static int btree_get_block(struct inode *inode, sector_t iblock,
130 struct buffer_head *bh, int create)
131{
132 if (iblock >= max_block(inode->i_sb->s_bdev)) {
133 if (create)
134 return -EIO;
135
136 /*
137 * for reads, we're just trying to fill a partial page.
138 * return a hole, they will have to call get_block again
139 * before they can fill it, and they will get -EIO at that
140 * time
141 */
142 return 0;
143 }
144 bh->b_bdev = inode->i_sb->s_bdev;
145 bh->b_blocknr = iblock;
146 set_buffer_mapped(bh);
147 return 0;
148}
149
Chris Masonf254e522007-03-29 15:15:27 -0400150int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
151 char *result)
Chris Mason87cbda52007-03-28 19:44:27 -0400152{
Chris Mason87cbda52007-03-28 19:44:27 -0400153 struct scatterlist sg;
154 struct crypto_hash *tfm = root->fs_info->hash_tfm;
155 struct hash_desc desc;
156 int ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400157
158 desc.tfm = tfm;
159 desc.flags = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400160 sg_init_one(&sg, data, len);
Chris Mason87cbda52007-03-28 19:44:27 -0400161 spin_lock(&root->fs_info->hash_lock);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400162 ret = crypto_hash_digest(&desc, &sg, 1, result);
Chris Mason87cbda52007-03-28 19:44:27 -0400163 spin_unlock(&root->fs_info->hash_lock);
164 if (ret) {
165 printk("sha256 digest failed\n");
166 }
Chris Masonf254e522007-03-29 15:15:27 -0400167 return ret;
168}
169static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
170 int verify)
171{
172 char result[BTRFS_CSUM_SIZE];
173 int ret;
174 struct btrfs_node *node;
175
Chris Mason22b0ebd2007-03-30 08:47:31 -0400176 return 0;
Chris Masonf254e522007-03-29 15:15:27 -0400177 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
178 bh->b_size - BTRFS_CSUM_SIZE, result);
179 if (ret)
180 return ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400181 if (verify) {
Chris Masonf254e522007-03-29 15:15:27 -0400182 if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) {
183 printk("checksum verify failed on %lu\n",
184 bh->b_blocknr);
185 return 1;
186 }
187 } else {
188 node = btrfs_buffer_node(bh);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400189 memcpy(node->header.csum, result, BTRFS_CSUM_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -0400190 }
Chris Mason87cbda52007-03-28 19:44:27 -0400191 return 0;
192}
193
Chris Masond98237b2007-03-28 13:57:48 -0400194static int btree_writepage(struct page *page, struct writeback_control *wbc)
195{
Chris Mason22b0ebd2007-03-30 08:47:31 -0400196#if 0
Chris Mason87cbda52007-03-28 19:44:27 -0400197 struct buffer_head *bh;
198 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
199 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400200 if (!page_has_buffers(page)) {
201 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
202 (1 << BH_Dirty)|(1 << BH_Uptodate));
203 }
204 head = page_buffers(page);
205 bh = head;
206 do {
207 if (buffer_dirty(bh))
208 csum_tree_block(root, bh, 0);
209 bh = bh->b_this_page;
210 } while (bh != head);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400211#endif
Chris Masond98237b2007-03-28 13:57:48 -0400212 return block_write_full_page(page, btree_get_block, wbc);
213}
214
215static int btree_readpage(struct file * file, struct page * page)
216{
217 return block_read_full_page(page, btree_get_block);
218}
219
220static struct address_space_operations btree_aops = {
221 .readpage = btree_readpage,
222 .writepage = btree_writepage,
223 .sync_page = block_sync_page,
224};
225
Chris Masone20d96d2007-03-22 12:13:20 -0400226struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
227{
Chris Masond98237b2007-03-28 13:57:48 -0400228 struct buffer_head *bh = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400229
Chris Masond98237b2007-03-28 13:57:48 -0400230 bh = btrfs_find_create_tree_block(root, blocknr);
231 if (!bh)
232 return bh;
233 lock_buffer(bh);
234 if (!buffer_uptodate(bh)) {
235 get_bh(bh);
236 bh->b_end_io = end_buffer_read_sync;
237 submit_bh(READ, bh);
238 wait_on_buffer(bh);
239 if (!buffer_uptodate(bh))
240 goto fail;
Chris Mason87cbda52007-03-28 19:44:27 -0400241 csum_tree_block(root, bh, 1);
Chris Masond6025572007-03-30 14:27:56 -0400242 set_pattern(bh);
Chris Masond98237b2007-03-28 13:57:48 -0400243 } else {
244 unlock_buffer(bh);
245 }
246 if (check_tree_block(root, bh))
Chris Masoncfaa7292007-02-21 17:04:57 -0500247 BUG();
Chris Masond98237b2007-03-28 13:57:48 -0400248 return bh;
249fail:
250 brelse(bh);
251 return NULL;
252
Chris Masoneb60cea2007-02-02 09:18:22 -0500253}
254
Chris Masone089f052007-03-16 16:20:31 -0400255int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400256 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500257{
Chris Masond6025572007-03-30 14:27:56 -0400258 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400259 mark_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500260 return 0;
261}
262
Chris Masone089f052007-03-16 16:20:31 -0400263int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400264 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500265{
Chris Masond6025572007-03-30 14:27:56 -0400266 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400267 clear_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500268 return 0;
269}
270
Chris Mason123abc82007-03-14 14:14:43 -0400271static int __setup_root(struct btrfs_super_block *super,
Chris Mason9f5fae22007-03-20 14:38:32 -0400272 struct btrfs_root *root,
273 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400274 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500275{
Chris Masoncfaa7292007-02-21 17:04:57 -0500276 root->node = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500277 root->commit_root = NULL;
Chris Mason123abc82007-03-14 14:14:43 -0400278 root->blocksize = btrfs_super_blocksize(super);
279 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400280 root->fs_info = fs_info;
Chris Mason3768f362007-03-13 16:47:54 -0400281 memset(&root->root_key, 0, sizeof(root->root_key));
282 memset(&root->root_item, 0, sizeof(root->root_item));
283 return 0;
284}
285
Chris Mason123abc82007-03-14 14:14:43 -0400286static int find_and_setup_root(struct btrfs_super_block *super,
Chris Mason9f5fae22007-03-20 14:38:32 -0400287 struct btrfs_root *tree_root,
288 struct btrfs_fs_info *fs_info,
289 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400290 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400291{
292 int ret;
293
Chris Masone20d96d2007-03-22 12:13:20 -0400294 __setup_root(super, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400295 ret = btrfs_find_last_root(tree_root, objectid,
296 &root->root_item, &root->root_key);
297 BUG_ON(ret);
298
299 root->node = read_tree_block(root,
300 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400301 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500302 return 0;
303}
304
Chris Masone20d96d2007-03-22 12:13:20 -0400305struct btrfs_root *open_ctree(struct super_block *sb,
306 struct buffer_head *sb_buffer,
307 struct btrfs_super_block *disk_super)
Chris Masoneb60cea2007-02-02 09:18:22 -0500308{
Chris Masone20d96d2007-03-22 12:13:20 -0400309 struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
310 GFP_NOFS);
311 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
312 GFP_NOFS);
313 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
314 GFP_NOFS);
315 struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
316 GFP_NOFS);
317 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
318 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500319 int ret;
320
Chris Mason87cbda52007-03-28 19:44:27 -0400321 if (!btrfs_super_root(disk_super)) {
Chris Masone20d96d2007-03-22 12:13:20 -0400322 return NULL;
Chris Mason87cbda52007-03-28 19:44:27 -0400323 }
Chris Mason8ef97622007-03-26 10:15:30 -0400324 init_bit_radix(&fs_info->pinned_radix);
325 init_bit_radix(&fs_info->pending_del_radix);
Chris Masond98237b2007-03-28 13:57:48 -0400326 sb_set_blocksize(sb, sb_buffer->b_size);
Chris Mason9f5fae22007-03-20 14:38:32 -0400327 fs_info->running_transaction = NULL;
328 fs_info->fs_root = root;
329 fs_info->tree_root = tree_root;
330 fs_info->extent_root = extent_root;
331 fs_info->inode_root = inode_root;
332 fs_info->last_inode_alloc = 0;
333 fs_info->last_inode_alloc_dirid = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400334 fs_info->disk_super = disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400335 fs_info->sb = sb;
Chris Masond98237b2007-03-28 13:57:48 -0400336 fs_info->btree_inode = new_inode(sb);
337 fs_info->btree_inode->i_ino = 1;
338 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
339 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason22b0ebd2007-03-30 08:47:31 -0400340 insert_inode_hash(fs_info->btree_inode);
341
Chris Masond98237b2007-03-28 13:57:48 -0400342 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason87cbda52007-03-28 19:44:27 -0400343 fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
Chris Mason30ae8462007-03-29 09:59:15 -0400344 spin_lock_init(&fs_info->hash_lock);
345
346 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
Chris Mason87cbda52007-03-28 19:44:27 -0400347 printk("failed to allocate sha256 hash\n");
348 return NULL;
349 }
Chris Masond98237b2007-03-28 13:57:48 -0400350
Chris Mason79154b12007-03-22 15:59:16 -0400351 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400352 mutex_init(&fs_info->fs_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400353 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
354 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
Chris Mason3768f362007-03-13 16:47:54 -0400355
Chris Masone20d96d2007-03-22 12:13:20 -0400356 __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Masond98237b2007-03-28 13:57:48 -0400357
358 fs_info->sb_buffer = read_tree_block(tree_root, sb_buffer->b_blocknr);
359
Chris Mason87cbda52007-03-28 19:44:27 -0400360 if (!fs_info->sb_buffer) {
361printk("failed2\n");
Chris Masond98237b2007-03-28 13:57:48 -0400362 return NULL;
Chris Mason87cbda52007-03-28 19:44:27 -0400363 }
Chris Masond98237b2007-03-28 13:57:48 -0400364 brelse(sb_buffer);
365 sb_buffer = NULL;
366 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
367 fs_info->disk_super = disk_super;
368
Chris Masone20d96d2007-03-22 12:13:20 -0400369 tree_root->node = read_tree_block(tree_root,
370 btrfs_super_root(disk_super));
Chris Mason3768f362007-03-13 16:47:54 -0400371 BUG_ON(!tree_root->node);
372
Chris Masone20d96d2007-03-22 12:13:20 -0400373 ret = find_and_setup_root(disk_super, tree_root, fs_info,
374 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400375 BUG_ON(ret);
376
Chris Masone20d96d2007-03-22 12:13:20 -0400377 ret = find_and_setup_root(disk_super, tree_root, fs_info,
378 BTRFS_INODE_MAP_OBJECTID, inode_root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400379 BUG_ON(ret);
380
Chris Masone20d96d2007-03-22 12:13:20 -0400381 ret = find_and_setup_root(disk_super, tree_root, fs_info,
382 BTRFS_FS_TREE_OBJECTID, root);
Chris Mason3768f362007-03-13 16:47:54 -0400383 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500384 root->commit_root = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400385 get_bh(root->node);
Chris Mason3768f362007-03-13 16:47:54 -0400386 root->ref_cows = 1;
Chris Mason293ffd52007-03-20 15:57:25 -0400387 root->fs_info->generation = root->root_key.offset + 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500388 return root;
389}
390
Chris Masone089f052007-03-16 16:20:31 -0400391int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400392 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500393{
Chris Masond5719762007-03-23 10:01:08 -0400394 struct buffer_head *bh = root->fs_info->sb_buffer;
395 btrfs_set_super_root(root->fs_info->disk_super,
396 root->fs_info->tree_root->node->b_blocknr);
397 lock_buffer(bh);
398 clear_buffer_dirty(bh);
Chris Mason87cbda52007-03-28 19:44:27 -0400399 csum_tree_block(root, bh, 0);
Chris Masond5719762007-03-23 10:01:08 -0400400 bh->b_end_io = end_buffer_write_sync;
401 get_bh(bh);
402 submit_bh(WRITE, bh);
403 wait_on_buffer(bh);
404 if (!buffer_uptodate(bh)) {
405 WARN_ON(1);
406 return -EIO;
Chris Masoncfaa7292007-02-21 17:04:57 -0500407 }
408 return 0;
409}
410
Chris Masone20d96d2007-03-22 12:13:20 -0400411int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500412{
Chris Mason3768f362007-03-13 16:47:54 -0400413 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400414 struct btrfs_trans_handle *trans;
415
Chris Mason79154b12007-03-22 15:59:16 -0400416 trans = btrfs_start_transaction(root, 1);
417 btrfs_commit_transaction(trans, root);
418 /* run commit again to drop the original snapshot */
419 trans = btrfs_start_transaction(root, 1);
420 btrfs_commit_transaction(trans, root);
421 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400422 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400423 write_ctree_super(NULL, root);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500424
Chris Masoneb60cea2007-02-02 09:18:22 -0500425 if (root->node)
Chris Mason234b63a2007-03-13 10:46:10 -0400426 btrfs_block_release(root, root->node);
Chris Mason9f5fae22007-03-20 14:38:32 -0400427 if (root->fs_info->extent_root->node)
428 btrfs_block_release(root->fs_info->extent_root,
429 root->fs_info->extent_root->node);
430 if (root->fs_info->inode_root->node)
431 btrfs_block_release(root->fs_info->inode_root,
432 root->fs_info->inode_root->node);
433 if (root->fs_info->tree_root->node)
434 btrfs_block_release(root->fs_info->tree_root,
435 root->fs_info->tree_root->node);
Chris Mason234b63a2007-03-13 10:46:10 -0400436 btrfs_block_release(root, root->commit_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400437 btrfs_block_release(root, root->fs_info->sb_buffer);
Chris Mason87cbda52007-03-28 19:44:27 -0400438 crypto_free_hash(root->fs_info->hash_tfm);
Chris Mason30ae8462007-03-29 09:59:15 -0400439 truncate_inode_pages(root->fs_info->btree_inode->i_mapping, 0);
Chris Masond98237b2007-03-28 13:57:48 -0400440 iput(root->fs_info->btree_inode);
Chris Masone20d96d2007-03-22 12:13:20 -0400441 kfree(root->fs_info->extent_root);
442 kfree(root->fs_info->inode_root);
443 kfree(root->fs_info->tree_root);
444 kfree(root->fs_info);
445 kfree(root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500446 return 0;
447}
448
Chris Masone20d96d2007-03-22 12:13:20 -0400449void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500450{
Chris Masond6025572007-03-30 14:27:56 -0400451 check_pattern(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400452 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500453}
454