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 | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 21 | #include <linux/scatterlist.h> |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 22 | #include <linux/swap.h> |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 23 | #include <linux/radix-tree.h> |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 24 | #include <linux/writeback.h> |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 25 | #include <linux/buffer_head.h> // for block_sync_page |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 26 | #include <linux/workqueue.h> |
Miguel | a5eb62e | 2008-04-11 15:45:51 -0400 | [diff] [blame] | 27 | #include "crc32c.h" |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 28 | #include "ctree.h" |
| 29 | #include "disk-io.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 30 | #include "transaction.h" |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 31 | #include "btrfs_inode.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 32 | #include "volumes.h" |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 33 | #include "print-tree.h" |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 34 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 35 | #if 0 |
| 36 | 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] | 37 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 38 | if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) { |
| 39 | printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n", |
| 40 | (unsigned long long)extent_buffer_blocknr(buf), |
| 41 | (unsigned long long)btrfs_header_blocknr(buf)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 42 | return 1; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 43 | } |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 44 | return 0; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 45 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 46 | #endif |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 47 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 48 | static struct extent_io_ops btree_extent_io_ops; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 49 | static struct workqueue_struct *end_io_workqueue; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 50 | static struct workqueue_struct *async_submit_workqueue; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 51 | |
| 52 | struct end_io_wq { |
| 53 | struct bio *bio; |
| 54 | bio_end_io_t *end_io; |
| 55 | void *private; |
| 56 | struct btrfs_fs_info *info; |
| 57 | int error; |
Chris Mason | 22c5994 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 58 | int metadata; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 59 | struct list_head list; |
| 60 | }; |
Chris Mason | 0da5468 | 2007-11-07 21:08:01 -0500 | [diff] [blame] | 61 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 62 | struct async_submit_bio { |
| 63 | struct inode *inode; |
| 64 | struct bio *bio; |
| 65 | struct list_head list; |
| 66 | extent_submit_bio_hook_t *submit_bio_hook; |
| 67 | int rw; |
| 68 | int mirror_num; |
| 69 | }; |
| 70 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 71 | struct extent_map *btree_get_extent(struct inode *inode, struct page *page, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 72 | size_t page_offset, u64 start, u64 len, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 73 | int create) |
| 74 | { |
| 75 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 76 | struct extent_map *em; |
| 77 | int ret; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 78 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 79 | again: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 80 | spin_lock(&em_tree->lock); |
| 81 | em = lookup_extent_mapping(em_tree, start, len); |
| 82 | spin_unlock(&em_tree->lock); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 83 | if (em) { |
| 84 | goto out; |
| 85 | } |
| 86 | em = alloc_extent_map(GFP_NOFS); |
| 87 | if (!em) { |
| 88 | em = ERR_PTR(-ENOMEM); |
| 89 | goto out; |
| 90 | } |
| 91 | em->start = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 92 | em->len = i_size_read(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 93 | em->block_start = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 94 | em->bdev = inode->i_sb->s_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 95 | |
| 96 | spin_lock(&em_tree->lock); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 97 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 98 | spin_unlock(&em_tree->lock); |
| 99 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 100 | if (ret == -EEXIST) { |
| 101 | free_extent_map(em); |
| 102 | em = NULL; |
| 103 | goto again; |
| 104 | } else if (ret) { |
| 105 | em = ERR_PTR(ret); |
| 106 | } |
| 107 | out: |
| 108 | return em; |
| 109 | } |
| 110 | |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 111 | u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len) |
| 112 | { |
Miguel | a5eb62e | 2008-04-11 15:45:51 -0400 | [diff] [blame] | 113 | return btrfs_crc32c(seed, data, len); |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void btrfs_csum_final(u32 crc, char *result) |
| 117 | { |
| 118 | *(__le32 *)result = ~cpu_to_le32(crc); |
| 119 | } |
| 120 | |
| 121 | static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf, |
| 122 | int verify) |
| 123 | { |
| 124 | char result[BTRFS_CRC32_SIZE]; |
| 125 | unsigned long len; |
| 126 | unsigned long cur_len; |
| 127 | unsigned long offset = BTRFS_CSUM_SIZE; |
| 128 | char *map_token = NULL; |
| 129 | char *kaddr; |
| 130 | unsigned long map_start; |
| 131 | unsigned long map_len; |
| 132 | int err; |
| 133 | u32 crc = ~(u32)0; |
| 134 | |
| 135 | len = buf->len - offset; |
| 136 | while(len > 0) { |
| 137 | err = map_private_extent_buffer(buf, offset, 32, |
| 138 | &map_token, &kaddr, |
| 139 | &map_start, &map_len, KM_USER0); |
| 140 | if (err) { |
| 141 | printk("failed to map extent buffer! %lu\n", |
| 142 | offset); |
| 143 | return 1; |
| 144 | } |
| 145 | cur_len = min(len, map_len - (offset - map_start)); |
| 146 | crc = btrfs_csum_data(root, kaddr + offset - map_start, |
| 147 | crc, cur_len); |
| 148 | len -= cur_len; |
| 149 | offset += cur_len; |
| 150 | unmap_extent_buffer(buf, map_token, KM_USER0); |
| 151 | } |
| 152 | btrfs_csum_final(crc, result); |
| 153 | |
| 154 | if (verify) { |
Chris Mason | e4204de | 2008-01-08 15:46:27 -0500 | [diff] [blame] | 155 | int from_this_trans = 0; |
| 156 | |
| 157 | if (root->fs_info->running_transaction && |
| 158 | btrfs_header_generation(buf) == |
| 159 | root->fs_info->running_transaction->transid) |
| 160 | from_this_trans = 1; |
| 161 | |
| 162 | /* FIXME, this is not good */ |
Chris Mason | 63b10fc | 2008-04-01 11:21:32 -0400 | [diff] [blame] | 163 | if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) { |
Chris Mason | e4204de | 2008-01-08 15:46:27 -0500 | [diff] [blame] | 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 " |
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 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 183 | static int btree_read_extent_buffer_pages(struct btrfs_root *root, |
| 184 | struct extent_buffer *eb, |
| 185 | u64 start) |
| 186 | { |
| 187 | struct extent_io_tree *io_tree; |
| 188 | int ret; |
| 189 | int num_copies = 0; |
| 190 | int mirror_num = 0; |
| 191 | |
| 192 | io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree; |
| 193 | while (1) { |
| 194 | ret = read_extent_buffer_pages(io_tree, eb, start, 1, |
| 195 | btree_get_extent, mirror_num); |
| 196 | if (!ret) { |
| 197 | if (mirror_num) |
| 198 | printk("good read %Lu mirror %d total %d\n", eb->start, mirror_num, num_copies); |
| 199 | return ret; |
| 200 | } |
| 201 | num_copies = btrfs_num_copies(&root->fs_info->mapping_tree, |
| 202 | eb->start, eb->len); |
| 203 | printk("failed to read %Lu mirror %d total %d\n", eb->start, mirror_num, num_copies); |
| 204 | if (num_copies == 1) { |
| 205 | printk("reading %Lu failed only one copy\n", eb->start); |
| 206 | return ret; |
| 207 | } |
| 208 | mirror_num++; |
| 209 | if (mirror_num > num_copies) { |
| 210 | printk("bailing at mirror %d of %d\n", mirror_num, num_copies); |
| 211 | return ret; |
| 212 | } |
| 213 | } |
| 214 | printk("read extent buffer page last\n"); |
| 215 | return -EIO; |
| 216 | } |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 217 | |
| 218 | int csum_dirty_buffer(struct btrfs_root *root, struct page *page) |
| 219 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 220 | struct extent_io_tree *tree; |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 221 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 222 | u64 found_start; |
| 223 | int found_level; |
| 224 | unsigned long len; |
| 225 | struct extent_buffer *eb; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 226 | int ret; |
| 227 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 228 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 229 | |
| 230 | if (page->private == EXTENT_PAGE_PRIVATE) |
| 231 | goto out; |
| 232 | if (!page->private) |
| 233 | goto out; |
| 234 | len = page->private >> 2; |
| 235 | if (len == 0) { |
| 236 | WARN_ON(1); |
| 237 | } |
| 238 | eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 239 | ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE); |
| 240 | BUG_ON(ret); |
Chris Mason | e18e480 | 2008-01-18 10:54:22 -0500 | [diff] [blame] | 241 | btrfs_clear_buffer_defrag(eb); |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 242 | found_start = btrfs_header_bytenr(eb); |
| 243 | if (found_start != start) { |
| 244 | printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n", |
| 245 | start, found_start, len); |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 246 | WARN_ON(1); |
| 247 | goto err; |
| 248 | } |
| 249 | if (eb->first_page != page) { |
| 250 | printk("bad first page %lu %lu\n", eb->first_page->index, |
| 251 | page->index); |
| 252 | WARN_ON(1); |
| 253 | goto err; |
| 254 | } |
| 255 | if (!PageUptodate(page)) { |
| 256 | printk("csum not up to date page %lu\n", page->index); |
| 257 | WARN_ON(1); |
| 258 | goto err; |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 259 | } |
| 260 | found_level = btrfs_header_level(eb); |
Chris Mason | 63b10fc | 2008-04-01 11:21:32 -0400 | [diff] [blame] | 261 | spin_lock(&root->fs_info->hash_lock); |
| 262 | btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); |
| 263 | spin_unlock(&root->fs_info->hash_lock); |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 264 | csum_tree_block(root, eb, 0); |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 265 | err: |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 266 | free_extent_buffer(eb); |
| 267 | out: |
| 268 | return 0; |
| 269 | } |
| 270 | |
Chris Mason | 0da5468 | 2007-11-07 21:08:01 -0500 | [diff] [blame] | 271 | static int btree_writepage_io_hook(struct page *page, u64 start, u64 end) |
| 272 | { |
| 273 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 274 | |
| 275 | csum_dirty_buffer(root, page); |
| 276 | return 0; |
| 277 | } |
| 278 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 279 | int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end, |
| 280 | struct extent_state *state) |
| 281 | { |
| 282 | struct extent_io_tree *tree; |
| 283 | u64 found_start; |
| 284 | int found_level; |
| 285 | unsigned long len; |
| 286 | struct extent_buffer *eb; |
| 287 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 288 | int ret = 0; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 289 | |
| 290 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 291 | if (page->private == EXTENT_PAGE_PRIVATE) |
| 292 | goto out; |
| 293 | if (!page->private) |
| 294 | goto out; |
| 295 | len = page->private >> 2; |
| 296 | if (len == 0) { |
| 297 | WARN_ON(1); |
| 298 | } |
| 299 | eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 300 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 301 | btrfs_clear_buffer_defrag(eb); |
| 302 | found_start = btrfs_header_bytenr(eb); |
| 303 | if (found_start != start) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 304 | printk("bad start on %Lu found %Lu\n", eb->start, found_start); |
| 305 | ret = -EIO; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 306 | goto err; |
| 307 | } |
| 308 | if (eb->first_page != page) { |
| 309 | printk("bad first page %lu %lu\n", eb->first_page->index, |
| 310 | page->index); |
| 311 | WARN_ON(1); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 312 | ret = -EIO; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 313 | goto err; |
| 314 | } |
| 315 | found_level = btrfs_header_level(eb); |
| 316 | |
| 317 | ret = csum_tree_block(root, eb, 1); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 318 | if (ret) |
| 319 | ret = -EIO; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 320 | |
| 321 | end = min_t(u64, eb->len, PAGE_CACHE_SIZE); |
| 322 | end = eb->start + end - 1; |
| 323 | release_extent_buffer_tail_pages(eb); |
| 324 | err: |
| 325 | free_extent_buffer(eb); |
| 326 | out: |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 327 | return ret; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
| 331 | static void end_workqueue_bio(struct bio *bio, int err) |
| 332 | #else |
| 333 | static int end_workqueue_bio(struct bio *bio, |
| 334 | unsigned int bytes_done, int err) |
| 335 | #endif |
| 336 | { |
| 337 | struct end_io_wq *end_io_wq = bio->bi_private; |
| 338 | struct btrfs_fs_info *fs_info; |
| 339 | unsigned long flags; |
| 340 | |
| 341 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 342 | if (bio->bi_size) |
| 343 | return 1; |
| 344 | #endif |
| 345 | |
| 346 | fs_info = end_io_wq->info; |
| 347 | spin_lock_irqsave(&fs_info->end_io_work_lock, flags); |
| 348 | end_io_wq->error = err; |
| 349 | list_add_tail(&end_io_wq->list, &fs_info->end_io_work_list); |
| 350 | spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags); |
| 351 | queue_work(end_io_workqueue, &fs_info->end_io_work); |
| 352 | |
| 353 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 354 | return 0; |
| 355 | #endif |
| 356 | } |
| 357 | |
Chris Mason | 22c5994 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 358 | int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, |
| 359 | int metadata) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 360 | { |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 361 | struct end_io_wq *end_io_wq; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 362 | end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS); |
| 363 | if (!end_io_wq) |
| 364 | return -ENOMEM; |
| 365 | |
| 366 | end_io_wq->private = bio->bi_private; |
| 367 | end_io_wq->end_io = bio->bi_end_io; |
Chris Mason | 22c5994 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 368 | end_io_wq->info = info; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 369 | end_io_wq->error = 0; |
| 370 | end_io_wq->bio = bio; |
Chris Mason | 22c5994 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 371 | end_io_wq->metadata = metadata; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 372 | |
| 373 | bio->bi_private = end_io_wq; |
| 374 | bio->bi_end_io = end_workqueue_bio; |
Chris Mason | 22c5994 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 375 | return 0; |
| 376 | } |
| 377 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 378 | int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode, |
| 379 | int rw, struct bio *bio, int mirror_num, |
| 380 | extent_submit_bio_hook_t *submit_bio_hook) |
| 381 | { |
| 382 | struct async_submit_bio *async; |
| 383 | |
Chris Mason | 7b859fe | 2008-04-16 13:06:16 -0400 | [diff] [blame^] | 384 | /* |
| 385 | * inline writerback should stay inline, only hop to the async |
| 386 | * queue if we're pdflush |
| 387 | */ |
| 388 | if (!current_is_pdflush()) |
| 389 | return submit_bio_hook(inode, rw, bio, mirror_num); |
| 390 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 391 | async = kmalloc(sizeof(*async), GFP_NOFS); |
| 392 | if (!async) |
| 393 | return -ENOMEM; |
| 394 | |
| 395 | async->inode = inode; |
| 396 | async->rw = rw; |
| 397 | async->bio = bio; |
| 398 | async->mirror_num = mirror_num; |
| 399 | async->submit_bio_hook = submit_bio_hook; |
| 400 | |
| 401 | spin_lock(&fs_info->async_submit_work_lock); |
| 402 | list_add_tail(&async->list, &fs_info->async_submit_work_list); |
| 403 | spin_unlock(&fs_info->async_submit_work_lock); |
| 404 | |
| 405 | queue_work(async_submit_workqueue, &fs_info->async_submit_work); |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | static int __btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 410 | int mirror_num) |
Chris Mason | 22c5994 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 411 | { |
| 412 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 413 | u64 offset; |
| 414 | int ret; |
| 415 | |
| 416 | offset = bio->bi_sector << 9; |
| 417 | |
| 418 | if (rw & (1 << BIO_RW)) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 419 | return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num); |
Chris Mason | 22c5994 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 1); |
| 423 | BUG_ON(ret); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 424 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 425 | if (offset == BTRFS_SUPER_INFO_OFFSET) { |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 426 | bio->bi_bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 427 | submit_bio(rw, bio); |
| 428 | return 0; |
| 429 | } |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 430 | return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 431 | } |
| 432 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 433 | static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
| 434 | int mirror_num) |
| 435 | { |
| 436 | if (!(rw & (1 << BIO_RW))) { |
| 437 | return __btree_submit_bio_hook(inode, rw, bio, mirror_num); |
| 438 | } |
| 439 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
| 440 | inode, rw, bio, mirror_num, |
| 441 | __btree_submit_bio_hook); |
| 442 | } |
| 443 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 444 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
| 445 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 446 | struct extent_io_tree *tree; |
| 447 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 448 | return extent_write_full_page(tree, page, btree_get_extent, wbc); |
| 449 | } |
Chris Mason | 0da5468 | 2007-11-07 21:08:01 -0500 | [diff] [blame] | 450 | |
| 451 | static int btree_writepages(struct address_space *mapping, |
| 452 | struct writeback_control *wbc) |
| 453 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 454 | struct extent_io_tree *tree; |
| 455 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 456 | if (wbc->sync_mode == WB_SYNC_NONE) { |
Chris Mason | 793955b | 2007-11-26 16:34:41 -0800 | [diff] [blame] | 457 | u64 num_dirty; |
| 458 | u64 start = 0; |
| 459 | unsigned long thresh = 96 * 1024 * 1024; |
Chris Mason | 448d640 | 2007-11-27 07:52:01 -0800 | [diff] [blame] | 460 | |
| 461 | if (wbc->for_kupdate) |
| 462 | return 0; |
| 463 | |
Chris Mason | ca66462 | 2007-11-27 11:16:35 -0500 | [diff] [blame] | 464 | if (current_is_pdflush()) { |
| 465 | thresh = 96 * 1024 * 1024; |
| 466 | } else { |
| 467 | thresh = 8 * 1024 * 1024; |
| 468 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 469 | num_dirty = count_range_bits(tree, &start, (u64)-1, |
| 470 | thresh, EXTENT_DIRTY); |
Chris Mason | 793955b | 2007-11-26 16:34:41 -0800 | [diff] [blame] | 471 | if (num_dirty < thresh) { |
| 472 | return 0; |
| 473 | } |
| 474 | } |
Chris Mason | 0da5468 | 2007-11-07 21:08:01 -0500 | [diff] [blame] | 475 | return extent_writepages(tree, mapping, btree_get_extent, wbc); |
| 476 | } |
| 477 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 478 | int btree_readpage(struct file *file, struct page *page) |
| 479 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 480 | struct extent_io_tree *tree; |
| 481 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 482 | return extent_read_full_page(tree, page, btree_get_extent); |
| 483 | } |
| 484 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 485 | static int btree_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 486 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 487 | struct extent_io_tree *tree; |
| 488 | struct extent_map_tree *map; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 489 | int ret; |
| 490 | |
Chris Mason | 3dd3991 | 2008-04-11 10:51:07 -0400 | [diff] [blame] | 491 | if (page_count(page) > 3) { |
| 492 | /* once for page->private, once for the caller, once |
| 493 | * once for the page cache |
| 494 | */ |
| 495 | return 0; |
| 496 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 497 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 498 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 499 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 500 | if (ret == 1) { |
Chris Mason | 728131d | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 501 | invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 502 | ClearPagePrivate(page); |
| 503 | set_page_private(page, 0); |
| 504 | page_cache_release(page); |
| 505 | } |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 506 | return ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 507 | } |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 508 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 509 | static void btree_invalidatepage(struct page *page, unsigned long offset) |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 510 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 511 | struct extent_io_tree *tree; |
| 512 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 513 | extent_invalidatepage(tree, page, offset); |
| 514 | btree_releasepage(page, GFP_NOFS); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 515 | } |
| 516 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 517 | #if 0 |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 518 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
| 519 | { |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 520 | struct buffer_head *bh; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 521 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 522 | struct buffer_head *head; |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 523 | if (!page_has_buffers(page)) { |
| 524 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, |
| 525 | (1 << BH_Dirty)|(1 << BH_Uptodate)); |
| 526 | } |
| 527 | head = page_buffers(page); |
| 528 | bh = head; |
| 529 | do { |
| 530 | if (buffer_dirty(bh)) |
| 531 | csum_tree_block(root, bh, 0); |
| 532 | bh = bh->b_this_page; |
| 533 | } while (bh != head); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 534 | return block_write_full_page(page, btree_get_block, wbc); |
| 535 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 536 | #endif |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 537 | |
| 538 | static struct address_space_operations btree_aops = { |
| 539 | .readpage = btree_readpage, |
| 540 | .writepage = btree_writepage, |
Chris Mason | 0da5468 | 2007-11-07 21:08:01 -0500 | [diff] [blame] | 541 | .writepages = btree_writepages, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 542 | .releasepage = btree_releasepage, |
| 543 | .invalidatepage = btree_invalidatepage, |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 544 | .sync_page = block_sync_page, |
| 545 | }; |
| 546 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 547 | int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize) |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 548 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 549 | struct extent_buffer *buf = NULL; |
| 550 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 551 | int ret = 0; |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 552 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 553 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 554 | if (!buf) |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 555 | return 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 556 | read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 557 | buf, 0, 0, btree_get_extent, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 558 | free_extent_buffer(buf); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 559 | return ret; |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 560 | } |
| 561 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 562 | static int close_all_devices(struct btrfs_fs_info *fs_info) |
| 563 | { |
| 564 | struct list_head *list; |
| 565 | struct list_head *next; |
| 566 | struct btrfs_device *device; |
| 567 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 568 | list = &fs_info->fs_devices->devices; |
| 569 | list_for_each(next, list) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 570 | device = list_entry(next, struct btrfs_device, dev_list); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 571 | if (device->bdev && device->bdev != fs_info->sb->s_bdev) |
| 572 | close_bdev_excl(device->bdev); |
| 573 | device->bdev = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 574 | } |
| 575 | return 0; |
| 576 | } |
| 577 | |
Chris Mason | 0999df5 | 2008-04-01 13:48:14 -0400 | [diff] [blame] | 578 | int btrfs_verify_block_csum(struct btrfs_root *root, |
| 579 | struct extent_buffer *buf) |
| 580 | { |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 581 | return btrfs_buffer_uptodate(buf); |
Chris Mason | 0999df5 | 2008-04-01 13:48:14 -0400 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, |
| 585 | u64 bytenr, u32 blocksize) |
| 586 | { |
| 587 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 588 | struct extent_buffer *eb; |
| 589 | eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree, |
| 590 | bytenr, blocksize, GFP_NOFS); |
| 591 | return eb; |
| 592 | } |
| 593 | |
| 594 | struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, |
| 595 | u64 bytenr, u32 blocksize) |
| 596 | { |
| 597 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 598 | struct extent_buffer *eb; |
| 599 | |
| 600 | eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree, |
| 601 | bytenr, blocksize, NULL, GFP_NOFS); |
| 602 | return eb; |
| 603 | } |
| 604 | |
| 605 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 606 | struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, |
| 607 | u32 blocksize) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 608 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 609 | struct extent_buffer *buf = NULL; |
| 610 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 611 | struct extent_io_tree *io_tree; |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 612 | int ret; |
| 613 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 614 | io_tree = &BTRFS_I(btree_inode)->io_tree; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 615 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 616 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 617 | if (!buf) |
| 618 | return NULL; |
Chris Mason | e4204de | 2008-01-08 15:46:27 -0500 | [diff] [blame] | 619 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 620 | ret = btree_read_extent_buffer_pages(root, buf, 0); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 621 | |
| 622 | if (ret == 0) { |
| 623 | buf->flags |= EXTENT_UPTODATE; |
| 624 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 625 | return buf; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 626 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 627 | } |
| 628 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 629 | 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] | 630 | struct extent_buffer *buf) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 631 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 632 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 633 | if (btrfs_header_generation(buf) == |
| 634 | root->fs_info->running_transaction->transid) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 635 | clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 636 | buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | int wait_on_tree_block_writeback(struct btrfs_root *root, |
| 641 | struct extent_buffer *buf) |
| 642 | { |
| 643 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 644 | wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 645 | buf); |
| 646 | return 0; |
| 647 | } |
| 648 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 649 | static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 650 | u32 stripesize, struct btrfs_root *root, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 651 | struct btrfs_fs_info *fs_info, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 652 | u64 objectid) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 653 | { |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 654 | root->node = NULL; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 655 | root->inode = NULL; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 656 | root->commit_root = NULL; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 657 | root->sectorsize = sectorsize; |
| 658 | root->nodesize = nodesize; |
| 659 | root->leafsize = leafsize; |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 660 | root->stripesize = stripesize; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 661 | root->ref_cows = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 662 | root->track_dirty = 0; |
| 663 | |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 664 | root->fs_info = fs_info; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 665 | root->objectid = objectid; |
| 666 | root->last_trans = 0; |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 667 | root->highest_inode = 0; |
| 668 | root->last_inode_alloc = 0; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 669 | root->name = NULL; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 670 | root->in_sysfs = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 671 | |
| 672 | INIT_LIST_HEAD(&root->dirty_list); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 673 | memset(&root->root_key, 0, sizeof(root->root_key)); |
| 674 | memset(&root->root_item, 0, sizeof(root->root_item)); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 675 | memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 676 | memset(&root->root_kobj, 0, sizeof(root->root_kobj)); |
| 677 | init_completion(&root->kobj_unregister); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 678 | root->defrag_running = 0; |
| 679 | root->defrag_level = 0; |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 680 | root->root_key.objectid = objectid; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 681 | return 0; |
| 682 | } |
| 683 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 684 | static int find_and_setup_root(struct btrfs_root *tree_root, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 685 | struct btrfs_fs_info *fs_info, |
| 686 | u64 objectid, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 687 | struct btrfs_root *root) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 688 | { |
| 689 | int ret; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 690 | u32 blocksize; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 691 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 692 | __setup_root(tree_root->nodesize, tree_root->leafsize, |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 693 | tree_root->sectorsize, tree_root->stripesize, |
| 694 | root, fs_info, objectid); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 695 | ret = btrfs_find_last_root(tree_root, objectid, |
| 696 | &root->root_item, &root->root_key); |
| 697 | BUG_ON(ret); |
| 698 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 699 | blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); |
| 700 | root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), |
| 701 | blocksize); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 702 | BUG_ON(!root->node); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 703 | return 0; |
| 704 | } |
| 705 | |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 706 | struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, |
| 707 | struct btrfs_key *location) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 708 | { |
| 709 | struct btrfs_root *root; |
| 710 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 711 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 712 | struct extent_buffer *l; |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 713 | u64 highest_inode; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 714 | u32 blocksize; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 715 | int ret = 0; |
| 716 | |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 717 | root = kzalloc(sizeof(*root), GFP_NOFS); |
Chris Mason | 0cf6c62 | 2007-06-09 09:22:25 -0400 | [diff] [blame] | 718 | if (!root) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 719 | return ERR_PTR(-ENOMEM); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 720 | if (location->offset == (u64)-1) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 721 | ret = find_and_setup_root(tree_root, fs_info, |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 722 | location->objectid, root); |
| 723 | if (ret) { |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 724 | kfree(root); |
| 725 | return ERR_PTR(ret); |
| 726 | } |
| 727 | goto insert; |
| 728 | } |
| 729 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 730 | __setup_root(tree_root->nodesize, tree_root->leafsize, |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 731 | tree_root->sectorsize, tree_root->stripesize, |
| 732 | root, fs_info, location->objectid); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 733 | |
| 734 | path = btrfs_alloc_path(); |
| 735 | BUG_ON(!path); |
| 736 | ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); |
| 737 | if (ret != 0) { |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 738 | if (ret > 0) |
| 739 | ret = -ENOENT; |
| 740 | goto out; |
| 741 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 742 | l = path->nodes[0]; |
| 743 | read_extent_buffer(l, &root->root_item, |
| 744 | btrfs_item_ptr_offset(l, path->slots[0]), |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 745 | sizeof(root->root_item)); |
Yan Zheng | 44b36eb | 2007-10-19 09:22:56 -0400 | [diff] [blame] | 746 | memcpy(&root->root_key, location, sizeof(*location)); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 747 | ret = 0; |
| 748 | out: |
| 749 | btrfs_release_path(root, path); |
| 750 | btrfs_free_path(path); |
| 751 | if (ret) { |
| 752 | kfree(root); |
| 753 | return ERR_PTR(ret); |
| 754 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 755 | blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); |
| 756 | root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), |
| 757 | blocksize); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 758 | BUG_ON(!root->node); |
| 759 | insert: |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 760 | root->ref_cows = 1; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 761 | ret = btrfs_find_highest_inode(root, &highest_inode); |
| 762 | if (ret == 0) { |
| 763 | root->highest_inode = highest_inode; |
| 764 | root->last_inode_alloc = highest_inode; |
| 765 | } |
| 766 | return root; |
| 767 | } |
| 768 | |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 769 | struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, |
| 770 | u64 root_objectid) |
| 771 | { |
| 772 | struct btrfs_root *root; |
| 773 | |
| 774 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID) |
| 775 | return fs_info->tree_root; |
| 776 | if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID) |
| 777 | return fs_info->extent_root; |
| 778 | |
| 779 | root = radix_tree_lookup(&fs_info->fs_roots_radix, |
| 780 | (unsigned long)root_objectid); |
| 781 | return root; |
| 782 | } |
| 783 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 784 | struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, |
| 785 | struct btrfs_key *location) |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 786 | { |
| 787 | struct btrfs_root *root; |
| 788 | int ret; |
| 789 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 790 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) |
| 791 | return fs_info->tree_root; |
| 792 | if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) |
| 793 | return fs_info->extent_root; |
| 794 | |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 795 | root = radix_tree_lookup(&fs_info->fs_roots_radix, |
| 796 | (unsigned long)location->objectid); |
| 797 | if (root) |
| 798 | return root; |
| 799 | |
| 800 | root = btrfs_read_fs_root_no_radix(fs_info, location); |
| 801 | if (IS_ERR(root)) |
| 802 | return root; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 803 | ret = radix_tree_insert(&fs_info->fs_roots_radix, |
| 804 | (unsigned long)root->root_key.objectid, |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 805 | root); |
| 806 | if (ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 807 | free_extent_buffer(root->node); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 808 | kfree(root); |
| 809 | return ERR_PTR(ret); |
| 810 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 811 | ret = btrfs_find_dead_roots(fs_info->tree_root, |
| 812 | root->root_key.objectid, root); |
| 813 | BUG_ON(ret); |
| 814 | |
| 815 | return root; |
| 816 | } |
| 817 | |
| 818 | struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, |
| 819 | struct btrfs_key *location, |
| 820 | const char *name, int namelen) |
| 821 | { |
| 822 | struct btrfs_root *root; |
| 823 | int ret; |
| 824 | |
| 825 | root = btrfs_read_fs_root_no_name(fs_info, location); |
| 826 | if (!root) |
| 827 | return NULL; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 828 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 829 | if (root->in_sysfs) |
| 830 | return root; |
| 831 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 832 | ret = btrfs_set_root_name(root, name, namelen); |
| 833 | if (ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 834 | free_extent_buffer(root->node); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 835 | kfree(root); |
| 836 | return ERR_PTR(ret); |
| 837 | } |
| 838 | |
| 839 | ret = btrfs_sysfs_add_root(root); |
| 840 | if (ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 841 | free_extent_buffer(root->node); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 842 | kfree(root->name); |
| 843 | kfree(root); |
| 844 | return ERR_PTR(ret); |
| 845 | } |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 846 | root->in_sysfs = 1; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 847 | return root; |
| 848 | } |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 849 | #if 0 |
| 850 | static int add_hasher(struct btrfs_fs_info *info, char *type) { |
| 851 | struct btrfs_hasher *hasher; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 852 | |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 853 | hasher = kmalloc(sizeof(*hasher), GFP_NOFS); |
| 854 | if (!hasher) |
| 855 | return -ENOMEM; |
| 856 | hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC); |
| 857 | if (!hasher->hash_tfm) { |
| 858 | kfree(hasher); |
| 859 | return -EINVAL; |
| 860 | } |
| 861 | spin_lock(&info->hash_lock); |
| 862 | list_add(&hasher->list, &info->hashers); |
| 863 | spin_unlock(&info->hash_lock); |
| 864 | return 0; |
| 865 | } |
| 866 | #endif |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 867 | |
| 868 | static int btrfs_congested_fn(void *congested_data, int bdi_bits) |
| 869 | { |
| 870 | struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; |
| 871 | int ret = 0; |
| 872 | struct list_head *cur; |
| 873 | struct btrfs_device *device; |
| 874 | struct backing_dev_info *bdi; |
| 875 | |
| 876 | list_for_each(cur, &info->fs_devices->devices) { |
| 877 | device = list_entry(cur, struct btrfs_device, dev_list); |
| 878 | bdi = blk_get_backing_dev_info(device->bdev); |
| 879 | if (bdi && bdi_congested(bdi, bdi_bits)) { |
| 880 | ret = 1; |
| 881 | break; |
| 882 | } |
| 883 | } |
| 884 | return ret; |
| 885 | } |
| 886 | |
| 887 | void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page) |
| 888 | { |
| 889 | struct list_head *cur; |
| 890 | struct btrfs_device *device; |
| 891 | struct btrfs_fs_info *info; |
| 892 | |
| 893 | info = (struct btrfs_fs_info *)bdi->unplug_io_data; |
| 894 | list_for_each(cur, &info->fs_devices->devices) { |
| 895 | device = list_entry(cur, struct btrfs_device, dev_list); |
| 896 | bdi = blk_get_backing_dev_info(device->bdev); |
| 897 | if (bdi->unplug_io_fn) { |
| 898 | bdi->unplug_io_fn(bdi, page); |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi) |
| 904 | { |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 905 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 906 | bdi_init(bdi); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 907 | #endif |
Chris Mason | 83041ad | 2008-03-26 12:02:55 -0400 | [diff] [blame] | 908 | bdi->ra_pages = default_backing_dev_info.ra_pages * 4; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 909 | bdi->state = 0; |
| 910 | bdi->capabilities = default_backing_dev_info.capabilities; |
| 911 | bdi->unplug_io_fn = btrfs_unplug_io_fn; |
| 912 | bdi->unplug_io_data = info; |
| 913 | bdi->congested_fn = btrfs_congested_fn; |
| 914 | bdi->congested_data = info; |
| 915 | return 0; |
| 916 | } |
| 917 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 918 | static int bio_ready_for_csum(struct bio *bio) |
| 919 | { |
| 920 | u64 length = 0; |
| 921 | u64 buf_len = 0; |
| 922 | u64 start = 0; |
| 923 | struct page *page; |
| 924 | struct extent_io_tree *io_tree = NULL; |
| 925 | struct btrfs_fs_info *info = NULL; |
| 926 | struct bio_vec *bvec; |
| 927 | int i; |
| 928 | int ret; |
| 929 | |
| 930 | bio_for_each_segment(bvec, bio, i) { |
| 931 | page = bvec->bv_page; |
| 932 | if (page->private == EXTENT_PAGE_PRIVATE) { |
| 933 | length += bvec->bv_len; |
| 934 | continue; |
| 935 | } |
| 936 | if (!page->private) { |
| 937 | length += bvec->bv_len; |
| 938 | continue; |
| 939 | } |
| 940 | length = bvec->bv_len; |
| 941 | buf_len = page->private >> 2; |
| 942 | start = page_offset(page) + bvec->bv_offset; |
| 943 | io_tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 944 | info = BTRFS_I(page->mapping->host)->root->fs_info; |
| 945 | } |
| 946 | /* are we fully contained in this bio? */ |
| 947 | if (buf_len <= length) |
| 948 | return 1; |
| 949 | |
| 950 | ret = extent_range_uptodate(io_tree, start + length, |
| 951 | start + buf_len - 1); |
| 952 | if (ret == 1) |
| 953 | return ret; |
| 954 | return ret; |
| 955 | } |
| 956 | |
| 957 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 958 | static void btrfs_end_io_csum(void *p) |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 959 | #else |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 960 | static void btrfs_end_io_csum(struct work_struct *work) |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 961 | #endif |
| 962 | { |
| 963 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
| 964 | struct btrfs_fs_info *fs_info = p; |
| 965 | #else |
| 966 | struct btrfs_fs_info *fs_info = container_of(work, |
| 967 | struct btrfs_fs_info, |
| 968 | end_io_work); |
| 969 | #endif |
| 970 | unsigned long flags; |
| 971 | struct end_io_wq *end_io_wq; |
| 972 | struct bio *bio; |
| 973 | struct list_head *next; |
| 974 | int error; |
| 975 | int was_empty; |
| 976 | |
| 977 | while(1) { |
| 978 | spin_lock_irqsave(&fs_info->end_io_work_lock, flags); |
| 979 | if (list_empty(&fs_info->end_io_work_list)) { |
| 980 | spin_unlock_irqrestore(&fs_info->end_io_work_lock, |
| 981 | flags); |
| 982 | return; |
| 983 | } |
| 984 | next = fs_info->end_io_work_list.next; |
| 985 | list_del(next); |
| 986 | spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags); |
| 987 | |
| 988 | end_io_wq = list_entry(next, struct end_io_wq, list); |
| 989 | |
| 990 | bio = end_io_wq->bio; |
Chris Mason | 22c5994 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 991 | if (end_io_wq->metadata && !bio_ready_for_csum(bio)) { |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 992 | spin_lock_irqsave(&fs_info->end_io_work_lock, flags); |
| 993 | was_empty = list_empty(&fs_info->end_io_work_list); |
| 994 | list_add_tail(&end_io_wq->list, |
| 995 | &fs_info->end_io_work_list); |
| 996 | spin_unlock_irqrestore(&fs_info->end_io_work_lock, |
| 997 | flags); |
| 998 | if (was_empty) |
| 999 | return; |
| 1000 | continue; |
| 1001 | } |
| 1002 | error = end_io_wq->error; |
| 1003 | bio->bi_private = end_io_wq->private; |
| 1004 | bio->bi_end_io = end_io_wq->end_io; |
| 1005 | kfree(end_io_wq); |
Miguel | 73f61b2 | 2008-04-11 15:50:59 -0400 | [diff] [blame] | 1006 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 1007 | bio_endio(bio, bio->bi_size, error); |
Miguel | 73f61b2 | 2008-04-11 15:50:59 -0400 | [diff] [blame] | 1008 | #else |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1009 | bio_endio(bio, error); |
Miguel | 73f61b2 | 2008-04-11 15:50:59 -0400 | [diff] [blame] | 1010 | #endif |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1014 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
| 1015 | static void btrfs_async_submit_work(void *p) |
| 1016 | #else |
| 1017 | static void btrfs_async_submit_work(struct work_struct *work) |
| 1018 | #endif |
| 1019 | { |
| 1020 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
| 1021 | struct btrfs_fs_info *fs_info = p; |
| 1022 | #else |
| 1023 | struct btrfs_fs_info *fs_info = container_of(work, |
| 1024 | struct btrfs_fs_info, |
| 1025 | async_submit_work); |
| 1026 | #endif |
| 1027 | struct async_submit_bio *async; |
| 1028 | struct list_head *next; |
| 1029 | |
| 1030 | while(1) { |
| 1031 | spin_lock(&fs_info->async_submit_work_lock); |
| 1032 | if (list_empty(&fs_info->async_submit_work_list)) { |
| 1033 | spin_unlock(&fs_info->async_submit_work_lock); |
| 1034 | return; |
| 1035 | } |
| 1036 | next = fs_info->async_submit_work_list.next; |
| 1037 | list_del(next); |
| 1038 | spin_unlock(&fs_info->async_submit_work_lock); |
| 1039 | |
| 1040 | async = list_entry(next, struct async_submit_bio, list); |
| 1041 | async->submit_bio_hook(async->inode, async->rw, async->bio, |
| 1042 | async->mirror_num); |
| 1043 | kfree(async); |
| 1044 | } |
| 1045 | } |
| 1046 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1047 | struct btrfs_root *open_ctree(struct super_block *sb, |
| 1048 | struct btrfs_fs_devices *fs_devices) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1049 | { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1050 | u32 sectorsize; |
| 1051 | u32 nodesize; |
| 1052 | u32 leafsize; |
| 1053 | u32 blocksize; |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1054 | u32 stripesize; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1055 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), |
| 1056 | GFP_NOFS); |
| 1057 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), |
| 1058 | GFP_NOFS); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1059 | struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info), |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1060 | GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1061 | struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root), |
| 1062 | GFP_NOFS); |
| 1063 | struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root), |
| 1064 | GFP_NOFS); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1065 | int ret; |
Yan | e58ca02 | 2008-04-01 11:21:34 -0400 | [diff] [blame] | 1066 | int err = -EINVAL; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1067 | struct btrfs_super_block *disk_super; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1068 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1069 | if (!extent_root || !tree_root || !fs_info) { |
| 1070 | err = -ENOMEM; |
| 1071 | goto fail; |
| 1072 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1073 | end_io_workqueue = create_workqueue("btrfs-end-io"); |
| 1074 | BUG_ON(!end_io_workqueue); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1075 | async_submit_workqueue = create_workqueue("btrfs-async-submit"); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1076 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 1077 | INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 1078 | INIT_LIST_HEAD(&fs_info->trans_list); |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 1079 | INIT_LIST_HEAD(&fs_info->dead_roots); |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 1080 | INIT_LIST_HEAD(&fs_info->hashers); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1081 | INIT_LIST_HEAD(&fs_info->end_io_work_list); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1082 | INIT_LIST_HEAD(&fs_info->async_submit_work_list); |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 1083 | spin_lock_init(&fs_info->hash_lock); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1084 | spin_lock_init(&fs_info->end_io_work_lock); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1085 | spin_lock_init(&fs_info->async_submit_work_lock); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1086 | spin_lock_init(&fs_info->delalloc_lock); |
Chris Mason | cee36a0 | 2008-01-15 08:40:48 -0500 | [diff] [blame] | 1087 | spin_lock_init(&fs_info->new_trans_lock); |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 1088 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1089 | init_completion(&fs_info->kobj_unregister); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 1090 | sb_set_blocksize(sb, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 1091 | fs_info->tree_root = tree_root; |
| 1092 | fs_info->extent_root = extent_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1093 | fs_info->chunk_root = chunk_root; |
| 1094 | fs_info->dev_root = dev_root; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1095 | fs_info->fs_devices = fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1096 | INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1097 | INIT_LIST_HEAD(&fs_info->space_info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1098 | btrfs_mapping_init(&fs_info->mapping_tree); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1099 | fs_info->sb = sb; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1100 | fs_info->max_extent = (u64)-1; |
Chris Mason | 6f568d3 | 2008-01-29 16:03:38 -0500 | [diff] [blame] | 1101 | fs_info->max_inline = 8192 * 1024; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 1102 | setup_bdi(fs_info, &fs_info->bdi); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 1103 | fs_info->btree_inode = new_inode(sb); |
| 1104 | fs_info->btree_inode->i_ino = 1; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1105 | fs_info->btree_inode->i_nlink = 1; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 1106 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
| 1107 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 1108 | fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi; |
| 1109 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1110 | extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1111 | fs_info->btree_inode->i_mapping, |
| 1112 | GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1113 | extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree, |
| 1114 | GFP_NOFS); |
Chris Mason | 0da5468 | 2007-11-07 21:08:01 -0500 | [diff] [blame] | 1115 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1116 | BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops; |
| 1117 | |
| 1118 | extent_io_tree_init(&fs_info->free_space_cache, |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1119 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1120 | extent_io_tree_init(&fs_info->block_group_cache, |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1121 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1122 | extent_io_tree_init(&fs_info->pinned_extents, |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1123 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1124 | extent_io_tree_init(&fs_info->pending_del, |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1125 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1126 | extent_io_tree_init(&fs_info->extent_ins, |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1127 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1128 | fs_info->do_barriers = 1; |
Chris Mason | e18e480 | 2008-01-18 10:54:22 -0500 | [diff] [blame] | 1129 | |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 1130 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 1131 | INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum, fs_info); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1132 | INIT_WORK(&fs_info->async_submit_work, btrfs_async_submit_work, |
| 1133 | fs_info); |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 1134 | INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info); |
| 1135 | #else |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 1136 | INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1137 | INIT_WORK(&fs_info->async_submit_work, btrfs_async_submit_work); |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 1138 | INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner); |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 1139 | #endif |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 1140 | BTRFS_I(fs_info->btree_inode)->root = tree_root; |
| 1141 | memset(&BTRFS_I(fs_info->btree_inode)->location, 0, |
| 1142 | sizeof(struct btrfs_key)); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 1143 | insert_inode_hash(fs_info->btree_inode); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 1144 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1145 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1146 | mutex_init(&fs_info->trans_mutex); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 1147 | mutex_init(&fs_info->fs_mutex); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 1148 | |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 1149 | #if 0 |
| 1150 | ret = add_hasher(fs_info, "crc32c"); |
| 1151 | if (ret) { |
| 1152 | printk("btrfs: failed hash setup, modprobe cryptomgr?\n"); |
| 1153 | err = -ENOMEM; |
| 1154 | goto fail_iput; |
| 1155 | } |
| 1156 | #endif |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1157 | __setup_root(4096, 4096, 4096, 4096, tree_root, |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1158 | fs_info, BTRFS_ROOT_TREE_OBJECTID); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1159 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1160 | fs_info->sb_buffer = read_tree_block(tree_root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1161 | BTRFS_SUPER_INFO_OFFSET, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1162 | 4096); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 1163 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 1164 | if (!fs_info->sb_buffer) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1165 | goto fail_iput; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1166 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1167 | read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0, |
| 1168 | sizeof(fs_info->super_copy)); |
| 1169 | |
| 1170 | read_extent_buffer(fs_info->sb_buffer, fs_info->fsid, |
| 1171 | (unsigned long)btrfs_super_fsid(fs_info->sb_buffer), |
| 1172 | BTRFS_FSID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1173 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1174 | disk_super = &fs_info->super_copy; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 1175 | if (!btrfs_super_root(disk_super)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1176 | goto fail_sb_buffer; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 1177 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1178 | if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) { |
| 1179 | printk("Btrfs: wanted %llu devices, but found %llu\n", |
| 1180 | (unsigned long long)btrfs_super_num_devices(disk_super), |
| 1181 | (unsigned long long)fs_devices->num_devices); |
| 1182 | goto fail_sb_buffer; |
| 1183 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1184 | nodesize = btrfs_super_nodesize(disk_super); |
| 1185 | leafsize = btrfs_super_leafsize(disk_super); |
| 1186 | sectorsize = btrfs_super_sectorsize(disk_super); |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1187 | stripesize = btrfs_super_stripesize(disk_super); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1188 | tree_root->nodesize = nodesize; |
| 1189 | tree_root->leafsize = leafsize; |
| 1190 | tree_root->sectorsize = sectorsize; |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1191 | tree_root->stripesize = stripesize; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1192 | sb_set_blocksize(sb, sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1193 | |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 1194 | i_size_write(fs_info->btree_inode, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1195 | btrfs_super_total_bytes(disk_super)); |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 1196 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1197 | if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, |
| 1198 | sizeof(disk_super->magic))) { |
| 1199 | printk("btrfs: valid FS not found on %s\n", sb->s_id); |
| 1200 | goto fail_sb_buffer; |
| 1201 | } |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 1202 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1203 | mutex_lock(&fs_info->fs_mutex); |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1204 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1205 | ret = btrfs_read_sys_array(tree_root); |
| 1206 | BUG_ON(ret); |
| 1207 | |
| 1208 | blocksize = btrfs_level_size(tree_root, |
| 1209 | btrfs_super_chunk_root_level(disk_super)); |
| 1210 | |
| 1211 | __setup_root(nodesize, leafsize, sectorsize, stripesize, |
| 1212 | chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); |
| 1213 | |
| 1214 | chunk_root->node = read_tree_block(chunk_root, |
| 1215 | btrfs_super_chunk_root(disk_super), |
| 1216 | blocksize); |
| 1217 | BUG_ON(!chunk_root->node); |
| 1218 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1219 | read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, |
| 1220 | (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node), |
| 1221 | BTRFS_UUID_SIZE); |
| 1222 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1223 | ret = btrfs_read_chunk_tree(chunk_root); |
| 1224 | BUG_ON(ret); |
| 1225 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1226 | blocksize = btrfs_level_size(tree_root, |
| 1227 | btrfs_super_root_level(disk_super)); |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 1228 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1229 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1230 | tree_root->node = read_tree_block(tree_root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1231 | btrfs_super_root(disk_super), |
| 1232 | blocksize); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1233 | if (!tree_root->node) |
| 1234 | goto fail_sb_buffer; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 1235 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1236 | |
| 1237 | ret = find_and_setup_root(tree_root, fs_info, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1238 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1239 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1240 | goto fail_tree_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1241 | extent_root->track_dirty = 1; |
| 1242 | |
| 1243 | ret = find_and_setup_root(tree_root, fs_info, |
| 1244 | BTRFS_DEV_TREE_OBJECTID, dev_root); |
| 1245 | dev_root->track_dirty = 1; |
| 1246 | |
| 1247 | if (ret) |
| 1248 | goto fail_extent_root; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 1249 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1250 | btrfs_read_block_groups(extent_root); |
| 1251 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 1252 | fs_info->generation = btrfs_super_generation(disk_super) + 1; |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 1253 | fs_info->data_alloc_profile = (u64)-1; |
| 1254 | fs_info->metadata_alloc_profile = (u64)-1; |
| 1255 | fs_info->system_alloc_profile = fs_info->metadata_alloc_profile; |
| 1256 | |
Chris Mason | 5be6f7f | 2007-04-05 13:35:25 -0400 | [diff] [blame] | 1257 | mutex_unlock(&fs_info->fs_mutex); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 1258 | return tree_root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1259 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1260 | fail_extent_root: |
| 1261 | free_extent_buffer(extent_root->node); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1262 | fail_tree_root: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1263 | mutex_unlock(&fs_info->fs_mutex); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1264 | free_extent_buffer(tree_root->node); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1265 | fail_sb_buffer: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1266 | free_extent_buffer(fs_info->sb_buffer); |
Chris Mason | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 1267 | 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] | 1268 | fail_iput: |
| 1269 | iput(fs_info->btree_inode); |
| 1270 | fail: |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1271 | close_all_devices(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1272 | kfree(extent_root); |
| 1273 | kfree(tree_root); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 1274 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) |
Chris Mason | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 1275 | bdi_destroy(&fs_info->bdi); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 1276 | #endif |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1277 | kfree(fs_info); |
| 1278 | return ERR_PTR(err); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1279 | } |
| 1280 | |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 1281 | static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) |
| 1282 | { |
| 1283 | char b[BDEVNAME_SIZE]; |
| 1284 | |
| 1285 | if (uptodate) { |
| 1286 | set_buffer_uptodate(bh); |
| 1287 | } else { |
| 1288 | if (!buffer_eopnotsupp(bh) && printk_ratelimit()) { |
| 1289 | printk(KERN_WARNING "lost page write due to " |
| 1290 | "I/O error on %s\n", |
| 1291 | bdevname(bh->b_bdev, b)); |
| 1292 | } |
| 1293 | set_buffer_write_io_error(bh); |
| 1294 | clear_buffer_uptodate(bh); |
| 1295 | } |
| 1296 | unlock_buffer(bh); |
| 1297 | put_bh(bh); |
| 1298 | } |
| 1299 | |
| 1300 | int write_all_supers(struct btrfs_root *root) |
| 1301 | { |
| 1302 | struct list_head *cur; |
| 1303 | struct list_head *head = &root->fs_info->fs_devices->devices; |
| 1304 | struct btrfs_device *dev; |
| 1305 | struct extent_buffer *sb; |
| 1306 | struct btrfs_dev_item *dev_item; |
| 1307 | struct buffer_head *bh; |
| 1308 | int ret; |
| 1309 | int do_barriers; |
| 1310 | |
| 1311 | do_barriers = !btrfs_test_opt(root, NOBARRIER); |
| 1312 | |
| 1313 | sb = root->fs_info->sb_buffer; |
| 1314 | dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block, |
| 1315 | dev_item); |
| 1316 | list_for_each(cur, head) { |
| 1317 | dev = list_entry(cur, struct btrfs_device, dev_list); |
| 1318 | btrfs_set_device_type(sb, dev_item, dev->type); |
| 1319 | btrfs_set_device_id(sb, dev_item, dev->devid); |
| 1320 | btrfs_set_device_total_bytes(sb, dev_item, dev->total_bytes); |
| 1321 | btrfs_set_device_bytes_used(sb, dev_item, dev->bytes_used); |
| 1322 | btrfs_set_device_io_align(sb, dev_item, dev->io_align); |
| 1323 | btrfs_set_device_io_width(sb, dev_item, dev->io_width); |
| 1324 | btrfs_set_device_sector_size(sb, dev_item, dev->sector_size); |
| 1325 | write_extent_buffer(sb, dev->uuid, |
| 1326 | (unsigned long)btrfs_device_uuid(dev_item), |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1327 | BTRFS_UUID_SIZE); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 1328 | |
| 1329 | btrfs_set_header_flag(sb, BTRFS_HEADER_FLAG_WRITTEN); |
| 1330 | csum_tree_block(root, sb, 0); |
| 1331 | |
| 1332 | bh = __getblk(dev->bdev, BTRFS_SUPER_INFO_OFFSET / |
| 1333 | root->fs_info->sb->s_blocksize, |
| 1334 | BTRFS_SUPER_INFO_SIZE); |
| 1335 | |
| 1336 | read_extent_buffer(sb, bh->b_data, 0, BTRFS_SUPER_INFO_SIZE); |
| 1337 | dev->pending_io = bh; |
| 1338 | |
| 1339 | get_bh(bh); |
| 1340 | set_buffer_uptodate(bh); |
| 1341 | lock_buffer(bh); |
| 1342 | bh->b_end_io = btrfs_end_buffer_write_sync; |
| 1343 | |
| 1344 | if (do_barriers && dev->barriers) { |
| 1345 | ret = submit_bh(WRITE_BARRIER, bh); |
| 1346 | if (ret == -EOPNOTSUPP) { |
| 1347 | printk("btrfs: disabling barriers on dev %s\n", |
| 1348 | dev->name); |
| 1349 | set_buffer_uptodate(bh); |
| 1350 | dev->barriers = 0; |
| 1351 | get_bh(bh); |
| 1352 | lock_buffer(bh); |
| 1353 | ret = submit_bh(WRITE, bh); |
| 1354 | } |
| 1355 | } else { |
| 1356 | ret = submit_bh(WRITE, bh); |
| 1357 | } |
| 1358 | BUG_ON(ret); |
| 1359 | } |
| 1360 | |
| 1361 | list_for_each(cur, head) { |
| 1362 | dev = list_entry(cur, struct btrfs_device, dev_list); |
| 1363 | BUG_ON(!dev->pending_io); |
| 1364 | bh = dev->pending_io; |
| 1365 | wait_on_buffer(bh); |
| 1366 | if (!buffer_uptodate(dev->pending_io)) { |
| 1367 | if (do_barriers && dev->barriers) { |
| 1368 | printk("btrfs: disabling barriers on dev %s\n", |
| 1369 | dev->name); |
| 1370 | set_buffer_uptodate(bh); |
| 1371 | get_bh(bh); |
| 1372 | lock_buffer(bh); |
| 1373 | dev->barriers = 0; |
| 1374 | ret = submit_bh(WRITE, bh); |
| 1375 | BUG_ON(ret); |
| 1376 | wait_on_buffer(bh); |
| 1377 | BUG_ON(!buffer_uptodate(bh)); |
| 1378 | } else { |
| 1379 | BUG(); |
| 1380 | } |
| 1381 | |
| 1382 | } |
| 1383 | dev->pending_io = NULL; |
| 1384 | brelse(bh); |
| 1385 | } |
| 1386 | return 0; |
| 1387 | } |
| 1388 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1389 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1390 | *root) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 1391 | { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1392 | int ret; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1393 | |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 1394 | ret = write_all_supers(root); |
| 1395 | #if 0 |
Chris Mason | 21ad10c | 2008-01-09 09:23:21 -0500 | [diff] [blame] | 1396 | if (!btrfs_test_opt(root, NOBARRIER)) |
| 1397 | blkdev_issue_flush(sb->s_bdev, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1398 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1399 | ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping, |
| 1400 | super->start, super->len); |
Chris Mason | 21ad10c | 2008-01-09 09:23:21 -0500 | [diff] [blame] | 1401 | if (!btrfs_test_opt(root, NOBARRIER)) |
| 1402 | blkdev_issue_flush(sb->s_bdev, NULL); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 1403 | #endif |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1404 | return ret; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 1405 | } |
| 1406 | |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1407 | 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] | 1408 | { |
| 1409 | radix_tree_delete(&fs_info->fs_roots_radix, |
| 1410 | (unsigned long)root->root_key.objectid); |
Chris Mason | b99aa6c | 2008-01-14 14:41:16 -0500 | [diff] [blame] | 1411 | if (root->in_sysfs) |
| 1412 | btrfs_sysfs_del_root(root); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 1413 | if (root->inode) |
| 1414 | iput(root->inode); |
| 1415 | if (root->node) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1416 | free_extent_buffer(root->node); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 1417 | if (root->commit_root) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1418 | free_extent_buffer(root->commit_root); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1419 | if (root->name) |
| 1420 | kfree(root->name); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 1421 | kfree(root); |
| 1422 | return 0; |
| 1423 | } |
| 1424 | |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 1425 | static int del_fs_roots(struct btrfs_fs_info *fs_info) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 1426 | { |
| 1427 | int ret; |
| 1428 | struct btrfs_root *gang[8]; |
| 1429 | int i; |
| 1430 | |
| 1431 | while(1) { |
| 1432 | ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, |
| 1433 | (void **)gang, 0, |
| 1434 | ARRAY_SIZE(gang)); |
| 1435 | if (!ret) |
| 1436 | break; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 1437 | for (i = 0; i < ret; i++) |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1438 | btrfs_free_fs_root(fs_info, gang[i]); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 1439 | } |
| 1440 | return 0; |
| 1441 | } |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 1442 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1443 | int close_ctree(struct btrfs_root *root) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1444 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 1445 | int ret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1446 | struct btrfs_trans_handle *trans; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 1447 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1448 | |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 1449 | fs_info->closing = 1; |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 1450 | btrfs_transaction_flush_work(root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 1451 | mutex_lock(&fs_info->fs_mutex); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1452 | btrfs_defrag_dirty_roots(root->fs_info); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1453 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1454 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1455 | /* run commit again to drop the original snapshot */ |
| 1456 | trans = btrfs_start_transaction(root, 1); |
| 1457 | btrfs_commit_transaction(trans, root); |
| 1458 | ret = btrfs_write_and_wait_transaction(NULL, root); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 1459 | BUG_ON(ret); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1460 | write_ctree_super(NULL, root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 1461 | mutex_unlock(&fs_info->fs_mutex); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 1462 | |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1463 | if (fs_info->delalloc_bytes) { |
| 1464 | printk("btrfs: at unmount delalloc count %Lu\n", |
| 1465 | fs_info->delalloc_bytes); |
| 1466 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 1467 | if (fs_info->extent_root->node) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1468 | free_extent_buffer(fs_info->extent_root->node); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1469 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 1470 | if (fs_info->tree_root->node) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1471 | free_extent_buffer(fs_info->tree_root->node); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1472 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1473 | if (root->fs_info->chunk_root->node); |
| 1474 | free_extent_buffer(root->fs_info->chunk_root->node); |
| 1475 | |
| 1476 | if (root->fs_info->dev_root->node); |
| 1477 | free_extent_buffer(root->fs_info->dev_root->node); |
| 1478 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1479 | free_extent_buffer(fs_info->sb_buffer); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1480 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1481 | btrfs_free_block_groups(root->fs_info); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 1482 | del_fs_roots(fs_info); |
Chris Mason | d10c5f3 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1483 | |
| 1484 | filemap_write_and_wait(fs_info->btree_inode->i_mapping); |
| 1485 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1486 | extent_io_tree_empty_lru(&fs_info->free_space_cache); |
| 1487 | extent_io_tree_empty_lru(&fs_info->block_group_cache); |
| 1488 | extent_io_tree_empty_lru(&fs_info->pinned_extents); |
| 1489 | extent_io_tree_empty_lru(&fs_info->pending_del); |
| 1490 | extent_io_tree_empty_lru(&fs_info->extent_ins); |
| 1491 | 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] | 1492 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1493 | truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1494 | flush_workqueue(end_io_workqueue); |
| 1495 | destroy_workqueue(end_io_workqueue); |
Chris Mason | d10c5f3 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1496 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1497 | flush_workqueue(async_submit_workqueue); |
| 1498 | destroy_workqueue(async_submit_workqueue); |
| 1499 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1500 | iput(fs_info->btree_inode); |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 1501 | #if 0 |
| 1502 | while(!list_empty(&fs_info->hashers)) { |
| 1503 | struct btrfs_hasher *hasher; |
| 1504 | hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher, |
| 1505 | hashers); |
| 1506 | list_del(&hasher->hashers); |
| 1507 | crypto_free_hash(&fs_info->hash_tfm); |
| 1508 | kfree(hasher); |
| 1509 | } |
| 1510 | #endif |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1511 | close_all_devices(fs_info); |
| 1512 | btrfs_mapping_tree_free(&fs_info->mapping_tree); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 1513 | |
| 1514 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 1515 | bdi_destroy(&fs_info->bdi); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 1516 | #endif |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1517 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 1518 | kfree(fs_info->extent_root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 1519 | kfree(fs_info->tree_root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1520 | kfree(fs_info->chunk_root); |
| 1521 | kfree(fs_info->dev_root); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1522 | return 0; |
| 1523 | } |
| 1524 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1525 | int btrfs_buffer_uptodate(struct extent_buffer *buf) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1526 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 1527 | struct inode *btree_inode = buf->first_page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1528 | return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1529 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1530 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1531 | int btrfs_set_buffer_uptodate(struct extent_buffer *buf) |
| 1532 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 1533 | struct inode *btree_inode = buf->first_page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1534 | return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1535 | buf); |
| 1536 | } |
| 1537 | |
| 1538 | void btrfs_mark_buffer_dirty(struct extent_buffer *buf) |
| 1539 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 1540 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1541 | u64 transid = btrfs_header_generation(buf); |
| 1542 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1543 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1544 | if (transid != root->fs_info->generation) { |
| 1545 | printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n", |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1546 | (unsigned long long)buf->start, |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1547 | transid, root->fs_info->generation); |
| 1548 | WARN_ON(1); |
| 1549 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1550 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1551 | } |
| 1552 | |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1553 | void btrfs_throttle(struct btrfs_root *root) |
| 1554 | { |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 1555 | struct backing_dev_info *bdi; |
| 1556 | |
| 1557 | 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] | 1558 | if (root->fs_info->throttles && bdi_write_congested(bdi)) { |
| 1559 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18) |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 1560 | congestion_wait(WRITE, HZ/20); |
Chris Mason | 04005cc | 2008-01-17 12:01:41 -0500 | [diff] [blame] | 1561 | #else |
| 1562 | blk_congestion_wait(WRITE, HZ/20); |
| 1563 | #endif |
| 1564 | } |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1565 | } |
| 1566 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1567 | void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr) |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 1568 | { |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1569 | balance_dirty_pages_ratelimited_nr( |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1570 | root->fs_info->btree_inode->i_mapping, 1); |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 1571 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1572 | |
| 1573 | void btrfs_set_buffer_defrag(struct extent_buffer *buf) |
| 1574 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 1575 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1576 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1577 | set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start, |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1578 | buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS); |
| 1579 | } |
| 1580 | |
| 1581 | void btrfs_set_buffer_defrag_done(struct extent_buffer *buf) |
| 1582 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 1583 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1584 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1585 | set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start, |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1586 | buf->start + buf->len - 1, EXTENT_DEFRAG_DONE, |
| 1587 | GFP_NOFS); |
| 1588 | } |
| 1589 | |
| 1590 | int btrfs_buffer_defrag(struct extent_buffer *buf) |
| 1591 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 1592 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1593 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1594 | return test_range_bit(&BTRFS_I(btree_inode)->io_tree, |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1595 | buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0); |
| 1596 | } |
| 1597 | |
| 1598 | int btrfs_buffer_defrag_done(struct extent_buffer *buf) |
| 1599 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 1600 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1601 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1602 | return test_range_bit(&BTRFS_I(btree_inode)->io_tree, |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1603 | buf->start, buf->start + buf->len - 1, |
| 1604 | EXTENT_DEFRAG_DONE, 0); |
| 1605 | } |
| 1606 | |
| 1607 | int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf) |
| 1608 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 1609 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1610 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1611 | return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree, |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1612 | buf->start, buf->start + buf->len - 1, |
| 1613 | EXTENT_DEFRAG_DONE, GFP_NOFS); |
| 1614 | } |
| 1615 | |
| 1616 | int btrfs_clear_buffer_defrag(struct extent_buffer *buf) |
| 1617 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 1618 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1619 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1620 | return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree, |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1621 | buf->start, buf->start + buf->len - 1, |
| 1622 | EXTENT_DEFRAG, GFP_NOFS); |
| 1623 | } |
| 1624 | |
| 1625 | int btrfs_read_buffer(struct extent_buffer *buf) |
| 1626 | { |
Chris Mason | 810191f | 2007-10-15 16:18:55 -0400 | [diff] [blame] | 1627 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1628 | int ret; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1629 | ret = btree_read_extent_buffer_pages(root, buf, 0); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1630 | if (ret == 0) { |
| 1631 | buf->flags |= EXTENT_UPTODATE; |
| 1632 | } |
| 1633 | return ret; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1634 | } |
Chris Mason | 0da5468 | 2007-11-07 21:08:01 -0500 | [diff] [blame] | 1635 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1636 | static struct extent_io_ops btree_extent_io_ops = { |
Chris Mason | 0da5468 | 2007-11-07 21:08:01 -0500 | [diff] [blame] | 1637 | .writepage_io_hook = btree_writepage_io_hook, |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1638 | .readpage_end_io_hook = btree_readpage_end_io_hook, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1639 | .submit_bio_hook = btree_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1640 | /* note we're sharing with inode.c for the merge bio hook */ |
| 1641 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 0da5468 | 2007-11-07 21:08:01 -0500 | [diff] [blame] | 1642 | }; |