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