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