blob: eebb4fb65c61cf414a96089b0ee587fc13d92cd9 [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 Masone20d96d2007-03-22 12:13:20 -040019#include <linux/fs.h>
Chris Masond98237b2007-03-28 13:57:48 -040020#include <linux/blkdev.h>
Chris Mason11bd1432007-06-22 14:16:24 -040021#include <linux/crc32c.h>
Chris Mason87cbda52007-03-28 19:44:27 -040022#include <linux/scatterlist.h>
Chris Mason22b0ebd2007-03-30 08:47:31 -040023#include <linux/swap.h>
Chris Mason0f7d52f2007-04-09 10:42:37 -040024#include <linux/radix-tree.h>
Chris Mason35b7e472007-05-02 15:53:43 -040025#include <linux/writeback.h>
Chris Mason5f39d392007-10-15 16:14:19 -040026#include <linux/buffer_head.h> // for block_sync_page
Chris Masoneb60cea2007-02-02 09:18:22 -050027#include "ctree.h"
28#include "disk-io.h"
Chris Masone089f052007-03-16 16:20:31 -040029#include "transaction.h"
Chris Mason0f7d52f2007-04-09 10:42:37 -040030#include "btrfs_inode.h"
Chris Masondb945352007-10-15 16:15:53 -040031#include "print-tree.h"
Chris Masoneb60cea2007-02-02 09:18:22 -050032
Chris Mason5f39d392007-10-15 16:14:19 -040033#if 0
34static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
Chris Mason7eccb902007-04-11 15:53:25 -040035{
Chris Mason5f39d392007-10-15 16:14:19 -040036 if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
37 printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
38 (unsigned long long)extent_buffer_blocknr(buf),
39 (unsigned long long)btrfs_header_blocknr(buf));
Chris Mason39279cc2007-06-12 06:35:45 -040040 return 1;
Chris Masond98237b2007-03-28 13:57:48 -040041 }
Chris Mason9a8dd152007-02-23 08:38:36 -050042 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050043}
Chris Mason5f39d392007-10-15 16:14:19 -040044#endif
Chris Masoneb60cea2007-02-02 09:18:22 -050045
Chris Mason0da54682007-11-07 21:08:01 -050046static struct extent_map_ops btree_extent_map_ops;
47
Chris Mason5f39d392007-10-15 16:14:19 -040048struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -040049 u64 bytenr, u32 blocksize)
Chris Masoned2ff2c2007-03-01 18:59:40 -050050{
Chris Mason5f39d392007-10-15 16:14:19 -040051 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonf510cfe2007-10-15 16:14:48 -040052 struct extent_buffer *eb;
53 eb = find_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
Chris Masondb945352007-10-15 16:15:53 -040054 bytenr, blocksize, GFP_NOFS);
Chris Masonf510cfe2007-10-15 16:14:48 -040055 return eb;
Chris Masoned2ff2c2007-03-01 18:59:40 -050056}
57
Chris Mason5f39d392007-10-15 16:14:19 -040058struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -040059 u64 bytenr, u32 blocksize)
Chris Masoneb60cea2007-02-02 09:18:22 -050060{
Chris Mason5f39d392007-10-15 16:14:19 -040061 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonf510cfe2007-10-15 16:14:48 -040062 struct extent_buffer *eb;
Chris Masondb945352007-10-15 16:15:53 -040063
Chris Masonf510cfe2007-10-15 16:14:48 -040064 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -040065 bytenr, blocksize, NULL, GFP_NOFS);
Chris Masonf510cfe2007-10-15 16:14:48 -040066 return eb;
Chris Mason5f39d392007-10-15 16:14:19 -040067}
Chris Mason22b0ebd2007-03-30 08:47:31 -040068
Chris Mason5f39d392007-10-15 16:14:19 -040069struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
70 size_t page_offset, u64 start, u64 end,
71 int create)
72{
73 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
74 struct extent_map *em;
75 int ret;
Chris Masond98237b2007-03-28 13:57:48 -040076
Chris Mason5f39d392007-10-15 16:14:19 -040077again:
78 em = lookup_extent_mapping(em_tree, start, end);
79 if (em) {
80 goto out;
81 }
82 em = alloc_extent_map(GFP_NOFS);
83 if (!em) {
84 em = ERR_PTR(-ENOMEM);
85 goto out;
86 }
87 em->start = 0;
88 em->end = (i_size_read(inode) & ~((u64)PAGE_CACHE_SIZE -1)) - 1;
89 em->block_start = 0;
90 em->block_end = em->end;
91 em->bdev = inode->i_sb->s_bdev;
92 ret = add_extent_mapping(em_tree, em);
93 if (ret == -EEXIST) {
94 free_extent_map(em);
95 em = NULL;
96 goto again;
97 } else if (ret) {
98 em = ERR_PTR(ret);
99 }
100out:
101 return em;
102}
103
Chris Mason19c00dd2007-10-15 16:19:22 -0400104u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
105{
106 return crc32c(seed, data, len);
107}
108
109void btrfs_csum_final(u32 crc, char *result)
110{
111 *(__le32 *)result = ~cpu_to_le32(crc);
112}
113
114static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
115 int verify)
116{
117 char result[BTRFS_CRC32_SIZE];
118 unsigned long len;
119 unsigned long cur_len;
120 unsigned long offset = BTRFS_CSUM_SIZE;
121 char *map_token = NULL;
122 char *kaddr;
123 unsigned long map_start;
124 unsigned long map_len;
125 int err;
126 u32 crc = ~(u32)0;
127
128 len = buf->len - offset;
129 while(len > 0) {
130 err = map_private_extent_buffer(buf, offset, 32,
131 &map_token, &kaddr,
132 &map_start, &map_len, KM_USER0);
133 if (err) {
134 printk("failed to map extent buffer! %lu\n",
135 offset);
136 return 1;
137 }
138 cur_len = min(len, map_len - (offset - map_start));
139 crc = btrfs_csum_data(root, kaddr + offset - map_start,
140 crc, cur_len);
141 len -= cur_len;
142 offset += cur_len;
143 unmap_extent_buffer(buf, map_token, KM_USER0);
144 }
145 btrfs_csum_final(crc, result);
146
147 if (verify) {
148 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
149 printk("btrfs: %s checksum verify failed on %llu\n",
150 root->fs_info->sb->s_id,
151 buf->start);
152 return 1;
153 }
154 } else {
155 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
156 }
157 return 0;
158}
159
160
161int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
162{
163 struct extent_map_tree *tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400164 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason19c00dd2007-10-15 16:19:22 -0400165 u64 found_start;
166 int found_level;
167 unsigned long len;
168 struct extent_buffer *eb;
169 tree = &BTRFS_I(page->mapping->host)->extent_tree;
170
171 if (page->private == EXTENT_PAGE_PRIVATE)
172 goto out;
173 if (!page->private)
174 goto out;
175 len = page->private >> 2;
176 if (len == 0) {
177 WARN_ON(1);
178 }
179 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
180 read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1);
181 found_start = btrfs_header_bytenr(eb);
182 if (found_start != start) {
183 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
184 start, found_start, len);
185 }
186 found_level = btrfs_header_level(eb);
187 csum_tree_block(root, eb, 0);
188 free_extent_buffer(eb);
189out:
190 return 0;
191}
192
Chris Mason0da54682007-11-07 21:08:01 -0500193static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
194{
195 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
196
197 csum_dirty_buffer(root, page);
198 return 0;
199}
200
Chris Mason5f39d392007-10-15 16:14:19 -0400201static int btree_writepage(struct page *page, struct writeback_control *wbc)
202{
203 struct extent_map_tree *tree;
204 tree = &BTRFS_I(page->mapping->host)->extent_tree;
205 return extent_write_full_page(tree, page, btree_get_extent, wbc);
206}
Chris Mason0da54682007-11-07 21:08:01 -0500207
208static int btree_writepages(struct address_space *mapping,
209 struct writeback_control *wbc)
210{
211 struct extent_map_tree *tree;
212 tree = &BTRFS_I(mapping->host)->extent_tree;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500213 if (wbc->sync_mode == WB_SYNC_NONE) {
Chris Mason793955b2007-11-26 16:34:41 -0800214 u64 num_dirty;
215 u64 start = 0;
216 unsigned long thresh = 96 * 1024 * 1024;
Chris Mason448d6402007-11-27 07:52:01 -0800217
218 if (wbc->for_kupdate)
219 return 0;
220
Chris Masonca664622007-11-27 11:16:35 -0500221 if (current_is_pdflush()) {
222 thresh = 96 * 1024 * 1024;
223 } else {
224 thresh = 8 * 1024 * 1024;
225 }
Chris Mason1832a6d2007-12-21 16:27:21 -0500226 num_dirty = count_range_bits(tree, &start, (u64)-1,
227 thresh, EXTENT_DIRTY);
Chris Mason793955b2007-11-26 16:34:41 -0800228 if (num_dirty < thresh) {
229 return 0;
230 }
231 }
Chris Mason0da54682007-11-07 21:08:01 -0500232 return extent_writepages(tree, mapping, btree_get_extent, wbc);
233}
234
Chris Mason5f39d392007-10-15 16:14:19 -0400235int btree_readpage(struct file *file, struct page *page)
236{
237 struct extent_map_tree *tree;
238 tree = &BTRFS_I(page->mapping->host)->extent_tree;
239 return extent_read_full_page(tree, page, btree_get_extent);
240}
241
242static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags)
243{
244 struct extent_map_tree *tree;
245 int ret;
246
Chris Mason5f39d392007-10-15 16:14:19 -0400247 tree = &BTRFS_I(page->mapping->host)->extent_tree;
248 ret = try_release_extent_mapping(tree, page);
249 if (ret == 1) {
250 ClearPagePrivate(page);
251 set_page_private(page, 0);
252 page_cache_release(page);
253 }
Chris Masond98237b2007-03-28 13:57:48 -0400254 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400255}
Chris Mason123abc82007-03-14 14:14:43 -0400256
Chris Mason5f39d392007-10-15 16:14:19 -0400257static void btree_invalidatepage(struct page *page, unsigned long offset)
Chris Masond98237b2007-03-28 13:57:48 -0400258{
Chris Mason5f39d392007-10-15 16:14:19 -0400259 struct extent_map_tree *tree;
260 tree = &BTRFS_I(page->mapping->host)->extent_tree;
261 extent_invalidatepage(tree, page, offset);
262 btree_releasepage(page, GFP_NOFS);
Chris Masond98237b2007-03-28 13:57:48 -0400263}
264
Chris Mason5f39d392007-10-15 16:14:19 -0400265#if 0
Chris Masond98237b2007-03-28 13:57:48 -0400266static int btree_writepage(struct page *page, struct writeback_control *wbc)
267{
Chris Mason87cbda52007-03-28 19:44:27 -0400268 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400269 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400270 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400271 if (!page_has_buffers(page)) {
272 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
273 (1 << BH_Dirty)|(1 << BH_Uptodate));
274 }
275 head = page_buffers(page);
276 bh = head;
277 do {
278 if (buffer_dirty(bh))
279 csum_tree_block(root, bh, 0);
280 bh = bh->b_this_page;
281 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400282 return block_write_full_page(page, btree_get_block, wbc);
283}
Chris Mason5f39d392007-10-15 16:14:19 -0400284#endif
Chris Masond98237b2007-03-28 13:57:48 -0400285
286static struct address_space_operations btree_aops = {
287 .readpage = btree_readpage,
288 .writepage = btree_writepage,
Chris Mason0da54682007-11-07 21:08:01 -0500289 .writepages = btree_writepages,
Chris Mason5f39d392007-10-15 16:14:19 -0400290 .releasepage = btree_releasepage,
291 .invalidatepage = btree_invalidatepage,
Chris Masond98237b2007-03-28 13:57:48 -0400292 .sync_page = block_sync_page,
293};
294
Chris Masondb945352007-10-15 16:15:53 -0400295int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
Chris Mason090d1872007-05-01 08:53:32 -0400296{
Chris Mason5f39d392007-10-15 16:14:19 -0400297 struct extent_buffer *buf = NULL;
298 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonde428b62007-05-18 13:28:27 -0400299 int ret = 0;
Chris Mason090d1872007-05-01 08:53:32 -0400300
Chris Masondb945352007-10-15 16:15:53 -0400301 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400302 if (!buf)
Chris Mason090d1872007-05-01 08:53:32 -0400303 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400304 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -0400305 buf, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400306 free_extent_buffer(buf);
Chris Masonde428b62007-05-18 13:28:27 -0400307 return ret;
Chris Mason090d1872007-05-01 08:53:32 -0400308}
309
Chris Masondb945352007-10-15 16:15:53 -0400310struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
311 u32 blocksize)
Chris Masone20d96d2007-03-22 12:13:20 -0400312{
Chris Mason5f39d392007-10-15 16:14:19 -0400313 struct extent_buffer *buf = NULL;
314 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason19c00dd2007-10-15 16:19:22 -0400315 struct extent_map_tree *extent_tree;
316 int ret;
317
318 extent_tree = &BTRFS_I(btree_inode)->extent_tree;
Chris Masone20d96d2007-03-22 12:13:20 -0400319
Chris Masondb945352007-10-15 16:15:53 -0400320 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400321 if (!buf)
322 return NULL;
323 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -0400324 buf, 0, 1);
325 if (buf->flags & EXTENT_CSUM) {
326 return buf;
327 }
328 if (test_range_bit(extent_tree, buf->start, buf->start + buf->len - 1,
329 EXTENT_CSUM, 1)) {
330 buf->flags |= EXTENT_CSUM;
331 return buf;
332 }
333 ret = csum_tree_block(root, buf, 1);
334 set_extent_bits(extent_tree, buf->start,
335 buf->start + buf->len - 1,
336 EXTENT_CSUM, GFP_NOFS);
337 buf->flags |= EXTENT_CSUM;
Chris Mason5f39d392007-10-15 16:14:19 -0400338 return buf;
Chris Masoneb60cea2007-02-02 09:18:22 -0500339}
340
Chris Masone089f052007-03-16 16:20:31 -0400341int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400342 struct extent_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500343{
Chris Mason5f39d392007-10-15 16:14:19 -0400344 struct inode *btree_inode = root->fs_info->btree_inode;
345 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
346 return 0;
347}
348
349int wait_on_tree_block_writeback(struct btrfs_root *root,
350 struct extent_buffer *buf)
351{
352 struct inode *btree_inode = root->fs_info->btree_inode;
353 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree,
354 buf);
355 return 0;
356}
357
Chris Masondb945352007-10-15 16:15:53 -0400358static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500359 u32 stripesize, struct btrfs_root *root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400360 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400361 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500362{
Chris Masoncfaa7292007-02-21 17:04:57 -0500363 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400364 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500365 root->commit_root = NULL;
Chris Masondb945352007-10-15 16:15:53 -0400366 root->sectorsize = sectorsize;
367 root->nodesize = nodesize;
368 root->leafsize = leafsize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500369 root->stripesize = stripesize;
Chris Mason123abc82007-03-14 14:14:43 -0400370 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400371 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400372 root->objectid = objectid;
373 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400374 root->highest_inode = 0;
375 root->last_inode_alloc = 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400376 root->name = NULL;
Chris Mason3768f362007-03-13 16:47:54 -0400377 memset(&root->root_key, 0, sizeof(root->root_key));
378 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason6702ed42007-08-07 16:15:09 -0400379 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
Josef Bacik58176a92007-08-29 15:47:34 -0400380 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
381 init_completion(&root->kobj_unregister);
Chris Mason011410b2007-09-10 19:58:36 -0400382 init_rwsem(&root->snap_sem);
Chris Mason6702ed42007-08-07 16:15:09 -0400383 root->defrag_running = 0;
384 root->defrag_level = 0;
Chris Mason4d775672007-04-20 20:23:12 -0400385 root->root_key.objectid = objectid;
Chris Mason3768f362007-03-13 16:47:54 -0400386 return 0;
387}
388
Chris Masondb945352007-10-15 16:15:53 -0400389static int find_and_setup_root(struct btrfs_root *tree_root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400390 struct btrfs_fs_info *fs_info,
391 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400392 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400393{
394 int ret;
Chris Masondb945352007-10-15 16:15:53 -0400395 u32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -0400396
Chris Masondb945352007-10-15 16:15:53 -0400397 __setup_root(tree_root->nodesize, tree_root->leafsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500398 tree_root->sectorsize, tree_root->stripesize,
399 root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400400 ret = btrfs_find_last_root(tree_root, objectid,
401 &root->root_item, &root->root_key);
402 BUG_ON(ret);
403
Chris Masondb945352007-10-15 16:15:53 -0400404 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
405 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
406 blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400407 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500408 return 0;
409}
410
Chris Mason5eda7b52007-06-22 14:16:25 -0400411struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
412 struct btrfs_key *location)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400413{
414 struct btrfs_root *root;
415 struct btrfs_root *tree_root = fs_info->tree_root;
416 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400417 struct extent_buffer *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400418 u64 highest_inode;
Chris Masondb945352007-10-15 16:15:53 -0400419 u32 blocksize;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400420 int ret = 0;
421
Chris Mason5eda7b52007-06-22 14:16:25 -0400422 root = kzalloc(sizeof(*root), GFP_NOFS);
Chris Mason0cf6c622007-06-09 09:22:25 -0400423 if (!root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400424 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400425 if (location->offset == (u64)-1) {
Chris Masondb945352007-10-15 16:15:53 -0400426 ret = find_and_setup_root(tree_root, fs_info,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400427 location->objectid, root);
428 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400429 kfree(root);
430 return ERR_PTR(ret);
431 }
432 goto insert;
433 }
434
Chris Masondb945352007-10-15 16:15:53 -0400435 __setup_root(tree_root->nodesize, tree_root->leafsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500436 tree_root->sectorsize, tree_root->stripesize,
437 root, fs_info, location->objectid);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400438
439 path = btrfs_alloc_path();
440 BUG_ON(!path);
441 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
442 if (ret != 0) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400443 if (ret > 0)
444 ret = -ENOENT;
445 goto out;
446 }
Chris Mason5f39d392007-10-15 16:14:19 -0400447 l = path->nodes[0];
448 read_extent_buffer(l, &root->root_item,
449 btrfs_item_ptr_offset(l, path->slots[0]),
Chris Mason0f7d52f2007-04-09 10:42:37 -0400450 sizeof(root->root_item));
Yan Zheng44b36eb2007-10-19 09:22:56 -0400451 memcpy(&root->root_key, location, sizeof(*location));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400452 ret = 0;
453out:
454 btrfs_release_path(root, path);
455 btrfs_free_path(path);
456 if (ret) {
457 kfree(root);
458 return ERR_PTR(ret);
459 }
Chris Masondb945352007-10-15 16:15:53 -0400460 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
461 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
462 blocksize);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400463 BUG_ON(!root->node);
464insert:
Chris Mason0f7d52f2007-04-09 10:42:37 -0400465 root->ref_cows = 1;
Chris Mason5eda7b52007-06-22 14:16:25 -0400466 ret = btrfs_find_highest_inode(root, &highest_inode);
467 if (ret == 0) {
468 root->highest_inode = highest_inode;
469 root->last_inode_alloc = highest_inode;
470 }
471 return root;
472}
473
474struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
Josef Bacik58176a92007-08-29 15:47:34 -0400475 struct btrfs_key *location,
476 const char *name, int namelen)
Chris Mason5eda7b52007-06-22 14:16:25 -0400477{
478 struct btrfs_root *root;
479 int ret;
480
481 root = radix_tree_lookup(&fs_info->fs_roots_radix,
482 (unsigned long)location->objectid);
483 if (root)
484 return root;
485
486 root = btrfs_read_fs_root_no_radix(fs_info, location);
487 if (IS_ERR(root))
488 return root;
Chris Mason2619ba12007-04-10 16:58:11 -0400489 ret = radix_tree_insert(&fs_info->fs_roots_radix,
490 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400491 root);
492 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400493 free_extent_buffer(root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400494 kfree(root);
495 return ERR_PTR(ret);
496 }
Josef Bacik58176a92007-08-29 15:47:34 -0400497
498 ret = btrfs_set_root_name(root, name, namelen);
499 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400500 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400501 kfree(root);
502 return ERR_PTR(ret);
503 }
504
505 ret = btrfs_sysfs_add_root(root);
506 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400507 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400508 kfree(root->name);
509 kfree(root);
510 return ERR_PTR(ret);
511 }
512
Chris Mason5ce14bb2007-09-11 11:15:39 -0400513 ret = btrfs_find_dead_roots(fs_info->tree_root,
514 root->root_key.objectid, root);
515 BUG_ON(ret);
516
Chris Mason0f7d52f2007-04-09 10:42:37 -0400517 return root;
518}
Chris Mason19c00dd2007-10-15 16:19:22 -0400519#if 0
520static int add_hasher(struct btrfs_fs_info *info, char *type) {
521 struct btrfs_hasher *hasher;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400522
Chris Mason19c00dd2007-10-15 16:19:22 -0400523 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
524 if (!hasher)
525 return -ENOMEM;
526 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
527 if (!hasher->hash_tfm) {
528 kfree(hasher);
529 return -EINVAL;
530 }
531 spin_lock(&info->hash_lock);
532 list_add(&hasher->list, &info->hashers);
533 spin_unlock(&info->hash_lock);
534 return 0;
535}
536#endif
Chris Mason2c90e5d2007-04-02 10:50:19 -0400537struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500538{
Chris Masondb945352007-10-15 16:15:53 -0400539 u32 sectorsize;
540 u32 nodesize;
541 u32 leafsize;
542 u32 blocksize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500543 u32 stripesize;
Chris Masone20d96d2007-03-22 12:13:20 -0400544 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
545 GFP_NOFS);
546 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
547 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400548 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
549 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500550 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400551 int err = -EIO;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400552 struct btrfs_super_block *disk_super;
Chris Masoneb60cea2007-02-02 09:18:22 -0500553
Chris Mason39279cc2007-06-12 06:35:45 -0400554 if (!extent_root || !tree_root || !fs_info) {
555 err = -ENOMEM;
556 goto fail;
557 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400558 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason8fd17792007-04-19 21:01:03 -0400559 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -0400560 INIT_LIST_HEAD(&fs_info->dead_roots);
Chris Mason19c00dd2007-10-15 16:19:22 -0400561 INIT_LIST_HEAD(&fs_info->hashers);
562 spin_lock_init(&fs_info->hash_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -0500563 spin_lock_init(&fs_info->delalloc_lock);
Chris Mason19c00dd2007-10-15 16:19:22 -0400564
Josef Bacik58176a92007-08-29 15:47:34 -0400565 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
566 init_completion(&fs_info->kobj_unregister);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400567 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400568 fs_info->running_transaction = NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400569 fs_info->last_trans_committed = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400570 fs_info->tree_root = tree_root;
571 fs_info->extent_root = extent_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400572 fs_info->sb = sb;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500573 fs_info->mount_opt = 0;
Chris Masonc59f8952007-12-17 20:14:04 -0500574 fs_info->max_extent = (u64)-1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500575 fs_info->delalloc_bytes = 0;
Chris Masond98237b2007-03-28 13:57:48 -0400576 fs_info->btree_inode = new_inode(sb);
577 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400578 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400579 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
580 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason5f39d392007-10-15 16:14:19 -0400581 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
582 fs_info->btree_inode->i_mapping,
583 GFP_NOFS);
Chris Mason0da54682007-11-07 21:08:01 -0500584 BTRFS_I(fs_info->btree_inode)->extent_tree.ops = &btree_extent_map_ops;
585
Chris Masonf510cfe2007-10-15 16:14:48 -0400586 extent_map_tree_init(&fs_info->free_space_cache,
587 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason96b51792007-10-15 16:15:19 -0400588 extent_map_tree_init(&fs_info->block_group_cache,
589 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason1a5bc162007-10-15 16:15:26 -0400590 extent_map_tree_init(&fs_info->pinned_extents,
591 fs_info->btree_inode->i_mapping, GFP_NOFS);
592 extent_map_tree_init(&fs_info->pending_del,
593 fs_info->btree_inode->i_mapping, GFP_NOFS);
594 extent_map_tree_init(&fs_info->extent_ins,
595 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masone66f7092007-04-20 13:16:02 -0400596 fs_info->do_barriers = 1;
Chris Masonfacda1e2007-06-08 18:11:48 -0400597 fs_info->closing = 0;
Yan324ae4d2007-11-16 14:57:08 -0500598 fs_info->total_pinned = 0;
Chris Mason6da6aba2007-12-18 16:15:09 -0500599#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
600 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
601#else
Chris Mason08607c12007-06-08 15:33:54 -0400602 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
Chris Mason6da6aba2007-12-18 16:15:09 -0500603#endif
Chris Mason0f7d52f2007-04-09 10:42:37 -0400604 BTRFS_I(fs_info->btree_inode)->root = tree_root;
605 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
606 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400607 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400608 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400609
Chris Mason79154b12007-03-22 15:59:16 -0400610 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400611 mutex_init(&fs_info->fs_mutex);
Chris Mason3768f362007-03-13 16:47:54 -0400612
Chris Mason19c00dd2007-10-15 16:19:22 -0400613#if 0
614 ret = add_hasher(fs_info, "crc32c");
615 if (ret) {
616 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
617 err = -ENOMEM;
618 goto fail_iput;
619 }
620#endif
Chris Mason87ee04e2007-11-30 11:30:34 -0500621 __setup_root(512, 512, 512, 512, tree_root,
Chris Mason2c90e5d2007-04-02 10:50:19 -0400622 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400623
Chris Mason2c90e5d2007-04-02 10:50:19 -0400624 fs_info->sb_buffer = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -0400625 BTRFS_SUPER_INFO_OFFSET,
626 512);
Chris Masond98237b2007-03-28 13:57:48 -0400627
Chris Mason0f7d52f2007-04-09 10:42:37 -0400628 if (!fs_info->sb_buffer)
Chris Mason39279cc2007-06-12 06:35:45 -0400629 goto fail_iput;
Chris Mason39279cc2007-06-12 06:35:45 -0400630
Chris Mason5f39d392007-10-15 16:14:19 -0400631 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
632 sizeof(fs_info->super_copy));
633
634 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
635 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
636 BTRFS_FSID_SIZE);
637 disk_super = &fs_info->super_copy;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400638 if (!btrfs_super_root(disk_super))
Chris Mason39279cc2007-06-12 06:35:45 -0400639 goto fail_sb_buffer;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400640
Chris Masondb945352007-10-15 16:15:53 -0400641 nodesize = btrfs_super_nodesize(disk_super);
642 leafsize = btrfs_super_leafsize(disk_super);
643 sectorsize = btrfs_super_sectorsize(disk_super);
Chris Mason87ee04e2007-11-30 11:30:34 -0500644 stripesize = btrfs_super_stripesize(disk_super);
Chris Masondb945352007-10-15 16:15:53 -0400645 tree_root->nodesize = nodesize;
646 tree_root->leafsize = leafsize;
647 tree_root->sectorsize = sectorsize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500648 tree_root->stripesize = stripesize;
Chris Masonff79f812007-10-15 16:22:25 -0400649 sb_set_blocksize(sb, sectorsize);
Chris Masondb945352007-10-15 16:15:53 -0400650
Chris Mason8352d8a2007-04-12 10:43:05 -0400651 i_size_write(fs_info->btree_inode,
Chris Masondb945352007-10-15 16:15:53 -0400652 btrfs_super_total_bytes(disk_super));
Chris Mason8352d8a2007-04-12 10:43:05 -0400653
Chris Mason39279cc2007-06-12 06:35:45 -0400654 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
655 sizeof(disk_super->magic))) {
656 printk("btrfs: valid FS not found on %s\n", sb->s_id);
657 goto fail_sb_buffer;
658 }
Chris Mason19c00dd2007-10-15 16:19:22 -0400659
Chris Masondb945352007-10-15 16:15:53 -0400660 blocksize = btrfs_level_size(tree_root,
661 btrfs_super_root_level(disk_super));
Chris Mason19c00dd2007-10-15 16:19:22 -0400662
Chris Masone20d96d2007-03-22 12:13:20 -0400663 tree_root->node = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -0400664 btrfs_super_root(disk_super),
665 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -0400666 if (!tree_root->node)
667 goto fail_sb_buffer;
Chris Mason3768f362007-03-13 16:47:54 -0400668
Chris Mason2c90e5d2007-04-02 10:50:19 -0400669 mutex_lock(&fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -0400670
671 ret = find_and_setup_root(tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400672 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason39279cc2007-06-12 06:35:45 -0400673 if (ret) {
674 mutex_unlock(&fs_info->fs_mutex);
675 goto fail_tree_root;
676 }
Chris Mason3768f362007-03-13 16:47:54 -0400677
Chris Mason9078a3e2007-04-26 16:46:15 -0400678 btrfs_read_block_groups(extent_root);
679
Chris Mason0f7d52f2007-04-09 10:42:37 -0400680 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400681 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400682 return tree_root;
Chris Mason39279cc2007-06-12 06:35:45 -0400683
684fail_tree_root:
Chris Mason5f39d392007-10-15 16:14:19 -0400685 free_extent_buffer(tree_root->node);
Chris Mason39279cc2007-06-12 06:35:45 -0400686fail_sb_buffer:
Chris Mason5f39d392007-10-15 16:14:19 -0400687 free_extent_buffer(fs_info->sb_buffer);
Chris Mason39279cc2007-06-12 06:35:45 -0400688fail_iput:
689 iput(fs_info->btree_inode);
690fail:
691 kfree(extent_root);
692 kfree(tree_root);
693 kfree(fs_info);
694 return ERR_PTR(err);
Chris Masoneb60cea2007-02-02 09:18:22 -0500695}
696
Chris Masone089f052007-03-16 16:20:31 -0400697int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400698 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500699{
Chris Masone66f7092007-04-20 13:16:02 -0400700 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -0400701 struct extent_buffer *super = root->fs_info->sb_buffer;
702 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400703
Chris Mason5f39d392007-10-15 16:14:19 -0400704 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super);
705 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
706 super->start, super->len);
707 return ret;
Chris Masoncfaa7292007-02-21 17:04:57 -0500708}
709
Chris Mason5eda7b52007-06-22 14:16:25 -0400710int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
Chris Mason2619ba12007-04-10 16:58:11 -0400711{
712 radix_tree_delete(&fs_info->fs_roots_radix,
713 (unsigned long)root->root_key.objectid);
Josef Bacik58176a92007-08-29 15:47:34 -0400714 btrfs_sysfs_del_root(root);
Chris Mason2619ba12007-04-10 16:58:11 -0400715 if (root->inode)
716 iput(root->inode);
717 if (root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400718 free_extent_buffer(root->node);
Chris Mason2619ba12007-04-10 16:58:11 -0400719 if (root->commit_root)
Chris Mason5f39d392007-10-15 16:14:19 -0400720 free_extent_buffer(root->commit_root);
Josef Bacik58176a92007-08-29 15:47:34 -0400721 if (root->name)
722 kfree(root->name);
Chris Mason2619ba12007-04-10 16:58:11 -0400723 kfree(root);
724 return 0;
725}
726
Chris Mason35b7e472007-05-02 15:53:43 -0400727static int del_fs_roots(struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400728{
729 int ret;
730 struct btrfs_root *gang[8];
731 int i;
732
733 while(1) {
734 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
735 (void **)gang, 0,
736 ARRAY_SIZE(gang));
737 if (!ret)
738 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400739 for (i = 0; i < ret; i++)
Chris Mason5eda7b52007-06-22 14:16:25 -0400740 btrfs_free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400741 }
742 return 0;
743}
Chris Masonb4100d62007-04-12 12:14:00 -0400744
Chris Masone20d96d2007-03-22 12:13:20 -0400745int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500746{
Chris Mason3768f362007-03-13 16:47:54 -0400747 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400748 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400749 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -0400750
Chris Masonfacda1e2007-06-08 18:11:48 -0400751 fs_info->closing = 1;
Chris Mason08607c12007-06-08 15:33:54 -0400752 btrfs_transaction_flush_work(root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400753 mutex_lock(&fs_info->fs_mutex);
Chris Mason6702ed42007-08-07 16:15:09 -0400754 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason79154b12007-03-22 15:59:16 -0400755 trans = btrfs_start_transaction(root, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400756 ret = btrfs_commit_transaction(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400757 /* run commit again to drop the original snapshot */
758 trans = btrfs_start_transaction(root, 1);
759 btrfs_commit_transaction(trans, root);
760 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400761 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400762 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400763 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500764
Chris Mason0f7d52f2007-04-09 10:42:37 -0400765 if (fs_info->extent_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400766 free_extent_buffer(fs_info->extent_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -0400767
Chris Mason0f7d52f2007-04-09 10:42:37 -0400768 if (fs_info->tree_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400769 free_extent_buffer(fs_info->tree_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -0400770
Chris Mason5f39d392007-10-15 16:14:19 -0400771 free_extent_buffer(fs_info->sb_buffer);
Chris Mason7eccb902007-04-11 15:53:25 -0400772
Chris Mason9078a3e2007-04-26 16:46:15 -0400773 btrfs_free_block_groups(root->fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400774 del_fs_roots(fs_info);
Chris Masond10c5f32007-12-17 20:14:04 -0500775
776 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
777
778 extent_map_tree_empty_lru(&fs_info->free_space_cache);
779 extent_map_tree_empty_lru(&fs_info->block_group_cache);
780 extent_map_tree_empty_lru(&fs_info->pinned_extents);
781 extent_map_tree_empty_lru(&fs_info->pending_del);
782 extent_map_tree_empty_lru(&fs_info->extent_ins);
Chris Mason19c00dd2007-10-15 16:19:22 -0400783 extent_map_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->extent_tree);
Chris Masond10c5f32007-12-17 20:14:04 -0500784
Chris Masondb945352007-10-15 16:15:53 -0400785 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
Chris Masond10c5f32007-12-17 20:14:04 -0500786
Chris Masondb945352007-10-15 16:15:53 -0400787 iput(fs_info->btree_inode);
Chris Mason19c00dd2007-10-15 16:19:22 -0400788#if 0
789 while(!list_empty(&fs_info->hashers)) {
790 struct btrfs_hasher *hasher;
791 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
792 hashers);
793 list_del(&hasher->hashers);
794 crypto_free_hash(&fs_info->hash_tfm);
795 kfree(hasher);
796 }
797#endif
Chris Mason0f7d52f2007-04-09 10:42:37 -0400798 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400799 kfree(fs_info->tree_root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500800 return 0;
801}
802
Chris Mason5f39d392007-10-15 16:14:19 -0400803int btrfs_buffer_uptodate(struct extent_buffer *buf)
Chris Masonccd467d2007-06-28 15:57:36 -0400804{
Chris Mason810191f2007-10-15 16:18:55 -0400805 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Mason5f39d392007-10-15 16:14:19 -0400806 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf);
807}
Chris Mason6702ed42007-08-07 16:15:09 -0400808
Chris Mason5f39d392007-10-15 16:14:19 -0400809int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
810{
Chris Mason810191f2007-10-15 16:18:55 -0400811 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Mason5f39d392007-10-15 16:14:19 -0400812 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree,
813 buf);
814}
815
816void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
817{
Chris Mason810191f2007-10-15 16:18:55 -0400818 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason5f39d392007-10-15 16:14:19 -0400819 u64 transid = btrfs_header_generation(buf);
820 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason6702ed42007-08-07 16:15:09 -0400821
Chris Masonccd467d2007-06-28 15:57:36 -0400822 if (transid != root->fs_info->generation) {
823 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
Chris Masondb945352007-10-15 16:15:53 -0400824 (unsigned long long)buf->start,
Chris Masonccd467d2007-06-28 15:57:36 -0400825 transid, root->fs_info->generation);
826 WARN_ON(1);
827 }
Chris Mason5f39d392007-10-15 16:14:19 -0400828 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500829}
830
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400831void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
Chris Mason35b7e472007-05-02 15:53:43 -0400832{
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400833 balance_dirty_pages_ratelimited_nr(
Chris Mason304fced2007-10-15 16:21:17 -0400834 root->fs_info->btree_inode->i_mapping, 1);
Chris Mason35b7e472007-05-02 15:53:43 -0400835}
Chris Mason6b800532007-10-15 16:17:34 -0400836
837void btrfs_set_buffer_defrag(struct extent_buffer *buf)
838{
Chris Mason810191f2007-10-15 16:18:55 -0400839 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400840 struct inode *btree_inode = root->fs_info->btree_inode;
841 set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start,
842 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
843}
844
845void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
846{
Chris Mason810191f2007-10-15 16:18:55 -0400847 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400848 struct inode *btree_inode = root->fs_info->btree_inode;
849 set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start,
850 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
851 GFP_NOFS);
852}
853
854int btrfs_buffer_defrag(struct extent_buffer *buf)
855{
Chris Mason810191f2007-10-15 16:18:55 -0400856 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400857 struct inode *btree_inode = root->fs_info->btree_inode;
858 return test_range_bit(&BTRFS_I(btree_inode)->extent_tree,
859 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
860}
861
862int btrfs_buffer_defrag_done(struct extent_buffer *buf)
863{
Chris Mason810191f2007-10-15 16:18:55 -0400864 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400865 struct inode *btree_inode = root->fs_info->btree_inode;
866 return test_range_bit(&BTRFS_I(btree_inode)->extent_tree,
867 buf->start, buf->start + buf->len - 1,
868 EXTENT_DEFRAG_DONE, 0);
869}
870
871int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
872{
Chris Mason810191f2007-10-15 16:18:55 -0400873 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400874 struct inode *btree_inode = root->fs_info->btree_inode;
875 return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree,
876 buf->start, buf->start + buf->len - 1,
877 EXTENT_DEFRAG_DONE, GFP_NOFS);
878}
879
880int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
881{
Chris Mason810191f2007-10-15 16:18:55 -0400882 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400883 struct inode *btree_inode = root->fs_info->btree_inode;
884 return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree,
885 buf->start, buf->start + buf->len - 1,
886 EXTENT_DEFRAG, GFP_NOFS);
887}
888
889int btrfs_read_buffer(struct extent_buffer *buf)
890{
Chris Mason810191f2007-10-15 16:18:55 -0400891 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400892 struct inode *btree_inode = root->fs_info->btree_inode;
893 return read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -0400894 buf, 0, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400895}
Chris Mason0da54682007-11-07 21:08:01 -0500896
897static struct extent_map_ops btree_extent_map_ops = {
898 .writepage_io_hook = btree_writepage_io_hook,
899};