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 | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame^] | 63 | bytenr, blocksize, NULL, 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 | |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame^] | 102 | u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len) |
| 103 | { |
| 104 | return crc32c(seed, data, len); |
| 105 | } |
| 106 | |
| 107 | void btrfs_csum_final(u32 crc, char *result) |
| 108 | { |
| 109 | *(__le32 *)result = ~cpu_to_le32(crc); |
| 110 | } |
| 111 | |
| 112 | static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf, |
| 113 | int verify) |
| 114 | { |
| 115 | char result[BTRFS_CRC32_SIZE]; |
| 116 | unsigned long len; |
| 117 | unsigned long cur_len; |
| 118 | unsigned long offset = BTRFS_CSUM_SIZE; |
| 119 | char *map_token = NULL; |
| 120 | char *kaddr; |
| 121 | unsigned long map_start; |
| 122 | unsigned long map_len; |
| 123 | int err; |
| 124 | u32 crc = ~(u32)0; |
| 125 | |
| 126 | len = buf->len - offset; |
| 127 | while(len > 0) { |
| 128 | err = map_private_extent_buffer(buf, offset, 32, |
| 129 | &map_token, &kaddr, |
| 130 | &map_start, &map_len, KM_USER0); |
| 131 | if (err) { |
| 132 | printk("failed to map extent buffer! %lu\n", |
| 133 | offset); |
| 134 | return 1; |
| 135 | } |
| 136 | cur_len = min(len, map_len - (offset - map_start)); |
| 137 | crc = btrfs_csum_data(root, kaddr + offset - map_start, |
| 138 | crc, cur_len); |
| 139 | len -= cur_len; |
| 140 | offset += cur_len; |
| 141 | unmap_extent_buffer(buf, map_token, KM_USER0); |
| 142 | } |
| 143 | btrfs_csum_final(crc, result); |
| 144 | |
| 145 | if (verify) { |
| 146 | if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) { |
| 147 | printk("btrfs: %s checksum verify failed on %llu\n", |
| 148 | root->fs_info->sb->s_id, |
| 149 | buf->start); |
| 150 | return 1; |
| 151 | } |
| 152 | } else { |
| 153 | write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE); |
| 154 | } |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | int csum_dirty_buffer(struct btrfs_root *root, struct page *page) |
| 160 | { |
| 161 | struct extent_map_tree *tree; |
| 162 | u64 start = page->index << PAGE_CACHE_SHIFT; |
| 163 | u64 found_start; |
| 164 | int found_level; |
| 165 | unsigned long len; |
| 166 | struct extent_buffer *eb; |
| 167 | tree = &BTRFS_I(page->mapping->host)->extent_tree; |
| 168 | |
| 169 | if (page->private == EXTENT_PAGE_PRIVATE) |
| 170 | goto out; |
| 171 | if (!page->private) |
| 172 | goto out; |
| 173 | len = page->private >> 2; |
| 174 | if (len == 0) { |
| 175 | WARN_ON(1); |
| 176 | } |
| 177 | eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS); |
| 178 | read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1); |
| 179 | found_start = btrfs_header_bytenr(eb); |
| 180 | if (found_start != start) { |
| 181 | printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n", |
| 182 | start, found_start, len); |
| 183 | } |
| 184 | found_level = btrfs_header_level(eb); |
| 185 | csum_tree_block(root, eb, 0); |
| 186 | free_extent_buffer(eb); |
| 187 | out: |
| 188 | return 0; |
| 189 | } |
| 190 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 191 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
| 192 | { |
| 193 | struct extent_map_tree *tree; |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame^] | 194 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 195 | tree = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame^] | 196 | |
| 197 | csum_dirty_buffer(root, page); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 198 | return extent_write_full_page(tree, page, btree_get_extent, wbc); |
| 199 | } |
| 200 | int btree_readpage(struct file *file, struct page *page) |
| 201 | { |
| 202 | struct extent_map_tree *tree; |
| 203 | tree = &BTRFS_I(page->mapping->host)->extent_tree; |
| 204 | return extent_read_full_page(tree, page, btree_get_extent); |
| 205 | } |
| 206 | |
| 207 | static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags) |
| 208 | { |
| 209 | struct extent_map_tree *tree; |
| 210 | int ret; |
| 211 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 212 | tree = &BTRFS_I(page->mapping->host)->extent_tree; |
| 213 | ret = try_release_extent_mapping(tree, page); |
| 214 | if (ret == 1) { |
| 215 | ClearPagePrivate(page); |
| 216 | set_page_private(page, 0); |
| 217 | page_cache_release(page); |
| 218 | } |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 219 | return ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 220 | } |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 221 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 222 | static void btree_invalidatepage(struct page *page, unsigned long offset) |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 223 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 224 | struct extent_map_tree *tree; |
| 225 | tree = &BTRFS_I(page->mapping->host)->extent_tree; |
| 226 | extent_invalidatepage(tree, page, offset); |
| 227 | btree_releasepage(page, GFP_NOFS); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 228 | } |
| 229 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 230 | #if 0 |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 231 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
| 232 | { |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 233 | struct buffer_head *bh; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 234 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 235 | struct buffer_head *head; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 236 | if (!page_has_buffers(page)) { |
| 237 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, |
| 238 | (1 << BH_Dirty)|(1 << BH_Uptodate)); |
| 239 | } |
| 240 | head = page_buffers(page); |
| 241 | bh = head; |
| 242 | do { |
| 243 | if (buffer_dirty(bh)) |
| 244 | csum_tree_block(root, bh, 0); |
| 245 | bh = bh->b_this_page; |
| 246 | } while (bh != head); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 247 | return block_write_full_page(page, btree_get_block, wbc); |
| 248 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 249 | #endif |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 250 | |
| 251 | static struct address_space_operations btree_aops = { |
| 252 | .readpage = btree_readpage, |
| 253 | .writepage = btree_writepage, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 254 | .releasepage = btree_releasepage, |
| 255 | .invalidatepage = btree_invalidatepage, |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 256 | .sync_page = block_sync_page, |
| 257 | }; |
| 258 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 259 | int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize) |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 260 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 261 | struct extent_buffer *buf = NULL; |
| 262 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 263 | int ret = 0; |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 264 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 265 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 266 | if (!buf) |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 267 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 268 | read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree, |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame^] | 269 | buf, 0, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 270 | free_extent_buffer(buf); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 271 | return ret; |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 272 | } |
| 273 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 274 | struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, |
| 275 | u32 blocksize) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 276 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 277 | struct extent_buffer *buf = NULL; |
| 278 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame^] | 279 | struct extent_map_tree *extent_tree; |
| 280 | int ret; |
| 281 | |
| 282 | extent_tree = &BTRFS_I(btree_inode)->extent_tree; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 283 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 284 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 285 | if (!buf) |
| 286 | return NULL; |
| 287 | read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree, |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame^] | 288 | buf, 0, 1); |
| 289 | if (buf->flags & EXTENT_CSUM) { |
| 290 | return buf; |
| 291 | } |
| 292 | if (test_range_bit(extent_tree, buf->start, buf->start + buf->len - 1, |
| 293 | EXTENT_CSUM, 1)) { |
| 294 | buf->flags |= EXTENT_CSUM; |
| 295 | return buf; |
| 296 | } |
| 297 | ret = csum_tree_block(root, buf, 1); |
| 298 | set_extent_bits(extent_tree, buf->start, |
| 299 | buf->start + buf->len - 1, |
| 300 | EXTENT_CSUM, GFP_NOFS); |
| 301 | buf->flags |= EXTENT_CSUM; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 302 | return buf; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 303 | } |
| 304 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 305 | 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] | 306 | struct extent_buffer *buf) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 307 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 308 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 309 | clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf); |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | int wait_on_tree_block_writeback(struct btrfs_root *root, |
| 314 | struct extent_buffer *buf) |
| 315 | { |
| 316 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 317 | wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree, |
| 318 | buf); |
| 319 | return 0; |
| 320 | } |
| 321 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 322 | static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 323 | struct btrfs_root *root, |
| 324 | struct btrfs_fs_info *fs_info, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 325 | u64 objectid) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 326 | { |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 327 | root->node = NULL; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 328 | root->inode = NULL; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 329 | root->commit_root = NULL; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 330 | root->sectorsize = sectorsize; |
| 331 | root->nodesize = nodesize; |
| 332 | root->leafsize = leafsize; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 333 | root->ref_cows = 0; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 334 | root->fs_info = fs_info; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 335 | root->objectid = objectid; |
| 336 | root->last_trans = 0; |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 337 | root->highest_inode = 0; |
| 338 | root->last_inode_alloc = 0; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 339 | root->name = NULL; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 340 | memset(&root->root_key, 0, sizeof(root->root_key)); |
| 341 | memset(&root->root_item, 0, sizeof(root->root_item)); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 342 | memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 343 | memset(&root->root_kobj, 0, sizeof(root->root_kobj)); |
| 344 | init_completion(&root->kobj_unregister); |
Chris Mason | 011410b | 2007-09-10 19:58:36 -0400 | [diff] [blame] | 345 | init_rwsem(&root->snap_sem); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 346 | root->defrag_running = 0; |
| 347 | root->defrag_level = 0; |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 348 | root->root_key.objectid = objectid; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 352 | static int find_and_setup_root(struct btrfs_root *tree_root, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 353 | struct btrfs_fs_info *fs_info, |
| 354 | u64 objectid, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 355 | struct btrfs_root *root) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 356 | { |
| 357 | int ret; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 358 | u32 blocksize; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 359 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 360 | __setup_root(tree_root->nodesize, tree_root->leafsize, |
| 361 | tree_root->sectorsize, root, fs_info, objectid); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 362 | ret = btrfs_find_last_root(tree_root, objectid, |
| 363 | &root->root_item, &root->root_key); |
| 364 | BUG_ON(ret); |
| 365 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 366 | blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); |
| 367 | root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), |
| 368 | blocksize); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 369 | BUG_ON(!root->node); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 370 | return 0; |
| 371 | } |
| 372 | |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 373 | struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, |
| 374 | struct btrfs_key *location) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 375 | { |
| 376 | struct btrfs_root *root; |
| 377 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 378 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 379 | struct extent_buffer *l; |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 380 | u64 highest_inode; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 381 | u32 blocksize; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 382 | int ret = 0; |
| 383 | |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 384 | root = kzalloc(sizeof(*root), GFP_NOFS); |
Chris Mason | 0cf6c62 | 2007-06-09 09:22:25 -0400 | [diff] [blame] | 385 | if (!root) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 386 | return ERR_PTR(-ENOMEM); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 387 | if (location->offset == (u64)-1) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 388 | ret = find_and_setup_root(tree_root, fs_info, |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 389 | location->objectid, root); |
| 390 | if (ret) { |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 391 | kfree(root); |
| 392 | return ERR_PTR(ret); |
| 393 | } |
| 394 | goto insert; |
| 395 | } |
| 396 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 397 | __setup_root(tree_root->nodesize, tree_root->leafsize, |
| 398 | tree_root->sectorsize, root, fs_info, |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 399 | location->objectid); |
| 400 | |
| 401 | path = btrfs_alloc_path(); |
| 402 | BUG_ON(!path); |
| 403 | ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); |
| 404 | if (ret != 0) { |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 405 | if (ret > 0) |
| 406 | ret = -ENOENT; |
| 407 | goto out; |
| 408 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 409 | l = path->nodes[0]; |
| 410 | read_extent_buffer(l, &root->root_item, |
| 411 | btrfs_item_ptr_offset(l, path->slots[0]), |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 412 | sizeof(root->root_item)); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 413 | ret = 0; |
| 414 | out: |
| 415 | btrfs_release_path(root, path); |
| 416 | btrfs_free_path(path); |
| 417 | if (ret) { |
| 418 | kfree(root); |
| 419 | return ERR_PTR(ret); |
| 420 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 421 | blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); |
| 422 | root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), |
| 423 | blocksize); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 424 | BUG_ON(!root->node); |
| 425 | insert: |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 426 | root->ref_cows = 1; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 427 | ret = btrfs_find_highest_inode(root, &highest_inode); |
| 428 | if (ret == 0) { |
| 429 | root->highest_inode = highest_inode; |
| 430 | root->last_inode_alloc = highest_inode; |
| 431 | } |
| 432 | return root; |
| 433 | } |
| 434 | |
| 435 | 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] | 436 | struct btrfs_key *location, |
| 437 | const char *name, int namelen) |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 438 | { |
| 439 | struct btrfs_root *root; |
| 440 | int ret; |
| 441 | |
| 442 | root = radix_tree_lookup(&fs_info->fs_roots_radix, |
| 443 | (unsigned long)location->objectid); |
| 444 | if (root) |
| 445 | return root; |
| 446 | |
| 447 | root = btrfs_read_fs_root_no_radix(fs_info, location); |
| 448 | if (IS_ERR(root)) |
| 449 | return root; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 450 | ret = radix_tree_insert(&fs_info->fs_roots_radix, |
| 451 | (unsigned long)root->root_key.objectid, |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 452 | root); |
| 453 | if (ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 454 | free_extent_buffer(root->node); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 455 | kfree(root); |
| 456 | return ERR_PTR(ret); |
| 457 | } |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 458 | |
| 459 | ret = btrfs_set_root_name(root, name, namelen); |
| 460 | if (ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 461 | free_extent_buffer(root->node); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 462 | kfree(root); |
| 463 | return ERR_PTR(ret); |
| 464 | } |
| 465 | |
| 466 | ret = btrfs_sysfs_add_root(root); |
| 467 | if (ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 468 | free_extent_buffer(root->node); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 469 | kfree(root->name); |
| 470 | kfree(root); |
| 471 | return ERR_PTR(ret); |
| 472 | } |
| 473 | |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 474 | ret = btrfs_find_dead_roots(fs_info->tree_root, |
| 475 | root->root_key.objectid, root); |
| 476 | BUG_ON(ret); |
| 477 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 478 | return root; |
| 479 | } |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame^] | 480 | #if 0 |
| 481 | static int add_hasher(struct btrfs_fs_info *info, char *type) { |
| 482 | struct btrfs_hasher *hasher; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 483 | |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame^] | 484 | hasher = kmalloc(sizeof(*hasher), GFP_NOFS); |
| 485 | if (!hasher) |
| 486 | return -ENOMEM; |
| 487 | hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC); |
| 488 | if (!hasher->hash_tfm) { |
| 489 | kfree(hasher); |
| 490 | return -EINVAL; |
| 491 | } |
| 492 | spin_lock(&info->hash_lock); |
| 493 | list_add(&hasher->list, &info->hashers); |
| 494 | spin_unlock(&info->hash_lock); |
| 495 | return 0; |
| 496 | } |
| 497 | #endif |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 498 | struct btrfs_root *open_ctree(struct super_block *sb) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 499 | { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 500 | u32 sectorsize; |
| 501 | u32 nodesize; |
| 502 | u32 leafsize; |
| 503 | u32 blocksize; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 504 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), |
| 505 | GFP_NOFS); |
| 506 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), |
| 507 | GFP_NOFS); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 508 | struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info), |
| 509 | GFP_NOFS); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 510 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 511 | int err = -EIO; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 512 | struct btrfs_super_block *disk_super; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 513 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 514 | if (!extent_root || !tree_root || !fs_info) { |
| 515 | err = -ENOMEM; |
| 516 | goto fail; |
| 517 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 518 | INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 519 | INIT_LIST_HEAD(&fs_info->trans_list); |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 520 | INIT_LIST_HEAD(&fs_info->dead_roots); |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame^] | 521 | INIT_LIST_HEAD(&fs_info->hashers); |
| 522 | spin_lock_init(&fs_info->hash_lock); |
| 523 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 524 | memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj)); |
| 525 | init_completion(&fs_info->kobj_unregister); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 526 | sb_set_blocksize(sb, 4096); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 527 | fs_info->running_transaction = NULL; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 528 | fs_info->last_trans_committed = 0; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 529 | fs_info->tree_root = tree_root; |
| 530 | fs_info->extent_root = extent_root; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 531 | fs_info->sb = sb; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 532 | fs_info->btree_inode = new_inode(sb); |
| 533 | fs_info->btree_inode->i_ino = 1; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 534 | fs_info->btree_inode->i_nlink = 1; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 535 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
| 536 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 537 | extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree, |
| 538 | fs_info->btree_inode->i_mapping, |
| 539 | GFP_NOFS); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 540 | extent_map_tree_init(&fs_info->free_space_cache, |
| 541 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 542 | extent_map_tree_init(&fs_info->block_group_cache, |
| 543 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 544 | extent_map_tree_init(&fs_info->pinned_extents, |
| 545 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
| 546 | extent_map_tree_init(&fs_info->pending_del, |
| 547 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
| 548 | extent_map_tree_init(&fs_info->extent_ins, |
| 549 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 550 | fs_info->do_barriers = 1; |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 551 | fs_info->closing = 0; |
| 552 | |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 553 | INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 554 | BTRFS_I(fs_info->btree_inode)->root = tree_root; |
| 555 | memset(&BTRFS_I(fs_info->btree_inode)->location, 0, |
| 556 | sizeof(struct btrfs_key)); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 557 | insert_inode_hash(fs_info->btree_inode); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 558 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 559 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 560 | mutex_init(&fs_info->trans_mutex); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 561 | mutex_init(&fs_info->fs_mutex); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 562 | |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame^] | 563 | #if 0 |
| 564 | ret = add_hasher(fs_info, "crc32c"); |
| 565 | if (ret) { |
| 566 | printk("btrfs: failed hash setup, modprobe cryptomgr?\n"); |
| 567 | err = -ENOMEM; |
| 568 | goto fail_iput; |
| 569 | } |
| 570 | #endif |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 571 | __setup_root(512, 512, 512, tree_root, |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 572 | fs_info, BTRFS_ROOT_TREE_OBJECTID); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 573 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 574 | fs_info->sb_buffer = read_tree_block(tree_root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 575 | BTRFS_SUPER_INFO_OFFSET, |
| 576 | 512); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 577 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 578 | if (!fs_info->sb_buffer) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 579 | goto fail_iput; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 580 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 581 | read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0, |
| 582 | sizeof(fs_info->super_copy)); |
| 583 | |
| 584 | read_extent_buffer(fs_info->sb_buffer, fs_info->fsid, |
| 585 | (unsigned long)btrfs_super_fsid(fs_info->sb_buffer), |
| 586 | BTRFS_FSID_SIZE); |
| 587 | disk_super = &fs_info->super_copy; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 588 | if (!btrfs_super_root(disk_super)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 589 | goto fail_sb_buffer; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 590 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 591 | nodesize = btrfs_super_nodesize(disk_super); |
| 592 | leafsize = btrfs_super_leafsize(disk_super); |
| 593 | sectorsize = btrfs_super_sectorsize(disk_super); |
| 594 | tree_root->nodesize = nodesize; |
| 595 | tree_root->leafsize = leafsize; |
| 596 | tree_root->sectorsize = sectorsize; |
| 597 | |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 598 | i_size_write(fs_info->btree_inode, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 599 | btrfs_super_total_bytes(disk_super)); |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 600 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 601 | if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, |
| 602 | sizeof(disk_super->magic))) { |
| 603 | printk("btrfs: valid FS not found on %s\n", sb->s_id); |
| 604 | goto fail_sb_buffer; |
| 605 | } |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame^] | 606 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 607 | blocksize = btrfs_level_size(tree_root, |
| 608 | btrfs_super_root_level(disk_super)); |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame^] | 609 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 610 | tree_root->node = read_tree_block(tree_root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 611 | btrfs_super_root(disk_super), |
| 612 | blocksize); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 613 | if (!tree_root->node) |
| 614 | goto fail_sb_buffer; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 615 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 616 | mutex_lock(&fs_info->fs_mutex); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 617 | |
| 618 | ret = find_and_setup_root(tree_root, fs_info, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 619 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 620 | if (ret) { |
| 621 | mutex_unlock(&fs_info->fs_mutex); |
| 622 | goto fail_tree_root; |
| 623 | } |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 624 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 625 | btrfs_read_block_groups(extent_root); |
| 626 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 627 | fs_info->generation = btrfs_super_generation(disk_super) + 1; |
Chris Mason | 5be6f7f | 2007-04-05 13:35:25 -0400 | [diff] [blame] | 628 | mutex_unlock(&fs_info->fs_mutex); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 629 | return tree_root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 630 | |
| 631 | fail_tree_root: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 632 | free_extent_buffer(tree_root->node); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 633 | fail_sb_buffer: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 634 | free_extent_buffer(fs_info->sb_buffer); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 635 | fail_iput: |
| 636 | iput(fs_info->btree_inode); |
| 637 | fail: |
| 638 | kfree(extent_root); |
| 639 | kfree(tree_root); |
| 640 | kfree(fs_info); |
| 641 | return ERR_PTR(err); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 642 | } |
| 643 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 644 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 645 | *root) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 646 | { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 647 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 648 | struct extent_buffer *super = root->fs_info->sb_buffer; |
| 649 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 650 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 651 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super); |
| 652 | ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping, |
| 653 | super->start, super->len); |
| 654 | return ret; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 655 | } |
| 656 | |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 657 | 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] | 658 | { |
| 659 | radix_tree_delete(&fs_info->fs_roots_radix, |
| 660 | (unsigned long)root->root_key.objectid); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 661 | btrfs_sysfs_del_root(root); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 662 | if (root->inode) |
| 663 | iput(root->inode); |
| 664 | if (root->node) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 665 | free_extent_buffer(root->node); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 666 | if (root->commit_root) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 667 | free_extent_buffer(root->commit_root); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 668 | if (root->name) |
| 669 | kfree(root->name); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 670 | kfree(root); |
| 671 | return 0; |
| 672 | } |
| 673 | |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 674 | static int del_fs_roots(struct btrfs_fs_info *fs_info) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 675 | { |
| 676 | int ret; |
| 677 | struct btrfs_root *gang[8]; |
| 678 | int i; |
| 679 | |
| 680 | while(1) { |
| 681 | ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, |
| 682 | (void **)gang, 0, |
| 683 | ARRAY_SIZE(gang)); |
| 684 | if (!ret) |
| 685 | break; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 686 | for (i = 0; i < ret; i++) |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 687 | btrfs_free_fs_root(fs_info, gang[i]); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 688 | } |
| 689 | return 0; |
| 690 | } |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 691 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 692 | int close_ctree(struct btrfs_root *root) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 693 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 694 | int ret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 695 | struct btrfs_trans_handle *trans; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 696 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 697 | |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 698 | fs_info->closing = 1; |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 699 | btrfs_transaction_flush_work(root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 700 | mutex_lock(&fs_info->fs_mutex); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 701 | btrfs_defrag_dirty_roots(root->fs_info); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 702 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 703 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 704 | /* run commit again to drop the original snapshot */ |
| 705 | trans = btrfs_start_transaction(root, 1); |
| 706 | btrfs_commit_transaction(trans, root); |
| 707 | ret = btrfs_write_and_wait_transaction(NULL, root); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 708 | BUG_ON(ret); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 709 | write_ctree_super(NULL, root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 710 | mutex_unlock(&fs_info->fs_mutex); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 711 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 712 | if (fs_info->extent_root->node) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 713 | free_extent_buffer(fs_info->extent_root->node); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 714 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 715 | if (fs_info->tree_root->node) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 716 | free_extent_buffer(fs_info->tree_root->node); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 717 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 718 | free_extent_buffer(fs_info->sb_buffer); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 719 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 720 | btrfs_free_block_groups(root->fs_info); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 721 | del_fs_roots(fs_info); |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame^] | 722 | extent_map_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->extent_tree); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 723 | truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); |
| 724 | iput(fs_info->btree_inode); |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame^] | 725 | #if 0 |
| 726 | while(!list_empty(&fs_info->hashers)) { |
| 727 | struct btrfs_hasher *hasher; |
| 728 | hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher, |
| 729 | hashers); |
| 730 | list_del(&hasher->hashers); |
| 731 | crypto_free_hash(&fs_info->hash_tfm); |
| 732 | kfree(hasher); |
| 733 | } |
| 734 | #endif |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 735 | kfree(fs_info->extent_root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 736 | kfree(fs_info->tree_root); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 737 | return 0; |
| 738 | } |
| 739 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 740 | int btrfs_buffer_uptodate(struct extent_buffer *buf) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 741 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 742 | struct inode *btree_inode = buf->first_page->mapping->host; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 743 | return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf); |
| 744 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 745 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 746 | int btrfs_set_buffer_uptodate(struct extent_buffer *buf) |
| 747 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 748 | struct inode *btree_inode = buf->first_page->mapping->host; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 749 | return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, |
| 750 | buf); |
| 751 | } |
| 752 | |
| 753 | void btrfs_mark_buffer_dirty(struct extent_buffer *buf) |
| 754 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 755 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 756 | u64 transid = btrfs_header_generation(buf); |
| 757 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 758 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 759 | if (transid != root->fs_info->generation) { |
| 760 | printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n", |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 761 | (unsigned long long)buf->start, |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 762 | transid, root->fs_info->generation); |
| 763 | WARN_ON(1); |
| 764 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 765 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 766 | } |
| 767 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 768 | void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr) |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 769 | { |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 770 | balance_dirty_pages_ratelimited_nr( |
| 771 | root->fs_info->btree_inode->i_mapping, nr); |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 772 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 773 | |
| 774 | void btrfs_set_buffer_defrag(struct extent_buffer *buf) |
| 775 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 776 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 777 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 778 | set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start, |
| 779 | buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS); |
| 780 | } |
| 781 | |
| 782 | void btrfs_set_buffer_defrag_done(struct extent_buffer *buf) |
| 783 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 784 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 785 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 786 | set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start, |
| 787 | buf->start + buf->len - 1, EXTENT_DEFRAG_DONE, |
| 788 | GFP_NOFS); |
| 789 | } |
| 790 | |
| 791 | int btrfs_buffer_defrag(struct extent_buffer *buf) |
| 792 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 793 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 794 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 795 | return test_range_bit(&BTRFS_I(btree_inode)->extent_tree, |
| 796 | buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0); |
| 797 | } |
| 798 | |
| 799 | int btrfs_buffer_defrag_done(struct extent_buffer *buf) |
| 800 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 801 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 802 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 803 | return test_range_bit(&BTRFS_I(btree_inode)->extent_tree, |
| 804 | buf->start, buf->start + buf->len - 1, |
| 805 | EXTENT_DEFRAG_DONE, 0); |
| 806 | } |
| 807 | |
| 808 | int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf) |
| 809 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 810 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 811 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 812 | return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree, |
| 813 | buf->start, buf->start + buf->len - 1, |
| 814 | EXTENT_DEFRAG_DONE, GFP_NOFS); |
| 815 | } |
| 816 | |
| 817 | int btrfs_clear_buffer_defrag(struct extent_buffer *buf) |
| 818 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 819 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 820 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 821 | return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree, |
| 822 | buf->start, buf->start + buf->len - 1, |
| 823 | EXTENT_DEFRAG, GFP_NOFS); |
| 824 | } |
| 825 | |
| 826 | int btrfs_read_buffer(struct extent_buffer *buf) |
| 827 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 828 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 829 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 830 | return read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree, |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame^] | 831 | buf, 0, 1); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 832 | } |