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