blob: 3da057f78bc89112a73876e93449c5a92824f82a [file] [log] [blame]
Chris Masone20d96d2007-03-22 12:13:20 -04001#include <linux/module.h>
2#include <linux/fs.h>
Chris Masond98237b2007-03-28 13:57:48 -04003#include <linux/blkdev.h>
Chris Mason87cbda52007-03-28 19:44:27 -04004#include <linux/crypto.h>
5#include <linux/scatterlist.h>
Chris Mason22b0ebd2007-03-30 08:47:31 -04006#include <linux/swap.h>
Chris Mason0f7d52f2007-04-09 10:42:37 -04007#include <linux/radix-tree.h>
Chris Mason35b7e472007-05-02 15:53:43 -04008#include <linux/writeback.h>
Chris Masoneb60cea2007-02-02 09:18:22 -05009#include "ctree.h"
10#include "disk-io.h"
Chris Masone089f052007-03-16 16:20:31 -040011#include "transaction.h"
Chris Mason0f7d52f2007-04-09 10:42:37 -040012#include "btrfs_inode.h"
Chris Masoneb60cea2007-02-02 09:18:22 -050013
Chris Mason7eccb902007-04-11 15:53:25 -040014struct dev_lookup {
15 u64 block_start;
16 u64 num_blocks;
Chris Masonb4100d62007-04-12 12:14:00 -040017 u64 device_id;
Chris Mason7eccb902007-04-11 15:53:25 -040018 struct block_device *bdev;
19};
20
Chris Mason8352d8a2007-04-12 10:43:05 -040021int btrfs_insert_dev_radix(struct btrfs_root *root,
22 struct block_device *bdev,
Chris Masonb4100d62007-04-12 12:14:00 -040023 u64 device_id,
Chris Mason8352d8a2007-04-12 10:43:05 -040024 u64 block_start,
25 u64 num_blocks)
26{
27 struct dev_lookup *lookup;
Chris Mason8352d8a2007-04-12 10:43:05 -040028 int ret;
29
30 lookup = kmalloc(sizeof(*lookup), GFP_NOFS);
31 if (!lookup)
32 return -ENOMEM;
33 lookup->block_start = block_start;
34 lookup->num_blocks = num_blocks;
35 lookup->bdev = bdev;
Chris Masonb4100d62007-04-12 12:14:00 -040036 lookup->device_id = device_id;
Chris Mason8352d8a2007-04-12 10:43:05 -040037
38 ret = radix_tree_insert(&root->fs_info->dev_radix, block_start +
39 num_blocks - 1, lookup);
40 return ret;
41}
42
Chris Mason7eccb902007-04-11 15:53:25 -040043u64 bh_blocknr(struct buffer_head *bh)
44{
45 int blkbits = bh->b_page->mapping->host->i_blkbits;
46 u64 blocknr = bh->b_page->index << (PAGE_CACHE_SHIFT - blkbits);
47 unsigned long offset;
48
49 if (PageHighMem(bh->b_page))
50 offset = (unsigned long)bh->b_data;
51 else
52 offset = bh->b_data - (char *)page_address(bh->b_page);
53 blocknr += offset >> (PAGE_CACHE_SHIFT - blkbits);
54 return blocknr;
55}
56
Chris Masone20d96d2007-03-22 12:13:20 -040057static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050058{
Chris Masone20d96d2007-03-22 12:13:20 -040059 struct btrfs_node *node = btrfs_buffer_node(buf);
Chris Mason7eccb902007-04-11 15:53:25 -040060 if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
Chris Mason8352d8a2007-04-12 10:43:05 -040061 printk(KERN_CRIT "bh_blocknr(buf) is %Lu, header is %Lu\n",
62 bh_blocknr(buf), btrfs_header_blocknr(&node->header));
Chris Mason9a8dd152007-02-23 08:38:36 -050063 BUG();
Chris Masond98237b2007-03-28 13:57:48 -040064 }
Chris Mason9a8dd152007-02-23 08:38:36 -050065 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050066}
67
Chris Masond98237b2007-03-28 13:57:48 -040068struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050069{
Chris Masond98237b2007-03-28 13:57:48 -040070 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
71 int blockbits = root->fs_info->sb->s_blocksize_bits;
72 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
73 struct page *page;
74 struct buffer_head *bh;
75 struct buffer_head *head;
76 struct buffer_head *ret = NULL;
77
Chris Mason2c90e5d2007-04-02 10:50:19 -040078
Chris Masond98237b2007-03-28 13:57:48 -040079 page = find_lock_page(mapping, index);
80 if (!page)
81 return NULL;
82
83 if (!page_has_buffers(page))
84 goto out_unlock;
85
86 head = page_buffers(page);
87 bh = head;
88 do {
Chris Mason7eccb902007-04-11 15:53:25 -040089 if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -040090 ret = bh;
91 get_bh(bh);
92 goto out_unlock;
93 }
94 bh = bh->b_this_page;
95 } while (bh != head);
96out_unlock:
97 unlock_page(page);
98 page_cache_release(page);
99 return ret;
Chris Masoned2ff2c2007-03-01 18:59:40 -0500100}
101
Chris Mason8352d8a2007-04-12 10:43:05 -0400102int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
Chris Mason7eccb902007-04-11 15:53:25 -0400103 u64 logical)
104{
105 struct dev_lookup *lookup[2];
Chris Mason7eccb902007-04-11 15:53:25 -0400106
107 int ret;
108
Chris Mason236454df2007-04-19 13:37:44 -0400109 if (logical == 0) {
110 bh->b_bdev = NULL;
111 bh->b_blocknr = 0;
112 set_buffer_mapped(bh);
113 return 0;
114 }
Chris Mason7eccb902007-04-11 15:53:25 -0400115 root = root->fs_info->dev_root;
116 ret = radix_tree_gang_lookup(&root->fs_info->dev_radix,
117 (void **)lookup,
118 (unsigned long)logical,
119 ARRAY_SIZE(lookup));
120 if (ret == 0 || lookup[0]->block_start > logical ||
121 lookup[0]->block_start + lookup[0]->num_blocks <= logical) {
122 ret = -ENOENT;
123 goto out;
124 }
125 bh->b_bdev = lookup[0]->bdev;
126 bh->b_blocknr = logical - lookup[0]->block_start;
Chris Mason7eccb902007-04-11 15:53:25 -0400127 set_buffer_mapped(bh);
128 ret = 0;
129out:
130 return ret;
131}
132
Chris Masond98237b2007-03-28 13:57:48 -0400133struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
134 u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -0500135{
Chris Masond98237b2007-03-28 13:57:48 -0400136 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
137 int blockbits = root->fs_info->sb->s_blocksize_bits;
138 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
139 struct page *page;
140 struct buffer_head *bh;
141 struct buffer_head *head;
142 struct buffer_head *ret = NULL;
Chris Mason7eccb902007-04-11 15:53:25 -0400143 int err;
Chris Masond98237b2007-03-28 13:57:48 -0400144 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400145
Chris Masond98237b2007-03-28 13:57:48 -0400146 page = grab_cache_page(mapping, index);
147 if (!page)
148 return NULL;
149
Chris Masond98237b2007-03-28 13:57:48 -0400150 if (!page_has_buffers(page))
151 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
152 head = page_buffers(page);
153 bh = head;
154 do {
155 if (!buffer_mapped(bh)) {
Chris Mason8352d8a2007-04-12 10:43:05 -0400156 err = btrfs_map_bh_to_logical(root, bh, first_block);
Chris Mason7eccb902007-04-11 15:53:25 -0400157 BUG_ON(err);
Chris Masond98237b2007-03-28 13:57:48 -0400158 }
Chris Mason7eccb902007-04-11 15:53:25 -0400159 if (bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -0400160 ret = bh;
161 get_bh(bh);
162 goto out_unlock;
163 }
164 bh = bh->b_this_page;
165 first_block++;
166 } while (bh != head);
167out_unlock:
168 unlock_page(page);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400169 if (ret)
170 touch_buffer(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400171 page_cache_release(page);
172 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400173}
Chris Mason123abc82007-03-14 14:14:43 -0400174
Chris Masond98237b2007-03-28 13:57:48 -0400175static int btree_get_block(struct inode *inode, sector_t iblock,
176 struct buffer_head *bh, int create)
177{
Chris Mason7eccb902007-04-11 15:53:25 -0400178 int err;
179 struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
Chris Mason8352d8a2007-04-12 10:43:05 -0400180 err = btrfs_map_bh_to_logical(root, bh, iblock);
Chris Mason7eccb902007-04-11 15:53:25 -0400181 return err;
Chris Masond98237b2007-03-28 13:57:48 -0400182}
183
Chris Masonf254e522007-03-29 15:15:27 -0400184int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
185 char *result)
Chris Mason87cbda52007-03-28 19:44:27 -0400186{
Chris Mason87cbda52007-03-28 19:44:27 -0400187 struct scatterlist sg;
188 struct crypto_hash *tfm = root->fs_info->hash_tfm;
189 struct hash_desc desc;
190 int ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400191
192 desc.tfm = tfm;
193 desc.flags = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400194 sg_init_one(&sg, data, len);
Chris Mason87cbda52007-03-28 19:44:27 -0400195 spin_lock(&root->fs_info->hash_lock);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400196 ret = crypto_hash_digest(&desc, &sg, 1, result);
Chris Mason87cbda52007-03-28 19:44:27 -0400197 spin_unlock(&root->fs_info->hash_lock);
198 if (ret) {
Chris Mason509659c2007-05-10 12:36:17 -0400199 printk("digest failed\n");
Chris Mason87cbda52007-03-28 19:44:27 -0400200 }
Chris Masonf254e522007-03-29 15:15:27 -0400201 return ret;
202}
203static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
204 int verify)
205{
Chris Mason509659c2007-05-10 12:36:17 -0400206 char result[BTRFS_CRC32_SIZE];
Chris Masonf254e522007-03-29 15:15:27 -0400207 int ret;
208 struct btrfs_node *node;
209
210 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
211 bh->b_size - BTRFS_CSUM_SIZE, result);
212 if (ret)
213 return ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400214 if (verify) {
Chris Mason509659c2007-05-10 12:36:17 -0400215 if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
Chris Mason7eccb902007-04-11 15:53:25 -0400216 printk("checksum verify failed on %Lu\n",
217 bh_blocknr(bh));
Chris Masonf254e522007-03-29 15:15:27 -0400218 return 1;
219 }
220 } else {
221 node = btrfs_buffer_node(bh);
Chris Mason509659c2007-05-10 12:36:17 -0400222 memcpy(node->header.csum, result, BTRFS_CRC32_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -0400223 }
Chris Mason87cbda52007-03-28 19:44:27 -0400224 return 0;
225}
226
Chris Masond98237b2007-03-28 13:57:48 -0400227static int btree_writepage(struct page *page, struct writeback_control *wbc)
228{
Chris Mason87cbda52007-03-28 19:44:27 -0400229 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400230 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400231 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400232 if (!page_has_buffers(page)) {
233 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
234 (1 << BH_Dirty)|(1 << BH_Uptodate));
235 }
236 head = page_buffers(page);
237 bh = head;
238 do {
239 if (buffer_dirty(bh))
240 csum_tree_block(root, bh, 0);
241 bh = bh->b_this_page;
242 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400243 return block_write_full_page(page, btree_get_block, wbc);
244}
245
246static int btree_readpage(struct file * file, struct page * page)
247{
248 return block_read_full_page(page, btree_get_block);
249}
250
251static struct address_space_operations btree_aops = {
252 .readpage = btree_readpage,
253 .writepage = btree_writepage,
254 .sync_page = block_sync_page,
255};
256
Chris Mason090d1872007-05-01 08:53:32 -0400257int readahead_tree_block(struct btrfs_root *root, u64 blocknr)
258{
259 struct buffer_head *bh = NULL;
Chris Masonde428b62007-05-18 13:28:27 -0400260 int ret = 0;
Chris Mason090d1872007-05-01 08:53:32 -0400261
262 bh = btrfs_find_create_tree_block(root, blocknr);
263 if (!bh)
264 return 0;
Chris Masonde428b62007-05-18 13:28:27 -0400265 if (buffer_uptodate(bh)) {
266 ret = 1;
Chris Mason090d1872007-05-01 08:53:32 -0400267 goto done;
Chris Masonde428b62007-05-18 13:28:27 -0400268 }
269 if (test_set_buffer_locked(bh)) {
270 ret = 1;
Chris Mason090d1872007-05-01 08:53:32 -0400271 goto done;
Chris Masonde428b62007-05-18 13:28:27 -0400272 }
Chris Mason090d1872007-05-01 08:53:32 -0400273 if (!buffer_uptodate(bh)) {
274 get_bh(bh);
275 bh->b_end_io = end_buffer_read_sync;
276 submit_bh(READ, bh);
277 } else {
278 unlock_buffer(bh);
Chris Masonde428b62007-05-18 13:28:27 -0400279 ret = 1;
Chris Mason090d1872007-05-01 08:53:32 -0400280 }
281done:
282 brelse(bh);
Chris Masonde428b62007-05-18 13:28:27 -0400283 return ret;
Chris Mason090d1872007-05-01 08:53:32 -0400284}
285
Chris Masone20d96d2007-03-22 12:13:20 -0400286struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
287{
Chris Masond98237b2007-03-28 13:57:48 -0400288 struct buffer_head *bh = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400289
Chris Masond98237b2007-03-28 13:57:48 -0400290 bh = btrfs_find_create_tree_block(root, blocknr);
291 if (!bh)
292 return bh;
Chris Mason9d642722007-04-03 11:43:19 -0400293 if (buffer_uptodate(bh))
294 goto uptodate;
Chris Masond98237b2007-03-28 13:57:48 -0400295 lock_buffer(bh);
296 if (!buffer_uptodate(bh)) {
297 get_bh(bh);
298 bh->b_end_io = end_buffer_read_sync;
299 submit_bh(READ, bh);
300 wait_on_buffer(bh);
301 if (!buffer_uptodate(bh))
302 goto fail;
303 } else {
304 unlock_buffer(bh);
305 }
Chris Mason9d642722007-04-03 11:43:19 -0400306uptodate:
Chris Mason090d1872007-05-01 08:53:32 -0400307 if (!buffer_checked(bh)) {
308 csum_tree_block(root, bh, 1);
309 set_buffer_checked(bh);
310 }
Chris Masond98237b2007-03-28 13:57:48 -0400311 if (check_tree_block(root, bh))
Chris Masoncfaa7292007-02-21 17:04:57 -0500312 BUG();
Chris Masond98237b2007-03-28 13:57:48 -0400313 return bh;
314fail:
315 brelse(bh);
316 return NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500317}
318
Chris Masone089f052007-03-16 16:20:31 -0400319int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400320 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500321{
Chris Masond6025572007-03-30 14:27:56 -0400322 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400323 mark_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500324 return 0;
325}
326
Chris Masone089f052007-03-16 16:20:31 -0400327int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400328 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500329{
Chris Masond6025572007-03-30 14:27:56 -0400330 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400331 clear_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500332 return 0;
333}
334
Chris Mason2c90e5d2007-04-02 10:50:19 -0400335static int __setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400336 struct btrfs_root *root,
337 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400338 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500339{
Chris Masoncfaa7292007-02-21 17:04:57 -0500340 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400341 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500342 root->commit_root = NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400343 root->blocksize = blocksize;
Chris Mason123abc82007-03-14 14:14:43 -0400344 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400345 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400346 root->objectid = objectid;
347 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400348 root->highest_inode = 0;
349 root->last_inode_alloc = 0;
Chris Mason3768f362007-03-13 16:47:54 -0400350 memset(&root->root_key, 0, sizeof(root->root_key));
351 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason4d775672007-04-20 20:23:12 -0400352 root->root_key.objectid = objectid;
Chris Mason3768f362007-03-13 16:47:54 -0400353 return 0;
354}
355
Chris Mason2c90e5d2007-04-02 10:50:19 -0400356static int find_and_setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400357 struct btrfs_root *tree_root,
358 struct btrfs_fs_info *fs_info,
359 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400360 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400361{
362 int ret;
363
Chris Mason2c90e5d2007-04-02 10:50:19 -0400364 __setup_root(blocksize, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400365 ret = btrfs_find_last_root(tree_root, objectid,
366 &root->root_item, &root->root_key);
367 BUG_ON(ret);
368
369 root->node = read_tree_block(root,
370 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400371 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500372 return 0;
373}
374
Chris Mason0f7d52f2007-04-09 10:42:37 -0400375struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
376 struct btrfs_key *location)
377{
378 struct btrfs_root *root;
379 struct btrfs_root *tree_root = fs_info->tree_root;
380 struct btrfs_path *path;
381 struct btrfs_leaf *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400382 u64 highest_inode;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400383 int ret = 0;
384
385printk("read_fs_root looking for %Lu %Lu %u\n", location->objectid, location->offset, location->flags);
Chris Mason2619ba12007-04-10 16:58:11 -0400386 root = radix_tree_lookup(&fs_info->fs_roots_radix,
387 (unsigned long)location->objectid);
388 if (root) {
389printk("found %p in cache\n", root);
390 return root;
391 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400392 root = kmalloc(sizeof(*root), GFP_NOFS);
393 if (!root) {
394printk("failed1\n");
395 return ERR_PTR(-ENOMEM);
396 }
397 if (location->offset == (u64)-1) {
398 ret = find_and_setup_root(fs_info->sb->s_blocksize,
399 fs_info->tree_root, fs_info,
400 location->objectid, root);
401 if (ret) {
402printk("failed2\n");
403 kfree(root);
404 return ERR_PTR(ret);
405 }
406 goto insert;
407 }
408
409 __setup_root(fs_info->sb->s_blocksize, root, fs_info,
410 location->objectid);
411
412 path = btrfs_alloc_path();
413 BUG_ON(!path);
414 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
415 if (ret != 0) {
416printk("internal search_slot gives us %d\n", ret);
417 if (ret > 0)
418 ret = -ENOENT;
419 goto out;
420 }
421 l = btrfs_buffer_leaf(path->nodes[0]);
422 memcpy(&root->root_item,
423 btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
424 sizeof(root->root_item));
425 memcpy(&root->root_key, location, sizeof(*location));
426 ret = 0;
427out:
428 btrfs_release_path(root, path);
429 btrfs_free_path(path);
430 if (ret) {
431 kfree(root);
432 return ERR_PTR(ret);
433 }
434 root->node = read_tree_block(root,
435 btrfs_root_blocknr(&root->root_item));
436 BUG_ON(!root->node);
437insert:
438printk("inserting %p\n", root);
439 root->ref_cows = 1;
Chris Mason2619ba12007-04-10 16:58:11 -0400440 ret = radix_tree_insert(&fs_info->fs_roots_radix,
441 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400442 root);
443 if (ret) {
444printk("radix_tree_insert gives us %d\n", ret);
445 brelse(root->node);
446 kfree(root);
447 return ERR_PTR(ret);
448 }
Chris Mason1b05da22007-04-10 12:13:09 -0400449 ret = btrfs_find_highest_inode(root, &highest_inode);
450 if (ret == 0) {
451 root->highest_inode = highest_inode;
452 root->last_inode_alloc = highest_inode;
453printk("highest inode is %Lu\n", highest_inode);
454 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400455printk("all worked\n");
456 return root;
457}
458
Chris Masonb4100d62007-04-12 12:14:00 -0400459static int btrfs_open_disk(struct btrfs_root *root, u64 device_id,
460 u64 block_start, u64 num_blocks,
461 char *filename, int name_len)
Chris Mason8352d8a2007-04-12 10:43:05 -0400462{
463 char *null_filename;
464 struct block_device *bdev;
465 int ret;
466
Chris Mason8352d8a2007-04-12 10:43:05 -0400467 null_filename = kmalloc(name_len + 1, GFP_NOFS);
468 if (!null_filename)
469 return -ENOMEM;
470 memcpy(null_filename, filename, name_len);
471 null_filename[name_len] = '\0';
472
473 bdev = open_bdev_excl(null_filename, O_RDWR, root->fs_info->sb);
474 if (IS_ERR(bdev)) {
475 ret = PTR_ERR(bdev);
476 goto out;
477 }
478 set_blocksize(bdev, root->fs_info->sb->s_blocksize);
Chris Masonb4100d62007-04-12 12:14:00 -0400479 ret = btrfs_insert_dev_radix(root, bdev, device_id,
480 block_start, num_blocks);
Chris Mason8352d8a2007-04-12 10:43:05 -0400481 BUG_ON(ret);
482 ret = 0;
483out:
484 kfree(null_filename);
485 return ret;
486}
487
488static int read_device_info(struct btrfs_root *root)
489{
490 struct btrfs_path *path;
491 int ret;
492 struct btrfs_key key;
493 struct btrfs_leaf *leaf;
494 struct btrfs_device_item *dev_item;
495 int nritems;
496 int slot;
497
498 root = root->fs_info->dev_root;
499
500 path = btrfs_alloc_path();
501 if (!path)
502 return -ENOMEM;
503 key.objectid = 0;
504 key.offset = 0;
505 key.flags = 0;
506 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
507
508 mutex_lock(&root->fs_info->fs_mutex);
509 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
510 leaf = btrfs_buffer_leaf(path->nodes[0]);
511 nritems = btrfs_header_nritems(&leaf->header);
512 while(1) {
513 slot = path->slots[0];
514 if (slot >= nritems) {
515 ret = btrfs_next_leaf(root, path);
516 if (ret)
517 break;
518 leaf = btrfs_buffer_leaf(path->nodes[0]);
519 nritems = btrfs_header_nritems(&leaf->header);
520 slot = path->slots[0];
521 }
522 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
523 if (btrfs_key_type(&key) != BTRFS_DEV_ITEM_KEY) {
524 path->slots[0]++;
525 continue;
526 }
527 dev_item = btrfs_item_ptr(leaf, slot, struct btrfs_device_item);
528printk("found key %Lu %Lu\n", key.objectid, key.offset);
Chris Masonb4100d62007-04-12 12:14:00 -0400529 if (btrfs_device_id(dev_item) !=
530 btrfs_super_device_id(root->fs_info->disk_super)) {
531 ret = btrfs_open_disk(root, btrfs_device_id(dev_item),
532 key.objectid, key.offset,
533 (char *)(dev_item + 1),
534 btrfs_device_pathlen(dev_item));
535 BUG_ON(ret);
536 }
Chris Mason8352d8a2007-04-12 10:43:05 -0400537 path->slots[0]++;
538 }
539 btrfs_free_path(path);
540 mutex_unlock(&root->fs_info->fs_mutex);
541 return 0;
542}
543
Chris Mason2c90e5d2007-04-02 10:50:19 -0400544struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500545{
Chris Masone20d96d2007-03-22 12:13:20 -0400546 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
547 GFP_NOFS);
Chris Mason0bd93ba2007-04-11 13:57:44 -0400548 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
549 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400550 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
551 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400552 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
553 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500554 int ret;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400555 struct btrfs_super_block *disk_super;
Chris Mason7eccb902007-04-11 15:53:25 -0400556 struct dev_lookup *dev_lookup;
Chris Masoneb60cea2007-02-02 09:18:22 -0500557
Chris Mason8ef97622007-03-26 10:15:30 -0400558 init_bit_radix(&fs_info->pinned_radix);
559 init_bit_radix(&fs_info->pending_del_radix);
Chris Masone37c9e62007-05-09 20:13:14 -0400560 init_bit_radix(&fs_info->extent_map_radix);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400561 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason7eccb902007-04-11 15:53:25 -0400562 INIT_RADIX_TREE(&fs_info->dev_radix, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400563 INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL);
Chris Masonbe744172007-05-06 10:15:01 -0400564 INIT_RADIX_TREE(&fs_info->block_group_data_radix, GFP_KERNEL);
Chris Mason8fd17792007-04-19 21:01:03 -0400565 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400566 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400567 fs_info->running_transaction = NULL;
Chris Mason9f5fae22007-03-20 14:38:32 -0400568 fs_info->tree_root = tree_root;
569 fs_info->extent_root = extent_root;
Chris Mason0bd93ba2007-04-11 13:57:44 -0400570 fs_info->dev_root = dev_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400571 fs_info->sb = sb;
Chris Masond98237b2007-03-28 13:57:48 -0400572 fs_info->btree_inode = new_inode(sb);
573 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400574 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400575 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
576 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Masone66f7092007-04-20 13:16:02 -0400577 fs_info->do_barriers = 1;
Chris Masonf2458e12007-04-25 15:52:25 -0400578 fs_info->extent_tree_insert_nr = 0;
579 fs_info->extent_tree_prealloc_nr = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400580 BTRFS_I(fs_info->btree_inode)->root = tree_root;
581 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
582 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400583 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400584 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason509659c2007-05-10 12:36:17 -0400585 fs_info->hash_tfm = crypto_alloc_hash("crc32c", 0, CRYPTO_ALG_ASYNC);
Chris Mason30ae8462007-03-29 09:59:15 -0400586 spin_lock_init(&fs_info->hash_lock);
Chris Mason30ae8462007-03-29 09:59:15 -0400587 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
Chris Mason509659c2007-05-10 12:36:17 -0400588 printk("failed to allocate digest hash\n");
Chris Mason87cbda52007-03-28 19:44:27 -0400589 return NULL;
590 }
Chris Mason79154b12007-03-22 15:59:16 -0400591 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400592 mutex_init(&fs_info->fs_mutex);
Chris Mason3768f362007-03-13 16:47:54 -0400593
Chris Mason0bd93ba2007-04-11 13:57:44 -0400594 __setup_root(sb->s_blocksize, dev_root,
595 fs_info, BTRFS_DEV_TREE_OBJECTID);
596
Chris Mason2c90e5d2007-04-02 10:50:19 -0400597 __setup_root(sb->s_blocksize, tree_root,
598 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400599
600 dev_lookup = kmalloc(sizeof(*dev_lookup), GFP_NOFS);
601 dev_lookup->block_start = 0;
602 dev_lookup->num_blocks = (u32)-2;
603 dev_lookup->bdev = sb->s_bdev;
Chris Masonb4100d62007-04-12 12:14:00 -0400604 dev_lookup->device_id = 0;
Chris Mason7eccb902007-04-11 15:53:25 -0400605 ret = radix_tree_insert(&fs_info->dev_radix, (u32)-2, dev_lookup);
606 BUG_ON(ret);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400607 fs_info->sb_buffer = read_tree_block(tree_root,
608 BTRFS_SUPER_INFO_OFFSET /
609 sb->s_blocksize);
Chris Masond98237b2007-03-28 13:57:48 -0400610
Chris Mason0f7d52f2007-04-09 10:42:37 -0400611 if (!fs_info->sb_buffer)
Chris Masond98237b2007-03-28 13:57:48 -0400612 return NULL;
Chris Masond98237b2007-03-28 13:57:48 -0400613 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400614 if (!btrfs_super_root(disk_super))
Chris Mason2c90e5d2007-04-02 10:50:19 -0400615 return NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400616
Chris Mason8352d8a2007-04-12 10:43:05 -0400617 i_size_write(fs_info->btree_inode,
618 btrfs_super_total_blocks(disk_super) <<
619 fs_info->btree_inode->i_blkbits);
620
Chris Mason7eccb902007-04-11 15:53:25 -0400621 radix_tree_delete(&fs_info->dev_radix, (u32)-2);
622 dev_lookup->block_start = btrfs_super_device_block_start(disk_super);
623 dev_lookup->num_blocks = btrfs_super_device_num_blocks(disk_super);
Chris Masonb4100d62007-04-12 12:14:00 -0400624 dev_lookup->device_id = btrfs_super_device_id(disk_super);
625
Chris Mason7eccb902007-04-11 15:53:25 -0400626 ret = radix_tree_insert(&fs_info->dev_radix,
627 dev_lookup->block_start +
Chris Mason8352d8a2007-04-12 10:43:05 -0400628 dev_lookup->num_blocks - 1, dev_lookup);
Chris Mason7eccb902007-04-11 15:53:25 -0400629 BUG_ON(ret);
630
Chris Masond98237b2007-03-28 13:57:48 -0400631 fs_info->disk_super = disk_super;
Chris Mason8352d8a2007-04-12 10:43:05 -0400632
Chris Mason0bd93ba2007-04-11 13:57:44 -0400633 dev_root->node = read_tree_block(tree_root,
634 btrfs_super_device_root(disk_super));
Chris Mason8352d8a2007-04-12 10:43:05 -0400635
636 ret = read_device_info(dev_root);
637 BUG_ON(ret);
638
Chris Masone20d96d2007-03-22 12:13:20 -0400639 tree_root->node = read_tree_block(tree_root,
640 btrfs_super_root(disk_super));
Chris Mason3768f362007-03-13 16:47:54 -0400641 BUG_ON(!tree_root->node);
642
Chris Mason2c90e5d2007-04-02 10:50:19 -0400643 mutex_lock(&fs_info->fs_mutex);
644 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400645 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400646 BUG_ON(ret);
647
Chris Mason9078a3e2007-04-26 16:46:15 -0400648 btrfs_read_block_groups(extent_root);
649
Chris Mason0f7d52f2007-04-09 10:42:37 -0400650 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Masond6e4a422007-04-06 15:37:36 -0400651 memset(&fs_info->kobj, 0, sizeof(fs_info->kobj));
Chris Mason5be6f7f2007-04-05 13:35:25 -0400652 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400653 return tree_root;
Chris Masoneb60cea2007-02-02 09:18:22 -0500654}
655
Chris Masone089f052007-03-16 16:20:31 -0400656int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400657 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500658{
Chris Masone66f7092007-04-20 13:16:02 -0400659 int ret;
Chris Masond5719762007-03-23 10:01:08 -0400660 struct buffer_head *bh = root->fs_info->sb_buffer;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400661
Chris Masond5719762007-03-23 10:01:08 -0400662 btrfs_set_super_root(root->fs_info->disk_super,
Chris Mason7eccb902007-04-11 15:53:25 -0400663 bh_blocknr(root->fs_info->tree_root->node));
Chris Masond5719762007-03-23 10:01:08 -0400664 lock_buffer(bh);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400665 WARN_ON(atomic_read(&bh->b_count) < 1);
Chris Masond5719762007-03-23 10:01:08 -0400666 clear_buffer_dirty(bh);
Chris Mason87cbda52007-03-28 19:44:27 -0400667 csum_tree_block(root, bh, 0);
Chris Masond5719762007-03-23 10:01:08 -0400668 bh->b_end_io = end_buffer_write_sync;
669 get_bh(bh);
Chris Masone66f7092007-04-20 13:16:02 -0400670 if (root->fs_info->do_barriers)
671 ret = submit_bh(WRITE_BARRIER, bh);
672 else
673 ret = submit_bh(WRITE, bh);
674 if (ret == -EOPNOTSUPP) {
675 set_buffer_uptodate(bh);
676 root->fs_info->do_barriers = 0;
677 ret = submit_bh(WRITE, bh);
678 }
Chris Masond5719762007-03-23 10:01:08 -0400679 wait_on_buffer(bh);
680 if (!buffer_uptodate(bh)) {
681 WARN_ON(1);
682 return -EIO;
Chris Masoncfaa7292007-02-21 17:04:57 -0500683 }
684 return 0;
685}
686
Chris Mason2619ba12007-04-10 16:58:11 -0400687static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
688{
689 radix_tree_delete(&fs_info->fs_roots_radix,
690 (unsigned long)root->root_key.objectid);
691 if (root->inode)
692 iput(root->inode);
693 if (root->node)
694 brelse(root->node);
695 if (root->commit_root)
696 brelse(root->commit_root);
697 kfree(root);
698 return 0;
699}
700
Chris Mason35b7e472007-05-02 15:53:43 -0400701static int del_fs_roots(struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400702{
703 int ret;
704 struct btrfs_root *gang[8];
705 int i;
706
707 while(1) {
708 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
709 (void **)gang, 0,
710 ARRAY_SIZE(gang));
711 if (!ret)
712 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400713 for (i = 0; i < ret; i++)
714 free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400715 }
716 return 0;
717}
Chris Masonb4100d62007-04-12 12:14:00 -0400718
Chris Mason7eccb902007-04-11 15:53:25 -0400719static int free_dev_radix(struct btrfs_fs_info *fs_info)
720{
721 struct dev_lookup *lookup[8];
722 struct block_device *super_bdev = fs_info->sb->s_bdev;
723 int ret;
724 int i;
725 while(1) {
726 ret = radix_tree_gang_lookup(&fs_info->dev_radix,
727 (void **)lookup, 0,
728 ARRAY_SIZE(lookup));
729 if (!ret)
730 break;
731 for (i = 0; i < ret; i++) {
732 if (lookup[i]->bdev != super_bdev)
733 close_bdev_excl(lookup[i]->bdev);
734 radix_tree_delete(&fs_info->dev_radix,
735 lookup[i]->block_start +
Chris Mason8352d8a2007-04-12 10:43:05 -0400736 lookup[i]->num_blocks - 1);
Chris Mason7eccb902007-04-11 15:53:25 -0400737 kfree(lookup[i]);
738 }
739 }
740 return 0;
741}
Chris Mason0f7d52f2007-04-09 10:42:37 -0400742
Chris Masone20d96d2007-03-22 12:13:20 -0400743int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500744{
Chris Mason3768f362007-03-13 16:47:54 -0400745 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400746 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400747 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -0400748
Chris Mason0f7d52f2007-04-09 10:42:37 -0400749 mutex_lock(&fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400750 trans = btrfs_start_transaction(root, 1);
751 btrfs_commit_transaction(trans, root);
752 /* run commit again to drop the original snapshot */
753 trans = btrfs_start_transaction(root, 1);
754 btrfs_commit_transaction(trans, root);
755 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400756 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400757 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400758 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500759
Chris Mason0f7d52f2007-04-09 10:42:37 -0400760 if (fs_info->extent_root->node)
761 btrfs_block_release(fs_info->extent_root,
762 fs_info->extent_root->node);
Chris Mason0bd93ba2007-04-11 13:57:44 -0400763 if (fs_info->dev_root->node)
764 btrfs_block_release(fs_info->dev_root,
765 fs_info->dev_root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400766 if (fs_info->tree_root->node)
767 btrfs_block_release(fs_info->tree_root,
768 fs_info->tree_root->node);
769 btrfs_block_release(root, fs_info->sb_buffer);
770 crypto_free_hash(fs_info->hash_tfm);
771 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
772 iput(fs_info->btree_inode);
Chris Mason7eccb902007-04-11 15:53:25 -0400773
774 free_dev_radix(fs_info);
Chris Mason9078a3e2007-04-26 16:46:15 -0400775 btrfs_free_block_groups(root->fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400776 del_fs_roots(fs_info);
777 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400778 kfree(fs_info->tree_root);
779 kobject_unregister(&fs_info->kobj);
Chris Masoneb60cea2007-02-02 09:18:22 -0500780 return 0;
781}
782
Chris Masone20d96d2007-03-22 12:13:20 -0400783void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500784{
Chris Mason7cfcc172007-04-02 14:53:59 -0400785 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500786}
787
Chris Mason35b7e472007-05-02 15:53:43 -0400788void btrfs_btree_balance_dirty(struct btrfs_root *root)
789{
790 balance_dirty_pages_ratelimited(root->fs_info->btree_inode->i_mapping);
791}