Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [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 | |
| 19 | #include <linux/gfp.h> |
| 20 | #include <linux/slab.h> |
Chris Mason | d6bfde8 | 2008-04-30 13:59:35 -0400 | [diff] [blame] | 21 | #include <linux/blkdev.h> |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 22 | #include <linux/writeback.h> |
| 23 | #include <linux/pagevec.h> |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 24 | #include "ctree.h" |
| 25 | #include "transaction.h" |
| 26 | #include "btrfs_inode.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 27 | #include "extent_io.h" |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 28 | |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 29 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 30 | static u64 entry_end(struct btrfs_ordered_extent *entry) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 31 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 32 | if (entry->file_offset + entry->len < entry->file_offset) |
| 33 | return (u64)-1; |
| 34 | return entry->file_offset + entry->len; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 35 | } |
| 36 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 37 | static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset, |
| 38 | struct rb_node *node) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 39 | { |
| 40 | struct rb_node ** p = &root->rb_node; |
| 41 | struct rb_node * parent = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 42 | struct btrfs_ordered_extent *entry; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 43 | |
| 44 | while(*p) { |
| 45 | parent = *p; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 46 | entry = rb_entry(parent, struct btrfs_ordered_extent, rb_node); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 47 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 48 | if (file_offset < entry->file_offset) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 49 | p = &(*p)->rb_left; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 50 | else if (file_offset >= entry_end(entry)) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 51 | p = &(*p)->rb_right; |
| 52 | else |
| 53 | return parent; |
| 54 | } |
| 55 | |
| 56 | rb_link_node(node, parent, p); |
| 57 | rb_insert_color(node, root); |
| 58 | return NULL; |
| 59 | } |
| 60 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 61 | static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset, |
| 62 | struct rb_node **prev_ret) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 63 | { |
| 64 | struct rb_node * n = root->rb_node; |
| 65 | struct rb_node *prev = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 66 | struct rb_node *test; |
| 67 | struct btrfs_ordered_extent *entry; |
| 68 | struct btrfs_ordered_extent *prev_entry = NULL; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 69 | |
| 70 | while(n) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 71 | entry = rb_entry(n, struct btrfs_ordered_extent, rb_node); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 72 | prev = n; |
| 73 | prev_entry = entry; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 74 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 75 | if (file_offset < entry->file_offset) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 76 | n = n->rb_left; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 77 | else if (file_offset >= entry_end(entry)) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 78 | n = n->rb_right; |
| 79 | else |
| 80 | return n; |
| 81 | } |
| 82 | if (!prev_ret) |
| 83 | return NULL; |
| 84 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 85 | while(prev && file_offset >= entry_end(prev_entry)) { |
| 86 | test = rb_next(prev); |
| 87 | if (!test) |
| 88 | break; |
| 89 | prev_entry = rb_entry(test, struct btrfs_ordered_extent, |
| 90 | rb_node); |
| 91 | if (file_offset < entry_end(prev_entry)) |
| 92 | break; |
| 93 | |
| 94 | prev = test; |
| 95 | } |
| 96 | if (prev) |
| 97 | prev_entry = rb_entry(prev, struct btrfs_ordered_extent, |
| 98 | rb_node); |
| 99 | while(prev && file_offset < entry_end(prev_entry)) { |
| 100 | test = rb_prev(prev); |
| 101 | if (!test) |
| 102 | break; |
| 103 | prev_entry = rb_entry(test, struct btrfs_ordered_extent, |
| 104 | rb_node); |
| 105 | prev = test; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 106 | } |
| 107 | *prev_ret = prev; |
| 108 | return NULL; |
| 109 | } |
| 110 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 111 | static int offset_in_entry(struct btrfs_ordered_extent *entry, u64 file_offset) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 112 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 113 | if (file_offset < entry->file_offset || |
| 114 | entry->file_offset + entry->len <= file_offset) |
| 115 | return 0; |
| 116 | return 1; |
| 117 | } |
| 118 | |
| 119 | static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree, |
| 120 | u64 file_offset) |
| 121 | { |
| 122 | struct rb_root *root = &tree->tree; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 123 | struct rb_node *prev; |
| 124 | struct rb_node *ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 125 | struct btrfs_ordered_extent *entry; |
| 126 | |
| 127 | if (tree->last) { |
| 128 | entry = rb_entry(tree->last, struct btrfs_ordered_extent, |
| 129 | rb_node); |
| 130 | if (offset_in_entry(entry, file_offset)) |
| 131 | return tree->last; |
| 132 | } |
| 133 | ret = __tree_search(root, file_offset, &prev); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 134 | if (!ret) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 135 | ret = prev; |
| 136 | if (ret) |
| 137 | tree->last = ret; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 138 | return ret; |
| 139 | } |
| 140 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 141 | /* allocate and add a new ordered_extent into the per-inode tree. |
| 142 | * file_offset is the logical offset in the file |
| 143 | * |
| 144 | * start is the disk block number of an extent already reserved in the |
| 145 | * extent allocation tree |
| 146 | * |
| 147 | * len is the length of the extent |
| 148 | * |
| 149 | * This also sets the EXTENT_ORDERED bit on the range in the inode. |
| 150 | * |
| 151 | * The tree is given a single reference on the ordered extent that was |
| 152 | * inserted. |
| 153 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 154 | int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset, |
| 155 | u64 start, u64 len) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 156 | { |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 157 | struct btrfs_ordered_inode_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 158 | struct rb_node *node; |
| 159 | struct btrfs_ordered_extent *entry; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 160 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 161 | tree = &BTRFS_I(inode)->ordered_tree; |
| 162 | entry = kzalloc(sizeof(*entry), GFP_NOFS); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 163 | if (!entry) |
| 164 | return -ENOMEM; |
| 165 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 166 | mutex_lock(&tree->mutex); |
| 167 | entry->file_offset = file_offset; |
| 168 | entry->start = start; |
| 169 | entry->len = len; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 170 | entry->inode = inode; |
| 171 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 172 | /* one ref for the tree */ |
| 173 | atomic_set(&entry->refs, 1); |
| 174 | init_waitqueue_head(&entry->wait); |
| 175 | INIT_LIST_HEAD(&entry->list); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 176 | INIT_LIST_HEAD(&entry->root_extent_list); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 177 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 178 | node = tree_insert(&tree->tree, file_offset, |
| 179 | &entry->rb_node); |
| 180 | if (node) { |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 181 | printk("warning dup entry from add_ordered_extent\n"); |
| 182 | BUG(); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 183 | } |
| 184 | set_extent_ordered(&BTRFS_I(inode)->io_tree, file_offset, |
| 185 | entry_end(entry) - 1, GFP_NOFS); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 186 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 187 | spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock); |
| 188 | list_add_tail(&entry->root_extent_list, |
| 189 | &BTRFS_I(inode)->root->fs_info->ordered_extents); |
| 190 | spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock); |
| 191 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 192 | mutex_unlock(&tree->mutex); |
| 193 | BUG_ON(node); |
| 194 | return 0; |
| 195 | } |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 196 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 197 | /* |
| 198 | * Add a struct btrfs_ordered_sum into the list of checksums to be inserted |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 199 | * when an ordered extent is finished. If the list covers more than one |
| 200 | * ordered extent, it is split across multiples. |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 201 | */ |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 202 | int btrfs_add_ordered_sum(struct inode *inode, |
| 203 | struct btrfs_ordered_extent *entry, |
| 204 | struct btrfs_ordered_sum *sum) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 205 | { |
| 206 | struct btrfs_ordered_inode_tree *tree; |
Chris Mason | 1b1e213 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 207 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 208 | tree = &BTRFS_I(inode)->ordered_tree; |
| 209 | mutex_lock(&tree->mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 210 | list_add_tail(&sum->list, &entry->list); |
| 211 | mutex_unlock(&tree->mutex); |
| 212 | return 0; |
| 213 | } |
| 214 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 215 | /* |
| 216 | * this is used to account for finished IO across a given range |
| 217 | * of the file. The IO should not span ordered extents. If |
| 218 | * a given ordered_extent is completely done, 1 is returned, otherwise |
| 219 | * 0. |
| 220 | * |
| 221 | * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used |
| 222 | * to make sure this function only returns 1 once for a given ordered extent. |
| 223 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 224 | int btrfs_dec_test_ordered_pending(struct inode *inode, |
| 225 | u64 file_offset, u64 io_size) |
| 226 | { |
| 227 | struct btrfs_ordered_inode_tree *tree; |
| 228 | struct rb_node *node; |
| 229 | struct btrfs_ordered_extent *entry; |
| 230 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 231 | int ret; |
| 232 | |
| 233 | tree = &BTRFS_I(inode)->ordered_tree; |
| 234 | mutex_lock(&tree->mutex); |
| 235 | clear_extent_ordered(io_tree, file_offset, file_offset + io_size - 1, |
| 236 | GFP_NOFS); |
| 237 | node = tree_search(tree, file_offset); |
| 238 | if (!node) { |
| 239 | ret = 1; |
| 240 | goto out; |
| 241 | } |
| 242 | |
| 243 | entry = rb_entry(node, struct btrfs_ordered_extent, rb_node); |
| 244 | if (!offset_in_entry(entry, file_offset)) { |
| 245 | ret = 1; |
| 246 | goto out; |
| 247 | } |
| 248 | |
| 249 | ret = test_range_bit(io_tree, entry->file_offset, |
| 250 | entry->file_offset + entry->len - 1, |
| 251 | EXTENT_ORDERED, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 252 | if (ret == 0) |
| 253 | ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags); |
| 254 | out: |
| 255 | mutex_unlock(&tree->mutex); |
| 256 | return ret == 0; |
| 257 | } |
| 258 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 259 | /* |
| 260 | * used to drop a reference on an ordered extent. This will free |
| 261 | * the extent if the last reference is dropped |
| 262 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 263 | int btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry) |
| 264 | { |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 265 | struct list_head *cur; |
| 266 | struct btrfs_ordered_sum *sum; |
| 267 | |
| 268 | if (atomic_dec_and_test(&entry->refs)) { |
| 269 | while(!list_empty(&entry->list)) { |
| 270 | cur = entry->list.next; |
| 271 | sum = list_entry(cur, struct btrfs_ordered_sum, list); |
| 272 | list_del(&sum->list); |
| 273 | kfree(sum); |
| 274 | } |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 275 | kfree(entry); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 276 | } |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 277 | return 0; |
| 278 | } |
| 279 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 280 | /* |
| 281 | * remove an ordered extent from the tree. No references are dropped |
| 282 | * but, anyone waiting on this extent is woken up. |
| 283 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 284 | int btrfs_remove_ordered_extent(struct inode *inode, |
| 285 | struct btrfs_ordered_extent *entry) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 286 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 287 | struct btrfs_ordered_inode_tree *tree; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 288 | struct rb_node *node; |
| 289 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 290 | tree = &BTRFS_I(inode)->ordered_tree; |
| 291 | mutex_lock(&tree->mutex); |
| 292 | node = &entry->rb_node; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 293 | rb_erase(node, &tree->tree); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 294 | tree->last = NULL; |
| 295 | set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 296 | |
| 297 | spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock); |
| 298 | list_del_init(&entry->root_extent_list); |
| 299 | spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock); |
| 300 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 301 | mutex_unlock(&tree->mutex); |
| 302 | wake_up(&entry->wait); |
Chris Mason | 81d7ed2 | 2008-04-25 08:51:48 -0400 | [diff] [blame] | 303 | return 0; |
| 304 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 305 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 306 | int btrfs_wait_ordered_extents(struct btrfs_root *root) |
| 307 | { |
| 308 | struct list_head splice; |
| 309 | struct list_head *cur; |
| 310 | struct btrfs_ordered_extent *ordered; |
| 311 | struct inode *inode; |
| 312 | |
| 313 | INIT_LIST_HEAD(&splice); |
| 314 | |
| 315 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 316 | list_splice_init(&root->fs_info->ordered_extents, &splice); |
| 317 | while(!list_empty(&splice)) { |
| 318 | cur = splice.next; |
| 319 | ordered = list_entry(cur, struct btrfs_ordered_extent, |
| 320 | root_extent_list); |
| 321 | list_del_init(&ordered->root_extent_list); |
| 322 | atomic_inc(&ordered->refs); |
| 323 | inode = ordered->inode; |
| 324 | |
| 325 | /* |
| 326 | * the inode can't go away until all the pages are gone |
| 327 | * and the pages won't go away while there is still |
| 328 | * an ordered extent and the ordered extent won't go |
| 329 | * away until it is off this list. So, we can safely |
| 330 | * increment i_count here and call iput later |
| 331 | */ |
| 332 | atomic_inc(&inode->i_count); |
| 333 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 334 | |
| 335 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 336 | btrfs_put_ordered_extent(ordered); |
| 337 | iput(inode); |
| 338 | |
| 339 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 340 | } |
| 341 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 342 | return 0; |
| 343 | } |
| 344 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 345 | /* |
| 346 | * Used to start IO or wait for a given ordered extent to finish. |
| 347 | * |
| 348 | * If wait is one, this effectively waits on page writeback for all the pages |
| 349 | * in the extent, and it waits on the io completion code to insert |
| 350 | * metadata into the btree corresponding to the extent |
| 351 | */ |
| 352 | void btrfs_start_ordered_extent(struct inode *inode, |
| 353 | struct btrfs_ordered_extent *entry, |
| 354 | int wait) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 355 | { |
| 356 | u64 start = entry->file_offset; |
| 357 | u64 end = start + entry->len - 1; |
| 358 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 359 | /* |
| 360 | * pages in the range can be dirty, clean or writeback. We |
| 361 | * start IO on any dirty ones so the wait doesn't stall waiting |
| 362 | * for pdflush to find them |
| 363 | */ |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 364 | btrfs_fdatawrite_range(inode->i_mapping, start, end, WB_SYNC_NONE); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 365 | if (wait) |
| 366 | wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE, |
| 367 | &entry->flags)); |
| 368 | } |
| 369 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 370 | /* |
| 371 | * Used to wait on ordered extents across a large range of bytes. |
| 372 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 373 | void btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len) |
| 374 | { |
| 375 | u64 end; |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 376 | u64 orig_end; |
| 377 | u64 wait_end; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 378 | struct btrfs_ordered_extent *ordered; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 379 | |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 380 | if (start + len < start) { |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 381 | orig_end = INT_LIMIT(loff_t); |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 382 | } else { |
| 383 | orig_end = start + len - 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 384 | if (orig_end > INT_LIMIT(loff_t)) |
| 385 | orig_end = INT_LIMIT(loff_t); |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 386 | } |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 387 | wait_end = orig_end; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 388 | again: |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 389 | /* start IO across the range first to instantiate any delalloc |
| 390 | * extents |
| 391 | */ |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 392 | btrfs_fdatawrite_range(inode->i_mapping, start, orig_end, WB_SYNC_NONE); |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 393 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 394 | btrfs_wait_on_page_writeback_range(inode->i_mapping, |
| 395 | start >> PAGE_CACHE_SHIFT, |
| 396 | orig_end >> PAGE_CACHE_SHIFT); |
| 397 | |
| 398 | end = orig_end; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 399 | while(1) { |
| 400 | ordered = btrfs_lookup_first_ordered_extent(inode, end); |
| 401 | if (!ordered) { |
| 402 | break; |
| 403 | } |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 404 | if (ordered->file_offset > orig_end) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 405 | btrfs_put_ordered_extent(ordered); |
| 406 | break; |
| 407 | } |
| 408 | if (ordered->file_offset + ordered->len < start) { |
| 409 | btrfs_put_ordered_extent(ordered); |
| 410 | break; |
| 411 | } |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 412 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 413 | end = ordered->file_offset; |
| 414 | btrfs_put_ordered_extent(ordered); |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 415 | if (end == 0 || end == start) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 416 | break; |
| 417 | end--; |
| 418 | } |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 419 | if (test_range_bit(&BTRFS_I(inode)->io_tree, start, orig_end, |
| 420 | EXTENT_ORDERED | EXTENT_DELALLOC, 0)) { |
| 421 | printk("inode %lu still ordered or delalloc after wait " |
| 422 | "%llu %llu\n", inode->i_ino, |
| 423 | (unsigned long long)start, |
| 424 | (unsigned long long)orig_end); |
| 425 | goto again; |
| 426 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 427 | } |
| 428 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 429 | /* |
| 430 | * find an ordered extent corresponding to file_offset. return NULL if |
| 431 | * nothing is found, otherwise take a reference on the extent and return it |
| 432 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 433 | struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode, |
| 434 | u64 file_offset) |
| 435 | { |
| 436 | struct btrfs_ordered_inode_tree *tree; |
| 437 | struct rb_node *node; |
| 438 | struct btrfs_ordered_extent *entry = NULL; |
| 439 | |
| 440 | tree = &BTRFS_I(inode)->ordered_tree; |
| 441 | mutex_lock(&tree->mutex); |
| 442 | node = tree_search(tree, file_offset); |
| 443 | if (!node) |
| 444 | goto out; |
| 445 | |
| 446 | entry = rb_entry(node, struct btrfs_ordered_extent, rb_node); |
| 447 | if (!offset_in_entry(entry, file_offset)) |
| 448 | entry = NULL; |
| 449 | if (entry) |
| 450 | atomic_inc(&entry->refs); |
| 451 | out: |
| 452 | mutex_unlock(&tree->mutex); |
| 453 | return entry; |
| 454 | } |
| 455 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 456 | /* |
| 457 | * lookup and return any extent before 'file_offset'. NULL is returned |
| 458 | * if none is found |
| 459 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 460 | struct btrfs_ordered_extent * |
| 461 | btrfs_lookup_first_ordered_extent(struct inode * inode, u64 file_offset) |
| 462 | { |
| 463 | struct btrfs_ordered_inode_tree *tree; |
| 464 | struct rb_node *node; |
| 465 | struct btrfs_ordered_extent *entry = NULL; |
| 466 | |
| 467 | tree = &BTRFS_I(inode)->ordered_tree; |
| 468 | mutex_lock(&tree->mutex); |
| 469 | node = tree_search(tree, file_offset); |
| 470 | if (!node) |
| 471 | goto out; |
| 472 | |
| 473 | entry = rb_entry(node, struct btrfs_ordered_extent, rb_node); |
| 474 | atomic_inc(&entry->refs); |
| 475 | out: |
| 476 | mutex_unlock(&tree->mutex); |
| 477 | return entry; |
| 478 | } |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 479 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 480 | /* |
| 481 | * After an extent is done, call this to conditionally update the on disk |
| 482 | * i_size. i_size is updated to cover any fully written part of the file. |
| 483 | */ |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 484 | int btrfs_ordered_update_i_size(struct inode *inode, |
| 485 | struct btrfs_ordered_extent *ordered) |
| 486 | { |
| 487 | struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree; |
| 488 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 489 | u64 disk_i_size; |
| 490 | u64 new_i_size; |
| 491 | u64 i_size_test; |
| 492 | struct rb_node *node; |
| 493 | struct btrfs_ordered_extent *test; |
| 494 | |
| 495 | mutex_lock(&tree->mutex); |
| 496 | disk_i_size = BTRFS_I(inode)->disk_i_size; |
| 497 | |
| 498 | /* |
| 499 | * if the disk i_size is already at the inode->i_size, or |
| 500 | * this ordered extent is inside the disk i_size, we're done |
| 501 | */ |
| 502 | if (disk_i_size >= inode->i_size || |
| 503 | ordered->file_offset + ordered->len <= disk_i_size) { |
| 504 | goto out; |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | * we can't update the disk_isize if there are delalloc bytes |
| 509 | * between disk_i_size and this ordered extent |
| 510 | */ |
| 511 | if (test_range_bit(io_tree, disk_i_size, |
| 512 | ordered->file_offset + ordered->len - 1, |
| 513 | EXTENT_DELALLOC, 0)) { |
| 514 | goto out; |
| 515 | } |
| 516 | /* |
| 517 | * walk backward from this ordered extent to disk_i_size. |
| 518 | * if we find an ordered extent then we can't update disk i_size |
| 519 | * yet |
| 520 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 521 | node = &ordered->rb_node; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 522 | while(1) { |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 523 | node = rb_prev(node); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 524 | if (!node) |
| 525 | break; |
| 526 | test = rb_entry(node, struct btrfs_ordered_extent, rb_node); |
| 527 | if (test->file_offset + test->len <= disk_i_size) |
| 528 | break; |
| 529 | if (test->file_offset >= inode->i_size) |
| 530 | break; |
| 531 | if (test->file_offset >= disk_i_size) |
| 532 | goto out; |
| 533 | } |
| 534 | new_i_size = min_t(u64, entry_end(ordered), i_size_read(inode)); |
| 535 | |
| 536 | /* |
| 537 | * at this point, we know we can safely update i_size to at least |
| 538 | * the offset from this ordered extent. But, we need to |
| 539 | * walk forward and see if ios from higher up in the file have |
| 540 | * finished. |
| 541 | */ |
| 542 | node = rb_next(&ordered->rb_node); |
| 543 | i_size_test = 0; |
| 544 | if (node) { |
| 545 | /* |
| 546 | * do we have an area where IO might have finished |
| 547 | * between our ordered extent and the next one. |
| 548 | */ |
| 549 | test = rb_entry(node, struct btrfs_ordered_extent, rb_node); |
| 550 | if (test->file_offset > entry_end(ordered)) { |
Yan Zheng | b48652c | 2008-08-04 23:23:47 -0400 | [diff] [blame^] | 551 | i_size_test = test->file_offset; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 552 | } |
| 553 | } else { |
| 554 | i_size_test = i_size_read(inode); |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | * i_size_test is the end of a region after this ordered |
| 559 | * extent where there are no ordered extents. As long as there |
| 560 | * are no delalloc bytes in this area, it is safe to update |
| 561 | * disk_i_size to the end of the region. |
| 562 | */ |
| 563 | if (i_size_test > entry_end(ordered) && |
Yan Zheng | b48652c | 2008-08-04 23:23:47 -0400 | [diff] [blame^] | 564 | !test_range_bit(io_tree, entry_end(ordered), i_size_test - 1, |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 565 | EXTENT_DELALLOC, 0)) { |
| 566 | new_i_size = min_t(u64, i_size_test, i_size_read(inode)); |
| 567 | } |
| 568 | BTRFS_I(inode)->disk_i_size = new_i_size; |
| 569 | out: |
| 570 | mutex_unlock(&tree->mutex); |
| 571 | return 0; |
| 572 | } |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 573 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 574 | /* |
| 575 | * search the ordered extents for one corresponding to 'offset' and |
| 576 | * try to find a checksum. This is used because we allow pages to |
| 577 | * be reclaimed before their checksum is actually put into the btree |
| 578 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 579 | int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u32 *sum) |
| 580 | { |
| 581 | struct btrfs_ordered_sum *ordered_sum; |
| 582 | struct btrfs_sector_sum *sector_sums; |
| 583 | struct btrfs_ordered_extent *ordered; |
| 584 | struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree; |
| 585 | struct list_head *cur; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 586 | unsigned long num_sectors; |
| 587 | unsigned long i; |
| 588 | u32 sectorsize = BTRFS_I(inode)->root->sectorsize; |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 589 | int ret = 1; |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 590 | |
| 591 | ordered = btrfs_lookup_ordered_extent(inode, offset); |
| 592 | if (!ordered) |
| 593 | return 1; |
| 594 | |
| 595 | mutex_lock(&tree->mutex); |
| 596 | list_for_each_prev(cur, &ordered->list) { |
| 597 | ordered_sum = list_entry(cur, struct btrfs_ordered_sum, list); |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 598 | if (offset >= ordered_sum->file_offset) { |
| 599 | num_sectors = ordered_sum->len / sectorsize; |
Chris Mason | ed98b56 | 2008-07-22 23:06:42 -0400 | [diff] [blame] | 600 | sector_sums = ordered_sum->sums; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 601 | for (i = 0; i < num_sectors; i++) { |
| 602 | if (sector_sums[i].offset == offset) { |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 603 | *sum = sector_sums[i].sum; |
| 604 | ret = 0; |
| 605 | goto out; |
| 606 | } |
| 607 | } |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | out: |
| 611 | mutex_unlock(&tree->mutex); |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 612 | btrfs_put_ordered_extent(ordered); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 613 | return ret; |
| 614 | } |
| 615 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 616 | |
| 617 | /** |
| 618 | * taken from mm/filemap.c because it isn't exported |
| 619 | * |
| 620 | * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range |
| 621 | * @mapping: address space structure to write |
| 622 | * @start: offset in bytes where the range starts |
| 623 | * @end: offset in bytes where the range ends (inclusive) |
| 624 | * @sync_mode: enable synchronous operation |
| 625 | * |
| 626 | * Start writeback against all of a mapping's dirty pages that lie |
| 627 | * within the byte offsets <start, end> inclusive. |
| 628 | * |
| 629 | * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as |
| 630 | * opposed to a regular memory cleansing writeback. The difference between |
| 631 | * these two operations is that if a dirty page/buffer is encountered, it must |
| 632 | * be waited upon, and not just skipped over. |
| 633 | */ |
| 634 | int btrfs_fdatawrite_range(struct address_space *mapping, loff_t start, |
| 635 | loff_t end, int sync_mode) |
| 636 | { |
| 637 | struct writeback_control wbc = { |
| 638 | .sync_mode = sync_mode, |
| 639 | .nr_to_write = mapping->nrpages * 2, |
| 640 | .range_start = start, |
| 641 | .range_end = end, |
| 642 | .for_writepages = 1, |
| 643 | }; |
| 644 | return btrfs_writepages(mapping, &wbc); |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * taken from mm/filemap.c because it isn't exported |
| 649 | * |
| 650 | * wait_on_page_writeback_range - wait for writeback to complete |
| 651 | * @mapping: target address_space |
| 652 | * @start: beginning page index |
| 653 | * @end: ending page index |
| 654 | * |
| 655 | * Wait for writeback to complete against pages indexed by start->end |
| 656 | * inclusive |
| 657 | */ |
| 658 | int btrfs_wait_on_page_writeback_range(struct address_space *mapping, |
| 659 | pgoff_t start, pgoff_t end) |
| 660 | { |
| 661 | struct pagevec pvec; |
| 662 | int nr_pages; |
| 663 | int ret = 0; |
| 664 | pgoff_t index; |
| 665 | |
| 666 | if (end < start) |
| 667 | return 0; |
| 668 | |
| 669 | pagevec_init(&pvec, 0); |
| 670 | index = start; |
| 671 | while ((index <= end) && |
| 672 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, |
| 673 | PAGECACHE_TAG_WRITEBACK, |
| 674 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) { |
| 675 | unsigned i; |
| 676 | |
| 677 | for (i = 0; i < nr_pages; i++) { |
| 678 | struct page *page = pvec.pages[i]; |
| 679 | |
| 680 | /* until radix tree lookup accepts end_index */ |
| 681 | if (page->index > end) |
| 682 | continue; |
| 683 | |
| 684 | wait_on_page_writeback(page); |
| 685 | if (PageError(page)) |
| 686 | ret = -EIO; |
| 687 | } |
| 688 | pagevec_release(&pvec); |
| 689 | cond_resched(); |
| 690 | } |
| 691 | |
| 692 | /* Check for outstanding write errors */ |
| 693 | if (test_and_clear_bit(AS_ENOSPC, &mapping->flags)) |
| 694 | ret = -ENOSPC; |
| 695 | if (test_and_clear_bit(AS_EIO, &mapping->flags)) |
| 696 | ret = -EIO; |
| 697 | |
| 698 | return ret; |
| 699 | } |