Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1 | #include <linux/module.h> |
| 2 | #include <linux/fs.h> |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 3 | #include <linux/blkdev.h> |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 4 | #include <linux/crypto.h> |
| 5 | #include <linux/scatterlist.h> |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 6 | #include <linux/swap.h> |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 7 | #include "ctree.h" |
| 8 | #include "disk-io.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 9 | #include "transaction.h" |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 10 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 11 | static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 12 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 13 | struct btrfs_node *node = btrfs_buffer_node(buf); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 14 | if (buf->b_blocknr != btrfs_header_blocknr(&node->header)) { |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 15 | BUG(); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 16 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 17 | if (root->node && btrfs_header_parentid(&node->header) != |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 18 | btrfs_header_parentid(btrfs_buffer_header(root->node))) { |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 19 | BUG(); |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 20 | } |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 21 | return 0; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 22 | } |
| 23 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 24 | struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 25 | { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 26 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; |
| 27 | int blockbits = root->fs_info->sb->s_blocksize_bits; |
| 28 | unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); |
| 29 | struct page *page; |
| 30 | struct buffer_head *bh; |
| 31 | struct buffer_head *head; |
| 32 | struct buffer_head *ret = NULL; |
| 33 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 34 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 35 | page = find_lock_page(mapping, index); |
| 36 | if (!page) |
| 37 | return NULL; |
| 38 | |
| 39 | if (!page_has_buffers(page)) |
| 40 | goto out_unlock; |
| 41 | |
| 42 | head = page_buffers(page); |
| 43 | bh = head; |
| 44 | do { |
| 45 | if (buffer_mapped(bh) && bh->b_blocknr == blocknr) { |
| 46 | ret = bh; |
| 47 | get_bh(bh); |
| 48 | goto out_unlock; |
| 49 | } |
| 50 | bh = bh->b_this_page; |
| 51 | } while (bh != head); |
| 52 | out_unlock: |
| 53 | unlock_page(page); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 54 | if (ret) { |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 55 | touch_buffer(ret); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 56 | } |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 57 | page_cache_release(page); |
| 58 | return ret; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 59 | } |
| 60 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 61 | struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root, |
| 62 | u64 blocknr) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 63 | { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 64 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; |
| 65 | int blockbits = root->fs_info->sb->s_blocksize_bits; |
| 66 | unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); |
| 67 | struct page *page; |
| 68 | struct buffer_head *bh; |
| 69 | struct buffer_head *head; |
| 70 | struct buffer_head *ret = NULL; |
| 71 | u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 72 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 73 | page = grab_cache_page(mapping, index); |
| 74 | if (!page) |
| 75 | return NULL; |
| 76 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 77 | if (!page_has_buffers(page)) |
| 78 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0); |
| 79 | head = page_buffers(page); |
| 80 | bh = head; |
| 81 | do { |
| 82 | if (!buffer_mapped(bh)) { |
| 83 | bh->b_bdev = root->fs_info->sb->s_bdev; |
| 84 | bh->b_blocknr = first_block; |
| 85 | set_buffer_mapped(bh); |
| 86 | } |
| 87 | if (bh->b_blocknr == blocknr) { |
| 88 | ret = bh; |
| 89 | get_bh(bh); |
| 90 | goto out_unlock; |
| 91 | } |
| 92 | bh = bh->b_this_page; |
| 93 | first_block++; |
| 94 | } while (bh != head); |
| 95 | out_unlock: |
| 96 | unlock_page(page); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 97 | if (ret) |
| 98 | touch_buffer(ret); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 99 | page_cache_release(page); |
| 100 | return ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 101 | } |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 102 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 103 | static sector_t max_block(struct block_device *bdev) |
| 104 | { |
| 105 | sector_t retval = ~((sector_t)0); |
| 106 | loff_t sz = i_size_read(bdev->bd_inode); |
| 107 | |
| 108 | if (sz) { |
| 109 | unsigned int size = block_size(bdev); |
| 110 | unsigned int sizebits = blksize_bits(size); |
| 111 | retval = (sz >> sizebits); |
| 112 | } |
| 113 | return retval; |
| 114 | } |
| 115 | |
| 116 | static int btree_get_block(struct inode *inode, sector_t iblock, |
| 117 | struct buffer_head *bh, int create) |
| 118 | { |
| 119 | if (iblock >= max_block(inode->i_sb->s_bdev)) { |
| 120 | if (create) |
| 121 | return -EIO; |
| 122 | |
| 123 | /* |
| 124 | * for reads, we're just trying to fill a partial page. |
| 125 | * return a hole, they will have to call get_block again |
| 126 | * before they can fill it, and they will get -EIO at that |
| 127 | * time |
| 128 | */ |
| 129 | return 0; |
| 130 | } |
| 131 | bh->b_bdev = inode->i_sb->s_bdev; |
| 132 | bh->b_blocknr = iblock; |
| 133 | set_buffer_mapped(bh); |
| 134 | return 0; |
| 135 | } |
| 136 | |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 137 | int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len, |
| 138 | char *result) |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 139 | { |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 140 | struct scatterlist sg; |
| 141 | struct crypto_hash *tfm = root->fs_info->hash_tfm; |
| 142 | struct hash_desc desc; |
| 143 | int ret; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 144 | |
| 145 | desc.tfm = tfm; |
| 146 | desc.flags = 0; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 147 | sg_init_one(&sg, data, len); |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 148 | spin_lock(&root->fs_info->hash_lock); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 149 | ret = crypto_hash_digest(&desc, &sg, 1, result); |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 150 | spin_unlock(&root->fs_info->hash_lock); |
| 151 | if (ret) { |
| 152 | printk("sha256 digest failed\n"); |
| 153 | } |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 154 | return ret; |
| 155 | } |
| 156 | static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh, |
| 157 | int verify) |
| 158 | { |
| 159 | char result[BTRFS_CSUM_SIZE]; |
| 160 | int ret; |
| 161 | struct btrfs_node *node; |
| 162 | |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 163 | return 0; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 164 | ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE, |
| 165 | bh->b_size - BTRFS_CSUM_SIZE, result); |
| 166 | if (ret) |
| 167 | return ret; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 168 | if (verify) { |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 169 | if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) { |
| 170 | printk("checksum verify failed on %lu\n", |
| 171 | bh->b_blocknr); |
| 172 | return 1; |
| 173 | } |
| 174 | } else { |
| 175 | node = btrfs_buffer_node(bh); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 176 | memcpy(node->header.csum, result, BTRFS_CSUM_SIZE); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 177 | } |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 181 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
| 182 | { |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 183 | struct buffer_head *bh; |
| 184 | struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb); |
| 185 | struct buffer_head *head; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 186 | 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 Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 197 | return block_write_full_page(page, btree_get_block, wbc); |
| 198 | } |
| 199 | |
| 200 | static int btree_readpage(struct file * file, struct page * page) |
| 201 | { |
| 202 | return block_read_full_page(page, btree_get_block); |
| 203 | } |
| 204 | |
| 205 | static struct address_space_operations btree_aops = { |
| 206 | .readpage = btree_readpage, |
| 207 | .writepage = btree_writepage, |
| 208 | .sync_page = block_sync_page, |
| 209 | }; |
| 210 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 211 | struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr) |
| 212 | { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 213 | struct buffer_head *bh = NULL; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 214 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 215 | bh = btrfs_find_create_tree_block(root, blocknr); |
| 216 | if (!bh) |
| 217 | return bh; |
| 218 | lock_buffer(bh); |
| 219 | if (!buffer_uptodate(bh)) { |
| 220 | get_bh(bh); |
| 221 | bh->b_end_io = end_buffer_read_sync; |
| 222 | submit_bh(READ, bh); |
| 223 | wait_on_buffer(bh); |
| 224 | if (!buffer_uptodate(bh)) |
| 225 | goto fail; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 226 | csum_tree_block(root, bh, 1); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 227 | } else { |
| 228 | unlock_buffer(bh); |
| 229 | } |
| 230 | if (check_tree_block(root, bh)) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 231 | BUG(); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 232 | return bh; |
| 233 | fail: |
| 234 | brelse(bh); |
| 235 | return NULL; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 236 | } |
| 237 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 238 | int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 239 | struct buffer_head *buf) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 240 | { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 241 | WARN_ON(atomic_read(&buf->b_count) == 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 242 | mark_buffer_dirty(buf); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 246 | int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 247 | struct buffer_head *buf) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 248 | { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 249 | WARN_ON(atomic_read(&buf->b_count) == 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 250 | clear_buffer_dirty(buf); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 251 | return 0; |
| 252 | } |
| 253 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 254 | static int __setup_root(int blocksize, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 255 | struct btrfs_root *root, |
| 256 | struct btrfs_fs_info *fs_info, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 257 | u64 objectid) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 258 | { |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 259 | root->node = NULL; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 260 | root->commit_root = NULL; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 261 | root->blocksize = blocksize; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 262 | root->ref_cows = 0; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 263 | root->fs_info = fs_info; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 264 | memset(&root->root_key, 0, sizeof(root->root_key)); |
| 265 | memset(&root->root_item, 0, sizeof(root->root_item)); |
| 266 | return 0; |
| 267 | } |
| 268 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 269 | static int find_and_setup_root(int blocksize, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 270 | struct btrfs_root *tree_root, |
| 271 | struct btrfs_fs_info *fs_info, |
| 272 | u64 objectid, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 273 | struct btrfs_root *root) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 274 | { |
| 275 | int ret; |
| 276 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 277 | __setup_root(blocksize, root, fs_info, objectid); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 278 | ret = btrfs_find_last_root(tree_root, objectid, |
| 279 | &root->root_item, &root->root_key); |
| 280 | BUG_ON(ret); |
| 281 | |
| 282 | root->node = read_tree_block(root, |
| 283 | btrfs_root_blocknr(&root->root_item)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 284 | BUG_ON(!root->node); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 285 | return 0; |
| 286 | } |
| 287 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 288 | struct btrfs_root *open_ctree(struct super_block *sb) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 289 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 290 | struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root), |
| 291 | GFP_NOFS); |
| 292 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), |
| 293 | GFP_NOFS); |
| 294 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), |
| 295 | GFP_NOFS); |
| 296 | struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root), |
| 297 | GFP_NOFS); |
| 298 | struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info), |
| 299 | GFP_NOFS); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 300 | int ret; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 301 | struct btrfs_super_block *disk_super; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 302 | |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 303 | init_bit_radix(&fs_info->pinned_radix); |
| 304 | init_bit_radix(&fs_info->pending_del_radix); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 305 | sb_set_blocksize(sb, 4096); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 306 | fs_info->running_transaction = NULL; |
| 307 | fs_info->fs_root = root; |
| 308 | fs_info->tree_root = tree_root; |
| 309 | fs_info->extent_root = extent_root; |
| 310 | fs_info->inode_root = inode_root; |
| 311 | fs_info->last_inode_alloc = 0; |
| 312 | fs_info->last_inode_alloc_dirid = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 313 | fs_info->sb = sb; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 314 | fs_info->btree_inode = new_inode(sb); |
| 315 | fs_info->btree_inode->i_ino = 1; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 316 | fs_info->btree_inode->i_nlink = 1; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 317 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
| 318 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 319 | insert_inode_hash(fs_info->btree_inode); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 320 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 321 | fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC); |
Chris Mason | 30ae846 | 2007-03-29 09:59:15 -0400 | [diff] [blame] | 322 | spin_lock_init(&fs_info->hash_lock); |
Chris Mason | 30ae846 | 2007-03-29 09:59:15 -0400 | [diff] [blame] | 323 | if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) { |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 324 | printk("failed to allocate sha256 hash\n"); |
| 325 | return NULL; |
| 326 | } |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 327 | mutex_init(&fs_info->trans_mutex); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 328 | mutex_init(&fs_info->fs_mutex); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 329 | memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert)); |
| 330 | memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 331 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 332 | __setup_root(sb->s_blocksize, tree_root, |
| 333 | fs_info, BTRFS_ROOT_TREE_OBJECTID); |
| 334 | fs_info->sb_buffer = read_tree_block(tree_root, |
| 335 | BTRFS_SUPER_INFO_OFFSET / |
| 336 | sb->s_blocksize); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 337 | |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 338 | if (!fs_info->sb_buffer) { |
| 339 | printk("failed2\n"); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 340 | return NULL; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 341 | } |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 342 | disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 343 | if (!btrfs_super_root(disk_super)) { |
| 344 | return NULL; |
| 345 | } |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 346 | fs_info->disk_super = disk_super; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 347 | tree_root->node = read_tree_block(tree_root, |
| 348 | btrfs_super_root(disk_super)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 349 | BUG_ON(!tree_root->node); |
| 350 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 351 | mutex_lock(&fs_info->fs_mutex); |
| 352 | ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 353 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 354 | BUG_ON(ret); |
| 355 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 356 | ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 357 | BTRFS_INODE_MAP_OBJECTID, inode_root); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 358 | BUG_ON(ret); |
| 359 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 360 | ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 361 | BTRFS_FS_TREE_OBJECTID, root); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 362 | mutex_unlock(&fs_info->fs_mutex); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 363 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 364 | root->commit_root = root->node; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 365 | get_bh(root->node); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 366 | root->ref_cows = 1; |
Chris Mason | 293ffd5 | 2007-03-20 15:57:25 -0400 | [diff] [blame] | 367 | root->fs_info->generation = root->root_key.offset + 1; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 368 | return root; |
| 369 | } |
| 370 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 371 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 372 | *root) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 373 | { |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 374 | struct buffer_head *bh = root->fs_info->sb_buffer; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 375 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 376 | btrfs_set_super_root(root->fs_info->disk_super, |
| 377 | root->fs_info->tree_root->node->b_blocknr); |
| 378 | lock_buffer(bh); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 379 | WARN_ON(atomic_read(&bh->b_count) < 1); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 380 | clear_buffer_dirty(bh); |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 381 | csum_tree_block(root, bh, 0); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 382 | bh->b_end_io = end_buffer_write_sync; |
| 383 | get_bh(bh); |
| 384 | submit_bh(WRITE, bh); |
| 385 | wait_on_buffer(bh); |
| 386 | if (!buffer_uptodate(bh)) { |
| 387 | WARN_ON(1); |
| 388 | return -EIO; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 389 | } |
| 390 | return 0; |
| 391 | } |
| 392 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 393 | int close_ctree(struct btrfs_root *root) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 394 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 395 | int ret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 396 | struct btrfs_trans_handle *trans; |
| 397 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 398 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 399 | trans = btrfs_start_transaction(root, 1); |
| 400 | btrfs_commit_transaction(trans, root); |
| 401 | /* run commit again to drop the original snapshot */ |
| 402 | trans = btrfs_start_transaction(root, 1); |
| 403 | btrfs_commit_transaction(trans, root); |
| 404 | ret = btrfs_write_and_wait_transaction(NULL, root); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 405 | BUG_ON(ret); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 406 | write_ctree_super(NULL, root); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 407 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 408 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 409 | if (root->node) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 410 | btrfs_block_release(root, root->node); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 411 | if (root->fs_info->extent_root->node) |
| 412 | btrfs_block_release(root->fs_info->extent_root, |
| 413 | root->fs_info->extent_root->node); |
| 414 | if (root->fs_info->inode_root->node) |
| 415 | btrfs_block_release(root->fs_info->inode_root, |
| 416 | root->fs_info->inode_root->node); |
| 417 | if (root->fs_info->tree_root->node) |
| 418 | btrfs_block_release(root->fs_info->tree_root, |
| 419 | root->fs_info->tree_root->node); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 420 | btrfs_block_release(root, root->commit_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 421 | btrfs_block_release(root, root->fs_info->sb_buffer); |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 422 | crypto_free_hash(root->fs_info->hash_tfm); |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame^] | 423 | truncate_inode_pages(root->fs_info->btree_inode->i_mapping, 0); |
| 424 | iput(root->fs_info->btree_inode); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 425 | kfree(root->fs_info->extent_root); |
| 426 | kfree(root->fs_info->inode_root); |
| 427 | kfree(root->fs_info->tree_root); |
| 428 | kfree(root->fs_info); |
| 429 | kfree(root); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 430 | return 0; |
| 431 | } |
| 432 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 433 | void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 434 | { |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame^] | 435 | brelse(buf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 436 | } |
| 437 | |