blob: 8242933a1d8b411bc2ed7e9dec030d04ec9ac263 [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 Masoneb60cea2007-02-02 09:18:22 -050031
Chris Mason5f39d392007-10-15 16:14:19 -040032#if 0
33static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
Chris Mason7eccb902007-04-11 15:53:25 -040034{
Chris Mason5f39d392007-10-15 16:14:19 -040035 if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
36 printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
37 (unsigned long long)extent_buffer_blocknr(buf),
38 (unsigned long long)btrfs_header_blocknr(buf));
Chris Mason39279cc2007-06-12 06:35:45 -040039 return 1;
Chris Masond98237b2007-03-28 13:57:48 -040040 }
Chris Mason9a8dd152007-02-23 08:38:36 -050041 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050042}
Chris Mason5f39d392007-10-15 16:14:19 -040043#endif
Chris Masoneb60cea2007-02-02 09:18:22 -050044
Chris Mason5f39d392007-10-15 16:14:19 -040045struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
46 u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050047{
Chris Mason5f39d392007-10-15 16:14:19 -040048 struct inode *btree_inode = root->fs_info->btree_inode;
49 return find_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
50 blocknr * root->sectorsize,
51 root->sectorsize, GFP_NOFS);
Chris Masoned2ff2c2007-03-01 18:59:40 -050052}
53
Chris Mason5f39d392007-10-15 16:14:19 -040054struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
Chris Masond98237b2007-03-28 13:57:48 -040055 u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050056{
Chris Mason5f39d392007-10-15 16:14:19 -040057 struct inode *btree_inode = root->fs_info->btree_inode;
58 return alloc_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
59 blocknr * root->sectorsize,
60 root->sectorsize, GFP_NOFS);
61}
Chris Mason22b0ebd2007-03-30 08:47:31 -040062
Chris Mason5f39d392007-10-15 16:14:19 -040063struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
64 size_t page_offset, u64 start, u64 end,
65 int create)
66{
67 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
68 struct extent_map *em;
69 int ret;
Chris Masond98237b2007-03-28 13:57:48 -040070
Chris Mason5f39d392007-10-15 16:14:19 -040071again:
72 em = lookup_extent_mapping(em_tree, start, end);
73 if (em) {
74 goto out;
75 }
76 em = alloc_extent_map(GFP_NOFS);
77 if (!em) {
78 em = ERR_PTR(-ENOMEM);
79 goto out;
80 }
81 em->start = 0;
82 em->end = (i_size_read(inode) & ~((u64)PAGE_CACHE_SIZE -1)) - 1;
83 em->block_start = 0;
84 em->block_end = em->end;
85 em->bdev = inode->i_sb->s_bdev;
86 ret = add_extent_mapping(em_tree, em);
87 if (ret == -EEXIST) {
88 free_extent_map(em);
89 em = NULL;
90 goto again;
91 } else if (ret) {
92 em = ERR_PTR(ret);
93 }
94out:
95 return em;
96}
97
98static int btree_writepage(struct page *page, struct writeback_control *wbc)
99{
100 struct extent_map_tree *tree;
101 tree = &BTRFS_I(page->mapping->host)->extent_tree;
102 return extent_write_full_page(tree, page, btree_get_extent, wbc);
103}
104int btree_readpage(struct file *file, struct page *page)
105{
106 struct extent_map_tree *tree;
107 tree = &BTRFS_I(page->mapping->host)->extent_tree;
108 return extent_read_full_page(tree, page, btree_get_extent);
109}
110
111static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags)
112{
113 struct extent_map_tree *tree;
114 int ret;
115
116 BUG_ON(page->private != 1);
117 tree = &BTRFS_I(page->mapping->host)->extent_tree;
118 ret = try_release_extent_mapping(tree, page);
119 if (ret == 1) {
120 ClearPagePrivate(page);
121 set_page_private(page, 0);
122 page_cache_release(page);
123 }
Chris Masond98237b2007-03-28 13:57:48 -0400124 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400125}
Chris Mason123abc82007-03-14 14:14:43 -0400126
Chris Mason5f39d392007-10-15 16:14:19 -0400127static void btree_invalidatepage(struct page *page, unsigned long offset)
Chris Masond98237b2007-03-28 13:57:48 -0400128{
Chris Mason5f39d392007-10-15 16:14:19 -0400129 struct extent_map_tree *tree;
130 tree = &BTRFS_I(page->mapping->host)->extent_tree;
131 extent_invalidatepage(tree, page, offset);
132 btree_releasepage(page, GFP_NOFS);
Chris Masond98237b2007-03-28 13:57:48 -0400133}
134
Chris Masonf254e522007-03-29 15:15:27 -0400135int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
136 char *result)
Chris Mason87cbda52007-03-28 19:44:27 -0400137{
Chris Mason5f39d392007-10-15 16:14:19 -0400138 return 0;
139#if 0
Chris Mason11bd1432007-06-22 14:16:24 -0400140 u32 crc;
141 crc = crc32c(0, data, len);
142 memcpy(result, &crc, BTRFS_CRC32_SIZE);
143 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400144#endif
Chris Masonf254e522007-03-29 15:15:27 -0400145}
Chris Mason11bd1432007-06-22 14:16:24 -0400146
Chris Mason5f39d392007-10-15 16:14:19 -0400147#if 0
148static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
Chris Masonf254e522007-03-29 15:15:27 -0400149 int verify)
150{
Chris Mason5f39d392007-10-15 16:14:19 -0400151 return 0;
Chris Mason509659c2007-05-10 12:36:17 -0400152 char result[BTRFS_CRC32_SIZE];
Chris Masonf254e522007-03-29 15:15:27 -0400153 int ret;
154 struct btrfs_node *node;
155
156 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
157 bh->b_size - BTRFS_CSUM_SIZE, result);
158 if (ret)
159 return ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400160 if (verify) {
Chris Mason509659c2007-05-10 12:36:17 -0400161 if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
Chris Mason5af39812007-06-12 07:50:13 -0400162 printk("btrfs: %s checksum verify failed on %llu\n",
163 root->fs_info->sb->s_id,
164 (unsigned long long)bh_blocknr(bh));
Chris Masonf254e522007-03-29 15:15:27 -0400165 return 1;
166 }
167 } else {
168 node = btrfs_buffer_node(bh);
Chris Mason509659c2007-05-10 12:36:17 -0400169 memcpy(node->header.csum, result, BTRFS_CRC32_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -0400170 }
Chris Mason87cbda52007-03-28 19:44:27 -0400171 return 0;
172}
Chris Mason5f39d392007-10-15 16:14:19 -0400173#endif
Chris Mason87cbda52007-03-28 19:44:27 -0400174
Chris Mason5f39d392007-10-15 16:14:19 -0400175#if 0
Chris Masond98237b2007-03-28 13:57:48 -0400176static int btree_writepage(struct page *page, struct writeback_control *wbc)
177{
Chris Mason87cbda52007-03-28 19:44:27 -0400178 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400179 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400180 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400181 if (!page_has_buffers(page)) {
182 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
183 (1 << BH_Dirty)|(1 << BH_Uptodate));
184 }
185 head = page_buffers(page);
186 bh = head;
187 do {
188 if (buffer_dirty(bh))
189 csum_tree_block(root, bh, 0);
190 bh = bh->b_this_page;
191 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400192 return block_write_full_page(page, btree_get_block, wbc);
193}
Chris Mason5f39d392007-10-15 16:14:19 -0400194#endif
Chris Masond98237b2007-03-28 13:57:48 -0400195
196static struct address_space_operations btree_aops = {
197 .readpage = btree_readpage,
198 .writepage = btree_writepage,
Chris Mason5f39d392007-10-15 16:14:19 -0400199 .releasepage = btree_releasepage,
200 .invalidatepage = btree_invalidatepage,
Chris Masond98237b2007-03-28 13:57:48 -0400201 .sync_page = block_sync_page,
202};
203
Chris Mason090d1872007-05-01 08:53:32 -0400204int readahead_tree_block(struct btrfs_root *root, u64 blocknr)
205{
Chris Mason5f39d392007-10-15 16:14:19 -0400206 struct extent_buffer *buf = NULL;
207 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonde428b62007-05-18 13:28:27 -0400208 int ret = 0;
Chris Mason090d1872007-05-01 08:53:32 -0400209
Chris Mason5f39d392007-10-15 16:14:19 -0400210 buf = btrfs_find_create_tree_block(root, blocknr);
211 if (!buf)
Chris Mason090d1872007-05-01 08:53:32 -0400212 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400213 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
214 buf, 0);
215 free_extent_buffer(buf);
Chris Masonde428b62007-05-18 13:28:27 -0400216 return ret;
Chris Mason090d1872007-05-01 08:53:32 -0400217}
218
Chris Mason5f39d392007-10-15 16:14:19 -0400219struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masone20d96d2007-03-22 12:13:20 -0400220{
Chris Mason5f39d392007-10-15 16:14:19 -0400221 struct extent_buffer *buf = NULL;
222 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400223
Chris Mason5f39d392007-10-15 16:14:19 -0400224 buf = btrfs_find_create_tree_block(root, blocknr);
225 if (!buf)
226 return NULL;
227 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
228 buf, 1);
229 return buf;
Chris Masoneb60cea2007-02-02 09:18:22 -0500230}
231
Chris Masone089f052007-03-16 16:20:31 -0400232int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400233 struct extent_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500234{
Chris Mason5f39d392007-10-15 16:14:19 -0400235 struct inode *btree_inode = root->fs_info->btree_inode;
236 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
237 return 0;
238}
239
240int wait_on_tree_block_writeback(struct btrfs_root *root,
241 struct extent_buffer *buf)
242{
243 struct inode *btree_inode = root->fs_info->btree_inode;
244 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree,
245 buf);
246 return 0;
247}
248
249int set_tree_block_dirty(struct btrfs_root *root, struct extent_buffer *buf)
250{
251 struct inode *btree_inode = root->fs_info->btree_inode;
252 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500253 return 0;
254}
255
Chris Mason2c90e5d2007-04-02 10:50:19 -0400256static int __setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400257 struct btrfs_root *root,
258 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400259 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500260{
Chris Masoncfaa7292007-02-21 17:04:57 -0500261 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400262 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500263 root->commit_root = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400264 root->sectorsize = blocksize;
265 root->nodesize = blocksize;
266 root->leafsize = blocksize;
Chris Mason123abc82007-03-14 14:14:43 -0400267 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400268 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400269 root->objectid = objectid;
270 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400271 root->highest_inode = 0;
272 root->last_inode_alloc = 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400273 root->name = NULL;
Chris Mason3768f362007-03-13 16:47:54 -0400274 memset(&root->root_key, 0, sizeof(root->root_key));
275 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason6702ed42007-08-07 16:15:09 -0400276 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
Josef Bacik58176a92007-08-29 15:47:34 -0400277 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
278 init_completion(&root->kobj_unregister);
Chris Mason011410b2007-09-10 19:58:36 -0400279 init_rwsem(&root->snap_sem);
Chris Mason6702ed42007-08-07 16:15:09 -0400280 root->defrag_running = 0;
281 root->defrag_level = 0;
Chris Mason4d775672007-04-20 20:23:12 -0400282 root->root_key.objectid = objectid;
Chris Mason3768f362007-03-13 16:47:54 -0400283 return 0;
284}
285
Chris Mason2c90e5d2007-04-02 10:50:19 -0400286static int find_and_setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400287 struct btrfs_root *tree_root,
288 struct btrfs_fs_info *fs_info,
289 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400290 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400291{
292 int ret;
293
Chris Mason2c90e5d2007-04-02 10:50:19 -0400294 __setup_root(blocksize, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400295 ret = btrfs_find_last_root(tree_root, objectid,
296 &root->root_item, &root->root_key);
297 BUG_ON(ret);
298
299 root->node = read_tree_block(root,
300 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400301 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500302 return 0;
303}
304
Chris Mason5eda7b52007-06-22 14:16:25 -0400305struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
306 struct btrfs_key *location)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400307{
308 struct btrfs_root *root;
309 struct btrfs_root *tree_root = fs_info->tree_root;
310 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400311 struct extent_buffer *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400312 u64 highest_inode;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400313 int ret = 0;
314
Chris Mason5eda7b52007-06-22 14:16:25 -0400315 root = kzalloc(sizeof(*root), GFP_NOFS);
Chris Mason0cf6c622007-06-09 09:22:25 -0400316 if (!root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400317 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400318 if (location->offset == (u64)-1) {
319 ret = find_and_setup_root(fs_info->sb->s_blocksize,
320 fs_info->tree_root, fs_info,
321 location->objectid, root);
322 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400323 kfree(root);
324 return ERR_PTR(ret);
325 }
326 goto insert;
327 }
328
329 __setup_root(fs_info->sb->s_blocksize, root, fs_info,
330 location->objectid);
331
332 path = btrfs_alloc_path();
333 BUG_ON(!path);
334 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
335 if (ret != 0) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400336 if (ret > 0)
337 ret = -ENOENT;
338 goto out;
339 }
Chris Mason5f39d392007-10-15 16:14:19 -0400340 l = path->nodes[0];
341 read_extent_buffer(l, &root->root_item,
342 btrfs_item_ptr_offset(l, path->slots[0]),
Chris Mason0f7d52f2007-04-09 10:42:37 -0400343 sizeof(root->root_item));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400344 ret = 0;
345out:
346 btrfs_release_path(root, path);
347 btrfs_free_path(path);
348 if (ret) {
349 kfree(root);
350 return ERR_PTR(ret);
351 }
352 root->node = read_tree_block(root,
353 btrfs_root_blocknr(&root->root_item));
354 BUG_ON(!root->node);
355insert:
Chris Mason0f7d52f2007-04-09 10:42:37 -0400356 root->ref_cows = 1;
Chris Mason5eda7b52007-06-22 14:16:25 -0400357 ret = btrfs_find_highest_inode(root, &highest_inode);
358 if (ret == 0) {
359 root->highest_inode = highest_inode;
360 root->last_inode_alloc = highest_inode;
361 }
362 return root;
363}
364
365struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
Josef Bacik58176a92007-08-29 15:47:34 -0400366 struct btrfs_key *location,
367 const char *name, int namelen)
Chris Mason5eda7b52007-06-22 14:16:25 -0400368{
369 struct btrfs_root *root;
370 int ret;
371
372 root = radix_tree_lookup(&fs_info->fs_roots_radix,
373 (unsigned long)location->objectid);
374 if (root)
375 return root;
376
377 root = btrfs_read_fs_root_no_radix(fs_info, location);
378 if (IS_ERR(root))
379 return root;
Chris Mason2619ba12007-04-10 16:58:11 -0400380 ret = radix_tree_insert(&fs_info->fs_roots_radix,
381 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400382 root);
383 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400384 free_extent_buffer(root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400385 kfree(root);
386 return ERR_PTR(ret);
387 }
Josef Bacik58176a92007-08-29 15:47:34 -0400388
389 ret = btrfs_set_root_name(root, name, namelen);
390 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400391 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400392 kfree(root);
393 return ERR_PTR(ret);
394 }
395
396 ret = btrfs_sysfs_add_root(root);
397 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400398 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400399 kfree(root->name);
400 kfree(root);
401 return ERR_PTR(ret);
402 }
403
Chris Mason5ce14bb2007-09-11 11:15:39 -0400404 ret = btrfs_find_dead_roots(fs_info->tree_root,
405 root->root_key.objectid, root);
406 BUG_ON(ret);
407
Chris Mason0f7d52f2007-04-09 10:42:37 -0400408 return root;
409}
410
Chris Mason2c90e5d2007-04-02 10:50:19 -0400411struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500412{
Chris Masone20d96d2007-03-22 12:13:20 -0400413 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
414 GFP_NOFS);
415 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
416 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400417 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
418 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500419 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400420 int err = -EIO;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400421 struct btrfs_super_block *disk_super;
Chris Masoneb60cea2007-02-02 09:18:22 -0500422
Chris Mason39279cc2007-06-12 06:35:45 -0400423 if (!extent_root || !tree_root || !fs_info) {
424 err = -ENOMEM;
425 goto fail;
426 }
Chris Mason8ef97622007-03-26 10:15:30 -0400427 init_bit_radix(&fs_info->pinned_radix);
428 init_bit_radix(&fs_info->pending_del_radix);
Chris Masone37c9e62007-05-09 20:13:14 -0400429 init_bit_radix(&fs_info->extent_map_radix);
Chris Mason26b80032007-08-08 20:17:12 -0400430 init_bit_radix(&fs_info->extent_ins_radix);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400431 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400432 INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL);
Chris Masonbe744172007-05-06 10:15:01 -0400433 INIT_RADIX_TREE(&fs_info->block_group_data_radix, GFP_KERNEL);
Chris Mason8fd17792007-04-19 21:01:03 -0400434 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -0400435 INIT_LIST_HEAD(&fs_info->dead_roots);
Josef Bacik58176a92007-08-29 15:47:34 -0400436 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
437 init_completion(&fs_info->kobj_unregister);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400438 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400439 fs_info->running_transaction = NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400440 fs_info->last_trans_committed = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400441 fs_info->tree_root = tree_root;
442 fs_info->extent_root = extent_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400443 fs_info->sb = sb;
Chris Masond98237b2007-03-28 13:57:48 -0400444 fs_info->btree_inode = new_inode(sb);
445 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400446 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400447 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
448 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason5f39d392007-10-15 16:14:19 -0400449 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
450 fs_info->btree_inode->i_mapping,
451 GFP_NOFS);
Chris Masone66f7092007-04-20 13:16:02 -0400452 fs_info->do_barriers = 1;
Chris Masonfacda1e2007-06-08 18:11:48 -0400453 fs_info->closing = 0;
454
Chris Mason08607c12007-06-08 15:33:54 -0400455 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400456 BTRFS_I(fs_info->btree_inode)->root = tree_root;
457 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
458 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400459 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400460 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400461
Chris Mason79154b12007-03-22 15:59:16 -0400462 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400463 mutex_init(&fs_info->fs_mutex);
Chris Mason3768f362007-03-13 16:47:54 -0400464
Chris Mason2c90e5d2007-04-02 10:50:19 -0400465 __setup_root(sb->s_blocksize, tree_root,
466 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400467
Chris Mason2c90e5d2007-04-02 10:50:19 -0400468 fs_info->sb_buffer = read_tree_block(tree_root,
469 BTRFS_SUPER_INFO_OFFSET /
470 sb->s_blocksize);
Chris Masond98237b2007-03-28 13:57:48 -0400471
Chris Mason0f7d52f2007-04-09 10:42:37 -0400472 if (!fs_info->sb_buffer)
Chris Mason39279cc2007-06-12 06:35:45 -0400473 goto fail_iput;
Chris Mason39279cc2007-06-12 06:35:45 -0400474
Chris Mason5f39d392007-10-15 16:14:19 -0400475 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
476 sizeof(fs_info->super_copy));
477
478 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
479 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
480 BTRFS_FSID_SIZE);
481 disk_super = &fs_info->super_copy;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400482 if (!btrfs_super_root(disk_super))
Chris Mason39279cc2007-06-12 06:35:45 -0400483 goto fail_sb_buffer;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400484
Chris Mason8352d8a2007-04-12 10:43:05 -0400485 i_size_write(fs_info->btree_inode,
486 btrfs_super_total_blocks(disk_super) <<
487 fs_info->btree_inode->i_blkbits);
488
Chris Mason39279cc2007-06-12 06:35:45 -0400489
490 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
491 sizeof(disk_super->magic))) {
492 printk("btrfs: valid FS not found on %s\n", sb->s_id);
493 goto fail_sb_buffer;
494 }
Chris Masone20d96d2007-03-22 12:13:20 -0400495 tree_root->node = read_tree_block(tree_root,
496 btrfs_super_root(disk_super));
Chris Mason39279cc2007-06-12 06:35:45 -0400497 if (!tree_root->node)
498 goto fail_sb_buffer;
Chris Mason3768f362007-03-13 16:47:54 -0400499
Chris Mason2c90e5d2007-04-02 10:50:19 -0400500 mutex_lock(&fs_info->fs_mutex);
501 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400502 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason39279cc2007-06-12 06:35:45 -0400503 if (ret) {
504 mutex_unlock(&fs_info->fs_mutex);
505 goto fail_tree_root;
506 }
Chris Mason3768f362007-03-13 16:47:54 -0400507
Chris Mason9078a3e2007-04-26 16:46:15 -0400508 btrfs_read_block_groups(extent_root);
509
Chris Mason0f7d52f2007-04-09 10:42:37 -0400510 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400511 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400512 return tree_root;
Chris Mason39279cc2007-06-12 06:35:45 -0400513
514fail_tree_root:
Chris Mason5f39d392007-10-15 16:14:19 -0400515 free_extent_buffer(tree_root->node);
Chris Mason39279cc2007-06-12 06:35:45 -0400516fail_sb_buffer:
Chris Mason5f39d392007-10-15 16:14:19 -0400517 free_extent_buffer(fs_info->sb_buffer);
Chris Mason39279cc2007-06-12 06:35:45 -0400518fail_iput:
519 iput(fs_info->btree_inode);
520fail:
521 kfree(extent_root);
522 kfree(tree_root);
523 kfree(fs_info);
524 return ERR_PTR(err);
Chris Masoneb60cea2007-02-02 09:18:22 -0500525}
526
Chris Masone089f052007-03-16 16:20:31 -0400527int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400528 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500529{
Chris Masone66f7092007-04-20 13:16:02 -0400530 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -0400531 struct extent_buffer *super = root->fs_info->sb_buffer;
532 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400533
Chris Mason5f39d392007-10-15 16:14:19 -0400534 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super);
535 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
536 super->start, super->len);
537 return ret;
Chris Masoncfaa7292007-02-21 17:04:57 -0500538}
539
Chris Mason5eda7b52007-06-22 14:16:25 -0400540int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
Chris Mason2619ba12007-04-10 16:58:11 -0400541{
542 radix_tree_delete(&fs_info->fs_roots_radix,
543 (unsigned long)root->root_key.objectid);
Josef Bacik58176a92007-08-29 15:47:34 -0400544 btrfs_sysfs_del_root(root);
Chris Mason2619ba12007-04-10 16:58:11 -0400545 if (root->inode)
546 iput(root->inode);
547 if (root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400548 free_extent_buffer(root->node);
Chris Mason2619ba12007-04-10 16:58:11 -0400549 if (root->commit_root)
Chris Mason5f39d392007-10-15 16:14:19 -0400550 free_extent_buffer(root->commit_root);
Josef Bacik58176a92007-08-29 15:47:34 -0400551 if (root->name)
552 kfree(root->name);
Chris Mason2619ba12007-04-10 16:58:11 -0400553 kfree(root);
554 return 0;
555}
556
Chris Mason35b7e472007-05-02 15:53:43 -0400557static int del_fs_roots(struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400558{
559 int ret;
560 struct btrfs_root *gang[8];
561 int i;
562
563 while(1) {
564 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
565 (void **)gang, 0,
566 ARRAY_SIZE(gang));
567 if (!ret)
568 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400569 for (i = 0; i < ret; i++)
Chris Mason5eda7b52007-06-22 14:16:25 -0400570 btrfs_free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400571 }
572 return 0;
573}
Chris Masonb4100d62007-04-12 12:14:00 -0400574
Chris Masone20d96d2007-03-22 12:13:20 -0400575int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500576{
Chris Mason3768f362007-03-13 16:47:54 -0400577 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400578 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400579 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -0400580
Chris Masonfacda1e2007-06-08 18:11:48 -0400581 fs_info->closing = 1;
Chris Mason08607c12007-06-08 15:33:54 -0400582 btrfs_transaction_flush_work(root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400583 mutex_lock(&fs_info->fs_mutex);
Chris Mason6702ed42007-08-07 16:15:09 -0400584 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason79154b12007-03-22 15:59:16 -0400585 trans = btrfs_start_transaction(root, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400586 ret = btrfs_commit_transaction(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400587 /* run commit again to drop the original snapshot */
588 trans = btrfs_start_transaction(root, 1);
589 btrfs_commit_transaction(trans, root);
590 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400591 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400592 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400593 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500594
Chris Mason0f7d52f2007-04-09 10:42:37 -0400595 if (fs_info->extent_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400596 free_extent_buffer(fs_info->extent_root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400597 if (fs_info->tree_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400598 free_extent_buffer(fs_info->tree_root->node);
599 free_extent_buffer(fs_info->sb_buffer);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400600 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
601 iput(fs_info->btree_inode);
Chris Mason7eccb902007-04-11 15:53:25 -0400602
Chris Mason9078a3e2007-04-26 16:46:15 -0400603 btrfs_free_block_groups(root->fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400604 del_fs_roots(fs_info);
605 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400606 kfree(fs_info->tree_root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500607 return 0;
608}
609
Chris Mason5f39d392007-10-15 16:14:19 -0400610int btrfs_buffer_uptodate(struct extent_buffer *buf)
Chris Masonccd467d2007-06-28 15:57:36 -0400611{
Chris Mason6d36dcd2007-10-15 16:14:37 -0400612 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Mason5f39d392007-10-15 16:14:19 -0400613 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf);
614}
Chris Mason6702ed42007-08-07 16:15:09 -0400615
Chris Mason5f39d392007-10-15 16:14:19 -0400616int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
617{
Chris Mason6d36dcd2007-10-15 16:14:37 -0400618 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Mason5f39d392007-10-15 16:14:19 -0400619 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree,
620 buf);
621}
622
623void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
624{
Chris Mason6d36dcd2007-10-15 16:14:37 -0400625 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason5f39d392007-10-15 16:14:19 -0400626 u64 transid = btrfs_header_generation(buf);
627 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason6702ed42007-08-07 16:15:09 -0400628
Chris Masonccd467d2007-06-28 15:57:36 -0400629 if (transid != root->fs_info->generation) {
630 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
Chris Mason5f39d392007-10-15 16:14:19 -0400631 (unsigned long long)extent_buffer_blocknr(buf),
Chris Masonccd467d2007-06-28 15:57:36 -0400632 transid, root->fs_info->generation);
633 WARN_ON(1);
634 }
Chris Mason5f39d392007-10-15 16:14:19 -0400635 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500636}
637
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400638void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
Chris Mason35b7e472007-05-02 15:53:43 -0400639{
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400640 balance_dirty_pages_ratelimited_nr(
641 root->fs_info->btree_inode->i_mapping, nr);
Chris Mason35b7e472007-05-02 15:53:43 -0400642}