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