Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 19 | #include <linux/module.h> |
| 20 | #include <linux/fs.h> |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 21 | #include <linux/blkdev.h> |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 22 | #include <linux/crypto.h> |
| 23 | #include <linux/scatterlist.h> |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 24 | #include <linux/swap.h> |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 25 | #include <linux/radix-tree.h> |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 26 | #include <linux/writeback.h> |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 27 | #include "ctree.h" |
| 28 | #include "disk-io.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 29 | #include "transaction.h" |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 30 | #include "btrfs_inode.h" |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 31 | |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 32 | u64 bh_blocknr(struct buffer_head *bh) |
| 33 | { |
Chris Mason | 0cf6c62 | 2007-06-09 09:22:25 -0400 | [diff] [blame] | 34 | return bh->b_blocknr; |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 37 | 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] | 38 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 39 | struct btrfs_node *node = btrfs_buffer_node(buf); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 40 | if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) { |
Chris Mason | 5af3981 | 2007-06-12 07:50:13 -0400 | [diff] [blame] | 41 | printk(KERN_CRIT "bh_blocknr(buf) is %llu, header is %llu\n", |
| 42 | (unsigned long long)bh_blocknr(buf), |
| 43 | (unsigned long long)btrfs_header_blocknr(&node->header)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 44 | return 1; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 45 | } |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 46 | return 0; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 47 | } |
| 48 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 49 | 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] | 50 | { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 51 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; |
| 52 | int blockbits = root->fs_info->sb->s_blocksize_bits; |
| 53 | unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); |
| 54 | struct page *page; |
| 55 | struct buffer_head *bh; |
| 56 | struct buffer_head *head; |
| 57 | struct buffer_head *ret = NULL; |
| 58 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 59 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 60 | page = find_lock_page(mapping, index); |
| 61 | if (!page) |
| 62 | return NULL; |
| 63 | |
| 64 | if (!page_has_buffers(page)) |
| 65 | goto out_unlock; |
| 66 | |
| 67 | head = page_buffers(page); |
| 68 | bh = head; |
| 69 | do { |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 70 | if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 71 | ret = bh; |
| 72 | get_bh(bh); |
| 73 | goto out_unlock; |
| 74 | } |
| 75 | bh = bh->b_this_page; |
| 76 | } while (bh != head); |
| 77 | out_unlock: |
| 78 | unlock_page(page); |
| 79 | page_cache_release(page); |
| 80 | return ret; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 83 | 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] | 84 | u64 logical) |
| 85 | { |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 86 | if (logical == 0) { |
| 87 | bh->b_bdev = NULL; |
| 88 | bh->b_blocknr = 0; |
| 89 | set_buffer_mapped(bh); |
Chris Mason | 0cf6c62 | 2007-06-09 09:22:25 -0400 | [diff] [blame] | 90 | } else { |
| 91 | map_bh(bh, root->fs_info->sb, logical); |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 92 | } |
Chris Mason | 0cf6c62 | 2007-06-09 09:22:25 -0400 | [diff] [blame] | 93 | return 0; |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 96 | struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root, |
| 97 | u64 blocknr) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 98 | { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 99 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; |
| 100 | int blockbits = root->fs_info->sb->s_blocksize_bits; |
| 101 | unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); |
| 102 | struct page *page; |
| 103 | struct buffer_head *bh; |
| 104 | struct buffer_head *head; |
| 105 | struct buffer_head *ret = NULL; |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 106 | int err; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 107 | u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 108 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 109 | page = grab_cache_page(mapping, index); |
| 110 | if (!page) |
| 111 | return NULL; |
| 112 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 113 | if (!page_has_buffers(page)) |
| 114 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0); |
| 115 | head = page_buffers(page); |
| 116 | bh = head; |
| 117 | do { |
| 118 | if (!buffer_mapped(bh)) { |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 119 | err = btrfs_map_bh_to_logical(root, bh, first_block); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 120 | BUG_ON(err); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 121 | } |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 122 | if (bh_blocknr(bh) == blocknr) { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 123 | ret = bh; |
| 124 | get_bh(bh); |
| 125 | goto out_unlock; |
| 126 | } |
| 127 | bh = bh->b_this_page; |
| 128 | first_block++; |
| 129 | } while (bh != head); |
| 130 | out_unlock: |
| 131 | unlock_page(page); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 132 | if (ret) |
| 133 | touch_buffer(ret); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 134 | page_cache_release(page); |
| 135 | return ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 136 | } |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 137 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 138 | static int btree_get_block(struct inode *inode, sector_t iblock, |
| 139 | struct buffer_head *bh, int create) |
| 140 | { |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 141 | int err; |
| 142 | struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root; |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 143 | err = btrfs_map_bh_to_logical(root, bh, iblock); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 144 | return err; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 147 | int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len, |
| 148 | char *result) |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 149 | { |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 150 | struct scatterlist sg; |
| 151 | struct crypto_hash *tfm = root->fs_info->hash_tfm; |
| 152 | struct hash_desc desc; |
| 153 | int ret; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 154 | |
| 155 | desc.tfm = tfm; |
| 156 | desc.flags = 0; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 157 | sg_init_one(&sg, data, len); |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 158 | spin_lock(&root->fs_info->hash_lock); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 159 | ret = crypto_hash_digest(&desc, &sg, 1, result); |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 160 | spin_unlock(&root->fs_info->hash_lock); |
| 161 | if (ret) { |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 162 | printk("digest failed\n"); |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 163 | } |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 164 | return ret; |
| 165 | } |
| 166 | static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh, |
| 167 | int verify) |
| 168 | { |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 169 | char result[BTRFS_CRC32_SIZE]; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 170 | int ret; |
| 171 | struct btrfs_node *node; |
| 172 | |
| 173 | ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE, |
| 174 | bh->b_size - BTRFS_CSUM_SIZE, result); |
| 175 | if (ret) |
| 176 | return ret; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 177 | if (verify) { |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 178 | if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) { |
Chris Mason | 5af3981 | 2007-06-12 07:50:13 -0400 | [diff] [blame] | 179 | printk("btrfs: %s checksum verify failed on %llu\n", |
| 180 | root->fs_info->sb->s_id, |
| 181 | (unsigned long long)bh_blocknr(bh)); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 182 | return 1; |
| 183 | } |
| 184 | } else { |
| 185 | node = btrfs_buffer_node(bh); |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 186 | memcpy(node->header.csum, result, BTRFS_CRC32_SIZE); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 187 | } |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 191 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
| 192 | { |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 193 | struct buffer_head *bh; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 194 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 195 | struct buffer_head *head; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 196 | if (!page_has_buffers(page)) { |
| 197 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, |
| 198 | (1 << BH_Dirty)|(1 << BH_Uptodate)); |
| 199 | } |
| 200 | head = page_buffers(page); |
| 201 | bh = head; |
| 202 | do { |
| 203 | if (buffer_dirty(bh)) |
| 204 | csum_tree_block(root, bh, 0); |
| 205 | bh = bh->b_this_page; |
| 206 | } while (bh != head); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 207 | return block_write_full_page(page, btree_get_block, wbc); |
| 208 | } |
| 209 | |
| 210 | static int btree_readpage(struct file * file, struct page * page) |
| 211 | { |
| 212 | return block_read_full_page(page, btree_get_block); |
| 213 | } |
| 214 | |
| 215 | static struct address_space_operations btree_aops = { |
| 216 | .readpage = btree_readpage, |
| 217 | .writepage = btree_writepage, |
| 218 | .sync_page = block_sync_page, |
| 219 | }; |
| 220 | |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 221 | int readahead_tree_block(struct btrfs_root *root, u64 blocknr) |
| 222 | { |
| 223 | struct buffer_head *bh = NULL; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 224 | int ret = 0; |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 225 | |
| 226 | bh = btrfs_find_create_tree_block(root, blocknr); |
| 227 | if (!bh) |
| 228 | return 0; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 229 | if (buffer_uptodate(bh)) { |
| 230 | ret = 1; |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 231 | goto done; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 232 | } |
| 233 | if (test_set_buffer_locked(bh)) { |
| 234 | ret = 1; |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 235 | goto done; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 236 | } |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 237 | if (!buffer_uptodate(bh)) { |
| 238 | get_bh(bh); |
| 239 | bh->b_end_io = end_buffer_read_sync; |
| 240 | submit_bh(READ, bh); |
| 241 | } else { |
| 242 | unlock_buffer(bh); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 243 | ret = 1; |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 244 | } |
| 245 | done: |
| 246 | brelse(bh); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 247 | return ret; |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 248 | } |
| 249 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 250 | struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr) |
| 251 | { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 252 | struct buffer_head *bh = NULL; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 253 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 254 | bh = btrfs_find_create_tree_block(root, blocknr); |
| 255 | if (!bh) |
| 256 | return bh; |
Chris Mason | 9d64272 | 2007-04-03 11:43:19 -0400 | [diff] [blame] | 257 | if (buffer_uptodate(bh)) |
| 258 | goto uptodate; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 259 | lock_buffer(bh); |
| 260 | if (!buffer_uptodate(bh)) { |
| 261 | get_bh(bh); |
| 262 | bh->b_end_io = end_buffer_read_sync; |
| 263 | submit_bh(READ, bh); |
| 264 | wait_on_buffer(bh); |
| 265 | if (!buffer_uptodate(bh)) |
| 266 | goto fail; |
| 267 | } else { |
| 268 | unlock_buffer(bh); |
| 269 | } |
Chris Mason | 9d64272 | 2007-04-03 11:43:19 -0400 | [diff] [blame] | 270 | uptodate: |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 271 | if (!buffer_checked(bh)) { |
| 272 | csum_tree_block(root, bh, 1); |
| 273 | set_buffer_checked(bh); |
| 274 | } |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 275 | if (check_tree_block(root, bh)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 276 | goto fail; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 277 | return bh; |
| 278 | fail: |
| 279 | brelse(bh); |
| 280 | return NULL; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 281 | } |
| 282 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 283 | 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] | 284 | struct buffer_head *buf) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 285 | { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 286 | WARN_ON(atomic_read(&buf->b_count) == 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 287 | mark_buffer_dirty(buf); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 288 | return 0; |
| 289 | } |
| 290 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 291 | 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] | 292 | struct buffer_head *buf) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 293 | { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 294 | WARN_ON(atomic_read(&buf->b_count) == 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 295 | clear_buffer_dirty(buf); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 296 | return 0; |
| 297 | } |
| 298 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 299 | static int __setup_root(int blocksize, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 300 | struct btrfs_root *root, |
| 301 | struct btrfs_fs_info *fs_info, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 302 | u64 objectid) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 303 | { |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 304 | root->node = NULL; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 305 | root->inode = NULL; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 306 | root->commit_root = NULL; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 307 | root->blocksize = blocksize; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 308 | root->ref_cows = 0; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 309 | root->fs_info = fs_info; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 310 | root->objectid = objectid; |
| 311 | root->last_trans = 0; |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 312 | root->highest_inode = 0; |
| 313 | root->last_inode_alloc = 0; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 314 | memset(&root->root_key, 0, sizeof(root->root_key)); |
| 315 | memset(&root->root_item, 0, sizeof(root->root_item)); |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 316 | root->root_key.objectid = objectid; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 320 | static int find_and_setup_root(int blocksize, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 321 | struct btrfs_root *tree_root, |
| 322 | struct btrfs_fs_info *fs_info, |
| 323 | u64 objectid, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 324 | struct btrfs_root *root) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 325 | { |
| 326 | int ret; |
| 327 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 328 | __setup_root(blocksize, root, fs_info, objectid); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 329 | ret = btrfs_find_last_root(tree_root, objectid, |
| 330 | &root->root_item, &root->root_key); |
| 331 | BUG_ON(ret); |
| 332 | |
| 333 | root->node = read_tree_block(root, |
| 334 | btrfs_root_blocknr(&root->root_item)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 335 | BUG_ON(!root->node); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 336 | return 0; |
| 337 | } |
| 338 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 339 | struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, |
| 340 | struct btrfs_key *location) |
| 341 | { |
| 342 | struct btrfs_root *root; |
| 343 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 344 | struct btrfs_path *path; |
| 345 | struct btrfs_leaf *l; |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 346 | u64 highest_inode; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 347 | int ret = 0; |
| 348 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 349 | root = radix_tree_lookup(&fs_info->fs_roots_radix, |
| 350 | (unsigned long)location->objectid); |
Chris Mason | 0cf6c62 | 2007-06-09 09:22:25 -0400 | [diff] [blame] | 351 | if (root) |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 352 | return root; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 353 | root = kmalloc(sizeof(*root), GFP_NOFS); |
Chris Mason | 0cf6c62 | 2007-06-09 09:22:25 -0400 | [diff] [blame] | 354 | if (!root) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 355 | return ERR_PTR(-ENOMEM); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 356 | if (location->offset == (u64)-1) { |
| 357 | ret = find_and_setup_root(fs_info->sb->s_blocksize, |
| 358 | fs_info->tree_root, fs_info, |
| 359 | location->objectid, root); |
| 360 | if (ret) { |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 361 | kfree(root); |
| 362 | return ERR_PTR(ret); |
| 363 | } |
| 364 | goto insert; |
| 365 | } |
| 366 | |
| 367 | __setup_root(fs_info->sb->s_blocksize, root, fs_info, |
| 368 | location->objectid); |
| 369 | |
| 370 | path = btrfs_alloc_path(); |
| 371 | BUG_ON(!path); |
| 372 | ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); |
| 373 | if (ret != 0) { |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 374 | if (ret > 0) |
| 375 | ret = -ENOENT; |
| 376 | goto out; |
| 377 | } |
| 378 | l = btrfs_buffer_leaf(path->nodes[0]); |
| 379 | memcpy(&root->root_item, |
| 380 | btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item), |
| 381 | sizeof(root->root_item)); |
| 382 | memcpy(&root->root_key, location, sizeof(*location)); |
| 383 | ret = 0; |
| 384 | out: |
| 385 | btrfs_release_path(root, path); |
| 386 | btrfs_free_path(path); |
| 387 | if (ret) { |
| 388 | kfree(root); |
| 389 | return ERR_PTR(ret); |
| 390 | } |
| 391 | root->node = read_tree_block(root, |
| 392 | btrfs_root_blocknr(&root->root_item)); |
| 393 | BUG_ON(!root->node); |
| 394 | insert: |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 395 | root->ref_cows = 1; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 396 | ret = radix_tree_insert(&fs_info->fs_roots_radix, |
| 397 | (unsigned long)root->root_key.objectid, |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 398 | root); |
| 399 | if (ret) { |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 400 | brelse(root->node); |
| 401 | kfree(root); |
| 402 | return ERR_PTR(ret); |
| 403 | } |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 404 | ret = btrfs_find_highest_inode(root, &highest_inode); |
| 405 | if (ret == 0) { |
| 406 | root->highest_inode = highest_inode; |
| 407 | root->last_inode_alloc = highest_inode; |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 408 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 409 | return root; |
| 410 | } |
| 411 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 412 | struct btrfs_root *open_ctree(struct super_block *sb) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 413 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 414 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), |
| 415 | GFP_NOFS); |
| 416 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), |
| 417 | GFP_NOFS); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 418 | struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info), |
| 419 | GFP_NOFS); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 420 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 421 | int err = -EIO; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 422 | struct btrfs_super_block *disk_super; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 423 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 424 | if (!extent_root || !tree_root || !fs_info) { |
| 425 | err = -ENOMEM; |
| 426 | goto fail; |
| 427 | } |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 428 | init_bit_radix(&fs_info->pinned_radix); |
| 429 | init_bit_radix(&fs_info->pending_del_radix); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 430 | init_bit_radix(&fs_info->extent_map_radix); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 431 | INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 432 | INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 433 | INIT_RADIX_TREE(&fs_info->block_group_data_radix, GFP_KERNEL); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 434 | INIT_LIST_HEAD(&fs_info->trans_list); |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 435 | INIT_LIST_HEAD(&fs_info->dead_roots); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 436 | sb_set_blocksize(sb, 4096); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 437 | fs_info->running_transaction = NULL; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 438 | fs_info->tree_root = tree_root; |
| 439 | fs_info->extent_root = extent_root; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 440 | fs_info->sb = sb; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 441 | fs_info->btree_inode = new_inode(sb); |
| 442 | fs_info->btree_inode->i_ino = 1; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 443 | fs_info->btree_inode->i_nlink = 1; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 444 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
| 445 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 446 | fs_info->do_barriers = 1; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 447 | fs_info->extent_tree_insert_nr = 0; |
| 448 | fs_info->extent_tree_prealloc_nr = 0; |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 449 | fs_info->closing = 0; |
| 450 | |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 451 | INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 452 | BTRFS_I(fs_info->btree_inode)->root = tree_root; |
| 453 | memset(&BTRFS_I(fs_info->btree_inode)->location, 0, |
| 454 | sizeof(struct btrfs_key)); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 455 | insert_inode_hash(fs_info->btree_inode); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 456 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 457 | fs_info->hash_tfm = crypto_alloc_hash("crc32c", 0, CRYPTO_ALG_ASYNC); |
Chris Mason | 30ae846 | 2007-03-29 09:59:15 -0400 | [diff] [blame] | 458 | spin_lock_init(&fs_info->hash_lock); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 459 | |
Chris Mason | 30ae846 | 2007-03-29 09:59:15 -0400 | [diff] [blame] | 460 | if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 461 | printk("btrfs: failed hash setup, modprobe cryptomgr?\n"); |
| 462 | err = -ENOMEM; |
| 463 | goto fail_iput; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 464 | } |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 465 | mutex_init(&fs_info->trans_mutex); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 466 | mutex_init(&fs_info->fs_mutex); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 467 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 468 | __setup_root(sb->s_blocksize, tree_root, |
| 469 | fs_info, BTRFS_ROOT_TREE_OBJECTID); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 470 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 471 | fs_info->sb_buffer = read_tree_block(tree_root, |
| 472 | BTRFS_SUPER_INFO_OFFSET / |
| 473 | sb->s_blocksize); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 474 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 475 | if (!fs_info->sb_buffer) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 476 | goto fail_iput; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 477 | disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 478 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 479 | if (!btrfs_super_root(disk_super)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 480 | goto fail_sb_buffer; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 481 | |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 482 | i_size_write(fs_info->btree_inode, |
| 483 | btrfs_super_total_blocks(disk_super) << |
| 484 | fs_info->btree_inode->i_blkbits); |
| 485 | |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 486 | fs_info->disk_super = disk_super; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 487 | |
| 488 | if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, |
| 489 | sizeof(disk_super->magic))) { |
| 490 | printk("btrfs: valid FS not found on %s\n", sb->s_id); |
| 491 | goto fail_sb_buffer; |
| 492 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 493 | tree_root->node = read_tree_block(tree_root, |
| 494 | btrfs_super_root(disk_super)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 495 | if (!tree_root->node) |
| 496 | goto fail_sb_buffer; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 497 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 498 | mutex_lock(&fs_info->fs_mutex); |
| 499 | ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 500 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 501 | if (ret) { |
| 502 | mutex_unlock(&fs_info->fs_mutex); |
| 503 | goto fail_tree_root; |
| 504 | } |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 505 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 506 | btrfs_read_block_groups(extent_root); |
| 507 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 508 | fs_info->generation = btrfs_super_generation(disk_super) + 1; |
Chris Mason | 5be6f7f | 2007-04-05 13:35:25 -0400 | [diff] [blame] | 509 | mutex_unlock(&fs_info->fs_mutex); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 510 | return tree_root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 511 | |
| 512 | fail_tree_root: |
| 513 | btrfs_block_release(tree_root, tree_root->node); |
| 514 | fail_sb_buffer: |
| 515 | btrfs_block_release(tree_root, fs_info->sb_buffer); |
| 516 | fail_iput: |
| 517 | iput(fs_info->btree_inode); |
| 518 | fail: |
| 519 | kfree(extent_root); |
| 520 | kfree(tree_root); |
| 521 | kfree(fs_info); |
| 522 | return ERR_PTR(err); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 523 | } |
| 524 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 525 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 526 | *root) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 527 | { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 528 | int ret; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 529 | struct buffer_head *bh = root->fs_info->sb_buffer; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 530 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 531 | btrfs_set_super_root(root->fs_info->disk_super, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 532 | bh_blocknr(root->fs_info->tree_root->node)); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 533 | lock_buffer(bh); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 534 | WARN_ON(atomic_read(&bh->b_count) < 1); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 535 | clear_buffer_dirty(bh); |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 536 | csum_tree_block(root, bh, 0); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 537 | bh->b_end_io = end_buffer_write_sync; |
| 538 | get_bh(bh); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 539 | if (root->fs_info->do_barriers) |
| 540 | ret = submit_bh(WRITE_BARRIER, bh); |
| 541 | else |
| 542 | ret = submit_bh(WRITE, bh); |
| 543 | if (ret == -EOPNOTSUPP) { |
| 544 | set_buffer_uptodate(bh); |
| 545 | root->fs_info->do_barriers = 0; |
| 546 | ret = submit_bh(WRITE, bh); |
| 547 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 548 | wait_on_buffer(bh); |
| 549 | if (!buffer_uptodate(bh)) { |
| 550 | WARN_ON(1); |
| 551 | return -EIO; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 552 | } |
| 553 | return 0; |
| 554 | } |
| 555 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 556 | static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) |
| 557 | { |
| 558 | radix_tree_delete(&fs_info->fs_roots_radix, |
| 559 | (unsigned long)root->root_key.objectid); |
| 560 | if (root->inode) |
| 561 | iput(root->inode); |
| 562 | if (root->node) |
| 563 | brelse(root->node); |
| 564 | if (root->commit_root) |
| 565 | brelse(root->commit_root); |
| 566 | kfree(root); |
| 567 | return 0; |
| 568 | } |
| 569 | |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 570 | static int del_fs_roots(struct btrfs_fs_info *fs_info) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 571 | { |
| 572 | int ret; |
| 573 | struct btrfs_root *gang[8]; |
| 574 | int i; |
| 575 | |
| 576 | while(1) { |
| 577 | ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, |
| 578 | (void **)gang, 0, |
| 579 | ARRAY_SIZE(gang)); |
| 580 | if (!ret) |
| 581 | break; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 582 | for (i = 0; i < ret; i++) |
| 583 | free_fs_root(fs_info, gang[i]); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 584 | } |
| 585 | return 0; |
| 586 | } |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 587 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 588 | int close_ctree(struct btrfs_root *root) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 589 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 590 | int ret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 591 | struct btrfs_trans_handle *trans; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 592 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 593 | |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 594 | fs_info->closing = 1; |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 595 | btrfs_transaction_flush_work(root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 596 | mutex_lock(&fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 597 | trans = btrfs_start_transaction(root, 1); |
| 598 | btrfs_commit_transaction(trans, root); |
| 599 | /* run commit again to drop the original snapshot */ |
| 600 | trans = btrfs_start_transaction(root, 1); |
| 601 | btrfs_commit_transaction(trans, root); |
| 602 | ret = btrfs_write_and_wait_transaction(NULL, root); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 603 | BUG_ON(ret); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 604 | write_ctree_super(NULL, root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 605 | mutex_unlock(&fs_info->fs_mutex); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 606 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 607 | if (fs_info->extent_root->node) |
| 608 | btrfs_block_release(fs_info->extent_root, |
| 609 | fs_info->extent_root->node); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 610 | if (fs_info->tree_root->node) |
| 611 | btrfs_block_release(fs_info->tree_root, |
| 612 | fs_info->tree_root->node); |
| 613 | btrfs_block_release(root, fs_info->sb_buffer); |
| 614 | crypto_free_hash(fs_info->hash_tfm); |
| 615 | truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); |
| 616 | iput(fs_info->btree_inode); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 617 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 618 | btrfs_free_block_groups(root->fs_info); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 619 | del_fs_roots(fs_info); |
| 620 | kfree(fs_info->extent_root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 621 | kfree(fs_info->tree_root); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 622 | return 0; |
| 623 | } |
| 624 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 625 | void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 626 | { |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 627 | brelse(buf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 628 | } |
| 629 | |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 630 | void btrfs_btree_balance_dirty(struct btrfs_root *root) |
| 631 | { |
| 632 | balance_dirty_pages_ratelimited(root->fs_info->btree_inode->i_mapping); |
| 633 | } |