blob: 28e9ef8a3253db0e8443d26cb0c752577b380625 [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 Mason793955b2007-11-26 16:34:41 -0800213 if (wbc->sync_mode == WB_SYNC_NONE) {
214 u64 num_dirty;
215 u64 start = 0;
216 unsigned long thresh = 96 * 1024 * 1024;
217 num_dirty = count_range_bits(tree, &start, thresh, EXTENT_DIRTY);
218 if (num_dirty < thresh) {
219 return 0;
220 }
221 }
Chris Mason0da54682007-11-07 21:08:01 -0500222 return extent_writepages(tree, mapping, btree_get_extent, wbc);
223}
224
Chris Mason5f39d392007-10-15 16:14:19 -0400225int btree_readpage(struct file *file, struct page *page)
226{
227 struct extent_map_tree *tree;
228 tree = &BTRFS_I(page->mapping->host)->extent_tree;
229 return extent_read_full_page(tree, page, btree_get_extent);
230}
231
232static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags)
233{
234 struct extent_map_tree *tree;
235 int ret;
236
Chris Mason5f39d392007-10-15 16:14:19 -0400237 tree = &BTRFS_I(page->mapping->host)->extent_tree;
238 ret = try_release_extent_mapping(tree, page);
239 if (ret == 1) {
240 ClearPagePrivate(page);
241 set_page_private(page, 0);
242 page_cache_release(page);
243 }
Chris Masond98237b2007-03-28 13:57:48 -0400244 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400245}
Chris Mason123abc82007-03-14 14:14:43 -0400246
Chris Mason5f39d392007-10-15 16:14:19 -0400247static void btree_invalidatepage(struct page *page, unsigned long offset)
Chris Masond98237b2007-03-28 13:57:48 -0400248{
Chris Mason5f39d392007-10-15 16:14:19 -0400249 struct extent_map_tree *tree;
250 tree = &BTRFS_I(page->mapping->host)->extent_tree;
251 extent_invalidatepage(tree, page, offset);
252 btree_releasepage(page, GFP_NOFS);
Chris Masond98237b2007-03-28 13:57:48 -0400253}
254
Chris Mason5f39d392007-10-15 16:14:19 -0400255#if 0
Chris Masond98237b2007-03-28 13:57:48 -0400256static int btree_writepage(struct page *page, struct writeback_control *wbc)
257{
Chris Mason87cbda52007-03-28 19:44:27 -0400258 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400259 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400260 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400261 if (!page_has_buffers(page)) {
262 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
263 (1 << BH_Dirty)|(1 << BH_Uptodate));
264 }
265 head = page_buffers(page);
266 bh = head;
267 do {
268 if (buffer_dirty(bh))
269 csum_tree_block(root, bh, 0);
270 bh = bh->b_this_page;
271 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400272 return block_write_full_page(page, btree_get_block, wbc);
273}
Chris Mason5f39d392007-10-15 16:14:19 -0400274#endif
Chris Masond98237b2007-03-28 13:57:48 -0400275
276static struct address_space_operations btree_aops = {
277 .readpage = btree_readpage,
278 .writepage = btree_writepage,
Chris Mason0da54682007-11-07 21:08:01 -0500279 .writepages = btree_writepages,
Chris Mason5f39d392007-10-15 16:14:19 -0400280 .releasepage = btree_releasepage,
281 .invalidatepage = btree_invalidatepage,
Chris Masond98237b2007-03-28 13:57:48 -0400282 .sync_page = block_sync_page,
283};
284
Chris Masondb945352007-10-15 16:15:53 -0400285int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
Chris Mason090d1872007-05-01 08:53:32 -0400286{
Chris Mason5f39d392007-10-15 16:14:19 -0400287 struct extent_buffer *buf = NULL;
288 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonde428b62007-05-18 13:28:27 -0400289 int ret = 0;
Chris Mason090d1872007-05-01 08:53:32 -0400290
Chris Masondb945352007-10-15 16:15:53 -0400291 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400292 if (!buf)
Chris Mason090d1872007-05-01 08:53:32 -0400293 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400294 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -0400295 buf, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400296 free_extent_buffer(buf);
Chris Masonde428b62007-05-18 13:28:27 -0400297 return ret;
Chris Mason090d1872007-05-01 08:53:32 -0400298}
299
Chris Masondb945352007-10-15 16:15:53 -0400300struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
301 u32 blocksize)
Chris Masone20d96d2007-03-22 12:13:20 -0400302{
Chris Mason5f39d392007-10-15 16:14:19 -0400303 struct extent_buffer *buf = NULL;
304 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason19c00dd2007-10-15 16:19:22 -0400305 struct extent_map_tree *extent_tree;
306 int ret;
307
308 extent_tree = &BTRFS_I(btree_inode)->extent_tree;
Chris Masone20d96d2007-03-22 12:13:20 -0400309
Chris Masondb945352007-10-15 16:15:53 -0400310 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400311 if (!buf)
312 return NULL;
313 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -0400314 buf, 0, 1);
315 if (buf->flags & EXTENT_CSUM) {
316 return buf;
317 }
318 if (test_range_bit(extent_tree, buf->start, buf->start + buf->len - 1,
319 EXTENT_CSUM, 1)) {
320 buf->flags |= EXTENT_CSUM;
321 return buf;
322 }
323 ret = csum_tree_block(root, buf, 1);
324 set_extent_bits(extent_tree, buf->start,
325 buf->start + buf->len - 1,
326 EXTENT_CSUM, GFP_NOFS);
327 buf->flags |= EXTENT_CSUM;
Chris Mason5f39d392007-10-15 16:14:19 -0400328 return buf;
Chris Masoneb60cea2007-02-02 09:18:22 -0500329}
330
Chris Masone089f052007-03-16 16:20:31 -0400331int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400332 struct extent_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500333{
Chris Mason5f39d392007-10-15 16:14:19 -0400334 struct inode *btree_inode = root->fs_info->btree_inode;
335 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
336 return 0;
337}
338
339int wait_on_tree_block_writeback(struct btrfs_root *root,
340 struct extent_buffer *buf)
341{
342 struct inode *btree_inode = root->fs_info->btree_inode;
343 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree,
344 buf);
345 return 0;
346}
347
Chris Masondb945352007-10-15 16:15:53 -0400348static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400349 struct btrfs_root *root,
350 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400351 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500352{
Chris Masoncfaa7292007-02-21 17:04:57 -0500353 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400354 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500355 root->commit_root = NULL;
Chris Masondb945352007-10-15 16:15:53 -0400356 root->sectorsize = sectorsize;
357 root->nodesize = nodesize;
358 root->leafsize = leafsize;
Chris Mason123abc82007-03-14 14:14:43 -0400359 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400360 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400361 root->objectid = objectid;
362 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400363 root->highest_inode = 0;
364 root->last_inode_alloc = 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400365 root->name = NULL;
Chris Mason3768f362007-03-13 16:47:54 -0400366 memset(&root->root_key, 0, sizeof(root->root_key));
367 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason6702ed42007-08-07 16:15:09 -0400368 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
Josef Bacik58176a92007-08-29 15:47:34 -0400369 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
370 init_completion(&root->kobj_unregister);
Chris Mason011410b2007-09-10 19:58:36 -0400371 init_rwsem(&root->snap_sem);
Chris Mason6702ed42007-08-07 16:15:09 -0400372 root->defrag_running = 0;
373 root->defrag_level = 0;
Chris Mason4d775672007-04-20 20:23:12 -0400374 root->root_key.objectid = objectid;
Chris Mason3768f362007-03-13 16:47:54 -0400375 return 0;
376}
377
Chris Masondb945352007-10-15 16:15:53 -0400378static int find_and_setup_root(struct btrfs_root *tree_root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400379 struct btrfs_fs_info *fs_info,
380 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400381 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400382{
383 int ret;
Chris Masondb945352007-10-15 16:15:53 -0400384 u32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -0400385
Chris Masondb945352007-10-15 16:15:53 -0400386 __setup_root(tree_root->nodesize, tree_root->leafsize,
387 tree_root->sectorsize, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400388 ret = btrfs_find_last_root(tree_root, objectid,
389 &root->root_item, &root->root_key);
390 BUG_ON(ret);
391
Chris Masondb945352007-10-15 16:15:53 -0400392 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
393 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
394 blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400395 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500396 return 0;
397}
398
Chris Mason5eda7b52007-06-22 14:16:25 -0400399struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
400 struct btrfs_key *location)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400401{
402 struct btrfs_root *root;
403 struct btrfs_root *tree_root = fs_info->tree_root;
404 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400405 struct extent_buffer *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400406 u64 highest_inode;
Chris Masondb945352007-10-15 16:15:53 -0400407 u32 blocksize;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400408 int ret = 0;
409
Chris Mason5eda7b52007-06-22 14:16:25 -0400410 root = kzalloc(sizeof(*root), GFP_NOFS);
Chris Mason0cf6c622007-06-09 09:22:25 -0400411 if (!root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400412 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400413 if (location->offset == (u64)-1) {
Chris Masondb945352007-10-15 16:15:53 -0400414 ret = find_and_setup_root(tree_root, fs_info,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400415 location->objectid, root);
416 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400417 kfree(root);
418 return ERR_PTR(ret);
419 }
420 goto insert;
421 }
422
Chris Masondb945352007-10-15 16:15:53 -0400423 __setup_root(tree_root->nodesize, tree_root->leafsize,
424 tree_root->sectorsize, root, fs_info,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400425 location->objectid);
426
427 path = btrfs_alloc_path();
428 BUG_ON(!path);
429 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
430 if (ret != 0) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400431 if (ret > 0)
432 ret = -ENOENT;
433 goto out;
434 }
Chris Mason5f39d392007-10-15 16:14:19 -0400435 l = path->nodes[0];
436 read_extent_buffer(l, &root->root_item,
437 btrfs_item_ptr_offset(l, path->slots[0]),
Chris Mason0f7d52f2007-04-09 10:42:37 -0400438 sizeof(root->root_item));
Yan Zheng44b36eb2007-10-19 09:22:56 -0400439 memcpy(&root->root_key, location, sizeof(*location));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400440 ret = 0;
441out:
442 btrfs_release_path(root, path);
443 btrfs_free_path(path);
444 if (ret) {
445 kfree(root);
446 return ERR_PTR(ret);
447 }
Chris Masondb945352007-10-15 16:15:53 -0400448 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
449 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
450 blocksize);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400451 BUG_ON(!root->node);
452insert:
Chris Mason0f7d52f2007-04-09 10:42:37 -0400453 root->ref_cows = 1;
Chris Mason5eda7b52007-06-22 14:16:25 -0400454 ret = btrfs_find_highest_inode(root, &highest_inode);
455 if (ret == 0) {
456 root->highest_inode = highest_inode;
457 root->last_inode_alloc = highest_inode;
458 }
459 return root;
460}
461
462struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
Josef Bacik58176a92007-08-29 15:47:34 -0400463 struct btrfs_key *location,
464 const char *name, int namelen)
Chris Mason5eda7b52007-06-22 14:16:25 -0400465{
466 struct btrfs_root *root;
467 int ret;
468
469 root = radix_tree_lookup(&fs_info->fs_roots_radix,
470 (unsigned long)location->objectid);
471 if (root)
472 return root;
473
474 root = btrfs_read_fs_root_no_radix(fs_info, location);
475 if (IS_ERR(root))
476 return root;
Chris Mason2619ba12007-04-10 16:58:11 -0400477 ret = radix_tree_insert(&fs_info->fs_roots_radix,
478 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400479 root);
480 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400481 free_extent_buffer(root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400482 kfree(root);
483 return ERR_PTR(ret);
484 }
Josef Bacik58176a92007-08-29 15:47:34 -0400485
486 ret = btrfs_set_root_name(root, name, namelen);
487 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400488 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400489 kfree(root);
490 return ERR_PTR(ret);
491 }
492
493 ret = btrfs_sysfs_add_root(root);
494 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400495 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400496 kfree(root->name);
497 kfree(root);
498 return ERR_PTR(ret);
499 }
500
Chris Mason5ce14bb2007-09-11 11:15:39 -0400501 ret = btrfs_find_dead_roots(fs_info->tree_root,
502 root->root_key.objectid, root);
503 BUG_ON(ret);
504
Chris Mason0f7d52f2007-04-09 10:42:37 -0400505 return root;
506}
Chris Mason19c00dd2007-10-15 16:19:22 -0400507#if 0
508static int add_hasher(struct btrfs_fs_info *info, char *type) {
509 struct btrfs_hasher *hasher;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400510
Chris Mason19c00dd2007-10-15 16:19:22 -0400511 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
512 if (!hasher)
513 return -ENOMEM;
514 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
515 if (!hasher->hash_tfm) {
516 kfree(hasher);
517 return -EINVAL;
518 }
519 spin_lock(&info->hash_lock);
520 list_add(&hasher->list, &info->hashers);
521 spin_unlock(&info->hash_lock);
522 return 0;
523}
524#endif
Chris Mason2c90e5d2007-04-02 10:50:19 -0400525struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500526{
Chris Masondb945352007-10-15 16:15:53 -0400527 u32 sectorsize;
528 u32 nodesize;
529 u32 leafsize;
530 u32 blocksize;
Chris Masone20d96d2007-03-22 12:13:20 -0400531 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
532 GFP_NOFS);
533 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
534 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400535 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
536 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500537 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400538 int err = -EIO;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400539 struct btrfs_super_block *disk_super;
Chris Masoneb60cea2007-02-02 09:18:22 -0500540
Chris Mason39279cc2007-06-12 06:35:45 -0400541 if (!extent_root || !tree_root || !fs_info) {
542 err = -ENOMEM;
543 goto fail;
544 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400545 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason8fd17792007-04-19 21:01:03 -0400546 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -0400547 INIT_LIST_HEAD(&fs_info->dead_roots);
Chris Mason19c00dd2007-10-15 16:19:22 -0400548 INIT_LIST_HEAD(&fs_info->hashers);
549 spin_lock_init(&fs_info->hash_lock);
550
Josef Bacik58176a92007-08-29 15:47:34 -0400551 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
552 init_completion(&fs_info->kobj_unregister);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400553 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400554 fs_info->running_transaction = NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400555 fs_info->last_trans_committed = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400556 fs_info->tree_root = tree_root;
557 fs_info->extent_root = extent_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400558 fs_info->sb = sb;
Chris Masond98237b2007-03-28 13:57:48 -0400559 fs_info->btree_inode = new_inode(sb);
560 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400561 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400562 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
563 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason5f39d392007-10-15 16:14:19 -0400564 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
565 fs_info->btree_inode->i_mapping,
566 GFP_NOFS);
Chris Mason0da54682007-11-07 21:08:01 -0500567 BTRFS_I(fs_info->btree_inode)->extent_tree.ops = &btree_extent_map_ops;
568
Chris Masonf510cfe2007-10-15 16:14:48 -0400569 extent_map_tree_init(&fs_info->free_space_cache,
570 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason96b51792007-10-15 16:15:19 -0400571 extent_map_tree_init(&fs_info->block_group_cache,
572 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason1a5bc162007-10-15 16:15:26 -0400573 extent_map_tree_init(&fs_info->pinned_extents,
574 fs_info->btree_inode->i_mapping, GFP_NOFS);
575 extent_map_tree_init(&fs_info->pending_del,
576 fs_info->btree_inode->i_mapping, GFP_NOFS);
577 extent_map_tree_init(&fs_info->extent_ins,
578 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masone66f7092007-04-20 13:16:02 -0400579 fs_info->do_barriers = 1;
Chris Masonfacda1e2007-06-08 18:11:48 -0400580 fs_info->closing = 0;
Yan324ae4d2007-11-16 14:57:08 -0500581 fs_info->total_pinned = 0;
Chris Masonfacda1e2007-06-08 18:11:48 -0400582
Chris Mason08607c12007-06-08 15:33:54 -0400583 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400584 BTRFS_I(fs_info->btree_inode)->root = tree_root;
585 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
586 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400587 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400588 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400589
Chris Mason79154b12007-03-22 15:59:16 -0400590 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400591 mutex_init(&fs_info->fs_mutex);
Chris Mason3768f362007-03-13 16:47:54 -0400592
Chris Mason19c00dd2007-10-15 16:19:22 -0400593#if 0
594 ret = add_hasher(fs_info, "crc32c");
595 if (ret) {
596 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
597 err = -ENOMEM;
598 goto fail_iput;
599 }
600#endif
Chris Masondb945352007-10-15 16:15:53 -0400601 __setup_root(512, 512, 512, tree_root,
Chris Mason2c90e5d2007-04-02 10:50:19 -0400602 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400603
Chris Mason2c90e5d2007-04-02 10:50:19 -0400604 fs_info->sb_buffer = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -0400605 BTRFS_SUPER_INFO_OFFSET,
606 512);
Chris Masond98237b2007-03-28 13:57:48 -0400607
Chris Mason0f7d52f2007-04-09 10:42:37 -0400608 if (!fs_info->sb_buffer)
Chris Mason39279cc2007-06-12 06:35:45 -0400609 goto fail_iput;
Chris Mason39279cc2007-06-12 06:35:45 -0400610
Chris Mason5f39d392007-10-15 16:14:19 -0400611 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
612 sizeof(fs_info->super_copy));
613
614 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
615 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
616 BTRFS_FSID_SIZE);
617 disk_super = &fs_info->super_copy;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400618 if (!btrfs_super_root(disk_super))
Chris Mason39279cc2007-06-12 06:35:45 -0400619 goto fail_sb_buffer;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400620
Chris Masondb945352007-10-15 16:15:53 -0400621 nodesize = btrfs_super_nodesize(disk_super);
622 leafsize = btrfs_super_leafsize(disk_super);
623 sectorsize = btrfs_super_sectorsize(disk_super);
624 tree_root->nodesize = nodesize;
625 tree_root->leafsize = leafsize;
626 tree_root->sectorsize = sectorsize;
Chris Masonff79f812007-10-15 16:22:25 -0400627 sb_set_blocksize(sb, sectorsize);
Chris Masondb945352007-10-15 16:15:53 -0400628
Chris Mason8352d8a2007-04-12 10:43:05 -0400629 i_size_write(fs_info->btree_inode,
Chris Masondb945352007-10-15 16:15:53 -0400630 btrfs_super_total_bytes(disk_super));
Chris Mason8352d8a2007-04-12 10:43:05 -0400631
Chris Mason39279cc2007-06-12 06:35:45 -0400632 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
633 sizeof(disk_super->magic))) {
634 printk("btrfs: valid FS not found on %s\n", sb->s_id);
635 goto fail_sb_buffer;
636 }
Chris Mason19c00dd2007-10-15 16:19:22 -0400637
Chris Masondb945352007-10-15 16:15:53 -0400638 blocksize = btrfs_level_size(tree_root,
639 btrfs_super_root_level(disk_super));
Chris Mason19c00dd2007-10-15 16:19:22 -0400640
Chris Masone20d96d2007-03-22 12:13:20 -0400641 tree_root->node = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -0400642 btrfs_super_root(disk_super),
643 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -0400644 if (!tree_root->node)
645 goto fail_sb_buffer;
Chris Mason3768f362007-03-13 16:47:54 -0400646
Chris Mason2c90e5d2007-04-02 10:50:19 -0400647 mutex_lock(&fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -0400648
649 ret = find_and_setup_root(tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400650 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason39279cc2007-06-12 06:35:45 -0400651 if (ret) {
652 mutex_unlock(&fs_info->fs_mutex);
653 goto fail_tree_root;
654 }
Chris Mason3768f362007-03-13 16:47:54 -0400655
Chris Mason9078a3e2007-04-26 16:46:15 -0400656 btrfs_read_block_groups(extent_root);
657
Chris Mason0f7d52f2007-04-09 10:42:37 -0400658 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400659 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400660 return tree_root;
Chris Mason39279cc2007-06-12 06:35:45 -0400661
662fail_tree_root:
Chris Mason5f39d392007-10-15 16:14:19 -0400663 free_extent_buffer(tree_root->node);
Chris Mason39279cc2007-06-12 06:35:45 -0400664fail_sb_buffer:
Chris Mason5f39d392007-10-15 16:14:19 -0400665 free_extent_buffer(fs_info->sb_buffer);
Chris Mason39279cc2007-06-12 06:35:45 -0400666fail_iput:
667 iput(fs_info->btree_inode);
668fail:
669 kfree(extent_root);
670 kfree(tree_root);
671 kfree(fs_info);
672 return ERR_PTR(err);
Chris Masoneb60cea2007-02-02 09:18:22 -0500673}
674
Chris Masone089f052007-03-16 16:20:31 -0400675int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400676 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500677{
Chris Masone66f7092007-04-20 13:16:02 -0400678 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -0400679 struct extent_buffer *super = root->fs_info->sb_buffer;
680 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400681
Chris Mason5f39d392007-10-15 16:14:19 -0400682 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super);
683 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
684 super->start, super->len);
685 return ret;
Chris Masoncfaa7292007-02-21 17:04:57 -0500686}
687
Chris Mason5eda7b52007-06-22 14:16:25 -0400688int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
Chris Mason2619ba12007-04-10 16:58:11 -0400689{
690 radix_tree_delete(&fs_info->fs_roots_radix,
691 (unsigned long)root->root_key.objectid);
Josef Bacik58176a92007-08-29 15:47:34 -0400692 btrfs_sysfs_del_root(root);
Chris Mason2619ba12007-04-10 16:58:11 -0400693 if (root->inode)
694 iput(root->inode);
695 if (root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400696 free_extent_buffer(root->node);
Chris Mason2619ba12007-04-10 16:58:11 -0400697 if (root->commit_root)
Chris Mason5f39d392007-10-15 16:14:19 -0400698 free_extent_buffer(root->commit_root);
Josef Bacik58176a92007-08-29 15:47:34 -0400699 if (root->name)
700 kfree(root->name);
Chris Mason2619ba12007-04-10 16:58:11 -0400701 kfree(root);
702 return 0;
703}
704
Chris Mason35b7e472007-05-02 15:53:43 -0400705static int del_fs_roots(struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400706{
707 int ret;
708 struct btrfs_root *gang[8];
709 int i;
710
711 while(1) {
712 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
713 (void **)gang, 0,
714 ARRAY_SIZE(gang));
715 if (!ret)
716 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400717 for (i = 0; i < ret; i++)
Chris Mason5eda7b52007-06-22 14:16:25 -0400718 btrfs_free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400719 }
720 return 0;
721}
Chris Masonb4100d62007-04-12 12:14:00 -0400722
Chris Masone20d96d2007-03-22 12:13:20 -0400723int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500724{
Chris Mason3768f362007-03-13 16:47:54 -0400725 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400726 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400727 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -0400728
Chris Masonfacda1e2007-06-08 18:11:48 -0400729 fs_info->closing = 1;
Chris Mason08607c12007-06-08 15:33:54 -0400730 btrfs_transaction_flush_work(root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400731 mutex_lock(&fs_info->fs_mutex);
Chris Mason6702ed42007-08-07 16:15:09 -0400732 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason79154b12007-03-22 15:59:16 -0400733 trans = btrfs_start_transaction(root, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400734 ret = btrfs_commit_transaction(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400735 /* run commit again to drop the original snapshot */
736 trans = btrfs_start_transaction(root, 1);
737 btrfs_commit_transaction(trans, root);
738 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400739 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400740 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400741 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500742
Chris Mason0f7d52f2007-04-09 10:42:37 -0400743 if (fs_info->extent_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400744 free_extent_buffer(fs_info->extent_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -0400745
Chris Mason0f7d52f2007-04-09 10:42:37 -0400746 if (fs_info->tree_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400747 free_extent_buffer(fs_info->tree_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -0400748
Chris Mason5f39d392007-10-15 16:14:19 -0400749 free_extent_buffer(fs_info->sb_buffer);
Chris Mason7eccb902007-04-11 15:53:25 -0400750
Chris Mason9078a3e2007-04-26 16:46:15 -0400751 btrfs_free_block_groups(root->fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400752 del_fs_roots(fs_info);
Chris Mason19c00dd2007-10-15 16:19:22 -0400753 extent_map_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->extent_tree);
Chris Masondb945352007-10-15 16:15:53 -0400754 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
755 iput(fs_info->btree_inode);
Chris Mason19c00dd2007-10-15 16:19:22 -0400756#if 0
757 while(!list_empty(&fs_info->hashers)) {
758 struct btrfs_hasher *hasher;
759 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
760 hashers);
761 list_del(&hasher->hashers);
762 crypto_free_hash(&fs_info->hash_tfm);
763 kfree(hasher);
764 }
765#endif
Chris Mason0f7d52f2007-04-09 10:42:37 -0400766 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400767 kfree(fs_info->tree_root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500768 return 0;
769}
770
Chris Mason5f39d392007-10-15 16:14:19 -0400771int btrfs_buffer_uptodate(struct extent_buffer *buf)
Chris Masonccd467d2007-06-28 15:57:36 -0400772{
Chris Mason810191f2007-10-15 16:18:55 -0400773 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Mason5f39d392007-10-15 16:14:19 -0400774 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf);
775}
Chris Mason6702ed42007-08-07 16:15:09 -0400776
Chris Mason5f39d392007-10-15 16:14:19 -0400777int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
778{
Chris Mason810191f2007-10-15 16:18:55 -0400779 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Mason5f39d392007-10-15 16:14:19 -0400780 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree,
781 buf);
782}
783
784void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
785{
Chris Mason810191f2007-10-15 16:18:55 -0400786 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason5f39d392007-10-15 16:14:19 -0400787 u64 transid = btrfs_header_generation(buf);
788 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason6702ed42007-08-07 16:15:09 -0400789
Chris Masonccd467d2007-06-28 15:57:36 -0400790 if (transid != root->fs_info->generation) {
791 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
Chris Masondb945352007-10-15 16:15:53 -0400792 (unsigned long long)buf->start,
Chris Masonccd467d2007-06-28 15:57:36 -0400793 transid, root->fs_info->generation);
794 WARN_ON(1);
795 }
Chris Mason5f39d392007-10-15 16:14:19 -0400796 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500797}
798
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400799void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
Chris Mason35b7e472007-05-02 15:53:43 -0400800{
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400801 balance_dirty_pages_ratelimited_nr(
Chris Mason304fced2007-10-15 16:21:17 -0400802 root->fs_info->btree_inode->i_mapping, 1);
Chris Mason35b7e472007-05-02 15:53:43 -0400803}
Chris Mason6b800532007-10-15 16:17:34 -0400804
805void btrfs_set_buffer_defrag(struct extent_buffer *buf)
806{
Chris Mason810191f2007-10-15 16:18:55 -0400807 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400808 struct inode *btree_inode = root->fs_info->btree_inode;
809 set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start,
810 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
811}
812
813void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
814{
Chris Mason810191f2007-10-15 16:18:55 -0400815 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400816 struct inode *btree_inode = root->fs_info->btree_inode;
817 set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start,
818 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
819 GFP_NOFS);
820}
821
822int btrfs_buffer_defrag(struct extent_buffer *buf)
823{
Chris Mason810191f2007-10-15 16:18:55 -0400824 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400825 struct inode *btree_inode = root->fs_info->btree_inode;
826 return test_range_bit(&BTRFS_I(btree_inode)->extent_tree,
827 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
828}
829
830int btrfs_buffer_defrag_done(struct extent_buffer *buf)
831{
Chris Mason810191f2007-10-15 16:18:55 -0400832 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400833 struct inode *btree_inode = root->fs_info->btree_inode;
834 return test_range_bit(&BTRFS_I(btree_inode)->extent_tree,
835 buf->start, buf->start + buf->len - 1,
836 EXTENT_DEFRAG_DONE, 0);
837}
838
839int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
840{
Chris Mason810191f2007-10-15 16:18:55 -0400841 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400842 struct inode *btree_inode = root->fs_info->btree_inode;
843 return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree,
844 buf->start, buf->start + buf->len - 1,
845 EXTENT_DEFRAG_DONE, GFP_NOFS);
846}
847
848int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
849{
Chris Mason810191f2007-10-15 16:18:55 -0400850 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400851 struct inode *btree_inode = root->fs_info->btree_inode;
852 return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree,
853 buf->start, buf->start + buf->len - 1,
854 EXTENT_DEFRAG, GFP_NOFS);
855}
856
857int btrfs_read_buffer(struct extent_buffer *buf)
858{
Chris Mason810191f2007-10-15 16:18:55 -0400859 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400860 struct inode *btree_inode = root->fs_info->btree_inode;
861 return read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -0400862 buf, 0, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400863}
Chris Mason0da54682007-11-07 21:08:01 -0500864
865static struct extent_map_ops btree_extent_map_ops = {
866 .writepage_io_hook = btree_writepage_io_hook,
867};