blob: d7615e1578ccc0af2a776996bb8ac8a97eaf9dab [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/module.h>
20#include <linux/fs.h>
Chris Masond98237b2007-03-28 13:57:48 -040021#include <linux/blkdev.h>
Chris Mason11bd1432007-06-22 14:16:24 -040022#include <linux/crc32c.h>
Chris Mason87cbda52007-03-28 19:44:27 -040023#include <linux/scatterlist.h>
Chris Mason22b0ebd2007-03-30 08:47:31 -040024#include <linux/swap.h>
Chris Mason0f7d52f2007-04-09 10:42:37 -040025#include <linux/radix-tree.h>
Chris Mason35b7e472007-05-02 15:53:43 -040026#include <linux/writeback.h>
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 Mason7eccb902007-04-11 15:53:25 -040032u64 bh_blocknr(struct buffer_head *bh)
33{
Chris Mason0cf6c622007-06-09 09:22:25 -040034 return bh->b_blocknr;
Chris Mason7eccb902007-04-11 15:53:25 -040035}
36
Chris Masone20d96d2007-03-22 12:13:20 -040037static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050038{
Chris Masone20d96d2007-03-22 12:13:20 -040039 struct btrfs_node *node = btrfs_buffer_node(buf);
Chris Mason7eccb902007-04-11 15:53:25 -040040 if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
Chris Mason5af39812007-06-12 07:50:13 -040041 printk(KERN_CRIT "bh_blocknr(buf) is %llu, header is %llu\n",
42 (unsigned long long)bh_blocknr(buf),
43 (unsigned long long)btrfs_header_blocknr(&node->header));
Chris Mason39279cc2007-06-12 06:35:45 -040044 return 1;
Chris Masond98237b2007-03-28 13:57:48 -040045 }
Chris Mason9a8dd152007-02-23 08:38:36 -050046 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050047}
48
Chris Masond98237b2007-03-28 13:57:48 -040049struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050050{
Chris Masond98237b2007-03-28 13:57:48 -040051 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
52 int blockbits = root->fs_info->sb->s_blocksize_bits;
53 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
54 struct page *page;
55 struct buffer_head *bh;
56 struct buffer_head *head;
57 struct buffer_head *ret = NULL;
58
Chris Mason2c90e5d2007-04-02 10:50:19 -040059
Chris Masond98237b2007-03-28 13:57:48 -040060 page = find_lock_page(mapping, index);
61 if (!page)
62 return NULL;
63
64 if (!page_has_buffers(page))
65 goto out_unlock;
66
67 head = page_buffers(page);
68 bh = head;
69 do {
Chris Mason7eccb902007-04-11 15:53:25 -040070 if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -040071 ret = bh;
72 get_bh(bh);
73 goto out_unlock;
74 }
75 bh = bh->b_this_page;
76 } while (bh != head);
77out_unlock:
78 unlock_page(page);
79 page_cache_release(page);
80 return ret;
Chris Masoned2ff2c2007-03-01 18:59:40 -050081}
82
Chris Mason8352d8a2007-04-12 10:43:05 -040083int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
Chris Mason7eccb902007-04-11 15:53:25 -040084 u64 logical)
85{
Chris Mason236454df2007-04-19 13:37:44 -040086 if (logical == 0) {
87 bh->b_bdev = NULL;
88 bh->b_blocknr = 0;
89 set_buffer_mapped(bh);
Chris Mason0cf6c622007-06-09 09:22:25 -040090 } else {
91 map_bh(bh, root->fs_info->sb, logical);
Chris Mason236454df2007-04-19 13:37:44 -040092 }
Chris Mason0cf6c622007-06-09 09:22:25 -040093 return 0;
Chris Mason7eccb902007-04-11 15:53:25 -040094}
95
Chris Masond98237b2007-03-28 13:57:48 -040096struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
97 u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050098{
Chris Masond98237b2007-03-28 13:57:48 -040099 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
100 int blockbits = root->fs_info->sb->s_blocksize_bits;
101 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
102 struct page *page;
103 struct buffer_head *bh;
104 struct buffer_head *head;
105 struct buffer_head *ret = NULL;
Chris Mason7eccb902007-04-11 15:53:25 -0400106 int err;
Chris Masond98237b2007-03-28 13:57:48 -0400107 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400108
Chris Mason34088782007-06-12 11:36:58 -0400109 page = find_or_create_page(mapping, index, GFP_NOFS);
Chris Masond98237b2007-03-28 13:57:48 -0400110 if (!page)
111 return NULL;
112
Chris Masond98237b2007-03-28 13:57:48 -0400113 if (!page_has_buffers(page))
114 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
115 head = page_buffers(page);
116 bh = head;
117 do {
118 if (!buffer_mapped(bh)) {
Chris Mason8352d8a2007-04-12 10:43:05 -0400119 err = btrfs_map_bh_to_logical(root, bh, first_block);
Chris Mason7eccb902007-04-11 15:53:25 -0400120 BUG_ON(err);
Chris Masond98237b2007-03-28 13:57:48 -0400121 }
Chris Mason7eccb902007-04-11 15:53:25 -0400122 if (bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -0400123 ret = bh;
124 get_bh(bh);
125 goto out_unlock;
126 }
127 bh = bh->b_this_page;
128 first_block++;
129 } while (bh != head);
130out_unlock:
131 unlock_page(page);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400132 if (ret)
133 touch_buffer(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400134 page_cache_release(page);
135 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400136}
Chris Mason123abc82007-03-14 14:14:43 -0400137
Chris Masond98237b2007-03-28 13:57:48 -0400138static int btree_get_block(struct inode *inode, sector_t iblock,
139 struct buffer_head *bh, int create)
140{
Chris Mason7eccb902007-04-11 15:53:25 -0400141 int err;
142 struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
Chris Mason8352d8a2007-04-12 10:43:05 -0400143 err = btrfs_map_bh_to_logical(root, bh, iblock);
Chris Mason7eccb902007-04-11 15:53:25 -0400144 return err;
Chris Masond98237b2007-03-28 13:57:48 -0400145}
146
Chris Masonf254e522007-03-29 15:15:27 -0400147int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
148 char *result)
Chris Mason87cbda52007-03-28 19:44:27 -0400149{
Chris Mason11bd1432007-06-22 14:16:24 -0400150 u32 crc;
151 crc = crc32c(0, data, len);
152 memcpy(result, &crc, BTRFS_CRC32_SIZE);
153 return 0;
Chris Masonf254e522007-03-29 15:15:27 -0400154}
Chris Mason11bd1432007-06-22 14:16:24 -0400155
Chris Masonf254e522007-03-29 15:15:27 -0400156static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
157 int verify)
158{
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}
180
Chris Masond98237b2007-03-28 13:57:48 -0400181static int btree_writepage(struct page *page, struct writeback_control *wbc)
182{
Chris Mason87cbda52007-03-28 19:44:27 -0400183 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400184 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400185 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400186 if (!page_has_buffers(page)) {
187 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
188 (1 << BH_Dirty)|(1 << BH_Uptodate));
189 }
190 head = page_buffers(page);
191 bh = head;
192 do {
193 if (buffer_dirty(bh))
194 csum_tree_block(root, bh, 0);
195 bh = bh->b_this_page;
196 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400197 return block_write_full_page(page, btree_get_block, wbc);
198}
199
200static int btree_readpage(struct file * file, struct page * page)
201{
202 return block_read_full_page(page, btree_get_block);
203}
204
205static struct address_space_operations btree_aops = {
206 .readpage = btree_readpage,
207 .writepage = btree_writepage,
208 .sync_page = block_sync_page,
209};
210
Chris Mason090d1872007-05-01 08:53:32 -0400211int readahead_tree_block(struct btrfs_root *root, u64 blocknr)
212{
213 struct buffer_head *bh = NULL;
Chris Masonde428b62007-05-18 13:28:27 -0400214 int ret = 0;
Chris Mason090d1872007-05-01 08:53:32 -0400215
216 bh = btrfs_find_create_tree_block(root, blocknr);
217 if (!bh)
218 return 0;
Chris Masonde428b62007-05-18 13:28:27 -0400219 if (buffer_uptodate(bh)) {
220 ret = 1;
Chris Mason090d1872007-05-01 08:53:32 -0400221 goto done;
Chris Masonde428b62007-05-18 13:28:27 -0400222 }
223 if (test_set_buffer_locked(bh)) {
224 ret = 1;
Chris Mason090d1872007-05-01 08:53:32 -0400225 goto done;
Chris Masonde428b62007-05-18 13:28:27 -0400226 }
Chris Mason090d1872007-05-01 08:53:32 -0400227 if (!buffer_uptodate(bh)) {
228 get_bh(bh);
229 bh->b_end_io = end_buffer_read_sync;
230 submit_bh(READ, bh);
231 } else {
232 unlock_buffer(bh);
Chris Masonde428b62007-05-18 13:28:27 -0400233 ret = 1;
Chris Mason090d1872007-05-01 08:53:32 -0400234 }
235done:
236 brelse(bh);
Chris Masonde428b62007-05-18 13:28:27 -0400237 return ret;
Chris Mason090d1872007-05-01 08:53:32 -0400238}
239
Chris Masone20d96d2007-03-22 12:13:20 -0400240struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
241{
Chris Masond98237b2007-03-28 13:57:48 -0400242 struct buffer_head *bh = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400243
Chris Masond98237b2007-03-28 13:57:48 -0400244 bh = btrfs_find_create_tree_block(root, blocknr);
245 if (!bh)
246 return bh;
Chris Mason9d642722007-04-03 11:43:19 -0400247 if (buffer_uptodate(bh))
248 goto uptodate;
Chris Masond98237b2007-03-28 13:57:48 -0400249 lock_buffer(bh);
250 if (!buffer_uptodate(bh)) {
251 get_bh(bh);
252 bh->b_end_io = end_buffer_read_sync;
253 submit_bh(READ, bh);
254 wait_on_buffer(bh);
255 if (!buffer_uptodate(bh))
256 goto fail;
257 } else {
258 unlock_buffer(bh);
259 }
Chris Mason9d642722007-04-03 11:43:19 -0400260uptodate:
Chris Mason090d1872007-05-01 08:53:32 -0400261 if (!buffer_checked(bh)) {
262 csum_tree_block(root, bh, 1);
263 set_buffer_checked(bh);
264 }
Chris Masond98237b2007-03-28 13:57:48 -0400265 if (check_tree_block(root, bh))
Chris Mason39279cc2007-06-12 06:35:45 -0400266 goto fail;
Chris Masond98237b2007-03-28 13:57:48 -0400267 return bh;
268fail:
269 brelse(bh);
270 return NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500271}
272
Chris Masone089f052007-03-16 16:20:31 -0400273int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400274 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500275{
Chris Masond6025572007-03-30 14:27:56 -0400276 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400277 mark_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500278 return 0;
279}
280
Chris Masone089f052007-03-16 16:20:31 -0400281int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400282 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500283{
Chris Masond6025572007-03-30 14:27:56 -0400284 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400285 clear_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500286 return 0;
287}
288
Chris Mason2c90e5d2007-04-02 10:50:19 -0400289static int __setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400290 struct btrfs_root *root,
291 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400292 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500293{
Chris Masoncfaa7292007-02-21 17:04:57 -0500294 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400295 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500296 root->commit_root = NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400297 root->blocksize = blocksize;
Chris Mason123abc82007-03-14 14:14:43 -0400298 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400299 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400300 root->objectid = objectid;
301 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400302 root->highest_inode = 0;
303 root->last_inode_alloc = 0;
Chris Mason3768f362007-03-13 16:47:54 -0400304 memset(&root->root_key, 0, sizeof(root->root_key));
305 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason4d775672007-04-20 20:23:12 -0400306 root->root_key.objectid = objectid;
Chris Mason3768f362007-03-13 16:47:54 -0400307 return 0;
308}
309
Chris Mason2c90e5d2007-04-02 10:50:19 -0400310static int find_and_setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400311 struct btrfs_root *tree_root,
312 struct btrfs_fs_info *fs_info,
313 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400314 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400315{
316 int ret;
317
Chris Mason2c90e5d2007-04-02 10:50:19 -0400318 __setup_root(blocksize, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400319 ret = btrfs_find_last_root(tree_root, objectid,
320 &root->root_item, &root->root_key);
321 BUG_ON(ret);
322
323 root->node = read_tree_block(root,
324 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400325 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500326 return 0;
327}
328
Chris Mason5eda7b52007-06-22 14:16:25 -0400329struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
330 struct btrfs_key *location)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400331{
332 struct btrfs_root *root;
333 struct btrfs_root *tree_root = fs_info->tree_root;
334 struct btrfs_path *path;
335 struct btrfs_leaf *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400336 u64 highest_inode;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400337 int ret = 0;
338
Chris Mason5eda7b52007-06-22 14:16:25 -0400339 root = kzalloc(sizeof(*root), GFP_NOFS);
Chris Mason0cf6c622007-06-09 09:22:25 -0400340 if (!root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400341 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400342 if (location->offset == (u64)-1) {
343 ret = find_and_setup_root(fs_info->sb->s_blocksize,
344 fs_info->tree_root, fs_info,
345 location->objectid, root);
346 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400347 kfree(root);
348 return ERR_PTR(ret);
349 }
350 goto insert;
351 }
352
353 __setup_root(fs_info->sb->s_blocksize, root, fs_info,
354 location->objectid);
355
356 path = btrfs_alloc_path();
357 BUG_ON(!path);
358 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
359 if (ret != 0) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400360 if (ret > 0)
361 ret = -ENOENT;
362 goto out;
363 }
364 l = btrfs_buffer_leaf(path->nodes[0]);
365 memcpy(&root->root_item,
366 btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
367 sizeof(root->root_item));
368 memcpy(&root->root_key, location, sizeof(*location));
369 ret = 0;
370out:
371 btrfs_release_path(root, path);
372 btrfs_free_path(path);
373 if (ret) {
374 kfree(root);
375 return ERR_PTR(ret);
376 }
377 root->node = read_tree_block(root,
378 btrfs_root_blocknr(&root->root_item));
379 BUG_ON(!root->node);
380insert:
Chris Mason0f7d52f2007-04-09 10:42:37 -0400381 root->ref_cows = 1;
Chris Mason5eda7b52007-06-22 14:16:25 -0400382 ret = btrfs_find_highest_inode(root, &highest_inode);
383 if (ret == 0) {
384 root->highest_inode = highest_inode;
385 root->last_inode_alloc = highest_inode;
386 }
387 return root;
388}
389
390struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
391 struct btrfs_key *location)
392{
393 struct btrfs_root *root;
394 int ret;
395
396 root = radix_tree_lookup(&fs_info->fs_roots_radix,
397 (unsigned long)location->objectid);
398 if (root)
399 return root;
400
401 root = btrfs_read_fs_root_no_radix(fs_info, location);
402 if (IS_ERR(root))
403 return root;
Chris Mason2619ba12007-04-10 16:58:11 -0400404 ret = radix_tree_insert(&fs_info->fs_roots_radix,
405 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400406 root);
407 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400408 brelse(root->node);
409 kfree(root);
410 return ERR_PTR(ret);
411 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400412 return root;
413}
414
Chris Mason2c90e5d2007-04-02 10:50:19 -0400415struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500416{
Chris Masone20d96d2007-03-22 12:13:20 -0400417 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
418 GFP_NOFS);
419 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
420 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400421 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
422 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500423 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400424 int err = -EIO;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400425 struct btrfs_super_block *disk_super;
Chris Masoneb60cea2007-02-02 09:18:22 -0500426
Chris Mason39279cc2007-06-12 06:35:45 -0400427 if (!extent_root || !tree_root || !fs_info) {
428 err = -ENOMEM;
429 goto fail;
430 }
Chris Mason8ef97622007-03-26 10:15:30 -0400431 init_bit_radix(&fs_info->pinned_radix);
432 init_bit_radix(&fs_info->pending_del_radix);
Chris Masone37c9e62007-05-09 20:13:14 -0400433 init_bit_radix(&fs_info->extent_map_radix);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400434 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400435 INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL);
Chris Masonbe744172007-05-06 10:15:01 -0400436 INIT_RADIX_TREE(&fs_info->block_group_data_radix, GFP_KERNEL);
Chris Mason8fd17792007-04-19 21:01:03 -0400437 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -0400438 INIT_LIST_HEAD(&fs_info->dead_roots);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400439 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400440 fs_info->running_transaction = NULL;
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 Masone66f7092007-04-20 13:16:02 -0400449 fs_info->do_barriers = 1;
Chris Masonf2458e12007-04-25 15:52:25 -0400450 fs_info->extent_tree_insert_nr = 0;
451 fs_info->extent_tree_prealloc_nr = 0;
Chris Masonfacda1e2007-06-08 18:11:48 -0400452 fs_info->closing = 0;
453
Chris Mason08607c12007-06-08 15:33:54 -0400454 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400455 BTRFS_I(fs_info->btree_inode)->root = tree_root;
456 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
457 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400458 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400459 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400460
Chris Mason79154b12007-03-22 15:59:16 -0400461 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400462 mutex_init(&fs_info->fs_mutex);
Chris Mason3768f362007-03-13 16:47:54 -0400463
Chris Mason2c90e5d2007-04-02 10:50:19 -0400464 __setup_root(sb->s_blocksize, tree_root,
465 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400466
Chris Mason2c90e5d2007-04-02 10:50:19 -0400467 fs_info->sb_buffer = read_tree_block(tree_root,
468 BTRFS_SUPER_INFO_OFFSET /
469 sb->s_blocksize);
Chris Masond98237b2007-03-28 13:57:48 -0400470
Chris Mason0f7d52f2007-04-09 10:42:37 -0400471 if (!fs_info->sb_buffer)
Chris Mason39279cc2007-06-12 06:35:45 -0400472 goto fail_iput;
Chris Masond98237b2007-03-28 13:57:48 -0400473 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
Chris Mason39279cc2007-06-12 06:35:45 -0400474
Chris Mason0f7d52f2007-04-09 10:42:37 -0400475 if (!btrfs_super_root(disk_super))
Chris Mason39279cc2007-06-12 06:35:45 -0400476 goto fail_sb_buffer;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400477
Chris Mason8352d8a2007-04-12 10:43:05 -0400478 i_size_write(fs_info->btree_inode,
479 btrfs_super_total_blocks(disk_super) <<
480 fs_info->btree_inode->i_blkbits);
481
Chris Masond98237b2007-03-28 13:57:48 -0400482 fs_info->disk_super = disk_super;
Chris Mason39279cc2007-06-12 06:35:45 -0400483
484 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
485 sizeof(disk_super->magic))) {
486 printk("btrfs: valid FS not found on %s\n", sb->s_id);
487 goto fail_sb_buffer;
488 }
Chris Masone20d96d2007-03-22 12:13:20 -0400489 tree_root->node = read_tree_block(tree_root,
490 btrfs_super_root(disk_super));
Chris Mason39279cc2007-06-12 06:35:45 -0400491 if (!tree_root->node)
492 goto fail_sb_buffer;
Chris Mason3768f362007-03-13 16:47:54 -0400493
Chris Mason2c90e5d2007-04-02 10:50:19 -0400494 mutex_lock(&fs_info->fs_mutex);
495 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400496 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason39279cc2007-06-12 06:35:45 -0400497 if (ret) {
498 mutex_unlock(&fs_info->fs_mutex);
499 goto fail_tree_root;
500 }
Chris Mason3768f362007-03-13 16:47:54 -0400501
Chris Mason9078a3e2007-04-26 16:46:15 -0400502 btrfs_read_block_groups(extent_root);
503
Chris Mason0f7d52f2007-04-09 10:42:37 -0400504 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Mason5eda7b52007-06-22 14:16:25 -0400505 ret = btrfs_find_dead_roots(tree_root);
506 if (ret)
507 goto fail_tree_root;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400508 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400509 return tree_root;
Chris Mason39279cc2007-06-12 06:35:45 -0400510
511fail_tree_root:
512 btrfs_block_release(tree_root, tree_root->node);
513fail_sb_buffer:
514 btrfs_block_release(tree_root, fs_info->sb_buffer);
515fail_iput:
516 iput(fs_info->btree_inode);
517fail:
518 kfree(extent_root);
519 kfree(tree_root);
520 kfree(fs_info);
521 return ERR_PTR(err);
Chris Masoneb60cea2007-02-02 09:18:22 -0500522}
523
Chris Masone089f052007-03-16 16:20:31 -0400524int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400525 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500526{
Chris Masone66f7092007-04-20 13:16:02 -0400527 int ret;
Chris Masond5719762007-03-23 10:01:08 -0400528 struct buffer_head *bh = root->fs_info->sb_buffer;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400529
Chris Masond5719762007-03-23 10:01:08 -0400530 btrfs_set_super_root(root->fs_info->disk_super,
Chris Mason7eccb902007-04-11 15:53:25 -0400531 bh_blocknr(root->fs_info->tree_root->node));
Chris Masond5719762007-03-23 10:01:08 -0400532 lock_buffer(bh);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400533 WARN_ON(atomic_read(&bh->b_count) < 1);
Chris Masond5719762007-03-23 10:01:08 -0400534 clear_buffer_dirty(bh);
Chris Mason87cbda52007-03-28 19:44:27 -0400535 csum_tree_block(root, bh, 0);
Chris Masond5719762007-03-23 10:01:08 -0400536 bh->b_end_io = end_buffer_write_sync;
537 get_bh(bh);
Chris Masone66f7092007-04-20 13:16:02 -0400538 if (root->fs_info->do_barriers)
539 ret = submit_bh(WRITE_BARRIER, bh);
540 else
541 ret = submit_bh(WRITE, bh);
542 if (ret == -EOPNOTSUPP) {
Chris Mason8c2383c2007-06-18 09:57:58 -0400543 get_bh(bh);
544 lock_buffer(bh);
Chris Masone66f7092007-04-20 13:16:02 -0400545 set_buffer_uptodate(bh);
546 root->fs_info->do_barriers = 0;
547 ret = submit_bh(WRITE, bh);
548 }
Chris Masond5719762007-03-23 10:01:08 -0400549 wait_on_buffer(bh);
550 if (!buffer_uptodate(bh)) {
551 WARN_ON(1);
552 return -EIO;
Chris Masoncfaa7292007-02-21 17:04:57 -0500553 }
554 return 0;
555}
556
Chris Mason5eda7b52007-06-22 14:16:25 -0400557int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
Chris Mason2619ba12007-04-10 16:58:11 -0400558{
559 radix_tree_delete(&fs_info->fs_roots_radix,
560 (unsigned long)root->root_key.objectid);
561 if (root->inode)
562 iput(root->inode);
563 if (root->node)
564 brelse(root->node);
565 if (root->commit_root)
566 brelse(root->commit_root);
567 kfree(root);
568 return 0;
569}
570
Chris Mason35b7e472007-05-02 15:53:43 -0400571static int del_fs_roots(struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400572{
573 int ret;
574 struct btrfs_root *gang[8];
575 int i;
576
577 while(1) {
578 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
579 (void **)gang, 0,
580 ARRAY_SIZE(gang));
581 if (!ret)
582 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400583 for (i = 0; i < ret; i++)
Chris Mason5eda7b52007-06-22 14:16:25 -0400584 btrfs_free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400585 }
586 return 0;
587}
Chris Masonb4100d62007-04-12 12:14:00 -0400588
Chris Masone20d96d2007-03-22 12:13:20 -0400589int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500590{
Chris Mason3768f362007-03-13 16:47:54 -0400591 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400592 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400593 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -0400594
Chris Masonfacda1e2007-06-08 18:11:48 -0400595 fs_info->closing = 1;
Chris Mason08607c12007-06-08 15:33:54 -0400596 btrfs_transaction_flush_work(root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400597 mutex_lock(&fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400598 trans = btrfs_start_transaction(root, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400599 ret = btrfs_commit_transaction(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400600 /* run commit again to drop the original snapshot */
601 trans = btrfs_start_transaction(root, 1);
602 btrfs_commit_transaction(trans, root);
603 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400604 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400605 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400606 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500607
Chris Mason0f7d52f2007-04-09 10:42:37 -0400608 if (fs_info->extent_root->node)
609 btrfs_block_release(fs_info->extent_root,
610 fs_info->extent_root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400611 if (fs_info->tree_root->node)
612 btrfs_block_release(fs_info->tree_root,
613 fs_info->tree_root->node);
614 btrfs_block_release(root, fs_info->sb_buffer);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400615 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
616 iput(fs_info->btree_inode);
Chris Mason7eccb902007-04-11 15:53:25 -0400617
Chris Mason9078a3e2007-04-26 16:46:15 -0400618 btrfs_free_block_groups(root->fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400619 del_fs_roots(fs_info);
620 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400621 kfree(fs_info->tree_root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500622 return 0;
623}
624
Chris Masone20d96d2007-03-22 12:13:20 -0400625void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500626{
Chris Mason7cfcc172007-04-02 14:53:59 -0400627 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500628}
629
Chris Mason35b7e472007-05-02 15:53:43 -0400630void btrfs_btree_balance_dirty(struct btrfs_root *root)
631{
632 balance_dirty_pages_ratelimited(root->fs_info->btree_inode->i_mapping);
633}