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