blob: fad9298c696274c93612ac414d412aac76be289e [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 if (eb)
54 eb->alloc_addr = (unsigned long)__builtin_return_address(0);
55 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 Masondb945352007-10-15 16:15:53 -040065 bytenr, blocksize, GFP_NOFS);
Chris Masonf510cfe2007-10-15 16:14:48 -040066 eb->alloc_addr = (unsigned long)__builtin_return_address(0);
67 return eb;
Chris Mason5f39d392007-10-15 16:14:19 -040068}
Chris Mason22b0ebd2007-03-30 08:47:31 -040069
Chris Mason5f39d392007-10-15 16:14:19 -040070struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
71 size_t page_offset, u64 start, u64 end,
72 int create)
73{
74 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
75 struct extent_map *em;
76 int ret;
Chris Masond98237b2007-03-28 13:57:48 -040077
Chris Mason5f39d392007-10-15 16:14:19 -040078again:
79 em = lookup_extent_mapping(em_tree, start, end);
80 if (em) {
81 goto out;
82 }
83 em = alloc_extent_map(GFP_NOFS);
84 if (!em) {
85 em = ERR_PTR(-ENOMEM);
86 goto out;
87 }
88 em->start = 0;
89 em->end = (i_size_read(inode) & ~((u64)PAGE_CACHE_SIZE -1)) - 1;
90 em->block_start = 0;
91 em->block_end = em->end;
92 em->bdev = inode->i_sb->s_bdev;
93 ret = add_extent_mapping(em_tree, em);
94 if (ret == -EEXIST) {
95 free_extent_map(em);
96 em = NULL;
97 goto again;
98 } else if (ret) {
99 em = ERR_PTR(ret);
100 }
101out:
102 return em;
103}
104
105static int btree_writepage(struct page *page, struct writeback_control *wbc)
106{
107 struct extent_map_tree *tree;
108 tree = &BTRFS_I(page->mapping->host)->extent_tree;
109 return extent_write_full_page(tree, page, btree_get_extent, wbc);
110}
111int btree_readpage(struct file *file, struct page *page)
112{
113 struct extent_map_tree *tree;
114 tree = &BTRFS_I(page->mapping->host)->extent_tree;
115 return extent_read_full_page(tree, page, btree_get_extent);
116}
117
118static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags)
119{
120 struct extent_map_tree *tree;
121 int ret;
122
123 BUG_ON(page->private != 1);
124 tree = &BTRFS_I(page->mapping->host)->extent_tree;
125 ret = try_release_extent_mapping(tree, page);
126 if (ret == 1) {
127 ClearPagePrivate(page);
128 set_page_private(page, 0);
129 page_cache_release(page);
130 }
Chris Masond98237b2007-03-28 13:57:48 -0400131 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400132}
Chris Mason123abc82007-03-14 14:14:43 -0400133
Chris Mason5f39d392007-10-15 16:14:19 -0400134static void btree_invalidatepage(struct page *page, unsigned long offset)
Chris Masond98237b2007-03-28 13:57:48 -0400135{
Chris Mason5f39d392007-10-15 16:14:19 -0400136 struct extent_map_tree *tree;
137 tree = &BTRFS_I(page->mapping->host)->extent_tree;
138 extent_invalidatepage(tree, page, offset);
139 btree_releasepage(page, GFP_NOFS);
Chris Masond98237b2007-03-28 13:57:48 -0400140}
141
Chris Masonf254e522007-03-29 15:15:27 -0400142int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
143 char *result)
Chris Mason87cbda52007-03-28 19:44:27 -0400144{
Chris Mason5f39d392007-10-15 16:14:19 -0400145 return 0;
146#if 0
Chris Mason11bd1432007-06-22 14:16:24 -0400147 u32 crc;
148 crc = crc32c(0, data, len);
149 memcpy(result, &crc, BTRFS_CRC32_SIZE);
150 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400151#endif
Chris Masonf254e522007-03-29 15:15:27 -0400152}
Chris Mason11bd1432007-06-22 14:16:24 -0400153
Chris Mason5f39d392007-10-15 16:14:19 -0400154#if 0
155static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
Chris Masonf254e522007-03-29 15:15:27 -0400156 int verify)
157{
Chris Mason5f39d392007-10-15 16:14:19 -0400158 return 0;
Chris Mason509659c2007-05-10 12:36:17 -0400159 char result[BTRFS_CRC32_SIZE];
Chris Masonf254e522007-03-29 15:15:27 -0400160 int ret;
161 struct btrfs_node *node;
162
163 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
164 bh->b_size - BTRFS_CSUM_SIZE, result);
165 if (ret)
166 return ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400167 if (verify) {
Chris Mason509659c2007-05-10 12:36:17 -0400168 if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
Chris Mason5af39812007-06-12 07:50:13 -0400169 printk("btrfs: %s checksum verify failed on %llu\n",
170 root->fs_info->sb->s_id,
171 (unsigned long long)bh_blocknr(bh));
Chris Masonf254e522007-03-29 15:15:27 -0400172 return 1;
173 }
174 } else {
175 node = btrfs_buffer_node(bh);
Chris Mason509659c2007-05-10 12:36:17 -0400176 memcpy(node->header.csum, result, BTRFS_CRC32_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -0400177 }
Chris Mason87cbda52007-03-28 19:44:27 -0400178 return 0;
179}
Chris Mason5f39d392007-10-15 16:14:19 -0400180#endif
Chris Mason87cbda52007-03-28 19:44:27 -0400181
Chris Mason5f39d392007-10-15 16:14:19 -0400182#if 0
Chris Masond98237b2007-03-28 13:57:48 -0400183static int btree_writepage(struct page *page, struct writeback_control *wbc)
184{
Chris Mason87cbda52007-03-28 19:44:27 -0400185 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400186 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400187 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400188 if (!page_has_buffers(page)) {
189 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
190 (1 << BH_Dirty)|(1 << BH_Uptodate));
191 }
192 head = page_buffers(page);
193 bh = head;
194 do {
195 if (buffer_dirty(bh))
196 csum_tree_block(root, bh, 0);
197 bh = bh->b_this_page;
198 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400199 return block_write_full_page(page, btree_get_block, wbc);
200}
Chris Mason5f39d392007-10-15 16:14:19 -0400201#endif
Chris Masond98237b2007-03-28 13:57:48 -0400202
203static struct address_space_operations btree_aops = {
204 .readpage = btree_readpage,
205 .writepage = btree_writepage,
Chris Mason5f39d392007-10-15 16:14:19 -0400206 .releasepage = btree_releasepage,
207 .invalidatepage = btree_invalidatepage,
Chris Masond98237b2007-03-28 13:57:48 -0400208 .sync_page = block_sync_page,
209};
210
Chris Masondb945352007-10-15 16:15:53 -0400211int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
Chris Mason090d1872007-05-01 08:53:32 -0400212{
Chris Mason5f39d392007-10-15 16:14:19 -0400213 struct extent_buffer *buf = NULL;
214 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonde428b62007-05-18 13:28:27 -0400215 int ret = 0;
Chris Mason090d1872007-05-01 08:53:32 -0400216
Chris Masondb945352007-10-15 16:15:53 -0400217 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400218 if (!buf)
Chris Mason090d1872007-05-01 08:53:32 -0400219 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400220 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
221 buf, 0);
222 free_extent_buffer(buf);
Chris Masonde428b62007-05-18 13:28:27 -0400223 return ret;
Chris Mason090d1872007-05-01 08:53:32 -0400224}
225
Chris Masondb945352007-10-15 16:15:53 -0400226struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
227 u32 blocksize)
Chris Masone20d96d2007-03-22 12:13:20 -0400228{
Chris Mason5f39d392007-10-15 16:14:19 -0400229 struct extent_buffer *buf = NULL;
230 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400231
Chris Masondb945352007-10-15 16:15:53 -0400232 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400233 if (!buf)
234 return NULL;
235 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
236 buf, 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400237 buf->alloc_addr = (unsigned long)__builtin_return_address(0);
Chris Mason5f39d392007-10-15 16:14:19 -0400238 return buf;
Chris Masoneb60cea2007-02-02 09:18:22 -0500239}
240
Chris Masone089f052007-03-16 16:20:31 -0400241int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400242 struct extent_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500243{
Chris Mason5f39d392007-10-15 16:14:19 -0400244 struct inode *btree_inode = root->fs_info->btree_inode;
245 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
246 return 0;
247}
248
249int wait_on_tree_block_writeback(struct btrfs_root *root,
250 struct extent_buffer *buf)
251{
252 struct inode *btree_inode = root->fs_info->btree_inode;
253 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree,
254 buf);
255 return 0;
256}
257
258int set_tree_block_dirty(struct btrfs_root *root, struct extent_buffer *buf)
259{
260 struct inode *btree_inode = root->fs_info->btree_inode;
261 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500262 return 0;
263}
264
Chris Masondb945352007-10-15 16:15:53 -0400265static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400266 struct btrfs_root *root,
267 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400268 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500269{
Chris Masoncfaa7292007-02-21 17:04:57 -0500270 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400271 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500272 root->commit_root = NULL;
Chris Masondb945352007-10-15 16:15:53 -0400273 root->sectorsize = sectorsize;
274 root->nodesize = nodesize;
275 root->leafsize = leafsize;
Chris Mason123abc82007-03-14 14:14:43 -0400276 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400277 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400278 root->objectid = objectid;
279 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400280 root->highest_inode = 0;
281 root->last_inode_alloc = 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400282 root->name = NULL;
Chris Mason3768f362007-03-13 16:47:54 -0400283 memset(&root->root_key, 0, sizeof(root->root_key));
284 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason6702ed42007-08-07 16:15:09 -0400285 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
Josef Bacik58176a92007-08-29 15:47:34 -0400286 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
287 init_completion(&root->kobj_unregister);
Chris Mason011410b2007-09-10 19:58:36 -0400288 init_rwsem(&root->snap_sem);
Chris Mason6702ed42007-08-07 16:15:09 -0400289 root->defrag_running = 0;
290 root->defrag_level = 0;
Chris Mason4d775672007-04-20 20:23:12 -0400291 root->root_key.objectid = objectid;
Chris Mason3768f362007-03-13 16:47:54 -0400292 return 0;
293}
294
Chris Masondb945352007-10-15 16:15:53 -0400295static int find_and_setup_root(struct btrfs_root *tree_root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400296 struct btrfs_fs_info *fs_info,
297 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400298 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400299{
300 int ret;
Chris Masondb945352007-10-15 16:15:53 -0400301 u32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -0400302
Chris Masondb945352007-10-15 16:15:53 -0400303 __setup_root(tree_root->nodesize, tree_root->leafsize,
304 tree_root->sectorsize, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400305 ret = btrfs_find_last_root(tree_root, objectid,
306 &root->root_item, &root->root_key);
307 BUG_ON(ret);
308
Chris Masondb945352007-10-15 16:15:53 -0400309 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
310 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
311 blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400312 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500313 return 0;
314}
315
Chris Mason5eda7b52007-06-22 14:16:25 -0400316struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
317 struct btrfs_key *location)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400318{
319 struct btrfs_root *root;
320 struct btrfs_root *tree_root = fs_info->tree_root;
321 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400322 struct extent_buffer *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400323 u64 highest_inode;
Chris Masondb945352007-10-15 16:15:53 -0400324 u32 blocksize;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400325 int ret = 0;
326
Chris Mason5eda7b52007-06-22 14:16:25 -0400327 root = kzalloc(sizeof(*root), GFP_NOFS);
Chris Mason0cf6c622007-06-09 09:22:25 -0400328 if (!root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400329 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400330 if (location->offset == (u64)-1) {
Chris Masondb945352007-10-15 16:15:53 -0400331 ret = find_and_setup_root(tree_root, fs_info,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400332 location->objectid, root);
333 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400334 kfree(root);
335 return ERR_PTR(ret);
336 }
337 goto insert;
338 }
339
Chris Masondb945352007-10-15 16:15:53 -0400340 __setup_root(tree_root->nodesize, tree_root->leafsize,
341 tree_root->sectorsize, root, fs_info,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400342 location->objectid);
343
344 path = btrfs_alloc_path();
345 BUG_ON(!path);
346 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
347 if (ret != 0) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400348 if (ret > 0)
349 ret = -ENOENT;
350 goto out;
351 }
Chris Mason5f39d392007-10-15 16:14:19 -0400352 l = path->nodes[0];
353 read_extent_buffer(l, &root->root_item,
354 btrfs_item_ptr_offset(l, path->slots[0]),
Chris Mason0f7d52f2007-04-09 10:42:37 -0400355 sizeof(root->root_item));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400356 ret = 0;
357out:
358 btrfs_release_path(root, path);
359 btrfs_free_path(path);
360 if (ret) {
361 kfree(root);
362 return ERR_PTR(ret);
363 }
Chris Masondb945352007-10-15 16:15:53 -0400364 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
365 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
366 blocksize);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400367 BUG_ON(!root->node);
368insert:
Chris Mason0f7d52f2007-04-09 10:42:37 -0400369 root->ref_cows = 1;
Chris Mason5eda7b52007-06-22 14:16:25 -0400370 ret = btrfs_find_highest_inode(root, &highest_inode);
371 if (ret == 0) {
372 root->highest_inode = highest_inode;
373 root->last_inode_alloc = highest_inode;
374 }
375 return root;
376}
377
378struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
Josef Bacik58176a92007-08-29 15:47:34 -0400379 struct btrfs_key *location,
380 const char *name, int namelen)
Chris Mason5eda7b52007-06-22 14:16:25 -0400381{
382 struct btrfs_root *root;
383 int ret;
384
385 root = radix_tree_lookup(&fs_info->fs_roots_radix,
386 (unsigned long)location->objectid);
387 if (root)
388 return root;
389
390 root = btrfs_read_fs_root_no_radix(fs_info, location);
391 if (IS_ERR(root))
392 return root;
Chris Mason2619ba12007-04-10 16:58:11 -0400393 ret = radix_tree_insert(&fs_info->fs_roots_radix,
394 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400395 root);
396 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400397 free_extent_buffer(root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400398 kfree(root);
399 return ERR_PTR(ret);
400 }
Josef Bacik58176a92007-08-29 15:47:34 -0400401
402 ret = btrfs_set_root_name(root, name, namelen);
403 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400404 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400405 kfree(root);
406 return ERR_PTR(ret);
407 }
408
409 ret = btrfs_sysfs_add_root(root);
410 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400411 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400412 kfree(root->name);
413 kfree(root);
414 return ERR_PTR(ret);
415 }
416
Chris Mason5ce14bb2007-09-11 11:15:39 -0400417 ret = btrfs_find_dead_roots(fs_info->tree_root,
418 root->root_key.objectid, root);
419 BUG_ON(ret);
420
Chris Mason0f7d52f2007-04-09 10:42:37 -0400421 return root;
422}
423
Chris Mason2c90e5d2007-04-02 10:50:19 -0400424struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500425{
Chris Masondb945352007-10-15 16:15:53 -0400426 u32 sectorsize;
427 u32 nodesize;
428 u32 leafsize;
429 u32 blocksize;
Chris Masone20d96d2007-03-22 12:13:20 -0400430 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
431 GFP_NOFS);
432 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
433 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400434 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
435 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500436 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400437 int err = -EIO;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400438 struct btrfs_super_block *disk_super;
Chris Masoneb60cea2007-02-02 09:18:22 -0500439
Chris Mason39279cc2007-06-12 06:35:45 -0400440 if (!extent_root || !tree_root || !fs_info) {
441 err = -ENOMEM;
442 goto fail;
443 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400444 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason8fd17792007-04-19 21:01:03 -0400445 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -0400446 INIT_LIST_HEAD(&fs_info->dead_roots);
Josef Bacik58176a92007-08-29 15:47:34 -0400447 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
448 init_completion(&fs_info->kobj_unregister);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400449 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400450 fs_info->running_transaction = NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400451 fs_info->last_trans_committed = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400452 fs_info->tree_root = tree_root;
453 fs_info->extent_root = extent_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400454 fs_info->sb = sb;
Chris Masond98237b2007-03-28 13:57:48 -0400455 fs_info->btree_inode = new_inode(sb);
456 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400457 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400458 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
459 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason5f39d392007-10-15 16:14:19 -0400460 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
461 fs_info->btree_inode->i_mapping,
462 GFP_NOFS);
Chris Masonf510cfe2007-10-15 16:14:48 -0400463 extent_map_tree_init(&fs_info->free_space_cache,
464 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason96b51792007-10-15 16:15:19 -0400465 extent_map_tree_init(&fs_info->block_group_cache,
466 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason1a5bc162007-10-15 16:15:26 -0400467 extent_map_tree_init(&fs_info->pinned_extents,
468 fs_info->btree_inode->i_mapping, GFP_NOFS);
469 extent_map_tree_init(&fs_info->pending_del,
470 fs_info->btree_inode->i_mapping, GFP_NOFS);
471 extent_map_tree_init(&fs_info->extent_ins,
472 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masone66f7092007-04-20 13:16:02 -0400473 fs_info->do_barriers = 1;
Chris Masonfacda1e2007-06-08 18:11:48 -0400474 fs_info->closing = 0;
475
Chris Mason08607c12007-06-08 15:33:54 -0400476 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400477 BTRFS_I(fs_info->btree_inode)->root = tree_root;
478 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
479 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400480 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400481 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400482
Chris Mason79154b12007-03-22 15:59:16 -0400483 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400484 mutex_init(&fs_info->fs_mutex);
Chris Mason3768f362007-03-13 16:47:54 -0400485
Chris Masondb945352007-10-15 16:15:53 -0400486 __setup_root(512, 512, 512, tree_root,
Chris Mason2c90e5d2007-04-02 10:50:19 -0400487 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400488
Chris Mason2c90e5d2007-04-02 10:50:19 -0400489 fs_info->sb_buffer = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -0400490 BTRFS_SUPER_INFO_OFFSET,
491 512);
Chris Masond98237b2007-03-28 13:57:48 -0400492
Chris Mason0f7d52f2007-04-09 10:42:37 -0400493 if (!fs_info->sb_buffer)
Chris Mason39279cc2007-06-12 06:35:45 -0400494 goto fail_iput;
Chris Mason39279cc2007-06-12 06:35:45 -0400495
Chris Mason5f39d392007-10-15 16:14:19 -0400496 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
497 sizeof(fs_info->super_copy));
498
499 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
500 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
501 BTRFS_FSID_SIZE);
502 disk_super = &fs_info->super_copy;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400503 if (!btrfs_super_root(disk_super))
Chris Mason39279cc2007-06-12 06:35:45 -0400504 goto fail_sb_buffer;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400505
Chris Masondb945352007-10-15 16:15:53 -0400506 nodesize = btrfs_super_nodesize(disk_super);
507 leafsize = btrfs_super_leafsize(disk_super);
508 sectorsize = btrfs_super_sectorsize(disk_super);
509 tree_root->nodesize = nodesize;
510 tree_root->leafsize = leafsize;
511 tree_root->sectorsize = sectorsize;
512
Chris Mason8352d8a2007-04-12 10:43:05 -0400513 i_size_write(fs_info->btree_inode,
Chris Masondb945352007-10-15 16:15:53 -0400514 btrfs_super_total_bytes(disk_super));
Chris Mason8352d8a2007-04-12 10:43:05 -0400515
Chris Mason39279cc2007-06-12 06:35:45 -0400516
517 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
518 sizeof(disk_super->magic))) {
519 printk("btrfs: valid FS not found on %s\n", sb->s_id);
520 goto fail_sb_buffer;
521 }
Chris Masondb945352007-10-15 16:15:53 -0400522 blocksize = btrfs_level_size(tree_root,
523 btrfs_super_root_level(disk_super));
Chris Masone20d96d2007-03-22 12:13:20 -0400524 tree_root->node = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -0400525 btrfs_super_root(disk_super),
526 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -0400527 if (!tree_root->node)
528 goto fail_sb_buffer;
Chris Mason3768f362007-03-13 16:47:54 -0400529
Chris Masondb945352007-10-15 16:15:53 -0400530#if 0
531 btrfs_print_leaf(tree_root, tree_root->node);
532 err = -EIO;
533 goto fail_tree_root;
534#endif
Chris Mason2c90e5d2007-04-02 10:50:19 -0400535 mutex_lock(&fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -0400536
537 ret = find_and_setup_root(tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400538 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason39279cc2007-06-12 06:35:45 -0400539 if (ret) {
540 mutex_unlock(&fs_info->fs_mutex);
541 goto fail_tree_root;
542 }
Chris Mason3768f362007-03-13 16:47:54 -0400543
Chris Mason9078a3e2007-04-26 16:46:15 -0400544 btrfs_read_block_groups(extent_root);
545
Chris Mason0f7d52f2007-04-09 10:42:37 -0400546 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400547 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400548 return tree_root;
Chris Mason39279cc2007-06-12 06:35:45 -0400549
550fail_tree_root:
Chris Mason5f39d392007-10-15 16:14:19 -0400551 free_extent_buffer(tree_root->node);
Chris Mason39279cc2007-06-12 06:35:45 -0400552fail_sb_buffer:
Chris Mason5f39d392007-10-15 16:14:19 -0400553 free_extent_buffer(fs_info->sb_buffer);
Chris Mason39279cc2007-06-12 06:35:45 -0400554fail_iput:
555 iput(fs_info->btree_inode);
556fail:
557 kfree(extent_root);
558 kfree(tree_root);
559 kfree(fs_info);
560 return ERR_PTR(err);
Chris Masoneb60cea2007-02-02 09:18:22 -0500561}
562
Chris Masone089f052007-03-16 16:20:31 -0400563int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400564 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500565{
Chris Masone66f7092007-04-20 13:16:02 -0400566 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -0400567 struct extent_buffer *super = root->fs_info->sb_buffer;
568 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400569
Chris Mason5f39d392007-10-15 16:14:19 -0400570 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super);
571 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
572 super->start, super->len);
573 return ret;
Chris Masoncfaa7292007-02-21 17:04:57 -0500574}
575
Chris Mason5eda7b52007-06-22 14:16:25 -0400576int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
Chris Mason2619ba12007-04-10 16:58:11 -0400577{
578 radix_tree_delete(&fs_info->fs_roots_radix,
579 (unsigned long)root->root_key.objectid);
Josef Bacik58176a92007-08-29 15:47:34 -0400580 btrfs_sysfs_del_root(root);
Chris Mason2619ba12007-04-10 16:58:11 -0400581 if (root->inode)
582 iput(root->inode);
583 if (root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400584 free_extent_buffer(root->node);
Chris Mason2619ba12007-04-10 16:58:11 -0400585 if (root->commit_root)
Chris Mason5f39d392007-10-15 16:14:19 -0400586 free_extent_buffer(root->commit_root);
Josef Bacik58176a92007-08-29 15:47:34 -0400587 if (root->name)
588 kfree(root->name);
Chris Mason2619ba12007-04-10 16:58:11 -0400589 kfree(root);
590 return 0;
591}
592
Chris Mason35b7e472007-05-02 15:53:43 -0400593static int del_fs_roots(struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400594{
595 int ret;
596 struct btrfs_root *gang[8];
597 int i;
598
599 while(1) {
600 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
601 (void **)gang, 0,
602 ARRAY_SIZE(gang));
603 if (!ret)
604 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400605 for (i = 0; i < ret; i++)
Chris Mason5eda7b52007-06-22 14:16:25 -0400606 btrfs_free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400607 }
608 return 0;
609}
Chris Masonb4100d62007-04-12 12:14:00 -0400610
Chris Masone20d96d2007-03-22 12:13:20 -0400611int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500612{
Chris Mason3768f362007-03-13 16:47:54 -0400613 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400614 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400615 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -0400616
Chris Masonfacda1e2007-06-08 18:11:48 -0400617 fs_info->closing = 1;
Chris Mason08607c12007-06-08 15:33:54 -0400618 btrfs_transaction_flush_work(root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400619 mutex_lock(&fs_info->fs_mutex);
Chris Mason6702ed42007-08-07 16:15:09 -0400620 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason79154b12007-03-22 15:59:16 -0400621 trans = btrfs_start_transaction(root, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400622 ret = btrfs_commit_transaction(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400623 /* run commit again to drop the original snapshot */
624 trans = btrfs_start_transaction(root, 1);
625 btrfs_commit_transaction(trans, root);
626 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400627 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400628 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400629 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500630
Chris Mason0f7d52f2007-04-09 10:42:37 -0400631 if (fs_info->extent_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400632 free_extent_buffer(fs_info->extent_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -0400633
Chris Mason0f7d52f2007-04-09 10:42:37 -0400634 if (fs_info->tree_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400635 free_extent_buffer(fs_info->tree_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -0400636
Chris Mason5f39d392007-10-15 16:14:19 -0400637 free_extent_buffer(fs_info->sb_buffer);
Chris Mason7eccb902007-04-11 15:53:25 -0400638
Chris Mason9078a3e2007-04-26 16:46:15 -0400639 btrfs_free_block_groups(root->fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400640 del_fs_roots(fs_info);
Chris Masondb945352007-10-15 16:15:53 -0400641 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
642 iput(fs_info->btree_inode);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400643 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400644 kfree(fs_info->tree_root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500645 return 0;
646}
647
Chris Mason5f39d392007-10-15 16:14:19 -0400648int btrfs_buffer_uptodate(struct extent_buffer *buf)
Chris Masonccd467d2007-06-28 15:57:36 -0400649{
Chris Mason6d36dcd2007-10-15 16:14:37 -0400650 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Mason5f39d392007-10-15 16:14:19 -0400651 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf);
652}
Chris Mason6702ed42007-08-07 16:15:09 -0400653
Chris Mason5f39d392007-10-15 16:14:19 -0400654int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
655{
Chris Mason6d36dcd2007-10-15 16:14:37 -0400656 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Mason5f39d392007-10-15 16:14:19 -0400657 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree,
658 buf);
659}
660
661void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
662{
Chris Mason6d36dcd2007-10-15 16:14:37 -0400663 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason5f39d392007-10-15 16:14:19 -0400664 u64 transid = btrfs_header_generation(buf);
665 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason6702ed42007-08-07 16:15:09 -0400666
Chris Masonccd467d2007-06-28 15:57:36 -0400667 if (transid != root->fs_info->generation) {
668 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
Chris Masondb945352007-10-15 16:15:53 -0400669 (unsigned long long)buf->start,
Chris Masonccd467d2007-06-28 15:57:36 -0400670 transid, root->fs_info->generation);
671 WARN_ON(1);
672 }
Chris Mason5f39d392007-10-15 16:14:19 -0400673 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500674}
675
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400676void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
Chris Mason35b7e472007-05-02 15:53:43 -0400677{
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400678 balance_dirty_pages_ratelimited_nr(
679 root->fs_info->btree_inode->i_mapping, nr);
Chris Mason35b7e472007-05-02 15:53:43 -0400680}