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/fs.h> |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 20 | #include <linux/blkdev.h> |
Chris Mason | 11bd143 | 2007-06-22 14:16:24 -0400 | [diff] [blame] | 21 | #include <linux/crc32c.h> |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 22 | #include <linux/scatterlist.h> |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 23 | #include <linux/swap.h> |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 24 | #include <linux/radix-tree.h> |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 25 | #include <linux/writeback.h> |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 26 | #include <linux/buffer_head.h> // for block_sync_page |
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 | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 32 | #if 0 |
| 33 | static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf) |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 34 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 35 | if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) { |
| 36 | printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n", |
| 37 | (unsigned long long)extent_buffer_blocknr(buf), |
| 38 | (unsigned long long)btrfs_header_blocknr(buf)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 39 | return 1; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 40 | } |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 41 | return 0; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 42 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 43 | #endif |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 44 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 45 | struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, |
| 46 | u64 blocknr) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 47 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 48 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame^] | 49 | struct extent_buffer *eb; |
| 50 | eb = find_extent_buffer(&BTRFS_I(btree_inode)->extent_tree, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 51 | blocknr * root->sectorsize, |
| 52 | root->sectorsize, GFP_NOFS); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame^] | 53 | if (eb) |
| 54 | eb->alloc_addr = (unsigned long)__builtin_return_address(0); |
| 55 | return eb; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 56 | } |
| 57 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 58 | struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 59 | u64 blocknr) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 60 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 61 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame^] | 62 | struct extent_buffer *eb; |
| 63 | eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->extent_tree, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 64 | blocknr * root->sectorsize, |
| 65 | root->sectorsize, GFP_NOFS); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame^] | 66 | eb->alloc_addr = (unsigned long)__builtin_return_address(0); |
| 67 | return eb; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 68 | } |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 69 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 70 | struct extent_map *btree_get_extent(struct inode *inode, struct page *page, |
| 71 | size_t page_offset, u64 start, u64 end, |
| 72 | int create) |
| 73 | { |
| 74 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 75 | struct extent_map *em; |
| 76 | int ret; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 77 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 78 | again: |
| 79 | em = lookup_extent_mapping(em_tree, start, end); |
| 80 | if (em) { |
| 81 | goto out; |
| 82 | } |
| 83 | em = alloc_extent_map(GFP_NOFS); |
| 84 | if (!em) { |
| 85 | em = ERR_PTR(-ENOMEM); |
| 86 | goto out; |
| 87 | } |
| 88 | em->start = 0; |
| 89 | em->end = (i_size_read(inode) & ~((u64)PAGE_CACHE_SIZE -1)) - 1; |
| 90 | em->block_start = 0; |
| 91 | em->block_end = em->end; |
| 92 | em->bdev = inode->i_sb->s_bdev; |
| 93 | ret = add_extent_mapping(em_tree, em); |
| 94 | if (ret == -EEXIST) { |
| 95 | free_extent_map(em); |
| 96 | em = NULL; |
| 97 | goto again; |
| 98 | } else if (ret) { |
| 99 | em = ERR_PTR(ret); |
| 100 | } |
| 101 | out: |
| 102 | return em; |
| 103 | } |
| 104 | |
| 105 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
| 106 | { |
| 107 | struct extent_map_tree *tree; |
| 108 | tree = &BTRFS_I(page->mapping->host)->extent_tree; |
| 109 | return extent_write_full_page(tree, page, btree_get_extent, wbc); |
| 110 | } |
| 111 | int btree_readpage(struct file *file, struct page *page) |
| 112 | { |
| 113 | struct extent_map_tree *tree; |
| 114 | tree = &BTRFS_I(page->mapping->host)->extent_tree; |
| 115 | return extent_read_full_page(tree, page, btree_get_extent); |
| 116 | } |
| 117 | |
| 118 | static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags) |
| 119 | { |
| 120 | struct extent_map_tree *tree; |
| 121 | int ret; |
| 122 | |
| 123 | BUG_ON(page->private != 1); |
| 124 | tree = &BTRFS_I(page->mapping->host)->extent_tree; |
| 125 | ret = try_release_extent_mapping(tree, page); |
| 126 | if (ret == 1) { |
| 127 | ClearPagePrivate(page); |
| 128 | set_page_private(page, 0); |
| 129 | page_cache_release(page); |
| 130 | } |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 131 | return ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 132 | } |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 133 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 134 | static void btree_invalidatepage(struct page *page, unsigned long offset) |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 135 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 136 | struct extent_map_tree *tree; |
| 137 | tree = &BTRFS_I(page->mapping->host)->extent_tree; |
| 138 | extent_invalidatepage(tree, page, offset); |
| 139 | btree_releasepage(page, GFP_NOFS); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 142 | int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len, |
| 143 | char *result) |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 144 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 145 | return 0; |
| 146 | #if 0 |
Chris Mason | 11bd143 | 2007-06-22 14:16:24 -0400 | [diff] [blame] | 147 | u32 crc; |
| 148 | crc = crc32c(0, data, len); |
| 149 | memcpy(result, &crc, BTRFS_CRC32_SIZE); |
| 150 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 151 | #endif |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 152 | } |
Chris Mason | 11bd143 | 2007-06-22 14:16:24 -0400 | [diff] [blame] | 153 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 154 | #if 0 |
| 155 | static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf, |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 156 | int verify) |
| 157 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 158 | return 0; |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 159 | char result[BTRFS_CRC32_SIZE]; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 160 | int ret; |
| 161 | struct btrfs_node *node; |
| 162 | |
| 163 | ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE, |
| 164 | bh->b_size - BTRFS_CSUM_SIZE, result); |
| 165 | if (ret) |
| 166 | return ret; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 167 | if (verify) { |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 168 | if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) { |
Chris Mason | 5af3981 | 2007-06-12 07:50:13 -0400 | [diff] [blame] | 169 | printk("btrfs: %s checksum verify failed on %llu\n", |
| 170 | root->fs_info->sb->s_id, |
| 171 | (unsigned long long)bh_blocknr(bh)); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 172 | return 1; |
| 173 | } |
| 174 | } else { |
| 175 | node = btrfs_buffer_node(bh); |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 176 | memcpy(node->header.csum, result, BTRFS_CRC32_SIZE); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 177 | } |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 178 | return 0; |
| 179 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 180 | #endif |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 181 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 182 | #if 0 |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 183 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
| 184 | { |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 185 | struct buffer_head *bh; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 186 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 187 | struct buffer_head *head; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 188 | if (!page_has_buffers(page)) { |
| 189 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, |
| 190 | (1 << BH_Dirty)|(1 << BH_Uptodate)); |
| 191 | } |
| 192 | head = page_buffers(page); |
| 193 | bh = head; |
| 194 | do { |
| 195 | if (buffer_dirty(bh)) |
| 196 | csum_tree_block(root, bh, 0); |
| 197 | bh = bh->b_this_page; |
| 198 | } while (bh != head); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 199 | return block_write_full_page(page, btree_get_block, wbc); |
| 200 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 201 | #endif |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 202 | |
| 203 | static struct address_space_operations btree_aops = { |
| 204 | .readpage = btree_readpage, |
| 205 | .writepage = btree_writepage, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 206 | .releasepage = btree_releasepage, |
| 207 | .invalidatepage = btree_invalidatepage, |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 208 | .sync_page = block_sync_page, |
| 209 | }; |
| 210 | |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 211 | int readahead_tree_block(struct btrfs_root *root, u64 blocknr) |
| 212 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 213 | struct extent_buffer *buf = NULL; |
| 214 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 215 | int ret = 0; |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 216 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 217 | buf = btrfs_find_create_tree_block(root, blocknr); |
| 218 | if (!buf) |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 219 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 220 | read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree, |
| 221 | buf, 0); |
| 222 | free_extent_buffer(buf); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 223 | return ret; |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 224 | } |
| 225 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 226 | struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 227 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 228 | struct extent_buffer *buf = NULL; |
| 229 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 230 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 231 | buf = btrfs_find_create_tree_block(root, blocknr); |
| 232 | if (!buf) |
| 233 | return NULL; |
| 234 | read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree, |
| 235 | buf, 1); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame^] | 236 | buf->alloc_addr = (unsigned long)__builtin_return_address(0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 237 | return buf; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 238 | } |
| 239 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 240 | int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 241 | struct extent_buffer *buf) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 242 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 243 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 244 | clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf); |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | int wait_on_tree_block_writeback(struct btrfs_root *root, |
| 249 | struct extent_buffer *buf) |
| 250 | { |
| 251 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 252 | wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree, |
| 253 | buf); |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | int set_tree_block_dirty(struct btrfs_root *root, struct extent_buffer *buf) |
| 258 | { |
| 259 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 260 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 261 | return 0; |
| 262 | } |
| 263 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 264 | static int __setup_root(int blocksize, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 265 | struct btrfs_root *root, |
| 266 | struct btrfs_fs_info *fs_info, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 267 | u64 objectid) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 268 | { |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 269 | root->node = NULL; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 270 | root->inode = NULL; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 271 | root->commit_root = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 272 | root->sectorsize = blocksize; |
| 273 | root->nodesize = blocksize; |
| 274 | root->leafsize = blocksize; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 275 | root->ref_cows = 0; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 276 | root->fs_info = fs_info; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 277 | root->objectid = objectid; |
| 278 | root->last_trans = 0; |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 279 | root->highest_inode = 0; |
| 280 | root->last_inode_alloc = 0; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 281 | root->name = NULL; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 282 | memset(&root->root_key, 0, sizeof(root->root_key)); |
| 283 | memset(&root->root_item, 0, sizeof(root->root_item)); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 284 | memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 285 | memset(&root->root_kobj, 0, sizeof(root->root_kobj)); |
| 286 | init_completion(&root->kobj_unregister); |
Chris Mason | 011410b | 2007-09-10 19:58:36 -0400 | [diff] [blame] | 287 | init_rwsem(&root->snap_sem); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 288 | root->defrag_running = 0; |
| 289 | root->defrag_level = 0; |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 290 | root->root_key.objectid = objectid; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 294 | static int find_and_setup_root(int blocksize, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 295 | struct btrfs_root *tree_root, |
| 296 | struct btrfs_fs_info *fs_info, |
| 297 | u64 objectid, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 298 | struct btrfs_root *root) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 299 | { |
| 300 | int ret; |
| 301 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 302 | __setup_root(blocksize, root, fs_info, objectid); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 303 | ret = btrfs_find_last_root(tree_root, objectid, |
| 304 | &root->root_item, &root->root_key); |
| 305 | BUG_ON(ret); |
| 306 | |
| 307 | root->node = read_tree_block(root, |
| 308 | btrfs_root_blocknr(&root->root_item)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 309 | BUG_ON(!root->node); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 313 | struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, |
| 314 | struct btrfs_key *location) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 315 | { |
| 316 | struct btrfs_root *root; |
| 317 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 318 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 319 | struct extent_buffer *l; |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 320 | u64 highest_inode; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 321 | int ret = 0; |
| 322 | |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 323 | root = kzalloc(sizeof(*root), GFP_NOFS); |
Chris Mason | 0cf6c62 | 2007-06-09 09:22:25 -0400 | [diff] [blame] | 324 | if (!root) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 325 | return ERR_PTR(-ENOMEM); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 326 | if (location->offset == (u64)-1) { |
| 327 | ret = find_and_setup_root(fs_info->sb->s_blocksize, |
| 328 | fs_info->tree_root, fs_info, |
| 329 | location->objectid, root); |
| 330 | if (ret) { |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 331 | kfree(root); |
| 332 | return ERR_PTR(ret); |
| 333 | } |
| 334 | goto insert; |
| 335 | } |
| 336 | |
| 337 | __setup_root(fs_info->sb->s_blocksize, root, fs_info, |
| 338 | location->objectid); |
| 339 | |
| 340 | path = btrfs_alloc_path(); |
| 341 | BUG_ON(!path); |
| 342 | ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); |
| 343 | if (ret != 0) { |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 344 | if (ret > 0) |
| 345 | ret = -ENOENT; |
| 346 | goto out; |
| 347 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 348 | l = path->nodes[0]; |
| 349 | read_extent_buffer(l, &root->root_item, |
| 350 | btrfs_item_ptr_offset(l, path->slots[0]), |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 351 | sizeof(root->root_item)); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 352 | ret = 0; |
| 353 | out: |
| 354 | btrfs_release_path(root, path); |
| 355 | btrfs_free_path(path); |
| 356 | if (ret) { |
| 357 | kfree(root); |
| 358 | return ERR_PTR(ret); |
| 359 | } |
| 360 | root->node = read_tree_block(root, |
| 361 | btrfs_root_blocknr(&root->root_item)); |
| 362 | BUG_ON(!root->node); |
| 363 | insert: |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 364 | root->ref_cows = 1; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 365 | ret = btrfs_find_highest_inode(root, &highest_inode); |
| 366 | if (ret == 0) { |
| 367 | root->highest_inode = highest_inode; |
| 368 | root->last_inode_alloc = highest_inode; |
| 369 | } |
| 370 | return root; |
| 371 | } |
| 372 | |
| 373 | struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 374 | struct btrfs_key *location, |
| 375 | const char *name, int namelen) |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 376 | { |
| 377 | struct btrfs_root *root; |
| 378 | int ret; |
| 379 | |
| 380 | root = radix_tree_lookup(&fs_info->fs_roots_radix, |
| 381 | (unsigned long)location->objectid); |
| 382 | if (root) |
| 383 | return root; |
| 384 | |
| 385 | root = btrfs_read_fs_root_no_radix(fs_info, location); |
| 386 | if (IS_ERR(root)) |
| 387 | return root; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 388 | ret = radix_tree_insert(&fs_info->fs_roots_radix, |
| 389 | (unsigned long)root->root_key.objectid, |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 390 | root); |
| 391 | if (ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 392 | free_extent_buffer(root->node); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 393 | kfree(root); |
| 394 | return ERR_PTR(ret); |
| 395 | } |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 396 | |
| 397 | ret = btrfs_set_root_name(root, name, namelen); |
| 398 | if (ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 399 | free_extent_buffer(root->node); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 400 | kfree(root); |
| 401 | return ERR_PTR(ret); |
| 402 | } |
| 403 | |
| 404 | ret = btrfs_sysfs_add_root(root); |
| 405 | if (ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 406 | free_extent_buffer(root->node); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 407 | kfree(root->name); |
| 408 | kfree(root); |
| 409 | return ERR_PTR(ret); |
| 410 | } |
| 411 | |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 412 | ret = btrfs_find_dead_roots(fs_info->tree_root, |
| 413 | root->root_key.objectid, root); |
| 414 | BUG_ON(ret); |
| 415 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 416 | return root; |
| 417 | } |
| 418 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 419 | struct btrfs_root *open_ctree(struct super_block *sb) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 420 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 421 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), |
| 422 | GFP_NOFS); |
| 423 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), |
| 424 | GFP_NOFS); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 425 | struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info), |
| 426 | GFP_NOFS); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 427 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 428 | int err = -EIO; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 429 | struct btrfs_super_block *disk_super; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 430 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 431 | if (!extent_root || !tree_root || !fs_info) { |
| 432 | err = -ENOMEM; |
| 433 | goto fail; |
| 434 | } |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 435 | init_bit_radix(&fs_info->pinned_radix); |
| 436 | init_bit_radix(&fs_info->pending_del_radix); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 437 | init_bit_radix(&fs_info->extent_ins_radix); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 438 | INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 439 | INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 440 | INIT_RADIX_TREE(&fs_info->block_group_data_radix, GFP_KERNEL); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 441 | INIT_LIST_HEAD(&fs_info->trans_list); |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 442 | INIT_LIST_HEAD(&fs_info->dead_roots); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 443 | memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj)); |
| 444 | init_completion(&fs_info->kobj_unregister); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 445 | sb_set_blocksize(sb, 4096); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 446 | fs_info->running_transaction = NULL; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 447 | fs_info->last_trans_committed = 0; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 448 | fs_info->tree_root = tree_root; |
| 449 | fs_info->extent_root = extent_root; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 450 | fs_info->sb = sb; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 451 | fs_info->btree_inode = new_inode(sb); |
| 452 | fs_info->btree_inode->i_ino = 1; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 453 | fs_info->btree_inode->i_nlink = 1; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 454 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
| 455 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 456 | extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree, |
| 457 | fs_info->btree_inode->i_mapping, |
| 458 | GFP_NOFS); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame^] | 459 | extent_map_tree_init(&fs_info->free_space_cache, |
| 460 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 461 | fs_info->do_barriers = 1; |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 462 | fs_info->closing = 0; |
| 463 | |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 464 | INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 465 | BTRFS_I(fs_info->btree_inode)->root = tree_root; |
| 466 | memset(&BTRFS_I(fs_info->btree_inode)->location, 0, |
| 467 | sizeof(struct btrfs_key)); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 468 | insert_inode_hash(fs_info->btree_inode); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 469 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 470 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 471 | mutex_init(&fs_info->trans_mutex); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 472 | mutex_init(&fs_info->fs_mutex); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 473 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 474 | __setup_root(sb->s_blocksize, tree_root, |
| 475 | fs_info, BTRFS_ROOT_TREE_OBJECTID); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 476 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 477 | fs_info->sb_buffer = read_tree_block(tree_root, |
| 478 | BTRFS_SUPER_INFO_OFFSET / |
| 479 | sb->s_blocksize); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 480 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 481 | if (!fs_info->sb_buffer) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 482 | goto fail_iput; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 483 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 484 | read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0, |
| 485 | sizeof(fs_info->super_copy)); |
| 486 | |
| 487 | read_extent_buffer(fs_info->sb_buffer, fs_info->fsid, |
| 488 | (unsigned long)btrfs_super_fsid(fs_info->sb_buffer), |
| 489 | BTRFS_FSID_SIZE); |
| 490 | disk_super = &fs_info->super_copy; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 491 | if (!btrfs_super_root(disk_super)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 492 | goto fail_sb_buffer; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 493 | |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 494 | i_size_write(fs_info->btree_inode, |
| 495 | btrfs_super_total_blocks(disk_super) << |
| 496 | fs_info->btree_inode->i_blkbits); |
| 497 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 498 | |
| 499 | if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, |
| 500 | sizeof(disk_super->magic))) { |
| 501 | printk("btrfs: valid FS not found on %s\n", sb->s_id); |
| 502 | goto fail_sb_buffer; |
| 503 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 504 | tree_root->node = read_tree_block(tree_root, |
| 505 | btrfs_super_root(disk_super)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 506 | if (!tree_root->node) |
| 507 | goto fail_sb_buffer; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 508 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 509 | mutex_lock(&fs_info->fs_mutex); |
| 510 | ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 511 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 512 | if (ret) { |
| 513 | mutex_unlock(&fs_info->fs_mutex); |
| 514 | goto fail_tree_root; |
| 515 | } |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 516 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 517 | btrfs_read_block_groups(extent_root); |
| 518 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 519 | fs_info->generation = btrfs_super_generation(disk_super) + 1; |
Chris Mason | 5be6f7f | 2007-04-05 13:35:25 -0400 | [diff] [blame] | 520 | mutex_unlock(&fs_info->fs_mutex); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 521 | return tree_root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 522 | |
| 523 | fail_tree_root: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 524 | free_extent_buffer(tree_root->node); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 525 | fail_sb_buffer: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 526 | free_extent_buffer(fs_info->sb_buffer); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 527 | fail_iput: |
| 528 | iput(fs_info->btree_inode); |
| 529 | fail: |
| 530 | kfree(extent_root); |
| 531 | kfree(tree_root); |
| 532 | kfree(fs_info); |
| 533 | return ERR_PTR(err); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 534 | } |
| 535 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 536 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 537 | *root) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 538 | { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 539 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 540 | struct extent_buffer *super = root->fs_info->sb_buffer; |
| 541 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 542 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 543 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super); |
| 544 | ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping, |
| 545 | super->start, super->len); |
| 546 | return ret; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 547 | } |
| 548 | |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 549 | int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 550 | { |
| 551 | radix_tree_delete(&fs_info->fs_roots_radix, |
| 552 | (unsigned long)root->root_key.objectid); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 553 | btrfs_sysfs_del_root(root); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 554 | if (root->inode) |
| 555 | iput(root->inode); |
| 556 | if (root->node) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 557 | free_extent_buffer(root->node); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 558 | if (root->commit_root) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 559 | free_extent_buffer(root->commit_root); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 560 | if (root->name) |
| 561 | kfree(root->name); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 562 | kfree(root); |
| 563 | return 0; |
| 564 | } |
| 565 | |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 566 | static int del_fs_roots(struct btrfs_fs_info *fs_info) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 567 | { |
| 568 | int ret; |
| 569 | struct btrfs_root *gang[8]; |
| 570 | int i; |
| 571 | |
| 572 | while(1) { |
| 573 | ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, |
| 574 | (void **)gang, 0, |
| 575 | ARRAY_SIZE(gang)); |
| 576 | if (!ret) |
| 577 | break; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 578 | for (i = 0; i < ret; i++) |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 579 | btrfs_free_fs_root(fs_info, gang[i]); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 580 | } |
| 581 | return 0; |
| 582 | } |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 583 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 584 | int close_ctree(struct btrfs_root *root) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 585 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 586 | int ret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 587 | struct btrfs_trans_handle *trans; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 588 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 589 | |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 590 | fs_info->closing = 1; |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 591 | btrfs_transaction_flush_work(root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 592 | mutex_lock(&fs_info->fs_mutex); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 593 | btrfs_defrag_dirty_roots(root->fs_info); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 594 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 595 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 596 | /* run commit again to drop the original snapshot */ |
| 597 | trans = btrfs_start_transaction(root, 1); |
| 598 | btrfs_commit_transaction(trans, root); |
| 599 | ret = btrfs_write_and_wait_transaction(NULL, root); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 600 | BUG_ON(ret); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 601 | write_ctree_super(NULL, root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 602 | mutex_unlock(&fs_info->fs_mutex); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 603 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 604 | if (fs_info->extent_root->node) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 605 | free_extent_buffer(fs_info->extent_root->node); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame^] | 606 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 607 | if (fs_info->tree_root->node) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 608 | free_extent_buffer(fs_info->tree_root->node); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame^] | 609 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 610 | free_extent_buffer(fs_info->sb_buffer); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 611 | truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); |
| 612 | iput(fs_info->btree_inode); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 613 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 614 | btrfs_free_block_groups(root->fs_info); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 615 | del_fs_roots(fs_info); |
| 616 | kfree(fs_info->extent_root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 617 | kfree(fs_info->tree_root); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 618 | return 0; |
| 619 | } |
| 620 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 621 | int btrfs_buffer_uptodate(struct extent_buffer *buf) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 622 | { |
Chris Mason | 6d36dcd | 2007-10-15 16:14:37 -0400 | [diff] [blame] | 623 | struct inode *btree_inode = buf->first_page->mapping->host; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 624 | return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf); |
| 625 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 626 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 627 | int btrfs_set_buffer_uptodate(struct extent_buffer *buf) |
| 628 | { |
Chris Mason | 6d36dcd | 2007-10-15 16:14:37 -0400 | [diff] [blame] | 629 | struct inode *btree_inode = buf->first_page->mapping->host; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 630 | return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, |
| 631 | buf); |
| 632 | } |
| 633 | |
| 634 | void btrfs_mark_buffer_dirty(struct extent_buffer *buf) |
| 635 | { |
Chris Mason | 6d36dcd | 2007-10-15 16:14:37 -0400 | [diff] [blame] | 636 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 637 | u64 transid = btrfs_header_generation(buf); |
| 638 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 639 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 640 | if (transid != root->fs_info->generation) { |
| 641 | printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n", |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 642 | (unsigned long long)extent_buffer_blocknr(buf), |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 643 | transid, root->fs_info->generation); |
| 644 | WARN_ON(1); |
| 645 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 646 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 647 | } |
| 648 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 649 | void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr) |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 650 | { |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 651 | balance_dirty_pages_ratelimited_nr( |
| 652 | root->fs_info->btree_inode->i_mapping, nr); |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 653 | } |