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 | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 7 | #include <linux/radix-tree.h> |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 8 | #include "ctree.h" |
| 9 | #include "disk-io.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 10 | #include "transaction.h" |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 11 | #include "btrfs_inode.h" |
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 | 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] | 14 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 15 | struct btrfs_node *node = btrfs_buffer_node(buf); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 16 | if (buf->b_blocknr != btrfs_header_blocknr(&node->header)) { |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 17 | BUG(); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 18 | } |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 19 | return 0; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 20 | } |
| 21 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 22 | 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] | 23 | { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 24 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; |
| 25 | int blockbits = root->fs_info->sb->s_blocksize_bits; |
| 26 | unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); |
| 27 | struct page *page; |
| 28 | struct buffer_head *bh; |
| 29 | struct buffer_head *head; |
| 30 | struct buffer_head *ret = NULL; |
| 31 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 32 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 33 | page = find_lock_page(mapping, index); |
| 34 | if (!page) |
| 35 | return NULL; |
| 36 | |
| 37 | if (!page_has_buffers(page)) |
| 38 | goto out_unlock; |
| 39 | |
| 40 | head = page_buffers(page); |
| 41 | bh = head; |
| 42 | do { |
| 43 | if (buffer_mapped(bh) && bh->b_blocknr == blocknr) { |
| 44 | ret = bh; |
| 45 | get_bh(bh); |
| 46 | goto out_unlock; |
| 47 | } |
| 48 | bh = bh->b_this_page; |
| 49 | } while (bh != head); |
| 50 | out_unlock: |
| 51 | unlock_page(page); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 52 | if (ret) { |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 53 | touch_buffer(ret); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 54 | } |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 55 | page_cache_release(page); |
| 56 | return ret; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 57 | } |
| 58 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 59 | struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root, |
| 60 | u64 blocknr) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 61 | { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 62 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; |
| 63 | int blockbits = root->fs_info->sb->s_blocksize_bits; |
| 64 | unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); |
| 65 | struct page *page; |
| 66 | struct buffer_head *bh; |
| 67 | struct buffer_head *head; |
| 68 | struct buffer_head *ret = NULL; |
| 69 | u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 70 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 71 | page = grab_cache_page(mapping, index); |
| 72 | if (!page) |
| 73 | return NULL; |
| 74 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 75 | if (!page_has_buffers(page)) |
| 76 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0); |
| 77 | head = page_buffers(page); |
| 78 | bh = head; |
| 79 | do { |
| 80 | if (!buffer_mapped(bh)) { |
| 81 | bh->b_bdev = root->fs_info->sb->s_bdev; |
| 82 | bh->b_blocknr = first_block; |
| 83 | set_buffer_mapped(bh); |
| 84 | } |
| 85 | if (bh->b_blocknr == blocknr) { |
| 86 | ret = bh; |
| 87 | get_bh(bh); |
| 88 | goto out_unlock; |
| 89 | } |
| 90 | bh = bh->b_this_page; |
| 91 | first_block++; |
| 92 | } while (bh != head); |
| 93 | out_unlock: |
| 94 | unlock_page(page); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 95 | if (ret) |
| 96 | touch_buffer(ret); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 97 | page_cache_release(page); |
| 98 | return ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 99 | } |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 100 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 101 | static sector_t max_block(struct block_device *bdev) |
| 102 | { |
| 103 | sector_t retval = ~((sector_t)0); |
| 104 | loff_t sz = i_size_read(bdev->bd_inode); |
| 105 | |
| 106 | if (sz) { |
| 107 | unsigned int size = block_size(bdev); |
| 108 | unsigned int sizebits = blksize_bits(size); |
| 109 | retval = (sz >> sizebits); |
| 110 | } |
| 111 | return retval; |
| 112 | } |
| 113 | |
| 114 | static int btree_get_block(struct inode *inode, sector_t iblock, |
| 115 | struct buffer_head *bh, int create) |
| 116 | { |
| 117 | if (iblock >= max_block(inode->i_sb->s_bdev)) { |
| 118 | if (create) |
| 119 | return -EIO; |
| 120 | |
| 121 | /* |
| 122 | * for reads, we're just trying to fill a partial page. |
| 123 | * return a hole, they will have to call get_block again |
| 124 | * before they can fill it, and they will get -EIO at that |
| 125 | * time |
| 126 | */ |
| 127 | return 0; |
| 128 | } |
| 129 | bh->b_bdev = inode->i_sb->s_bdev; |
| 130 | bh->b_blocknr = iblock; |
| 131 | set_buffer_mapped(bh); |
| 132 | return 0; |
| 133 | } |
| 134 | |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 135 | int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len, |
| 136 | char *result) |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 137 | { |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 138 | struct scatterlist sg; |
| 139 | struct crypto_hash *tfm = root->fs_info->hash_tfm; |
| 140 | struct hash_desc desc; |
| 141 | int ret; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 142 | |
| 143 | desc.tfm = tfm; |
| 144 | desc.flags = 0; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 145 | sg_init_one(&sg, data, len); |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 146 | spin_lock(&root->fs_info->hash_lock); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 147 | ret = crypto_hash_digest(&desc, &sg, 1, result); |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 148 | spin_unlock(&root->fs_info->hash_lock); |
| 149 | if (ret) { |
| 150 | printk("sha256 digest failed\n"); |
| 151 | } |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 152 | return ret; |
| 153 | } |
| 154 | static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh, |
| 155 | int verify) |
| 156 | { |
| 157 | char result[BTRFS_CSUM_SIZE]; |
| 158 | int ret; |
| 159 | struct btrfs_node *node; |
| 160 | |
| 161 | ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE, |
| 162 | bh->b_size - BTRFS_CSUM_SIZE, result); |
| 163 | if (ret) |
| 164 | return ret; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 165 | if (verify) { |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 166 | if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) { |
| 167 | printk("checksum verify failed on %lu\n", |
| 168 | bh->b_blocknr); |
| 169 | return 1; |
| 170 | } |
| 171 | } else { |
| 172 | node = btrfs_buffer_node(bh); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 173 | memcpy(node->header.csum, result, BTRFS_CSUM_SIZE); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 174 | } |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 178 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
| 179 | { |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 180 | struct buffer_head *bh; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 181 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 182 | struct buffer_head *head; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 183 | if (!page_has_buffers(page)) { |
| 184 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, |
| 185 | (1 << BH_Dirty)|(1 << BH_Uptodate)); |
| 186 | } |
| 187 | head = page_buffers(page); |
| 188 | bh = head; |
| 189 | do { |
| 190 | if (buffer_dirty(bh)) |
| 191 | csum_tree_block(root, bh, 0); |
| 192 | bh = bh->b_this_page; |
| 193 | } while (bh != head); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 194 | return block_write_full_page(page, btree_get_block, wbc); |
| 195 | } |
| 196 | |
| 197 | static int btree_readpage(struct file * file, struct page * page) |
| 198 | { |
| 199 | return block_read_full_page(page, btree_get_block); |
| 200 | } |
| 201 | |
| 202 | static struct address_space_operations btree_aops = { |
| 203 | .readpage = btree_readpage, |
| 204 | .writepage = btree_writepage, |
| 205 | .sync_page = block_sync_page, |
| 206 | }; |
| 207 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 208 | struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr) |
| 209 | { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 210 | struct buffer_head *bh = NULL; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 211 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 212 | bh = btrfs_find_create_tree_block(root, blocknr); |
| 213 | if (!bh) |
| 214 | return bh; |
Chris Mason | 9d64272 | 2007-04-03 11:43:19 -0400 | [diff] [blame] | 215 | if (buffer_uptodate(bh)) |
| 216 | goto uptodate; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 217 | lock_buffer(bh); |
| 218 | if (!buffer_uptodate(bh)) { |
| 219 | get_bh(bh); |
| 220 | bh->b_end_io = end_buffer_read_sync; |
| 221 | submit_bh(READ, bh); |
| 222 | wait_on_buffer(bh); |
| 223 | if (!buffer_uptodate(bh)) |
| 224 | goto fail; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 225 | csum_tree_block(root, bh, 1); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 226 | } else { |
| 227 | unlock_buffer(bh); |
| 228 | } |
Chris Mason | 9d64272 | 2007-04-03 11:43:19 -0400 | [diff] [blame] | 229 | uptodate: |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 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 | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 260 | root->inode = NULL; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 261 | root->commit_root = NULL; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 262 | root->blocksize = blocksize; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 263 | root->ref_cows = 0; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 264 | root->fs_info = fs_info; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 265 | root->objectid = objectid; |
| 266 | root->last_trans = 0; |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 267 | root->highest_inode = 0; |
| 268 | root->last_inode_alloc = 0; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 269 | memset(&root->root_key, 0, sizeof(root->root_key)); |
| 270 | memset(&root->root_item, 0, sizeof(root->root_item)); |
| 271 | return 0; |
| 272 | } |
| 273 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 274 | static int find_and_setup_root(int blocksize, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 275 | struct btrfs_root *tree_root, |
| 276 | struct btrfs_fs_info *fs_info, |
| 277 | u64 objectid, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 278 | struct btrfs_root *root) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 279 | { |
| 280 | int ret; |
| 281 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 282 | __setup_root(blocksize, root, fs_info, objectid); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 283 | ret = btrfs_find_last_root(tree_root, objectid, |
| 284 | &root->root_item, &root->root_key); |
| 285 | BUG_ON(ret); |
| 286 | |
| 287 | root->node = read_tree_block(root, |
| 288 | btrfs_root_blocknr(&root->root_item)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 289 | BUG_ON(!root->node); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 290 | return 0; |
| 291 | } |
| 292 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 293 | struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, |
| 294 | struct btrfs_key *location) |
| 295 | { |
| 296 | struct btrfs_root *root; |
| 297 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 298 | struct btrfs_path *path; |
| 299 | struct btrfs_leaf *l; |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 300 | u64 highest_inode; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 301 | int ret = 0; |
| 302 | |
| 303 | printk("read_fs_root looking for %Lu %Lu %u\n", location->objectid, location->offset, location->flags); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame^] | 304 | root = radix_tree_lookup(&fs_info->fs_roots_radix, |
| 305 | (unsigned long)location->objectid); |
| 306 | if (root) { |
| 307 | printk("found %p in cache\n", root); |
| 308 | return root; |
| 309 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 310 | root = kmalloc(sizeof(*root), GFP_NOFS); |
| 311 | if (!root) { |
| 312 | printk("failed1\n"); |
| 313 | return ERR_PTR(-ENOMEM); |
| 314 | } |
| 315 | if (location->offset == (u64)-1) { |
| 316 | ret = find_and_setup_root(fs_info->sb->s_blocksize, |
| 317 | fs_info->tree_root, fs_info, |
| 318 | location->objectid, root); |
| 319 | if (ret) { |
| 320 | printk("failed2\n"); |
| 321 | kfree(root); |
| 322 | return ERR_PTR(ret); |
| 323 | } |
| 324 | goto insert; |
| 325 | } |
| 326 | |
| 327 | __setup_root(fs_info->sb->s_blocksize, root, fs_info, |
| 328 | location->objectid); |
| 329 | |
| 330 | path = btrfs_alloc_path(); |
| 331 | BUG_ON(!path); |
| 332 | ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); |
| 333 | if (ret != 0) { |
| 334 | printk("internal search_slot gives us %d\n", ret); |
| 335 | if (ret > 0) |
| 336 | ret = -ENOENT; |
| 337 | goto out; |
| 338 | } |
| 339 | l = btrfs_buffer_leaf(path->nodes[0]); |
| 340 | memcpy(&root->root_item, |
| 341 | btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item), |
| 342 | sizeof(root->root_item)); |
| 343 | memcpy(&root->root_key, location, sizeof(*location)); |
| 344 | ret = 0; |
| 345 | out: |
| 346 | btrfs_release_path(root, path); |
| 347 | btrfs_free_path(path); |
| 348 | if (ret) { |
| 349 | kfree(root); |
| 350 | return ERR_PTR(ret); |
| 351 | } |
| 352 | root->node = read_tree_block(root, |
| 353 | btrfs_root_blocknr(&root->root_item)); |
| 354 | BUG_ON(!root->node); |
| 355 | insert: |
| 356 | printk("inserting %p\n", root); |
| 357 | root->ref_cows = 1; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame^] | 358 | ret = radix_tree_insert(&fs_info->fs_roots_radix, |
| 359 | (unsigned long)root->root_key.objectid, |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 360 | root); |
| 361 | if (ret) { |
| 362 | printk("radix_tree_insert gives us %d\n", ret); |
| 363 | brelse(root->node); |
| 364 | kfree(root); |
| 365 | return ERR_PTR(ret); |
| 366 | } |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 367 | ret = btrfs_find_highest_inode(root, &highest_inode); |
| 368 | if (ret == 0) { |
| 369 | root->highest_inode = highest_inode; |
| 370 | root->last_inode_alloc = highest_inode; |
| 371 | printk("highest inode is %Lu\n", highest_inode); |
| 372 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 373 | printk("all worked\n"); |
| 374 | return root; |
| 375 | } |
| 376 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 377 | struct btrfs_root *open_ctree(struct super_block *sb) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 378 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 379 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), |
| 380 | GFP_NOFS); |
| 381 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), |
| 382 | GFP_NOFS); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 383 | struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info), |
| 384 | GFP_NOFS); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 385 | int ret; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 386 | struct btrfs_super_block *disk_super; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 387 | |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 388 | init_bit_radix(&fs_info->pinned_radix); |
| 389 | init_bit_radix(&fs_info->pending_del_radix); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 390 | INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 391 | sb_set_blocksize(sb, 4096); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 392 | fs_info->running_transaction = NULL; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 393 | fs_info->tree_root = tree_root; |
| 394 | fs_info->extent_root = extent_root; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 395 | fs_info->sb = sb; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 396 | fs_info->btree_inode = new_inode(sb); |
| 397 | fs_info->btree_inode->i_ino = 1; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 398 | fs_info->btree_inode->i_nlink = 1; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 399 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
| 400 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 401 | BTRFS_I(fs_info->btree_inode)->root = tree_root; |
| 402 | memset(&BTRFS_I(fs_info->btree_inode)->location, 0, |
| 403 | sizeof(struct btrfs_key)); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 404 | insert_inode_hash(fs_info->btree_inode); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 405 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 406 | fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC); |
Chris Mason | 30ae846 | 2007-03-29 09:59:15 -0400 | [diff] [blame] | 407 | spin_lock_init(&fs_info->hash_lock); |
Chris Mason | 30ae846 | 2007-03-29 09:59:15 -0400 | [diff] [blame] | 408 | if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) { |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 409 | printk("failed to allocate sha256 hash\n"); |
| 410 | return NULL; |
| 411 | } |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 412 | mutex_init(&fs_info->trans_mutex); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 413 | mutex_init(&fs_info->fs_mutex); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 414 | memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert)); |
| 415 | memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 416 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 417 | __setup_root(sb->s_blocksize, tree_root, |
| 418 | fs_info, BTRFS_ROOT_TREE_OBJECTID); |
| 419 | fs_info->sb_buffer = read_tree_block(tree_root, |
| 420 | BTRFS_SUPER_INFO_OFFSET / |
| 421 | sb->s_blocksize); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 422 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 423 | if (!fs_info->sb_buffer) |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 424 | return NULL; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 425 | disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 426 | if (!btrfs_super_root(disk_super)) |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 427 | return NULL; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 428 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 429 | fs_info->disk_super = disk_super; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 430 | tree_root->node = read_tree_block(tree_root, |
| 431 | btrfs_super_root(disk_super)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 432 | BUG_ON(!tree_root->node); |
| 433 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 434 | mutex_lock(&fs_info->fs_mutex); |
| 435 | ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 436 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 437 | BUG_ON(ret); |
| 438 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 439 | fs_info->generation = btrfs_super_generation(disk_super) + 1; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 440 | memset(&fs_info->kobj, 0, sizeof(fs_info->kobj)); |
| 441 | kobj_set_kset_s(fs_info, btrfs_subsys); |
| 442 | kobject_set_name(&fs_info->kobj, "%s", sb->s_id); |
| 443 | kobject_register(&fs_info->kobj); |
Chris Mason | 5be6f7f | 2007-04-05 13:35:25 -0400 | [diff] [blame] | 444 | mutex_unlock(&fs_info->fs_mutex); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 445 | return tree_root; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 446 | } |
| 447 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 448 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 449 | *root) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 450 | { |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 451 | struct buffer_head *bh = root->fs_info->sb_buffer; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 452 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 453 | btrfs_set_super_root(root->fs_info->disk_super, |
| 454 | root->fs_info->tree_root->node->b_blocknr); |
| 455 | lock_buffer(bh); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 456 | WARN_ON(atomic_read(&bh->b_count) < 1); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 457 | clear_buffer_dirty(bh); |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 458 | csum_tree_block(root, bh, 0); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 459 | bh->b_end_io = end_buffer_write_sync; |
| 460 | get_bh(bh); |
| 461 | submit_bh(WRITE, bh); |
| 462 | wait_on_buffer(bh); |
| 463 | if (!buffer_uptodate(bh)) { |
| 464 | WARN_ON(1); |
| 465 | return -EIO; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 466 | } |
| 467 | return 0; |
| 468 | } |
| 469 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame^] | 470 | static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) |
| 471 | { |
| 472 | radix_tree_delete(&fs_info->fs_roots_radix, |
| 473 | (unsigned long)root->root_key.objectid); |
| 474 | if (root->inode) |
| 475 | iput(root->inode); |
| 476 | if (root->node) |
| 477 | brelse(root->node); |
| 478 | if (root->commit_root) |
| 479 | brelse(root->commit_root); |
| 480 | kfree(root); |
| 481 | return 0; |
| 482 | } |
| 483 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 484 | int del_fs_roots(struct btrfs_fs_info *fs_info) |
| 485 | { |
| 486 | int ret; |
| 487 | struct btrfs_root *gang[8]; |
| 488 | int i; |
| 489 | |
| 490 | while(1) { |
| 491 | ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, |
| 492 | (void **)gang, 0, |
| 493 | ARRAY_SIZE(gang)); |
| 494 | if (!ret) |
| 495 | break; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame^] | 496 | for (i = 0; i < ret; i++) |
| 497 | free_fs_root(fs_info, gang[i]); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 498 | } |
| 499 | return 0; |
| 500 | } |
| 501 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 502 | int close_ctree(struct btrfs_root *root) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 503 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 504 | int ret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 505 | struct btrfs_trans_handle *trans; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 506 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 507 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 508 | mutex_lock(&fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 509 | trans = btrfs_start_transaction(root, 1); |
| 510 | btrfs_commit_transaction(trans, root); |
| 511 | /* run commit again to drop the original snapshot */ |
| 512 | trans = btrfs_start_transaction(root, 1); |
| 513 | btrfs_commit_transaction(trans, root); |
| 514 | ret = btrfs_write_and_wait_transaction(NULL, root); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 515 | BUG_ON(ret); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 516 | write_ctree_super(NULL, root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 517 | mutex_unlock(&fs_info->fs_mutex); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 518 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 519 | if (fs_info->extent_root->node) |
| 520 | btrfs_block_release(fs_info->extent_root, |
| 521 | fs_info->extent_root->node); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 522 | if (fs_info->tree_root->node) |
| 523 | btrfs_block_release(fs_info->tree_root, |
| 524 | fs_info->tree_root->node); |
| 525 | btrfs_block_release(root, fs_info->sb_buffer); |
| 526 | crypto_free_hash(fs_info->hash_tfm); |
| 527 | truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); |
| 528 | iput(fs_info->btree_inode); |
| 529 | del_fs_roots(fs_info); |
| 530 | kfree(fs_info->extent_root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 531 | kfree(fs_info->tree_root); |
| 532 | kobject_unregister(&fs_info->kobj); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 533 | return 0; |
| 534 | } |
| 535 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 536 | void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 537 | { |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 538 | brelse(buf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 539 | } |
| 540 | |