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