blob: 5a7e60d7f443044cb457ec637dadabdad96c36e9 [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 Mason5f39d392007-10-15 16:14:19 -040046struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -040047 u64 bytenr, u32 blocksize)
Chris Masoned2ff2c2007-03-01 18:59:40 -050048{
Chris Mason5f39d392007-10-15 16:14:19 -040049 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonf510cfe2007-10-15 16:14:48 -040050 struct extent_buffer *eb;
51 eb = find_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
Chris Masondb945352007-10-15 16:15:53 -040052 bytenr, blocksize, GFP_NOFS);
Chris Masonf510cfe2007-10-15 16:14:48 -040053 return eb;
Chris Masoned2ff2c2007-03-01 18:59:40 -050054}
55
Chris Mason5f39d392007-10-15 16:14:19 -040056struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -040057 u64 bytenr, u32 blocksize)
Chris Masoneb60cea2007-02-02 09:18:22 -050058{
Chris Mason5f39d392007-10-15 16:14:19 -040059 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonf510cfe2007-10-15 16:14:48 -040060 struct extent_buffer *eb;
Chris Masondb945352007-10-15 16:15:53 -040061
Chris Masonf510cfe2007-10-15 16:14:48 -040062 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -040063 bytenr, blocksize, NULL, GFP_NOFS);
Chris Masonf510cfe2007-10-15 16:14:48 -040064 return eb;
Chris Mason5f39d392007-10-15 16:14:19 -040065}
Chris Mason22b0ebd2007-03-30 08:47:31 -040066
Chris Mason5f39d392007-10-15 16:14:19 -040067struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
68 size_t page_offset, u64 start, u64 end,
69 int create)
70{
71 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
72 struct extent_map *em;
73 int ret;
Chris Masond98237b2007-03-28 13:57:48 -040074
Chris Mason5f39d392007-10-15 16:14:19 -040075again:
76 em = lookup_extent_mapping(em_tree, start, end);
77 if (em) {
78 goto out;
79 }
80 em = alloc_extent_map(GFP_NOFS);
81 if (!em) {
82 em = ERR_PTR(-ENOMEM);
83 goto out;
84 }
85 em->start = 0;
86 em->end = (i_size_read(inode) & ~((u64)PAGE_CACHE_SIZE -1)) - 1;
87 em->block_start = 0;
88 em->block_end = em->end;
89 em->bdev = inode->i_sb->s_bdev;
90 ret = add_extent_mapping(em_tree, em);
91 if (ret == -EEXIST) {
92 free_extent_map(em);
93 em = NULL;
94 goto again;
95 } else if (ret) {
96 em = ERR_PTR(ret);
97 }
98out:
99 return em;
100}
101
Chris Mason19c00dd2007-10-15 16:19:22 -0400102u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
103{
104 return crc32c(seed, data, len);
105}
106
107void btrfs_csum_final(u32 crc, char *result)
108{
109 *(__le32 *)result = ~cpu_to_le32(crc);
110}
111
112static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
113 int verify)
114{
115 char result[BTRFS_CRC32_SIZE];
116 unsigned long len;
117 unsigned long cur_len;
118 unsigned long offset = BTRFS_CSUM_SIZE;
119 char *map_token = NULL;
120 char *kaddr;
121 unsigned long map_start;
122 unsigned long map_len;
123 int err;
124 u32 crc = ~(u32)0;
125
126 len = buf->len - offset;
127 while(len > 0) {
128 err = map_private_extent_buffer(buf, offset, 32,
129 &map_token, &kaddr,
130 &map_start, &map_len, KM_USER0);
131 if (err) {
132 printk("failed to map extent buffer! %lu\n",
133 offset);
134 return 1;
135 }
136 cur_len = min(len, map_len - (offset - map_start));
137 crc = btrfs_csum_data(root, kaddr + offset - map_start,
138 crc, cur_len);
139 len -= cur_len;
140 offset += cur_len;
141 unmap_extent_buffer(buf, map_token, KM_USER0);
142 }
143 btrfs_csum_final(crc, result);
144
145 if (verify) {
146 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
147 printk("btrfs: %s checksum verify failed on %llu\n",
148 root->fs_info->sb->s_id,
149 buf->start);
150 return 1;
151 }
152 } else {
153 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
154 }
155 return 0;
156}
157
158
159int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
160{
161 struct extent_map_tree *tree;
162 u64 start = page->index << PAGE_CACHE_SHIFT;
163 u64 found_start;
164 int found_level;
165 unsigned long len;
166 struct extent_buffer *eb;
167 tree = &BTRFS_I(page->mapping->host)->extent_tree;
168
169 if (page->private == EXTENT_PAGE_PRIVATE)
170 goto out;
171 if (!page->private)
172 goto out;
173 len = page->private >> 2;
174 if (len == 0) {
175 WARN_ON(1);
176 }
177 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
178 read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1);
179 found_start = btrfs_header_bytenr(eb);
180 if (found_start != start) {
181 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
182 start, found_start, len);
183 }
184 found_level = btrfs_header_level(eb);
185 csum_tree_block(root, eb, 0);
186 free_extent_buffer(eb);
187out:
188 return 0;
189}
190
Chris Mason5f39d392007-10-15 16:14:19 -0400191static int btree_writepage(struct page *page, struct writeback_control *wbc)
192{
193 struct extent_map_tree *tree;
Chris Mason19c00dd2007-10-15 16:19:22 -0400194 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason5f39d392007-10-15 16:14:19 -0400195 tree = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason19c00dd2007-10-15 16:19:22 -0400196
197 csum_dirty_buffer(root, page);
Chris Mason5f39d392007-10-15 16:14:19 -0400198 return extent_write_full_page(tree, page, btree_get_extent, wbc);
199}
200int btree_readpage(struct file *file, struct page *page)
201{
202 struct extent_map_tree *tree;
203 tree = &BTRFS_I(page->mapping->host)->extent_tree;
204 return extent_read_full_page(tree, page, btree_get_extent);
205}
206
207static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags)
208{
209 struct extent_map_tree *tree;
210 int ret;
211
Chris Mason5f39d392007-10-15 16:14:19 -0400212 tree = &BTRFS_I(page->mapping->host)->extent_tree;
213 ret = try_release_extent_mapping(tree, page);
214 if (ret == 1) {
215 ClearPagePrivate(page);
216 set_page_private(page, 0);
217 page_cache_release(page);
218 }
Chris Masond98237b2007-03-28 13:57:48 -0400219 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400220}
Chris Mason123abc82007-03-14 14:14:43 -0400221
Chris Mason5f39d392007-10-15 16:14:19 -0400222static void btree_invalidatepage(struct page *page, unsigned long offset)
Chris Masond98237b2007-03-28 13:57:48 -0400223{
Chris Mason5f39d392007-10-15 16:14:19 -0400224 struct extent_map_tree *tree;
225 tree = &BTRFS_I(page->mapping->host)->extent_tree;
226 extent_invalidatepage(tree, page, offset);
227 btree_releasepage(page, GFP_NOFS);
Chris Masond98237b2007-03-28 13:57:48 -0400228}
229
Chris Mason5f39d392007-10-15 16:14:19 -0400230#if 0
Chris Masond98237b2007-03-28 13:57:48 -0400231static int btree_writepage(struct page *page, struct writeback_control *wbc)
232{
Chris Mason87cbda52007-03-28 19:44:27 -0400233 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400234 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400235 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400236 if (!page_has_buffers(page)) {
237 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
238 (1 << BH_Dirty)|(1 << BH_Uptodate));
239 }
240 head = page_buffers(page);
241 bh = head;
242 do {
243 if (buffer_dirty(bh))
244 csum_tree_block(root, bh, 0);
245 bh = bh->b_this_page;
246 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400247 return block_write_full_page(page, btree_get_block, wbc);
248}
Chris Mason5f39d392007-10-15 16:14:19 -0400249#endif
Chris Masond98237b2007-03-28 13:57:48 -0400250
251static struct address_space_operations btree_aops = {
252 .readpage = btree_readpage,
253 .writepage = btree_writepage,
Chris Mason5f39d392007-10-15 16:14:19 -0400254 .releasepage = btree_releasepage,
255 .invalidatepage = btree_invalidatepage,
Chris Masond98237b2007-03-28 13:57:48 -0400256 .sync_page = block_sync_page,
257};
258
Chris Masondb945352007-10-15 16:15:53 -0400259int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
Chris Mason090d1872007-05-01 08:53:32 -0400260{
Chris Mason5f39d392007-10-15 16:14:19 -0400261 struct extent_buffer *buf = NULL;
262 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonde428b62007-05-18 13:28:27 -0400263 int ret = 0;
Chris Mason090d1872007-05-01 08:53:32 -0400264
Chris Masondb945352007-10-15 16:15:53 -0400265 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400266 if (!buf)
Chris Mason090d1872007-05-01 08:53:32 -0400267 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400268 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -0400269 buf, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400270 free_extent_buffer(buf);
Chris Masonde428b62007-05-18 13:28:27 -0400271 return ret;
Chris Mason090d1872007-05-01 08:53:32 -0400272}
273
Chris Masondb945352007-10-15 16:15:53 -0400274struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
275 u32 blocksize)
Chris Masone20d96d2007-03-22 12:13:20 -0400276{
Chris Mason5f39d392007-10-15 16:14:19 -0400277 struct extent_buffer *buf = NULL;
278 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason19c00dd2007-10-15 16:19:22 -0400279 struct extent_map_tree *extent_tree;
280 int ret;
281
282 extent_tree = &BTRFS_I(btree_inode)->extent_tree;
Chris Masone20d96d2007-03-22 12:13:20 -0400283
Chris Masondb945352007-10-15 16:15:53 -0400284 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400285 if (!buf)
286 return NULL;
287 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -0400288 buf, 0, 1);
289 if (buf->flags & EXTENT_CSUM) {
290 return buf;
291 }
292 if (test_range_bit(extent_tree, buf->start, buf->start + buf->len - 1,
293 EXTENT_CSUM, 1)) {
294 buf->flags |= EXTENT_CSUM;
295 return buf;
296 }
297 ret = csum_tree_block(root, buf, 1);
298 set_extent_bits(extent_tree, buf->start,
299 buf->start + buf->len - 1,
300 EXTENT_CSUM, GFP_NOFS);
301 buf->flags |= EXTENT_CSUM;
Chris Mason5f39d392007-10-15 16:14:19 -0400302 return buf;
Chris Masoneb60cea2007-02-02 09:18:22 -0500303}
304
Chris Masone089f052007-03-16 16:20:31 -0400305int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400306 struct extent_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500307{
Chris Mason5f39d392007-10-15 16:14:19 -0400308 struct inode *btree_inode = root->fs_info->btree_inode;
309 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
310 return 0;
311}
312
313int wait_on_tree_block_writeback(struct btrfs_root *root,
314 struct extent_buffer *buf)
315{
316 struct inode *btree_inode = root->fs_info->btree_inode;
317 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree,
318 buf);
319 return 0;
320}
321
Chris Masondb945352007-10-15 16:15:53 -0400322static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400323 struct btrfs_root *root,
324 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400325 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500326{
Chris Masoncfaa7292007-02-21 17:04:57 -0500327 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400328 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500329 root->commit_root = NULL;
Chris Masondb945352007-10-15 16:15:53 -0400330 root->sectorsize = sectorsize;
331 root->nodesize = nodesize;
332 root->leafsize = leafsize;
Chris Mason123abc82007-03-14 14:14:43 -0400333 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400334 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400335 root->objectid = objectid;
336 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400337 root->highest_inode = 0;
338 root->last_inode_alloc = 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400339 root->name = NULL;
Chris Mason3768f362007-03-13 16:47:54 -0400340 memset(&root->root_key, 0, sizeof(root->root_key));
341 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason6702ed42007-08-07 16:15:09 -0400342 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
Josef Bacik58176a92007-08-29 15:47:34 -0400343 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
344 init_completion(&root->kobj_unregister);
Chris Mason011410b2007-09-10 19:58:36 -0400345 init_rwsem(&root->snap_sem);
Chris Mason6702ed42007-08-07 16:15:09 -0400346 root->defrag_running = 0;
347 root->defrag_level = 0;
Chris Mason4d775672007-04-20 20:23:12 -0400348 root->root_key.objectid = objectid;
Chris Mason3768f362007-03-13 16:47:54 -0400349 return 0;
350}
351
Chris Masondb945352007-10-15 16:15:53 -0400352static int find_and_setup_root(struct btrfs_root *tree_root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400353 struct btrfs_fs_info *fs_info,
354 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400355 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400356{
357 int ret;
Chris Masondb945352007-10-15 16:15:53 -0400358 u32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -0400359
Chris Masondb945352007-10-15 16:15:53 -0400360 __setup_root(tree_root->nodesize, tree_root->leafsize,
361 tree_root->sectorsize, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400362 ret = btrfs_find_last_root(tree_root, objectid,
363 &root->root_item, &root->root_key);
364 BUG_ON(ret);
365
Chris Masondb945352007-10-15 16:15:53 -0400366 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
367 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
368 blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400369 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500370 return 0;
371}
372
Chris Mason5eda7b52007-06-22 14:16:25 -0400373struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
374 struct btrfs_key *location)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400375{
376 struct btrfs_root *root;
377 struct btrfs_root *tree_root = fs_info->tree_root;
378 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400379 struct extent_buffer *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400380 u64 highest_inode;
Chris Masondb945352007-10-15 16:15:53 -0400381 u32 blocksize;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400382 int ret = 0;
383
Chris Mason5eda7b52007-06-22 14:16:25 -0400384 root = kzalloc(sizeof(*root), GFP_NOFS);
Chris Mason0cf6c622007-06-09 09:22:25 -0400385 if (!root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400386 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400387 if (location->offset == (u64)-1) {
Chris Masondb945352007-10-15 16:15:53 -0400388 ret = find_and_setup_root(tree_root, fs_info,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400389 location->objectid, root);
390 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400391 kfree(root);
392 return ERR_PTR(ret);
393 }
394 goto insert;
395 }
396
Chris Masondb945352007-10-15 16:15:53 -0400397 __setup_root(tree_root->nodesize, tree_root->leafsize,
398 tree_root->sectorsize, root, fs_info,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400399 location->objectid);
400
401 path = btrfs_alloc_path();
402 BUG_ON(!path);
403 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
404 if (ret != 0) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400405 if (ret > 0)
406 ret = -ENOENT;
407 goto out;
408 }
Chris Mason5f39d392007-10-15 16:14:19 -0400409 l = path->nodes[0];
410 read_extent_buffer(l, &root->root_item,
411 btrfs_item_ptr_offset(l, path->slots[0]),
Chris Mason0f7d52f2007-04-09 10:42:37 -0400412 sizeof(root->root_item));
Yan Zheng44b36eb2007-10-19 09:22:56 -0400413 memcpy(&root->root_key, location, sizeof(*location));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400414 ret = 0;
415out:
416 btrfs_release_path(root, path);
417 btrfs_free_path(path);
418 if (ret) {
419 kfree(root);
420 return ERR_PTR(ret);
421 }
Chris Masondb945352007-10-15 16:15:53 -0400422 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
423 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
424 blocksize);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400425 BUG_ON(!root->node);
426insert:
Chris Mason0f7d52f2007-04-09 10:42:37 -0400427 root->ref_cows = 1;
Chris Mason5eda7b52007-06-22 14:16:25 -0400428 ret = btrfs_find_highest_inode(root, &highest_inode);
429 if (ret == 0) {
430 root->highest_inode = highest_inode;
431 root->last_inode_alloc = highest_inode;
432 }
433 return root;
434}
435
436struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
Josef Bacik58176a92007-08-29 15:47:34 -0400437 struct btrfs_key *location,
438 const char *name, int namelen)
Chris Mason5eda7b52007-06-22 14:16:25 -0400439{
440 struct btrfs_root *root;
441 int ret;
442
443 root = radix_tree_lookup(&fs_info->fs_roots_radix,
444 (unsigned long)location->objectid);
445 if (root)
446 return root;
447
448 root = btrfs_read_fs_root_no_radix(fs_info, location);
449 if (IS_ERR(root))
450 return root;
Chris Mason2619ba12007-04-10 16:58:11 -0400451 ret = radix_tree_insert(&fs_info->fs_roots_radix,
452 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400453 root);
454 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400455 free_extent_buffer(root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400456 kfree(root);
457 return ERR_PTR(ret);
458 }
Josef Bacik58176a92007-08-29 15:47:34 -0400459
460 ret = btrfs_set_root_name(root, name, namelen);
461 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400462 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400463 kfree(root);
464 return ERR_PTR(ret);
465 }
466
467 ret = btrfs_sysfs_add_root(root);
468 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400469 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400470 kfree(root->name);
471 kfree(root);
472 return ERR_PTR(ret);
473 }
474
Chris Mason5ce14bb2007-09-11 11:15:39 -0400475 ret = btrfs_find_dead_roots(fs_info->tree_root,
476 root->root_key.objectid, root);
477 BUG_ON(ret);
478
Chris Mason0f7d52f2007-04-09 10:42:37 -0400479 return root;
480}
Chris Mason19c00dd2007-10-15 16:19:22 -0400481#if 0
482static int add_hasher(struct btrfs_fs_info *info, char *type) {
483 struct btrfs_hasher *hasher;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400484
Chris Mason19c00dd2007-10-15 16:19:22 -0400485 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
486 if (!hasher)
487 return -ENOMEM;
488 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
489 if (!hasher->hash_tfm) {
490 kfree(hasher);
491 return -EINVAL;
492 }
493 spin_lock(&info->hash_lock);
494 list_add(&hasher->list, &info->hashers);
495 spin_unlock(&info->hash_lock);
496 return 0;
497}
498#endif
Chris Mason2c90e5d2007-04-02 10:50:19 -0400499struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500500{
Chris Masondb945352007-10-15 16:15:53 -0400501 u32 sectorsize;
502 u32 nodesize;
503 u32 leafsize;
504 u32 blocksize;
Chris Masone20d96d2007-03-22 12:13:20 -0400505 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
506 GFP_NOFS);
507 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
508 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400509 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
510 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500511 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400512 int err = -EIO;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400513 struct btrfs_super_block *disk_super;
Chris Masoneb60cea2007-02-02 09:18:22 -0500514
Chris Mason39279cc2007-06-12 06:35:45 -0400515 if (!extent_root || !tree_root || !fs_info) {
516 err = -ENOMEM;
517 goto fail;
518 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400519 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason8fd17792007-04-19 21:01:03 -0400520 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -0400521 INIT_LIST_HEAD(&fs_info->dead_roots);
Chris Mason19c00dd2007-10-15 16:19:22 -0400522 INIT_LIST_HEAD(&fs_info->hashers);
523 spin_lock_init(&fs_info->hash_lock);
524
Josef Bacik58176a92007-08-29 15:47:34 -0400525 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
526 init_completion(&fs_info->kobj_unregister);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400527 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400528 fs_info->running_transaction = NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400529 fs_info->last_trans_committed = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400530 fs_info->tree_root = tree_root;
531 fs_info->extent_root = extent_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400532 fs_info->sb = sb;
Chris Masond98237b2007-03-28 13:57:48 -0400533 fs_info->btree_inode = new_inode(sb);
534 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400535 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400536 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
537 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason5f39d392007-10-15 16:14:19 -0400538 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
539 fs_info->btree_inode->i_mapping,
540 GFP_NOFS);
Chris Masonf510cfe2007-10-15 16:14:48 -0400541 extent_map_tree_init(&fs_info->free_space_cache,
542 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason96b51792007-10-15 16:15:19 -0400543 extent_map_tree_init(&fs_info->block_group_cache,
544 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason1a5bc162007-10-15 16:15:26 -0400545 extent_map_tree_init(&fs_info->pinned_extents,
546 fs_info->btree_inode->i_mapping, GFP_NOFS);
547 extent_map_tree_init(&fs_info->pending_del,
548 fs_info->btree_inode->i_mapping, GFP_NOFS);
549 extent_map_tree_init(&fs_info->extent_ins,
550 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masone66f7092007-04-20 13:16:02 -0400551 fs_info->do_barriers = 1;
Chris Masonfacda1e2007-06-08 18:11:48 -0400552 fs_info->closing = 0;
553
Chris Mason08607c12007-06-08 15:33:54 -0400554 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400555 BTRFS_I(fs_info->btree_inode)->root = tree_root;
556 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
557 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400558 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400559 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400560
Chris Mason79154b12007-03-22 15:59:16 -0400561 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400562 mutex_init(&fs_info->fs_mutex);
Chris Mason3768f362007-03-13 16:47:54 -0400563
Chris Mason19c00dd2007-10-15 16:19:22 -0400564#if 0
565 ret = add_hasher(fs_info, "crc32c");
566 if (ret) {
567 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
568 err = -ENOMEM;
569 goto fail_iput;
570 }
571#endif
Chris Masondb945352007-10-15 16:15:53 -0400572 __setup_root(512, 512, 512, tree_root,
Chris Mason2c90e5d2007-04-02 10:50:19 -0400573 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400574
Chris Mason2c90e5d2007-04-02 10:50:19 -0400575 fs_info->sb_buffer = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -0400576 BTRFS_SUPER_INFO_OFFSET,
577 512);
Chris Masond98237b2007-03-28 13:57:48 -0400578
Chris Mason0f7d52f2007-04-09 10:42:37 -0400579 if (!fs_info->sb_buffer)
Chris Mason39279cc2007-06-12 06:35:45 -0400580 goto fail_iput;
Chris Mason39279cc2007-06-12 06:35:45 -0400581
Chris Mason5f39d392007-10-15 16:14:19 -0400582 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
583 sizeof(fs_info->super_copy));
584
585 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
586 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
587 BTRFS_FSID_SIZE);
588 disk_super = &fs_info->super_copy;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400589 if (!btrfs_super_root(disk_super))
Chris Mason39279cc2007-06-12 06:35:45 -0400590 goto fail_sb_buffer;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400591
Chris Masondb945352007-10-15 16:15:53 -0400592 nodesize = btrfs_super_nodesize(disk_super);
593 leafsize = btrfs_super_leafsize(disk_super);
594 sectorsize = btrfs_super_sectorsize(disk_super);
595 tree_root->nodesize = nodesize;
596 tree_root->leafsize = leafsize;
597 tree_root->sectorsize = sectorsize;
Chris Masonff79f812007-10-15 16:22:25 -0400598 sb_set_blocksize(sb, sectorsize);
Chris Masondb945352007-10-15 16:15:53 -0400599
Chris Mason8352d8a2007-04-12 10:43:05 -0400600 i_size_write(fs_info->btree_inode,
Chris Masondb945352007-10-15 16:15:53 -0400601 btrfs_super_total_bytes(disk_super));
Chris Mason8352d8a2007-04-12 10:43:05 -0400602
Chris Mason39279cc2007-06-12 06:35:45 -0400603 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
604 sizeof(disk_super->magic))) {
605 printk("btrfs: valid FS not found on %s\n", sb->s_id);
606 goto fail_sb_buffer;
607 }
Chris Mason19c00dd2007-10-15 16:19:22 -0400608
Chris Masondb945352007-10-15 16:15:53 -0400609 blocksize = btrfs_level_size(tree_root,
610 btrfs_super_root_level(disk_super));
Chris Mason19c00dd2007-10-15 16:19:22 -0400611
Chris Masone20d96d2007-03-22 12:13:20 -0400612 tree_root->node = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -0400613 btrfs_super_root(disk_super),
614 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -0400615 if (!tree_root->node)
616 goto fail_sb_buffer;
Chris Mason3768f362007-03-13 16:47:54 -0400617
Chris Mason2c90e5d2007-04-02 10:50:19 -0400618 mutex_lock(&fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -0400619
620 ret = find_and_setup_root(tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400621 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason39279cc2007-06-12 06:35:45 -0400622 if (ret) {
623 mutex_unlock(&fs_info->fs_mutex);
624 goto fail_tree_root;
625 }
Chris Mason3768f362007-03-13 16:47:54 -0400626
Chris Mason9078a3e2007-04-26 16:46:15 -0400627 btrfs_read_block_groups(extent_root);
628
Chris Mason0f7d52f2007-04-09 10:42:37 -0400629 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400630 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400631 return tree_root;
Chris Mason39279cc2007-06-12 06:35:45 -0400632
633fail_tree_root:
Chris Mason5f39d392007-10-15 16:14:19 -0400634 free_extent_buffer(tree_root->node);
Chris Mason39279cc2007-06-12 06:35:45 -0400635fail_sb_buffer:
Chris Mason5f39d392007-10-15 16:14:19 -0400636 free_extent_buffer(fs_info->sb_buffer);
Chris Mason39279cc2007-06-12 06:35:45 -0400637fail_iput:
638 iput(fs_info->btree_inode);
639fail:
640 kfree(extent_root);
641 kfree(tree_root);
642 kfree(fs_info);
643 return ERR_PTR(err);
Chris Masoneb60cea2007-02-02 09:18:22 -0500644}
645
Chris Masone089f052007-03-16 16:20:31 -0400646int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400647 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500648{
Chris Masone66f7092007-04-20 13:16:02 -0400649 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -0400650 struct extent_buffer *super = root->fs_info->sb_buffer;
651 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400652
Chris Mason5f39d392007-10-15 16:14:19 -0400653 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super);
654 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
655 super->start, super->len);
656 return ret;
Chris Masoncfaa7292007-02-21 17:04:57 -0500657}
658
Chris Mason5eda7b52007-06-22 14:16:25 -0400659int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
Chris Mason2619ba12007-04-10 16:58:11 -0400660{
661 radix_tree_delete(&fs_info->fs_roots_radix,
662 (unsigned long)root->root_key.objectid);
Josef Bacik58176a92007-08-29 15:47:34 -0400663 btrfs_sysfs_del_root(root);
Chris Mason2619ba12007-04-10 16:58:11 -0400664 if (root->inode)
665 iput(root->inode);
666 if (root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400667 free_extent_buffer(root->node);
Chris Mason2619ba12007-04-10 16:58:11 -0400668 if (root->commit_root)
Chris Mason5f39d392007-10-15 16:14:19 -0400669 free_extent_buffer(root->commit_root);
Josef Bacik58176a92007-08-29 15:47:34 -0400670 if (root->name)
671 kfree(root->name);
Chris Mason2619ba12007-04-10 16:58:11 -0400672 kfree(root);
673 return 0;
674}
675
Chris Mason35b7e472007-05-02 15:53:43 -0400676static int del_fs_roots(struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400677{
678 int ret;
679 struct btrfs_root *gang[8];
680 int i;
681
682 while(1) {
683 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
684 (void **)gang, 0,
685 ARRAY_SIZE(gang));
686 if (!ret)
687 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400688 for (i = 0; i < ret; i++)
Chris Mason5eda7b52007-06-22 14:16:25 -0400689 btrfs_free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400690 }
691 return 0;
692}
Chris Masonb4100d62007-04-12 12:14:00 -0400693
Chris Masone20d96d2007-03-22 12:13:20 -0400694int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500695{
Chris Mason3768f362007-03-13 16:47:54 -0400696 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400697 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400698 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -0400699
Chris Masonfacda1e2007-06-08 18:11:48 -0400700 fs_info->closing = 1;
Chris Mason08607c12007-06-08 15:33:54 -0400701 btrfs_transaction_flush_work(root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400702 mutex_lock(&fs_info->fs_mutex);
Chris Mason6702ed42007-08-07 16:15:09 -0400703 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason79154b12007-03-22 15:59:16 -0400704 trans = btrfs_start_transaction(root, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400705 ret = btrfs_commit_transaction(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400706 /* run commit again to drop the original snapshot */
707 trans = btrfs_start_transaction(root, 1);
708 btrfs_commit_transaction(trans, root);
709 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400710 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400711 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400712 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500713
Chris Mason0f7d52f2007-04-09 10:42:37 -0400714 if (fs_info->extent_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400715 free_extent_buffer(fs_info->extent_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -0400716
Chris Mason0f7d52f2007-04-09 10:42:37 -0400717 if (fs_info->tree_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400718 free_extent_buffer(fs_info->tree_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -0400719
Chris Mason5f39d392007-10-15 16:14:19 -0400720 free_extent_buffer(fs_info->sb_buffer);
Chris Mason7eccb902007-04-11 15:53:25 -0400721
Chris Mason9078a3e2007-04-26 16:46:15 -0400722 btrfs_free_block_groups(root->fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400723 del_fs_roots(fs_info);
Chris Mason19c00dd2007-10-15 16:19:22 -0400724 extent_map_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->extent_tree);
Chris Masondb945352007-10-15 16:15:53 -0400725 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
726 iput(fs_info->btree_inode);
Chris Mason19c00dd2007-10-15 16:19:22 -0400727#if 0
728 while(!list_empty(&fs_info->hashers)) {
729 struct btrfs_hasher *hasher;
730 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
731 hashers);
732 list_del(&hasher->hashers);
733 crypto_free_hash(&fs_info->hash_tfm);
734 kfree(hasher);
735 }
736#endif
Chris Mason0f7d52f2007-04-09 10:42:37 -0400737 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400738 kfree(fs_info->tree_root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500739 return 0;
740}
741
Chris Mason5f39d392007-10-15 16:14:19 -0400742int btrfs_buffer_uptodate(struct extent_buffer *buf)
Chris Masonccd467d2007-06-28 15:57:36 -0400743{
Chris Mason810191f2007-10-15 16:18:55 -0400744 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Mason5f39d392007-10-15 16:14:19 -0400745 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf);
746}
Chris Mason6702ed42007-08-07 16:15:09 -0400747
Chris Mason5f39d392007-10-15 16:14:19 -0400748int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
749{
Chris Mason810191f2007-10-15 16:18:55 -0400750 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Mason5f39d392007-10-15 16:14:19 -0400751 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree,
752 buf);
753}
754
755void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
756{
Chris Mason810191f2007-10-15 16:18:55 -0400757 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason5f39d392007-10-15 16:14:19 -0400758 u64 transid = btrfs_header_generation(buf);
759 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason6702ed42007-08-07 16:15:09 -0400760
Chris Masonccd467d2007-06-28 15:57:36 -0400761 if (transid != root->fs_info->generation) {
762 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
Chris Masondb945352007-10-15 16:15:53 -0400763 (unsigned long long)buf->start,
Chris Masonccd467d2007-06-28 15:57:36 -0400764 transid, root->fs_info->generation);
765 WARN_ON(1);
766 }
Chris Mason5f39d392007-10-15 16:14:19 -0400767 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500768}
769
Chris Masond3c2fdc2007-09-17 10:58:06 -0400770void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
Chris Mason35b7e472007-05-02 15:53:43 -0400771{
Chris Masond3c2fdc2007-09-17 10:58:06 -0400772 balance_dirty_pages_ratelimited_nr(
Chris Mason304fced2007-10-15 16:21:17 -0400773 root->fs_info->btree_inode->i_mapping, 1);
Chris Mason35b7e472007-05-02 15:53:43 -0400774}
Chris Mason6b800532007-10-15 16:17:34 -0400775
776void btrfs_set_buffer_defrag(struct extent_buffer *buf)
777{
Chris Mason810191f2007-10-15 16:18:55 -0400778 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400779 struct inode *btree_inode = root->fs_info->btree_inode;
780 set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start,
781 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
782}
783
784void btrfs_set_buffer_defrag_done(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 Mason6b800532007-10-15 16:17:34 -0400787 struct inode *btree_inode = root->fs_info->btree_inode;
788 set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start,
789 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
790 GFP_NOFS);
791}
792
793int btrfs_buffer_defrag(struct extent_buffer *buf)
794{
Chris Mason810191f2007-10-15 16:18:55 -0400795 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400796 struct inode *btree_inode = root->fs_info->btree_inode;
797 return test_range_bit(&BTRFS_I(btree_inode)->extent_tree,
798 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
799}
800
801int btrfs_buffer_defrag_done(struct extent_buffer *buf)
802{
Chris Mason810191f2007-10-15 16:18:55 -0400803 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400804 struct inode *btree_inode = root->fs_info->btree_inode;
805 return test_range_bit(&BTRFS_I(btree_inode)->extent_tree,
806 buf->start, buf->start + buf->len - 1,
807 EXTENT_DEFRAG_DONE, 0);
808}
809
810int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
811{
Chris Mason810191f2007-10-15 16:18:55 -0400812 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400813 struct inode *btree_inode = root->fs_info->btree_inode;
814 return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree,
815 buf->start, buf->start + buf->len - 1,
816 EXTENT_DEFRAG_DONE, GFP_NOFS);
817}
818
819int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
820{
Chris Mason810191f2007-10-15 16:18:55 -0400821 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400822 struct inode *btree_inode = root->fs_info->btree_inode;
823 return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree,
824 buf->start, buf->start + buf->len - 1,
825 EXTENT_DEFRAG, GFP_NOFS);
826}
827
828int btrfs_read_buffer(struct extent_buffer *buf)
829{
Chris Mason810191f2007-10-15 16:18:55 -0400830 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400831 struct inode *btree_inode = root->fs_info->btree_inode;
832 return read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -0400833 buf, 0, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400834}