blob: c25ef0a68f18dce2c2cb18218348db76aeba6ad5 [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 Masoneb60cea2007-02-02 09:18:22 -050026#include "ctree.h"
27#include "disk-io.h"
Chris Masone089f052007-03-16 16:20:31 -040028#include "transaction.h"
Chris Mason0f7d52f2007-04-09 10:42:37 -040029#include "btrfs_inode.h"
Chris Masoneb60cea2007-02-02 09:18:22 -050030
Chris Mason7eccb902007-04-11 15:53:25 -040031u64 bh_blocknr(struct buffer_head *bh)
32{
Chris Mason0cf6c622007-06-09 09:22:25 -040033 return bh->b_blocknr;
Chris Mason7eccb902007-04-11 15:53:25 -040034}
35
Chris Masone20d96d2007-03-22 12:13:20 -040036static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050037{
Chris Masone20d96d2007-03-22 12:13:20 -040038 struct btrfs_node *node = btrfs_buffer_node(buf);
Chris Mason7eccb902007-04-11 15:53:25 -040039 if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
Chris Mason5af39812007-06-12 07:50:13 -040040 printk(KERN_CRIT "bh_blocknr(buf) is %llu, header is %llu\n",
41 (unsigned long long)bh_blocknr(buf),
42 (unsigned long long)btrfs_header_blocknr(&node->header));
Chris Mason39279cc2007-06-12 06:35:45 -040043 return 1;
Chris Masond98237b2007-03-28 13:57:48 -040044 }
Chris Mason9a8dd152007-02-23 08:38:36 -050045 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050046}
47
Chris Masond98237b2007-03-28 13:57:48 -040048struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050049{
Chris Masond98237b2007-03-28 13:57:48 -040050 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
51 int blockbits = root->fs_info->sb->s_blocksize_bits;
52 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
53 struct page *page;
54 struct buffer_head *bh;
55 struct buffer_head *head;
56 struct buffer_head *ret = NULL;
57
Chris Mason2c90e5d2007-04-02 10:50:19 -040058
Chris Masond98237b2007-03-28 13:57:48 -040059 page = find_lock_page(mapping, index);
60 if (!page)
61 return NULL;
62
63 if (!page_has_buffers(page))
64 goto out_unlock;
65
66 head = page_buffers(page);
67 bh = head;
68 do {
Chris Mason7eccb902007-04-11 15:53:25 -040069 if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -040070 ret = bh;
71 get_bh(bh);
72 goto out_unlock;
73 }
74 bh = bh->b_this_page;
75 } while (bh != head);
76out_unlock:
77 unlock_page(page);
78 page_cache_release(page);
79 return ret;
Chris Masoned2ff2c2007-03-01 18:59:40 -050080}
81
Chris Mason8352d8a2007-04-12 10:43:05 -040082int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
Chris Mason7eccb902007-04-11 15:53:25 -040083 u64 logical)
84{
Chris Mason236454d2007-04-19 13:37:44 -040085 if (logical == 0) {
86 bh->b_bdev = NULL;
87 bh->b_blocknr = 0;
88 set_buffer_mapped(bh);
Chris Mason0cf6c622007-06-09 09:22:25 -040089 } else {
90 map_bh(bh, root->fs_info->sb, logical);
Chris Mason236454d2007-04-19 13:37:44 -040091 }
Chris Mason0cf6c622007-06-09 09:22:25 -040092 return 0;
Chris Mason7eccb902007-04-11 15:53:25 -040093}
94
Chris Masond98237b2007-03-28 13:57:48 -040095struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
96 u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050097{
Chris Masond98237b2007-03-28 13:57:48 -040098 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
99 int blockbits = root->fs_info->sb->s_blocksize_bits;
100 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
101 struct page *page;
102 struct buffer_head *bh;
103 struct buffer_head *head;
104 struct buffer_head *ret = NULL;
Chris Mason7eccb902007-04-11 15:53:25 -0400105 int err;
Chris Masond98237b2007-03-28 13:57:48 -0400106 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400107
Chris Mason34088782007-06-12 11:36:58 -0400108 page = find_or_create_page(mapping, index, GFP_NOFS);
Chris Masond98237b2007-03-28 13:57:48 -0400109 if (!page)
110 return NULL;
111
Chris Masond98237b2007-03-28 13:57:48 -0400112 if (!page_has_buffers(page))
113 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
114 head = page_buffers(page);
115 bh = head;
116 do {
117 if (!buffer_mapped(bh)) {
Chris Mason8352d8a2007-04-12 10:43:05 -0400118 err = btrfs_map_bh_to_logical(root, bh, first_block);
Chris Mason7eccb902007-04-11 15:53:25 -0400119 BUG_ON(err);
Chris Masond98237b2007-03-28 13:57:48 -0400120 }
Chris Mason7eccb902007-04-11 15:53:25 -0400121 if (bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -0400122 ret = bh;
123 get_bh(bh);
124 goto out_unlock;
125 }
126 bh = bh->b_this_page;
127 first_block++;
128 } while (bh != head);
129out_unlock:
130 unlock_page(page);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400131 if (ret)
132 touch_buffer(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400133 page_cache_release(page);
134 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400135}
Chris Mason123abc82007-03-14 14:14:43 -0400136
Chris Masond98237b2007-03-28 13:57:48 -0400137static int btree_get_block(struct inode *inode, sector_t iblock,
138 struct buffer_head *bh, int create)
139{
Chris Mason7eccb902007-04-11 15:53:25 -0400140 int err;
141 struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
Chris Mason8352d8a2007-04-12 10:43:05 -0400142 err = btrfs_map_bh_to_logical(root, bh, iblock);
Chris Mason7eccb902007-04-11 15:53:25 -0400143 return err;
Chris Masond98237b2007-03-28 13:57:48 -0400144}
145
Chris Masonf254e522007-03-29 15:15:27 -0400146int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
147 char *result)
Chris Mason87cbda52007-03-28 19:44:27 -0400148{
Chris Mason11bd1432007-06-22 14:16:24 -0400149 u32 crc;
150 crc = crc32c(0, data, len);
151 memcpy(result, &crc, BTRFS_CRC32_SIZE);
152 return 0;
Chris Masonf254e522007-03-29 15:15:27 -0400153}
Chris Mason11bd1432007-06-22 14:16:24 -0400154
Chris Masonf254e522007-03-29 15:15:27 -0400155static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
156 int verify)
157{
Chris Mason509659c2007-05-10 12:36:17 -0400158 char result[BTRFS_CRC32_SIZE];
Chris Masonf254e522007-03-29 15:15:27 -0400159 int ret;
160 struct btrfs_node *node;
161
162 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
163 bh->b_size - BTRFS_CSUM_SIZE, result);
164 if (ret)
165 return ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400166 if (verify) {
Chris Mason509659c2007-05-10 12:36:17 -0400167 if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
Chris Mason5af39812007-06-12 07:50:13 -0400168 printk("btrfs: %s checksum verify failed on %llu\n",
169 root->fs_info->sb->s_id,
170 (unsigned long long)bh_blocknr(bh));
Chris Masonf254e522007-03-29 15:15:27 -0400171 return 1;
172 }
173 } else {
174 node = btrfs_buffer_node(bh);
Chris Mason509659c2007-05-10 12:36:17 -0400175 memcpy(node->header.csum, result, BTRFS_CRC32_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -0400176 }
Chris Mason87cbda52007-03-28 19:44:27 -0400177 return 0;
178}
179
Chris Masond98237b2007-03-28 13:57:48 -0400180static int btree_writepage(struct page *page, struct writeback_control *wbc)
181{
Chris Mason87cbda52007-03-28 19:44:27 -0400182 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400183 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400184 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400185 if (!page_has_buffers(page)) {
186 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
187 (1 << BH_Dirty)|(1 << BH_Uptodate));
188 }
189 head = page_buffers(page);
190 bh = head;
191 do {
192 if (buffer_dirty(bh))
193 csum_tree_block(root, bh, 0);
194 bh = bh->b_this_page;
195 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400196 return block_write_full_page(page, btree_get_block, wbc);
197}
198
199static int btree_readpage(struct file * file, struct page * page)
200{
201 return block_read_full_page(page, btree_get_block);
202}
203
204static struct address_space_operations btree_aops = {
205 .readpage = btree_readpage,
206 .writepage = btree_writepage,
207 .sync_page = block_sync_page,
208};
209
Chris Mason090d1872007-05-01 08:53:32 -0400210int readahead_tree_block(struct btrfs_root *root, u64 blocknr)
211{
212 struct buffer_head *bh = NULL;
Chris Masonde428b62007-05-18 13:28:27 -0400213 int ret = 0;
Chris Mason090d1872007-05-01 08:53:32 -0400214
215 bh = btrfs_find_create_tree_block(root, blocknr);
216 if (!bh)
217 return 0;
Chris Masonde428b62007-05-18 13:28:27 -0400218 if (buffer_uptodate(bh)) {
219 ret = 1;
Chris Mason090d1872007-05-01 08:53:32 -0400220 goto done;
Chris Masonde428b62007-05-18 13:28:27 -0400221 }
222 if (test_set_buffer_locked(bh)) {
223 ret = 1;
Chris Mason090d1872007-05-01 08:53:32 -0400224 goto done;
Chris Masonde428b62007-05-18 13:28:27 -0400225 }
Chris Mason090d1872007-05-01 08:53:32 -0400226 if (!buffer_uptodate(bh)) {
227 get_bh(bh);
228 bh->b_end_io = end_buffer_read_sync;
229 submit_bh(READ, bh);
230 } else {
231 unlock_buffer(bh);
Chris Masonde428b62007-05-18 13:28:27 -0400232 ret = 1;
Chris Mason090d1872007-05-01 08:53:32 -0400233 }
234done:
235 brelse(bh);
Chris Masonde428b62007-05-18 13:28:27 -0400236 return ret;
Chris Mason090d1872007-05-01 08:53:32 -0400237}
238
Chris Masone20d96d2007-03-22 12:13:20 -0400239struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
240{
Chris Masond98237b2007-03-28 13:57:48 -0400241 struct buffer_head *bh = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400242
Chris Masond98237b2007-03-28 13:57:48 -0400243 bh = btrfs_find_create_tree_block(root, blocknr);
244 if (!bh)
245 return bh;
Chris Mason9d642722007-04-03 11:43:19 -0400246 if (buffer_uptodate(bh))
247 goto uptodate;
Chris Masond98237b2007-03-28 13:57:48 -0400248 lock_buffer(bh);
249 if (!buffer_uptodate(bh)) {
250 get_bh(bh);
251 bh->b_end_io = end_buffer_read_sync;
252 submit_bh(READ, bh);
253 wait_on_buffer(bh);
254 if (!buffer_uptodate(bh))
255 goto fail;
256 } else {
257 unlock_buffer(bh);
258 }
Chris Mason9d642722007-04-03 11:43:19 -0400259uptodate:
Chris Mason090d1872007-05-01 08:53:32 -0400260 if (!buffer_checked(bh)) {
261 csum_tree_block(root, bh, 1);
262 set_buffer_checked(bh);
263 }
Chris Masond98237b2007-03-28 13:57:48 -0400264 if (check_tree_block(root, bh))
Chris Mason39279cc2007-06-12 06:35:45 -0400265 goto fail;
Chris Masond98237b2007-03-28 13:57:48 -0400266 return bh;
267fail:
268 brelse(bh);
269 return NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500270}
271
Chris Masone089f052007-03-16 16:20:31 -0400272int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400273 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500274{
Chris Masond6025572007-03-30 14:27:56 -0400275 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400276 lock_buffer(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400277 clear_buffer_dirty(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400278 unlock_buffer(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500279 return 0;
280}
281
Chris Mason2c90e5d2007-04-02 10:50:19 -0400282static int __setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400283 struct btrfs_root *root,
284 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400285 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500286{
Chris Masoncfaa7292007-02-21 17:04:57 -0500287 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400288 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500289 root->commit_root = NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400290 root->blocksize = blocksize;
Chris Mason123abc82007-03-14 14:14:43 -0400291 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400292 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400293 root->objectid = objectid;
294 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400295 root->highest_inode = 0;
296 root->last_inode_alloc = 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400297 root->name = NULL;
Chris Mason3768f362007-03-13 16:47:54 -0400298 memset(&root->root_key, 0, sizeof(root->root_key));
299 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason6702ed42007-08-07 16:15:09 -0400300 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
Josef Bacik58176a92007-08-29 15:47:34 -0400301 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
302 init_completion(&root->kobj_unregister);
Chris Mason6702ed42007-08-07 16:15:09 -0400303 root->defrag_running = 0;
304 root->defrag_level = 0;
Chris Mason4d775672007-04-20 20:23:12 -0400305 root->root_key.objectid = objectid;
Chris Mason3768f362007-03-13 16:47:54 -0400306 return 0;
307}
308
Chris Mason2c90e5d2007-04-02 10:50:19 -0400309static int find_and_setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400310 struct btrfs_root *tree_root,
311 struct btrfs_fs_info *fs_info,
312 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400313 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400314{
315 int ret;
316
Chris Mason2c90e5d2007-04-02 10:50:19 -0400317 __setup_root(blocksize, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400318 ret = btrfs_find_last_root(tree_root, objectid,
319 &root->root_item, &root->root_key);
320 BUG_ON(ret);
321
322 root->node = read_tree_block(root,
323 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400324 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500325 return 0;
326}
327
Chris Mason5eda7b52007-06-22 14:16:25 -0400328struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
329 struct btrfs_key *location)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400330{
331 struct btrfs_root *root;
332 struct btrfs_root *tree_root = fs_info->tree_root;
333 struct btrfs_path *path;
334 struct btrfs_leaf *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400335 u64 highest_inode;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400336 int ret = 0;
337
Chris Mason5eda7b52007-06-22 14:16:25 -0400338 root = kzalloc(sizeof(*root), GFP_NOFS);
Chris Mason0cf6c622007-06-09 09:22:25 -0400339 if (!root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400340 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400341 if (location->offset == (u64)-1) {
342 ret = find_and_setup_root(fs_info->sb->s_blocksize,
343 fs_info->tree_root, fs_info,
344 location->objectid, root);
345 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400346 kfree(root);
347 return ERR_PTR(ret);
348 }
349 goto insert;
350 }
351
352 __setup_root(fs_info->sb->s_blocksize, root, fs_info,
353 location->objectid);
354
355 path = btrfs_alloc_path();
356 BUG_ON(!path);
357 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
358 if (ret != 0) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400359 if (ret > 0)
360 ret = -ENOENT;
361 goto out;
362 }
363 l = btrfs_buffer_leaf(path->nodes[0]);
364 memcpy(&root->root_item,
365 btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
366 sizeof(root->root_item));
367 memcpy(&root->root_key, location, sizeof(*location));
368 ret = 0;
369out:
370 btrfs_release_path(root, path);
371 btrfs_free_path(path);
372 if (ret) {
373 kfree(root);
374 return ERR_PTR(ret);
375 }
376 root->node = read_tree_block(root,
377 btrfs_root_blocknr(&root->root_item));
378 BUG_ON(!root->node);
379insert:
Chris Mason0f7d52f2007-04-09 10:42:37 -0400380 root->ref_cows = 1;
Chris Mason5eda7b52007-06-22 14:16:25 -0400381 ret = btrfs_find_highest_inode(root, &highest_inode);
382 if (ret == 0) {
383 root->highest_inode = highest_inode;
384 root->last_inode_alloc = highest_inode;
385 }
386 return root;
387}
388
389struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
Josef Bacik58176a92007-08-29 15:47:34 -0400390 struct btrfs_key *location,
391 const char *name, int namelen)
Chris Mason5eda7b52007-06-22 14:16:25 -0400392{
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 }
Josef Bacik58176a92007-08-29 15:47:34 -0400412
413 ret = btrfs_set_root_name(root, name, namelen);
414 if (ret) {
415 brelse(root->node);
416 kfree(root);
417 return ERR_PTR(ret);
418 }
419
420 ret = btrfs_sysfs_add_root(root);
421 if (ret) {
422 brelse(root->node);
423 kfree(root->name);
424 kfree(root);
425 return ERR_PTR(ret);
426 }
427
Chris Mason0f7d52f2007-04-09 10:42:37 -0400428 return root;
429}
430
Chris Mason2c90e5d2007-04-02 10:50:19 -0400431struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500432{
Chris Masone20d96d2007-03-22 12:13:20 -0400433 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
434 GFP_NOFS);
435 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
436 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400437 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
438 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500439 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400440 int err = -EIO;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400441 struct btrfs_super_block *disk_super;
Chris Masoneb60cea2007-02-02 09:18:22 -0500442
Chris Mason39279cc2007-06-12 06:35:45 -0400443 if (!extent_root || !tree_root || !fs_info) {
444 err = -ENOMEM;
445 goto fail;
446 }
Chris Mason8ef97622007-03-26 10:15:30 -0400447 init_bit_radix(&fs_info->pinned_radix);
448 init_bit_radix(&fs_info->pending_del_radix);
Chris Masone37c9e62007-05-09 20:13:14 -0400449 init_bit_radix(&fs_info->extent_map_radix);
Chris Mason26b80032007-08-08 20:17:12 -0400450 init_bit_radix(&fs_info->extent_ins_radix);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400451 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400452 INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL);
Chris Masonbe744172007-05-06 10:15:01 -0400453 INIT_RADIX_TREE(&fs_info->block_group_data_radix, GFP_KERNEL);
Chris Mason8fd17792007-04-19 21:01:03 -0400454 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -0400455 INIT_LIST_HEAD(&fs_info->dead_roots);
Josef Bacik58176a92007-08-29 15:47:34 -0400456 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
457 init_completion(&fs_info->kobj_unregister);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400458 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400459 fs_info->running_transaction = NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400460 fs_info->last_trans_committed = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400461 fs_info->tree_root = tree_root;
462 fs_info->extent_root = extent_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400463 fs_info->sb = sb;
Chris Masond98237b2007-03-28 13:57:48 -0400464 fs_info->btree_inode = new_inode(sb);
465 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400466 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400467 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
468 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Masone66f7092007-04-20 13:16:02 -0400469 fs_info->do_barriers = 1;
Chris Masonfacda1e2007-06-08 18:11:48 -0400470 fs_info->closing = 0;
471
Chris Mason08607c12007-06-08 15:33:54 -0400472 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400473 BTRFS_I(fs_info->btree_inode)->root = tree_root;
474 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
475 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400476 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400477 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400478
Chris Mason79154b12007-03-22 15:59:16 -0400479 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400480 mutex_init(&fs_info->fs_mutex);
Chris Mason3768f362007-03-13 16:47:54 -0400481
Chris Mason2c90e5d2007-04-02 10:50:19 -0400482 __setup_root(sb->s_blocksize, tree_root,
483 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400484
Chris Mason2c90e5d2007-04-02 10:50:19 -0400485 fs_info->sb_buffer = read_tree_block(tree_root,
486 BTRFS_SUPER_INFO_OFFSET /
487 sb->s_blocksize);
Chris Masond98237b2007-03-28 13:57:48 -0400488
Chris Mason0f7d52f2007-04-09 10:42:37 -0400489 if (!fs_info->sb_buffer)
Chris Mason39279cc2007-06-12 06:35:45 -0400490 goto fail_iput;
Chris Masond98237b2007-03-28 13:57:48 -0400491 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
Chris Mason4b52dff2007-06-26 10:06:50 -0400492 fs_info->disk_super = disk_super;
493 memcpy(&fs_info->super_copy, disk_super, sizeof(fs_info->super_copy));
Chris Mason39279cc2007-06-12 06:35:45 -0400494
Chris Mason0f7d52f2007-04-09 10:42:37 -0400495 if (!btrfs_super_root(disk_super))
Chris Mason39279cc2007-06-12 06:35:45 -0400496 goto fail_sb_buffer;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400497
Chris Mason8352d8a2007-04-12 10:43:05 -0400498 i_size_write(fs_info->btree_inode,
499 btrfs_super_total_blocks(disk_super) <<
500 fs_info->btree_inode->i_blkbits);
501
Chris Mason39279cc2007-06-12 06:35:45 -0400502
503 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
504 sizeof(disk_super->magic))) {
505 printk("btrfs: valid FS not found on %s\n", sb->s_id);
506 goto fail_sb_buffer;
507 }
Chris Masone20d96d2007-03-22 12:13:20 -0400508 tree_root->node = read_tree_block(tree_root,
509 btrfs_super_root(disk_super));
Chris Mason39279cc2007-06-12 06:35:45 -0400510 if (!tree_root->node)
511 goto fail_sb_buffer;
Chris Mason3768f362007-03-13 16:47:54 -0400512
Chris Mason2c90e5d2007-04-02 10:50:19 -0400513 mutex_lock(&fs_info->fs_mutex);
514 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400515 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason39279cc2007-06-12 06:35:45 -0400516 if (ret) {
517 mutex_unlock(&fs_info->fs_mutex);
518 goto fail_tree_root;
519 }
Chris Mason3768f362007-03-13 16:47:54 -0400520
Chris Mason9078a3e2007-04-26 16:46:15 -0400521 btrfs_read_block_groups(extent_root);
522
Chris Mason0f7d52f2007-04-09 10:42:37 -0400523 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Mason5eda7b52007-06-22 14:16:25 -0400524 ret = btrfs_find_dead_roots(tree_root);
Josef Bacik58176a92007-08-29 15:47:34 -0400525 if (ret) {
526 mutex_unlock(&fs_info->fs_mutex);
Chris Mason5eda7b52007-06-22 14:16:25 -0400527 goto fail_tree_root;
Josef Bacik58176a92007-08-29 15:47:34 -0400528 }
Chris Mason5be6f7f2007-04-05 13:35:25 -0400529 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400530 return tree_root;
Chris Mason39279cc2007-06-12 06:35:45 -0400531
532fail_tree_root:
533 btrfs_block_release(tree_root, tree_root->node);
534fail_sb_buffer:
535 btrfs_block_release(tree_root, fs_info->sb_buffer);
536fail_iput:
537 iput(fs_info->btree_inode);
538fail:
539 kfree(extent_root);
540 kfree(tree_root);
541 kfree(fs_info);
542 return ERR_PTR(err);
Chris Masoneb60cea2007-02-02 09:18:22 -0500543}
544
Chris Masone089f052007-03-16 16:20:31 -0400545int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400546 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500547{
Chris Masone66f7092007-04-20 13:16:02 -0400548 int ret;
Chris Masond5719762007-03-23 10:01:08 -0400549 struct buffer_head *bh = root->fs_info->sb_buffer;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400550
Chris Masond5719762007-03-23 10:01:08 -0400551 lock_buffer(bh);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400552 WARN_ON(atomic_read(&bh->b_count) < 1);
Chris Masond5719762007-03-23 10:01:08 -0400553 clear_buffer_dirty(bh);
Chris Mason87cbda52007-03-28 19:44:27 -0400554 csum_tree_block(root, bh, 0);
Chris Masond5719762007-03-23 10:01:08 -0400555 bh->b_end_io = end_buffer_write_sync;
556 get_bh(bh);
Chris Masone66f7092007-04-20 13:16:02 -0400557 if (root->fs_info->do_barriers)
558 ret = submit_bh(WRITE_BARRIER, bh);
559 else
560 ret = submit_bh(WRITE, bh);
561 if (ret == -EOPNOTSUPP) {
Chris Mason8c2383c2007-06-18 09:57:58 -0400562 get_bh(bh);
563 lock_buffer(bh);
Chris Masone66f7092007-04-20 13:16:02 -0400564 set_buffer_uptodate(bh);
565 root->fs_info->do_barriers = 0;
566 ret = submit_bh(WRITE, bh);
567 }
Chris Masond5719762007-03-23 10:01:08 -0400568 wait_on_buffer(bh);
569 if (!buffer_uptodate(bh)) {
570 WARN_ON(1);
571 return -EIO;
Chris Masoncfaa7292007-02-21 17:04:57 -0500572 }
573 return 0;
574}
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)
584 brelse(root->node);
585 if (root->commit_root)
586 brelse(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)
632 btrfs_block_release(fs_info->extent_root,
633 fs_info->extent_root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400634 if (fs_info->tree_root->node)
635 btrfs_block_release(fs_info->tree_root,
636 fs_info->tree_root->node);
637 btrfs_block_release(root, fs_info->sb_buffer);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400638 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
639 iput(fs_info->btree_inode);
Chris Mason7eccb902007-04-11 15:53:25 -0400640
Chris Mason9078a3e2007-04-26 16:46:15 -0400641 btrfs_free_block_groups(root->fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400642 del_fs_roots(fs_info);
643 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 Masonccd467d2007-06-28 15:57:36 -0400648void btrfs_mark_buffer_dirty(struct buffer_head *bh)
649{
650 struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
651 u64 transid = btrfs_header_generation(btrfs_buffer_header(bh));
Chris Mason6702ed42007-08-07 16:15:09 -0400652
Chris Masonccd467d2007-06-28 15:57:36 -0400653 WARN_ON(!atomic_read(&bh->b_count));
Chris Mason6702ed42007-08-07 16:15:09 -0400654
Chris Masonccd467d2007-06-28 15:57:36 -0400655 if (transid != root->fs_info->generation) {
656 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
657 (unsigned long long)bh->b_blocknr,
658 transid, root->fs_info->generation);
659 WARN_ON(1);
660 }
661 mark_buffer_dirty(bh);
662}
663
Chris Masone20d96d2007-03-22 12:13:20 -0400664void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500665{
Chris Mason7cfcc172007-04-02 14:53:59 -0400666 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500667}
668
Chris Mason35b7e472007-05-02 15:53:43 -0400669void btrfs_btree_balance_dirty(struct btrfs_root *root)
670{
671 balance_dirty_pages_ratelimited(root->fs_info->btree_inode->i_mapping);
672}