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 | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 13 | struct dev_lookup { |
| 14 | u64 block_start; |
| 15 | u64 num_blocks; |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 16 | u64 device_id; |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 17 | struct block_device *bdev; |
| 18 | }; |
| 19 | |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 20 | int btrfs_insert_dev_radix(struct btrfs_root *root, |
| 21 | struct block_device *bdev, |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 22 | u64 device_id, |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 23 | u64 block_start, |
| 24 | u64 num_blocks) |
| 25 | { |
| 26 | struct dev_lookup *lookup; |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 27 | int ret; |
| 28 | |
| 29 | lookup = kmalloc(sizeof(*lookup), GFP_NOFS); |
| 30 | if (!lookup) |
| 31 | return -ENOMEM; |
| 32 | lookup->block_start = block_start; |
| 33 | lookup->num_blocks = num_blocks; |
| 34 | lookup->bdev = bdev; |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 35 | lookup->device_id = device_id; |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 36 | |
| 37 | ret = radix_tree_insert(&root->fs_info->dev_radix, block_start + |
| 38 | num_blocks - 1, lookup); |
| 39 | return ret; |
| 40 | } |
| 41 | |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 42 | u64 bh_blocknr(struct buffer_head *bh) |
| 43 | { |
| 44 | int blkbits = bh->b_page->mapping->host->i_blkbits; |
| 45 | u64 blocknr = bh->b_page->index << (PAGE_CACHE_SHIFT - blkbits); |
| 46 | unsigned long offset; |
| 47 | |
| 48 | if (PageHighMem(bh->b_page)) |
| 49 | offset = (unsigned long)bh->b_data; |
| 50 | else |
| 51 | offset = bh->b_data - (char *)page_address(bh->b_page); |
| 52 | blocknr += offset >> (PAGE_CACHE_SHIFT - blkbits); |
| 53 | return blocknr; |
| 54 | } |
| 55 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 56 | 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] | 57 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 58 | struct btrfs_node *node = btrfs_buffer_node(buf); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 59 | if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) { |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 60 | printk(KERN_CRIT "bh_blocknr(buf) is %Lu, header is %Lu\n", |
| 61 | bh_blocknr(buf), btrfs_header_blocknr(&node->header)); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 62 | BUG(); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 63 | } |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 64 | return 0; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 65 | } |
| 66 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 67 | 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] | 68 | { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 69 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; |
| 70 | int blockbits = root->fs_info->sb->s_blocksize_bits; |
| 71 | unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); |
| 72 | struct page *page; |
| 73 | struct buffer_head *bh; |
| 74 | struct buffer_head *head; |
| 75 | struct buffer_head *ret = NULL; |
| 76 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 77 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 78 | page = find_lock_page(mapping, index); |
| 79 | if (!page) |
| 80 | return NULL; |
| 81 | |
| 82 | if (!page_has_buffers(page)) |
| 83 | goto out_unlock; |
| 84 | |
| 85 | head = page_buffers(page); |
| 86 | bh = head; |
| 87 | do { |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 88 | if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 89 | ret = bh; |
| 90 | get_bh(bh); |
| 91 | goto out_unlock; |
| 92 | } |
| 93 | bh = bh->b_this_page; |
| 94 | } while (bh != head); |
| 95 | out_unlock: |
| 96 | unlock_page(page); |
| 97 | page_cache_release(page); |
| 98 | return ret; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 99 | } |
| 100 | |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 101 | int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 102 | u64 logical) |
| 103 | { |
| 104 | struct dev_lookup *lookup[2]; |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 105 | |
| 106 | int ret; |
| 107 | |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 108 | if (logical == 0) { |
| 109 | bh->b_bdev = NULL; |
| 110 | bh->b_blocknr = 0; |
| 111 | set_buffer_mapped(bh); |
| 112 | return 0; |
| 113 | } |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 114 | root = root->fs_info->dev_root; |
| 115 | ret = radix_tree_gang_lookup(&root->fs_info->dev_radix, |
| 116 | (void **)lookup, |
| 117 | (unsigned long)logical, |
| 118 | ARRAY_SIZE(lookup)); |
| 119 | if (ret == 0 || lookup[0]->block_start > logical || |
| 120 | lookup[0]->block_start + lookup[0]->num_blocks <= logical) { |
| 121 | ret = -ENOENT; |
| 122 | goto out; |
| 123 | } |
| 124 | bh->b_bdev = lookup[0]->bdev; |
| 125 | bh->b_blocknr = logical - lookup[0]->block_start; |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 126 | set_buffer_mapped(bh); |
| 127 | ret = 0; |
| 128 | out: |
| 129 | return ret; |
| 130 | } |
| 131 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 132 | struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root, |
| 133 | u64 blocknr) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 134 | { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 135 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; |
| 136 | int blockbits = root->fs_info->sb->s_blocksize_bits; |
| 137 | unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); |
| 138 | struct page *page; |
| 139 | struct buffer_head *bh; |
| 140 | struct buffer_head *head; |
| 141 | struct buffer_head *ret = NULL; |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 142 | int err; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 143 | u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 144 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 145 | page = grab_cache_page(mapping, index); |
| 146 | if (!page) |
| 147 | return NULL; |
| 148 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 149 | if (!page_has_buffers(page)) |
| 150 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0); |
| 151 | head = page_buffers(page); |
| 152 | bh = head; |
| 153 | do { |
| 154 | if (!buffer_mapped(bh)) { |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 155 | err = btrfs_map_bh_to_logical(root, bh, first_block); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 156 | BUG_ON(err); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 157 | } |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 158 | if (bh_blocknr(bh) == blocknr) { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 159 | ret = bh; |
| 160 | get_bh(bh); |
| 161 | goto out_unlock; |
| 162 | } |
| 163 | bh = bh->b_this_page; |
| 164 | first_block++; |
| 165 | } while (bh != head); |
| 166 | out_unlock: |
| 167 | unlock_page(page); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 168 | if (ret) |
| 169 | touch_buffer(ret); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 170 | page_cache_release(page); |
| 171 | return ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 172 | } |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 173 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 174 | static int btree_get_block(struct inode *inode, sector_t iblock, |
| 175 | struct buffer_head *bh, int create) |
| 176 | { |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 177 | int err; |
| 178 | struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root; |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 179 | err = btrfs_map_bh_to_logical(root, bh, iblock); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 180 | return err; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 181 | } |
| 182 | |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 183 | int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len, |
| 184 | char *result) |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 185 | { |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 186 | struct scatterlist sg; |
| 187 | struct crypto_hash *tfm = root->fs_info->hash_tfm; |
| 188 | struct hash_desc desc; |
| 189 | int ret; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 190 | |
| 191 | desc.tfm = tfm; |
| 192 | desc.flags = 0; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 193 | sg_init_one(&sg, data, len); |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 194 | spin_lock(&root->fs_info->hash_lock); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 195 | ret = crypto_hash_digest(&desc, &sg, 1, result); |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 196 | spin_unlock(&root->fs_info->hash_lock); |
| 197 | if (ret) { |
| 198 | printk("sha256 digest failed\n"); |
| 199 | } |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 200 | return ret; |
| 201 | } |
| 202 | static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh, |
| 203 | int verify) |
| 204 | { |
| 205 | char result[BTRFS_CSUM_SIZE]; |
| 206 | int ret; |
| 207 | struct btrfs_node *node; |
| 208 | |
| 209 | ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE, |
| 210 | bh->b_size - BTRFS_CSUM_SIZE, result); |
| 211 | if (ret) |
| 212 | return ret; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 213 | if (verify) { |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 214 | if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) { |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 215 | printk("checksum verify failed on %Lu\n", |
| 216 | bh_blocknr(bh)); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 217 | return 1; |
| 218 | } |
| 219 | } else { |
| 220 | node = btrfs_buffer_node(bh); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 221 | memcpy(node->header.csum, result, BTRFS_CSUM_SIZE); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 222 | } |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 223 | return 0; |
| 224 | } |
| 225 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 226 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
| 227 | { |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 228 | struct buffer_head *bh; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 229 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 230 | struct buffer_head *head; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 231 | if (!page_has_buffers(page)) { |
| 232 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, |
| 233 | (1 << BH_Dirty)|(1 << BH_Uptodate)); |
| 234 | } |
| 235 | head = page_buffers(page); |
| 236 | bh = head; |
| 237 | do { |
| 238 | if (buffer_dirty(bh)) |
| 239 | csum_tree_block(root, bh, 0); |
| 240 | bh = bh->b_this_page; |
| 241 | } while (bh != head); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 242 | return block_write_full_page(page, btree_get_block, wbc); |
| 243 | } |
| 244 | |
| 245 | static int btree_readpage(struct file * file, struct page * page) |
| 246 | { |
| 247 | return block_read_full_page(page, btree_get_block); |
| 248 | } |
| 249 | |
| 250 | static struct address_space_operations btree_aops = { |
| 251 | .readpage = btree_readpage, |
| 252 | .writepage = btree_writepage, |
| 253 | .sync_page = block_sync_page, |
| 254 | }; |
| 255 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 256 | struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr) |
| 257 | { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 258 | struct buffer_head *bh = NULL; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 259 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 260 | bh = btrfs_find_create_tree_block(root, blocknr); |
| 261 | if (!bh) |
| 262 | return bh; |
Chris Mason | 9d64272 | 2007-04-03 11:43:19 -0400 | [diff] [blame] | 263 | if (buffer_uptodate(bh)) |
| 264 | goto uptodate; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 265 | lock_buffer(bh); |
| 266 | if (!buffer_uptodate(bh)) { |
| 267 | get_bh(bh); |
| 268 | bh->b_end_io = end_buffer_read_sync; |
| 269 | submit_bh(READ, bh); |
| 270 | wait_on_buffer(bh); |
| 271 | if (!buffer_uptodate(bh)) |
| 272 | goto fail; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 273 | csum_tree_block(root, bh, 1); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 274 | } else { |
| 275 | unlock_buffer(bh); |
| 276 | } |
Chris Mason | 9d64272 | 2007-04-03 11:43:19 -0400 | [diff] [blame] | 277 | uptodate: |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 278 | if (check_tree_block(root, bh)) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 279 | BUG(); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 280 | return bh; |
| 281 | fail: |
| 282 | brelse(bh); |
| 283 | return NULL; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 284 | } |
| 285 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 286 | 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] | 287 | struct buffer_head *buf) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 288 | { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 289 | WARN_ON(atomic_read(&buf->b_count) == 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 290 | mark_buffer_dirty(buf); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 294 | 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] | 295 | struct buffer_head *buf) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 296 | { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 297 | WARN_ON(atomic_read(&buf->b_count) == 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 298 | clear_buffer_dirty(buf); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 302 | static int __setup_root(int blocksize, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 303 | struct btrfs_root *root, |
| 304 | struct btrfs_fs_info *fs_info, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 305 | u64 objectid) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 306 | { |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 307 | root->node = NULL; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 308 | root->inode = NULL; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 309 | root->commit_root = NULL; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 310 | root->blocksize = blocksize; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 311 | root->ref_cows = 0; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 312 | root->fs_info = fs_info; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 313 | root->objectid = objectid; |
| 314 | root->last_trans = 0; |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 315 | root->highest_inode = 0; |
| 316 | root->last_inode_alloc = 0; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 317 | memset(&root->root_key, 0, sizeof(root->root_key)); |
| 318 | memset(&root->root_item, 0, sizeof(root->root_item)); |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 319 | root->root_key.objectid = objectid; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 320 | return 0; |
| 321 | } |
| 322 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 323 | static int find_and_setup_root(int blocksize, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 324 | struct btrfs_root *tree_root, |
| 325 | struct btrfs_fs_info *fs_info, |
| 326 | u64 objectid, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 327 | struct btrfs_root *root) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 328 | { |
| 329 | int ret; |
| 330 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 331 | __setup_root(blocksize, root, fs_info, objectid); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 332 | ret = btrfs_find_last_root(tree_root, objectid, |
| 333 | &root->root_item, &root->root_key); |
| 334 | BUG_ON(ret); |
| 335 | |
| 336 | root->node = read_tree_block(root, |
| 337 | btrfs_root_blocknr(&root->root_item)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 338 | BUG_ON(!root->node); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 342 | struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, |
| 343 | struct btrfs_key *location) |
| 344 | { |
| 345 | struct btrfs_root *root; |
| 346 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 347 | struct btrfs_path *path; |
| 348 | struct btrfs_leaf *l; |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 349 | u64 highest_inode; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 350 | int ret = 0; |
| 351 | |
| 352 | 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] | 353 | root = radix_tree_lookup(&fs_info->fs_roots_radix, |
| 354 | (unsigned long)location->objectid); |
| 355 | if (root) { |
| 356 | printk("found %p in cache\n", root); |
| 357 | return root; |
| 358 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 359 | root = kmalloc(sizeof(*root), GFP_NOFS); |
| 360 | if (!root) { |
| 361 | printk("failed1\n"); |
| 362 | return ERR_PTR(-ENOMEM); |
| 363 | } |
| 364 | if (location->offset == (u64)-1) { |
| 365 | ret = find_and_setup_root(fs_info->sb->s_blocksize, |
| 366 | fs_info->tree_root, fs_info, |
| 367 | location->objectid, root); |
| 368 | if (ret) { |
| 369 | printk("failed2\n"); |
| 370 | kfree(root); |
| 371 | return ERR_PTR(ret); |
| 372 | } |
| 373 | goto insert; |
| 374 | } |
| 375 | |
| 376 | __setup_root(fs_info->sb->s_blocksize, root, fs_info, |
| 377 | location->objectid); |
| 378 | |
| 379 | path = btrfs_alloc_path(); |
| 380 | BUG_ON(!path); |
| 381 | ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); |
| 382 | if (ret != 0) { |
| 383 | printk("internal search_slot gives us %d\n", ret); |
| 384 | if (ret > 0) |
| 385 | ret = -ENOENT; |
| 386 | goto out; |
| 387 | } |
| 388 | l = btrfs_buffer_leaf(path->nodes[0]); |
| 389 | memcpy(&root->root_item, |
| 390 | btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item), |
| 391 | sizeof(root->root_item)); |
| 392 | memcpy(&root->root_key, location, sizeof(*location)); |
| 393 | ret = 0; |
| 394 | out: |
| 395 | btrfs_release_path(root, path); |
| 396 | btrfs_free_path(path); |
| 397 | if (ret) { |
| 398 | kfree(root); |
| 399 | return ERR_PTR(ret); |
| 400 | } |
| 401 | root->node = read_tree_block(root, |
| 402 | btrfs_root_blocknr(&root->root_item)); |
| 403 | BUG_ON(!root->node); |
| 404 | insert: |
| 405 | printk("inserting %p\n", root); |
| 406 | root->ref_cows = 1; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 407 | ret = radix_tree_insert(&fs_info->fs_roots_radix, |
| 408 | (unsigned long)root->root_key.objectid, |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 409 | root); |
| 410 | if (ret) { |
| 411 | printk("radix_tree_insert gives us %d\n", ret); |
| 412 | brelse(root->node); |
| 413 | kfree(root); |
| 414 | return ERR_PTR(ret); |
| 415 | } |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 416 | ret = btrfs_find_highest_inode(root, &highest_inode); |
| 417 | if (ret == 0) { |
| 418 | root->highest_inode = highest_inode; |
| 419 | root->last_inode_alloc = highest_inode; |
| 420 | printk("highest inode is %Lu\n", highest_inode); |
| 421 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 422 | printk("all worked\n"); |
| 423 | return root; |
| 424 | } |
| 425 | |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 426 | static int btrfs_open_disk(struct btrfs_root *root, u64 device_id, |
| 427 | u64 block_start, u64 num_blocks, |
| 428 | char *filename, int name_len) |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 429 | { |
| 430 | char *null_filename; |
| 431 | struct block_device *bdev; |
| 432 | int ret; |
| 433 | |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 434 | null_filename = kmalloc(name_len + 1, GFP_NOFS); |
| 435 | if (!null_filename) |
| 436 | return -ENOMEM; |
| 437 | memcpy(null_filename, filename, name_len); |
| 438 | null_filename[name_len] = '\0'; |
| 439 | |
| 440 | bdev = open_bdev_excl(null_filename, O_RDWR, root->fs_info->sb); |
| 441 | if (IS_ERR(bdev)) { |
| 442 | ret = PTR_ERR(bdev); |
| 443 | goto out; |
| 444 | } |
| 445 | set_blocksize(bdev, root->fs_info->sb->s_blocksize); |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 446 | ret = btrfs_insert_dev_radix(root, bdev, device_id, |
| 447 | block_start, num_blocks); |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 448 | BUG_ON(ret); |
| 449 | ret = 0; |
| 450 | out: |
| 451 | kfree(null_filename); |
| 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | static int read_device_info(struct btrfs_root *root) |
| 456 | { |
| 457 | struct btrfs_path *path; |
| 458 | int ret; |
| 459 | struct btrfs_key key; |
| 460 | struct btrfs_leaf *leaf; |
| 461 | struct btrfs_device_item *dev_item; |
| 462 | int nritems; |
| 463 | int slot; |
| 464 | |
| 465 | root = root->fs_info->dev_root; |
| 466 | |
| 467 | path = btrfs_alloc_path(); |
| 468 | if (!path) |
| 469 | return -ENOMEM; |
| 470 | key.objectid = 0; |
| 471 | key.offset = 0; |
| 472 | key.flags = 0; |
| 473 | btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY); |
| 474 | |
| 475 | mutex_lock(&root->fs_info->fs_mutex); |
| 476 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 477 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 478 | nritems = btrfs_header_nritems(&leaf->header); |
| 479 | while(1) { |
| 480 | slot = path->slots[0]; |
| 481 | if (slot >= nritems) { |
| 482 | ret = btrfs_next_leaf(root, path); |
| 483 | if (ret) |
| 484 | break; |
| 485 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 486 | nritems = btrfs_header_nritems(&leaf->header); |
| 487 | slot = path->slots[0]; |
| 488 | } |
| 489 | btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key); |
| 490 | if (btrfs_key_type(&key) != BTRFS_DEV_ITEM_KEY) { |
| 491 | path->slots[0]++; |
| 492 | continue; |
| 493 | } |
| 494 | dev_item = btrfs_item_ptr(leaf, slot, struct btrfs_device_item); |
| 495 | printk("found key %Lu %Lu\n", key.objectid, key.offset); |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 496 | if (btrfs_device_id(dev_item) != |
| 497 | btrfs_super_device_id(root->fs_info->disk_super)) { |
| 498 | ret = btrfs_open_disk(root, btrfs_device_id(dev_item), |
| 499 | key.objectid, key.offset, |
| 500 | (char *)(dev_item + 1), |
| 501 | btrfs_device_pathlen(dev_item)); |
| 502 | BUG_ON(ret); |
| 503 | } |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 504 | path->slots[0]++; |
| 505 | } |
| 506 | btrfs_free_path(path); |
| 507 | mutex_unlock(&root->fs_info->fs_mutex); |
| 508 | return 0; |
| 509 | } |
| 510 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 511 | struct btrfs_root *open_ctree(struct super_block *sb) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 512 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 513 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), |
| 514 | GFP_NOFS); |
Chris Mason | 0bd93ba | 2007-04-11 13:57:44 -0400 | [diff] [blame] | 515 | struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root), |
| 516 | GFP_NOFS); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 517 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), |
| 518 | GFP_NOFS); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 519 | struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info), |
| 520 | GFP_NOFS); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 521 | int ret; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 522 | struct btrfs_super_block *disk_super; |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 523 | struct dev_lookup *dev_lookup; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 524 | |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 525 | init_bit_radix(&fs_info->pinned_radix); |
| 526 | init_bit_radix(&fs_info->pending_del_radix); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 527 | INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 528 | INIT_RADIX_TREE(&fs_info->dev_radix, GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 529 | INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 530 | INIT_LIST_HEAD(&fs_info->trans_list); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 531 | sb_set_blocksize(sb, 4096); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 532 | fs_info->running_transaction = NULL; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 533 | fs_info->tree_root = tree_root; |
| 534 | fs_info->extent_root = extent_root; |
Chris Mason | 0bd93ba | 2007-04-11 13:57:44 -0400 | [diff] [blame] | 535 | fs_info->dev_root = dev_root; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 536 | fs_info->sb = sb; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 537 | fs_info->btree_inode = new_inode(sb); |
| 538 | fs_info->btree_inode->i_ino = 1; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 539 | fs_info->btree_inode->i_nlink = 1; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 540 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
| 541 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 542 | fs_info->do_barriers = 1; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 543 | fs_info->extent_tree_insert_nr = 0; |
| 544 | fs_info->extent_tree_prealloc_nr = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 545 | BTRFS_I(fs_info->btree_inode)->root = tree_root; |
| 546 | memset(&BTRFS_I(fs_info->btree_inode)->location, 0, |
| 547 | sizeof(struct btrfs_key)); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 548 | insert_inode_hash(fs_info->btree_inode); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 549 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 550 | fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC); |
Chris Mason | 30ae846 | 2007-03-29 09:59:15 -0400 | [diff] [blame] | 551 | spin_lock_init(&fs_info->hash_lock); |
Chris Mason | 30ae846 | 2007-03-29 09:59:15 -0400 | [diff] [blame] | 552 | if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) { |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 553 | printk("failed to allocate sha256 hash\n"); |
| 554 | return NULL; |
| 555 | } |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 556 | mutex_init(&fs_info->trans_mutex); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 557 | mutex_init(&fs_info->fs_mutex); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 558 | fs_info->block_group_cache = NULL; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 559 | |
Chris Mason | 0bd93ba | 2007-04-11 13:57:44 -0400 | [diff] [blame] | 560 | __setup_root(sb->s_blocksize, dev_root, |
| 561 | fs_info, BTRFS_DEV_TREE_OBJECTID); |
| 562 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 563 | __setup_root(sb->s_blocksize, tree_root, |
| 564 | fs_info, BTRFS_ROOT_TREE_OBJECTID); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 565 | |
| 566 | dev_lookup = kmalloc(sizeof(*dev_lookup), GFP_NOFS); |
| 567 | dev_lookup->block_start = 0; |
| 568 | dev_lookup->num_blocks = (u32)-2; |
| 569 | dev_lookup->bdev = sb->s_bdev; |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 570 | dev_lookup->device_id = 0; |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 571 | ret = radix_tree_insert(&fs_info->dev_radix, (u32)-2, dev_lookup); |
| 572 | BUG_ON(ret); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 573 | fs_info->sb_buffer = read_tree_block(tree_root, |
| 574 | BTRFS_SUPER_INFO_OFFSET / |
| 575 | sb->s_blocksize); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 576 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 577 | if (!fs_info->sb_buffer) |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 578 | return NULL; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 579 | disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 580 | if (!btrfs_super_root(disk_super)) |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 581 | return NULL; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 582 | |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 583 | i_size_write(fs_info->btree_inode, |
| 584 | btrfs_super_total_blocks(disk_super) << |
| 585 | fs_info->btree_inode->i_blkbits); |
| 586 | |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 587 | radix_tree_delete(&fs_info->dev_radix, (u32)-2); |
| 588 | dev_lookup->block_start = btrfs_super_device_block_start(disk_super); |
| 589 | dev_lookup->num_blocks = btrfs_super_device_num_blocks(disk_super); |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 590 | dev_lookup->device_id = btrfs_super_device_id(disk_super); |
| 591 | |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 592 | ret = radix_tree_insert(&fs_info->dev_radix, |
| 593 | dev_lookup->block_start + |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 594 | dev_lookup->num_blocks - 1, dev_lookup); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 595 | BUG_ON(ret); |
| 596 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 597 | fs_info->disk_super = disk_super; |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 598 | |
Chris Mason | 0bd93ba | 2007-04-11 13:57:44 -0400 | [diff] [blame] | 599 | dev_root->node = read_tree_block(tree_root, |
| 600 | btrfs_super_device_root(disk_super)); |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 601 | |
| 602 | ret = read_device_info(dev_root); |
| 603 | BUG_ON(ret); |
| 604 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 605 | tree_root->node = read_tree_block(tree_root, |
| 606 | btrfs_super_root(disk_super)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 607 | BUG_ON(!tree_root->node); |
| 608 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 609 | mutex_lock(&fs_info->fs_mutex); |
| 610 | ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 611 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 612 | BUG_ON(ret); |
| 613 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 614 | btrfs_read_block_groups(extent_root); |
| 615 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 616 | fs_info->generation = btrfs_super_generation(disk_super) + 1; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 617 | memset(&fs_info->kobj, 0, sizeof(fs_info->kobj)); |
| 618 | kobj_set_kset_s(fs_info, btrfs_subsys); |
| 619 | kobject_set_name(&fs_info->kobj, "%s", sb->s_id); |
| 620 | kobject_register(&fs_info->kobj); |
Chris Mason | 5be6f7f | 2007-04-05 13:35:25 -0400 | [diff] [blame] | 621 | mutex_unlock(&fs_info->fs_mutex); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 622 | return tree_root; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 623 | } |
| 624 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 625 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 626 | *root) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 627 | { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 628 | int ret; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 629 | struct buffer_head *bh = root->fs_info->sb_buffer; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 630 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 631 | btrfs_set_super_root(root->fs_info->disk_super, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 632 | bh_blocknr(root->fs_info->tree_root->node)); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 633 | lock_buffer(bh); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 634 | WARN_ON(atomic_read(&bh->b_count) < 1); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 635 | clear_buffer_dirty(bh); |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 636 | csum_tree_block(root, bh, 0); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 637 | bh->b_end_io = end_buffer_write_sync; |
| 638 | get_bh(bh); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 639 | if (root->fs_info->do_barriers) |
| 640 | ret = submit_bh(WRITE_BARRIER, bh); |
| 641 | else |
| 642 | ret = submit_bh(WRITE, bh); |
| 643 | if (ret == -EOPNOTSUPP) { |
| 644 | set_buffer_uptodate(bh); |
| 645 | root->fs_info->do_barriers = 0; |
| 646 | ret = submit_bh(WRITE, bh); |
| 647 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 648 | wait_on_buffer(bh); |
| 649 | if (!buffer_uptodate(bh)) { |
| 650 | WARN_ON(1); |
| 651 | return -EIO; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 652 | } |
| 653 | return 0; |
| 654 | } |
| 655 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 656 | static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) |
| 657 | { |
| 658 | radix_tree_delete(&fs_info->fs_roots_radix, |
| 659 | (unsigned long)root->root_key.objectid); |
| 660 | if (root->inode) |
| 661 | iput(root->inode); |
| 662 | if (root->node) |
| 663 | brelse(root->node); |
| 664 | if (root->commit_root) |
| 665 | brelse(root->commit_root); |
| 666 | kfree(root); |
| 667 | return 0; |
| 668 | } |
| 669 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 670 | int del_fs_roots(struct btrfs_fs_info *fs_info) |
| 671 | { |
| 672 | int ret; |
| 673 | struct btrfs_root *gang[8]; |
| 674 | int i; |
| 675 | |
| 676 | while(1) { |
| 677 | ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, |
| 678 | (void **)gang, 0, |
| 679 | ARRAY_SIZE(gang)); |
| 680 | if (!ret) |
| 681 | break; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 682 | for (i = 0; i < ret; i++) |
| 683 | free_fs_root(fs_info, gang[i]); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 684 | } |
| 685 | return 0; |
| 686 | } |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 687 | |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 688 | static int free_dev_radix(struct btrfs_fs_info *fs_info) |
| 689 | { |
| 690 | struct dev_lookup *lookup[8]; |
| 691 | struct block_device *super_bdev = fs_info->sb->s_bdev; |
| 692 | int ret; |
| 693 | int i; |
| 694 | while(1) { |
| 695 | ret = radix_tree_gang_lookup(&fs_info->dev_radix, |
| 696 | (void **)lookup, 0, |
| 697 | ARRAY_SIZE(lookup)); |
| 698 | if (!ret) |
| 699 | break; |
| 700 | for (i = 0; i < ret; i++) { |
| 701 | if (lookup[i]->bdev != super_bdev) |
| 702 | close_bdev_excl(lookup[i]->bdev); |
| 703 | radix_tree_delete(&fs_info->dev_radix, |
| 704 | lookup[i]->block_start + |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 705 | lookup[i]->num_blocks - 1); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 706 | kfree(lookup[i]); |
| 707 | } |
| 708 | } |
| 709 | return 0; |
| 710 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 711 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 712 | int close_ctree(struct btrfs_root *root) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 713 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 714 | int ret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 715 | struct btrfs_trans_handle *trans; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 716 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 717 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 718 | mutex_lock(&fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 719 | trans = btrfs_start_transaction(root, 1); |
| 720 | btrfs_commit_transaction(trans, root); |
| 721 | /* run commit again to drop the original snapshot */ |
| 722 | trans = btrfs_start_transaction(root, 1); |
| 723 | btrfs_commit_transaction(trans, root); |
| 724 | ret = btrfs_write_and_wait_transaction(NULL, root); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 725 | BUG_ON(ret); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 726 | write_ctree_super(NULL, root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 727 | mutex_unlock(&fs_info->fs_mutex); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 728 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 729 | if (fs_info->extent_root->node) |
| 730 | btrfs_block_release(fs_info->extent_root, |
| 731 | fs_info->extent_root->node); |
Chris Mason | 0bd93ba | 2007-04-11 13:57:44 -0400 | [diff] [blame] | 732 | if (fs_info->dev_root->node) |
| 733 | btrfs_block_release(fs_info->dev_root, |
| 734 | fs_info->dev_root->node); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 735 | if (fs_info->tree_root->node) |
| 736 | btrfs_block_release(fs_info->tree_root, |
| 737 | fs_info->tree_root->node); |
| 738 | btrfs_block_release(root, fs_info->sb_buffer); |
| 739 | crypto_free_hash(fs_info->hash_tfm); |
| 740 | truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); |
| 741 | iput(fs_info->btree_inode); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 742 | |
| 743 | free_dev_radix(fs_info); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 744 | btrfs_free_block_groups(root->fs_info); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 745 | del_fs_roots(fs_info); |
| 746 | kfree(fs_info->extent_root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 747 | kfree(fs_info->tree_root); |
| 748 | kobject_unregister(&fs_info->kobj); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 749 | return 0; |
| 750 | } |
| 751 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 752 | void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 753 | { |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 754 | brelse(buf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 755 | } |
| 756 | |