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