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