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 | |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 19 | #include <linux/slab.h> |
Chris Mason | d6bfde8 | 2008-04-30 13:59:35 -0400 | [diff] [blame] | 20 | #include <linux/blkdev.h> |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 21 | #include <linux/writeback.h> |
| 22 | #include <linux/pagevec.h> |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 23 | #include "ctree.h" |
| 24 | #include "transaction.h" |
| 25 | #include "btrfs_inode.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 26 | #include "extent_io.h" |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 27 | |
Miao Xie | 6352b91 | 2012-09-06 04:01:51 -0600 | [diff] [blame] | 28 | static struct kmem_cache *btrfs_ordered_extent_cache; |
| 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 | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 37 | /* returns NULL if the insertion worked, or it returns the node it did find |
| 38 | * in the tree |
| 39 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 40 | static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset, |
| 41 | struct rb_node *node) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 42 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 43 | struct rb_node **p = &root->rb_node; |
| 44 | struct rb_node *parent = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 45 | struct btrfs_ordered_extent *entry; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 46 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 47 | while (*p) { |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 48 | parent = *p; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 49 | entry = rb_entry(parent, struct btrfs_ordered_extent, rb_node); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 50 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 51 | if (file_offset < entry->file_offset) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 52 | p = &(*p)->rb_left; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 53 | else if (file_offset >= entry_end(entry)) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 54 | p = &(*p)->rb_right; |
| 55 | else |
| 56 | return parent; |
| 57 | } |
| 58 | |
| 59 | rb_link_node(node, parent, p); |
| 60 | rb_insert_color(node, root); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
Jeff Mahoney | 43c04fb | 2011-10-03 23:22:33 -0400 | [diff] [blame] | 64 | static void ordered_data_tree_panic(struct inode *inode, int errno, |
| 65 | u64 offset) |
| 66 | { |
| 67 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 68 | btrfs_panic(fs_info, errno, "Inconsistency in ordered tree at offset " |
| 69 | "%llu\n", (unsigned long long)offset); |
| 70 | } |
| 71 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 72 | /* |
| 73 | * look for a given offset in the tree, and if it can't be found return the |
| 74 | * first lesser offset |
| 75 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 76 | static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset, |
| 77 | struct rb_node **prev_ret) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 78 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 79 | struct rb_node *n = root->rb_node; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 80 | struct rb_node *prev = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 81 | struct rb_node *test; |
| 82 | struct btrfs_ordered_extent *entry; |
| 83 | struct btrfs_ordered_extent *prev_entry = NULL; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 84 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 85 | while (n) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 86 | entry = rb_entry(n, struct btrfs_ordered_extent, rb_node); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 87 | prev = n; |
| 88 | prev_entry = entry; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 89 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 90 | if (file_offset < entry->file_offset) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 91 | n = n->rb_left; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 92 | else if (file_offset >= entry_end(entry)) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 93 | n = n->rb_right; |
| 94 | else |
| 95 | return n; |
| 96 | } |
| 97 | if (!prev_ret) |
| 98 | return NULL; |
| 99 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 100 | while (prev && file_offset >= entry_end(prev_entry)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 101 | test = rb_next(prev); |
| 102 | if (!test) |
| 103 | break; |
| 104 | prev_entry = rb_entry(test, struct btrfs_ordered_extent, |
| 105 | rb_node); |
| 106 | if (file_offset < entry_end(prev_entry)) |
| 107 | break; |
| 108 | |
| 109 | prev = test; |
| 110 | } |
| 111 | if (prev) |
| 112 | prev_entry = rb_entry(prev, struct btrfs_ordered_extent, |
| 113 | rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 114 | while (prev && file_offset < entry_end(prev_entry)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 115 | test = rb_prev(prev); |
| 116 | if (!test) |
| 117 | break; |
| 118 | prev_entry = rb_entry(test, struct btrfs_ordered_extent, |
| 119 | rb_node); |
| 120 | prev = test; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 121 | } |
| 122 | *prev_ret = prev; |
| 123 | return NULL; |
| 124 | } |
| 125 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 126 | /* |
| 127 | * helper to check if a given offset is inside a given entry |
| 128 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 129 | 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] | 130 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 131 | if (file_offset < entry->file_offset || |
| 132 | entry->file_offset + entry->len <= file_offset) |
| 133 | return 0; |
| 134 | return 1; |
| 135 | } |
| 136 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 137 | static int range_overlaps(struct btrfs_ordered_extent *entry, u64 file_offset, |
| 138 | u64 len) |
| 139 | { |
| 140 | if (file_offset + len <= entry->file_offset || |
| 141 | entry->file_offset + entry->len <= file_offset) |
| 142 | return 0; |
| 143 | return 1; |
| 144 | } |
| 145 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 146 | /* |
| 147 | * look find the first ordered struct that has this offset, otherwise |
| 148 | * the first one less than this offset |
| 149 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 150 | static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree, |
| 151 | u64 file_offset) |
| 152 | { |
| 153 | struct rb_root *root = &tree->tree; |
Chris Mason | c87fb6f | 2011-01-31 19:54:59 -0500 | [diff] [blame] | 154 | struct rb_node *prev = NULL; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 155 | struct rb_node *ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 156 | struct btrfs_ordered_extent *entry; |
| 157 | |
| 158 | if (tree->last) { |
| 159 | entry = rb_entry(tree->last, struct btrfs_ordered_extent, |
| 160 | rb_node); |
| 161 | if (offset_in_entry(entry, file_offset)) |
| 162 | return tree->last; |
| 163 | } |
| 164 | ret = __tree_search(root, file_offset, &prev); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 165 | if (!ret) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 166 | ret = prev; |
| 167 | if (ret) |
| 168 | tree->last = ret; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 169 | return ret; |
| 170 | } |
| 171 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 172 | /* allocate and add a new ordered_extent into the per-inode tree. |
| 173 | * file_offset is the logical offset in the file |
| 174 | * |
| 175 | * start is the disk block number of an extent already reserved in the |
| 176 | * extent allocation tree |
| 177 | * |
| 178 | * len is the length of the extent |
| 179 | * |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 180 | * The tree is given a single reference on the ordered extent that was |
| 181 | * inserted. |
| 182 | */ |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 183 | static int __btrfs_add_ordered_extent(struct inode *inode, u64 file_offset, |
| 184 | u64 start, u64 len, u64 disk_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 185 | int type, int dio, int compress_type) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 186 | { |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 187 | struct btrfs_ordered_inode_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 188 | struct rb_node *node; |
| 189 | struct btrfs_ordered_extent *entry; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 190 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 191 | tree = &BTRFS_I(inode)->ordered_tree; |
Miao Xie | 6352b91 | 2012-09-06 04:01:51 -0600 | [diff] [blame] | 192 | entry = kmem_cache_zalloc(btrfs_ordered_extent_cache, GFP_NOFS); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 193 | if (!entry) |
| 194 | return -ENOMEM; |
| 195 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 196 | entry->file_offset = file_offset; |
| 197 | entry->start = start; |
| 198 | entry->len = len; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 199 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) && |
| 200 | !(type == BTRFS_ORDERED_NOCOW)) |
| 201 | entry->csum_bytes_left = disk_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 202 | entry->disk_len = disk_len; |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 203 | entry->bytes_left = len; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 204 | entry->inode = igrab(inode); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 205 | entry->compress_type = compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 206 | if (type != BTRFS_ORDERED_IO_DONE && type != BTRFS_ORDERED_COMPLETE) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 207 | set_bit(type, &entry->flags); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 208 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 209 | if (dio) |
| 210 | set_bit(BTRFS_ORDERED_DIRECT, &entry->flags); |
| 211 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 212 | /* one ref for the tree */ |
| 213 | atomic_set(&entry->refs, 1); |
| 214 | init_waitqueue_head(&entry->wait); |
| 215 | INIT_LIST_HEAD(&entry->list); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 216 | INIT_LIST_HEAD(&entry->root_extent_list); |
Miao Xie | 9afab88 | 2012-10-25 09:41:36 +0000 | [diff] [blame] | 217 | INIT_LIST_HEAD(&entry->work_list); |
| 218 | init_completion(&entry->completion); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 219 | INIT_LIST_HEAD(&entry->log_list); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 220 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 221 | trace_btrfs_ordered_extent_add(inode, entry); |
| 222 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 223 | spin_lock_irq(&tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 224 | node = tree_insert(&tree->tree, file_offset, |
| 225 | &entry->rb_node); |
Jeff Mahoney | 43c04fb | 2011-10-03 23:22:33 -0400 | [diff] [blame] | 226 | if (node) |
| 227 | ordered_data_tree_panic(inode, -EEXIST, file_offset); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 228 | spin_unlock_irq(&tree->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 229 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 230 | spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock); |
| 231 | list_add_tail(&entry->root_extent_list, |
| 232 | &BTRFS_I(inode)->root->fs_info->ordered_extents); |
| 233 | spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock); |
| 234 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 235 | return 0; |
| 236 | } |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 237 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 238 | int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset, |
| 239 | u64 start, u64 len, u64 disk_len, int type) |
| 240 | { |
| 241 | return __btrfs_add_ordered_extent(inode, file_offset, start, len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 242 | disk_len, type, 0, |
| 243 | BTRFS_COMPRESS_NONE); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | int btrfs_add_ordered_extent_dio(struct inode *inode, u64 file_offset, |
| 247 | u64 start, u64 len, u64 disk_len, int type) |
| 248 | { |
| 249 | return __btrfs_add_ordered_extent(inode, file_offset, start, len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 250 | disk_len, type, 1, |
| 251 | BTRFS_COMPRESS_NONE); |
| 252 | } |
| 253 | |
| 254 | int btrfs_add_ordered_extent_compress(struct inode *inode, u64 file_offset, |
| 255 | u64 start, u64 len, u64 disk_len, |
| 256 | int type, int compress_type) |
| 257 | { |
| 258 | return __btrfs_add_ordered_extent(inode, file_offset, start, len, |
| 259 | disk_len, type, 0, |
| 260 | compress_type); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 261 | } |
| 262 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 263 | /* |
| 264 | * 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] | 265 | * when an ordered extent is finished. If the list covers more than one |
| 266 | * ordered extent, it is split across multiples. |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 267 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 268 | void btrfs_add_ordered_sum(struct inode *inode, |
| 269 | struct btrfs_ordered_extent *entry, |
| 270 | struct btrfs_ordered_sum *sum) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 271 | { |
| 272 | struct btrfs_ordered_inode_tree *tree; |
Chris Mason | 1b1e213 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 273 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 274 | tree = &BTRFS_I(inode)->ordered_tree; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 275 | spin_lock_irq(&tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 276 | list_add_tail(&sum->list, &entry->list); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 277 | WARN_ON(entry->csum_bytes_left < sum->len); |
| 278 | entry->csum_bytes_left -= sum->len; |
| 279 | if (entry->csum_bytes_left == 0) |
| 280 | wake_up(&entry->wait); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 281 | spin_unlock_irq(&tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 282 | } |
| 283 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 284 | /* |
| 285 | * this is used to account for finished IO across a given range |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 286 | * of the file. The IO may span ordered extents. If |
| 287 | * a given ordered_extent is completely done, 1 is returned, otherwise |
| 288 | * 0. |
| 289 | * |
| 290 | * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used |
| 291 | * to make sure this function only returns 1 once for a given ordered extent. |
| 292 | * |
| 293 | * file_offset is updated to one byte past the range that is recorded as |
| 294 | * complete. This allows you to walk forward in the file. |
| 295 | */ |
| 296 | int btrfs_dec_test_first_ordered_pending(struct inode *inode, |
| 297 | struct btrfs_ordered_extent **cached, |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 298 | u64 *file_offset, u64 io_size, int uptodate) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 299 | { |
| 300 | struct btrfs_ordered_inode_tree *tree; |
| 301 | struct rb_node *node; |
| 302 | struct btrfs_ordered_extent *entry = NULL; |
| 303 | int ret; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 304 | unsigned long flags; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 305 | u64 dec_end; |
| 306 | u64 dec_start; |
| 307 | u64 to_dec; |
| 308 | |
| 309 | tree = &BTRFS_I(inode)->ordered_tree; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 310 | spin_lock_irqsave(&tree->lock, flags); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 311 | node = tree_search(tree, *file_offset); |
| 312 | if (!node) { |
| 313 | ret = 1; |
| 314 | goto out; |
| 315 | } |
| 316 | |
| 317 | entry = rb_entry(node, struct btrfs_ordered_extent, rb_node); |
| 318 | if (!offset_in_entry(entry, *file_offset)) { |
| 319 | ret = 1; |
| 320 | goto out; |
| 321 | } |
| 322 | |
| 323 | dec_start = max(*file_offset, entry->file_offset); |
| 324 | dec_end = min(*file_offset + io_size, entry->file_offset + |
| 325 | entry->len); |
| 326 | *file_offset = dec_end; |
| 327 | if (dec_start > dec_end) { |
| 328 | printk(KERN_CRIT "bad ordering dec_start %llu end %llu\n", |
| 329 | (unsigned long long)dec_start, |
| 330 | (unsigned long long)dec_end); |
| 331 | } |
| 332 | to_dec = dec_end - dec_start; |
| 333 | if (to_dec > entry->bytes_left) { |
| 334 | printk(KERN_CRIT "bad ordered accounting left %llu size %llu\n", |
| 335 | (unsigned long long)entry->bytes_left, |
| 336 | (unsigned long long)to_dec); |
| 337 | } |
| 338 | entry->bytes_left -= to_dec; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 339 | if (!uptodate) |
| 340 | set_bit(BTRFS_ORDERED_IOERR, &entry->flags); |
| 341 | |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 342 | if (entry->bytes_left == 0) |
| 343 | ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags); |
| 344 | else |
| 345 | ret = 1; |
| 346 | out: |
| 347 | if (!ret && cached && entry) { |
| 348 | *cached = entry; |
| 349 | atomic_inc(&entry->refs); |
| 350 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 351 | spin_unlock_irqrestore(&tree->lock, flags); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 352 | return ret == 0; |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * this is used to account for finished IO across a given range |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 357 | * of the file. The IO should not span ordered extents. If |
| 358 | * a given ordered_extent is completely done, 1 is returned, otherwise |
| 359 | * 0. |
| 360 | * |
| 361 | * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used |
| 362 | * to make sure this function only returns 1 once for a given ordered extent. |
| 363 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 364 | int btrfs_dec_test_ordered_pending(struct inode *inode, |
Josef Bacik | 5a1a3df | 2010-02-02 20:51:14 +0000 | [diff] [blame] | 365 | struct btrfs_ordered_extent **cached, |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 366 | u64 file_offset, u64 io_size, int uptodate) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 367 | { |
| 368 | struct btrfs_ordered_inode_tree *tree; |
| 369 | struct rb_node *node; |
Josef Bacik | 5a1a3df | 2010-02-02 20:51:14 +0000 | [diff] [blame] | 370 | struct btrfs_ordered_extent *entry = NULL; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 371 | unsigned long flags; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 372 | int ret; |
| 373 | |
| 374 | tree = &BTRFS_I(inode)->ordered_tree; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 375 | spin_lock_irqsave(&tree->lock, flags); |
| 376 | if (cached && *cached) { |
| 377 | entry = *cached; |
| 378 | goto have_entry; |
| 379 | } |
| 380 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 381 | node = tree_search(tree, file_offset); |
| 382 | if (!node) { |
| 383 | ret = 1; |
| 384 | goto out; |
| 385 | } |
| 386 | |
| 387 | entry = rb_entry(node, struct btrfs_ordered_extent, rb_node); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 388 | have_entry: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 389 | if (!offset_in_entry(entry, file_offset)) { |
| 390 | ret = 1; |
| 391 | goto out; |
| 392 | } |
| 393 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 394 | if (io_size > entry->bytes_left) { |
| 395 | printk(KERN_CRIT "bad ordered accounting left %llu size %llu\n", |
| 396 | (unsigned long long)entry->bytes_left, |
| 397 | (unsigned long long)io_size); |
| 398 | } |
| 399 | entry->bytes_left -= io_size; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 400 | if (!uptodate) |
| 401 | set_bit(BTRFS_ORDERED_IOERR, &entry->flags); |
| 402 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 403 | if (entry->bytes_left == 0) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 404 | ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 405 | else |
| 406 | ret = 1; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 407 | out: |
Josef Bacik | 5a1a3df | 2010-02-02 20:51:14 +0000 | [diff] [blame] | 408 | if (!ret && cached && entry) { |
| 409 | *cached = entry; |
| 410 | atomic_inc(&entry->refs); |
| 411 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 412 | spin_unlock_irqrestore(&tree->lock, flags); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 413 | return ret == 0; |
| 414 | } |
| 415 | |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 416 | /* Needs to either be called under a log transaction or the log_mutex */ |
| 417 | void btrfs_get_logged_extents(struct btrfs_root *log, struct inode *inode) |
| 418 | { |
| 419 | struct btrfs_ordered_inode_tree *tree; |
| 420 | struct btrfs_ordered_extent *ordered; |
| 421 | struct rb_node *n; |
| 422 | int index = log->log_transid % 2; |
| 423 | |
| 424 | tree = &BTRFS_I(inode)->ordered_tree; |
| 425 | spin_lock_irq(&tree->lock); |
| 426 | for (n = rb_first(&tree->tree); n; n = rb_next(n)) { |
| 427 | ordered = rb_entry(n, struct btrfs_ordered_extent, rb_node); |
| 428 | spin_lock(&log->log_extents_lock[index]); |
| 429 | if (list_empty(&ordered->log_list)) { |
| 430 | list_add_tail(&ordered->log_list, &log->logged_list[index]); |
| 431 | atomic_inc(&ordered->refs); |
| 432 | } |
| 433 | spin_unlock(&log->log_extents_lock[index]); |
| 434 | } |
| 435 | spin_unlock_irq(&tree->lock); |
| 436 | } |
| 437 | |
| 438 | void btrfs_wait_logged_extents(struct btrfs_root *log, u64 transid) |
| 439 | { |
| 440 | struct btrfs_ordered_extent *ordered; |
| 441 | int index = transid % 2; |
| 442 | |
| 443 | spin_lock_irq(&log->log_extents_lock[index]); |
| 444 | while (!list_empty(&log->logged_list[index])) { |
| 445 | ordered = list_first_entry(&log->logged_list[index], |
| 446 | struct btrfs_ordered_extent, |
| 447 | log_list); |
| 448 | list_del_init(&ordered->log_list); |
| 449 | spin_unlock_irq(&log->log_extents_lock[index]); |
| 450 | wait_event(ordered->wait, test_bit(BTRFS_ORDERED_IO_DONE, |
| 451 | &ordered->flags)); |
| 452 | btrfs_put_ordered_extent(ordered); |
| 453 | spin_lock_irq(&log->log_extents_lock[index]); |
| 454 | } |
| 455 | spin_unlock_irq(&log->log_extents_lock[index]); |
| 456 | } |
| 457 | |
| 458 | void btrfs_free_logged_extents(struct btrfs_root *log, u64 transid) |
| 459 | { |
| 460 | struct btrfs_ordered_extent *ordered; |
| 461 | int index = transid % 2; |
| 462 | |
| 463 | spin_lock_irq(&log->log_extents_lock[index]); |
| 464 | while (!list_empty(&log->logged_list[index])) { |
| 465 | ordered = list_first_entry(&log->logged_list[index], |
| 466 | struct btrfs_ordered_extent, |
| 467 | log_list); |
| 468 | list_del_init(&ordered->log_list); |
| 469 | spin_unlock_irq(&log->log_extents_lock[index]); |
| 470 | btrfs_put_ordered_extent(ordered); |
| 471 | spin_lock_irq(&log->log_extents_lock[index]); |
| 472 | } |
| 473 | spin_unlock_irq(&log->log_extents_lock[index]); |
| 474 | } |
| 475 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 476 | /* |
| 477 | * used to drop a reference on an ordered extent. This will free |
| 478 | * the extent if the last reference is dropped |
| 479 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 480 | void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 481 | { |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 482 | struct list_head *cur; |
| 483 | struct btrfs_ordered_sum *sum; |
| 484 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 485 | trace_btrfs_ordered_extent_put(entry->inode, entry); |
| 486 | |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 487 | if (atomic_dec_and_test(&entry->refs)) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 488 | if (entry->inode) |
| 489 | btrfs_add_delayed_iput(entry->inode); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 490 | while (!list_empty(&entry->list)) { |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 491 | cur = entry->list.next; |
| 492 | sum = list_entry(cur, struct btrfs_ordered_sum, list); |
| 493 | list_del(&sum->list); |
| 494 | kfree(sum); |
| 495 | } |
Miao Xie | 6352b91 | 2012-09-06 04:01:51 -0600 | [diff] [blame] | 496 | kmem_cache_free(btrfs_ordered_extent_cache, entry); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 497 | } |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 498 | } |
| 499 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 500 | /* |
| 501 | * remove an ordered extent from the tree. No references are dropped |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 502 | * and waiters are woken up. |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 503 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 504 | void btrfs_remove_ordered_extent(struct inode *inode, |
| 505 | struct btrfs_ordered_extent *entry) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 506 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 507 | struct btrfs_ordered_inode_tree *tree; |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 508 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 509 | struct rb_node *node; |
| 510 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 511 | tree = &BTRFS_I(inode)->ordered_tree; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 512 | spin_lock_irq(&tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 513 | node = &entry->rb_node; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 514 | rb_erase(node, &tree->tree); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 515 | tree->last = NULL; |
| 516 | set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 517 | spin_unlock_irq(&tree->lock); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 518 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 519 | spin_lock(&root->fs_info->ordered_extent_lock); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 520 | list_del_init(&entry->root_extent_list); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 521 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 522 | trace_btrfs_ordered_extent_remove(inode, entry); |
| 523 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 524 | /* |
| 525 | * we have no more ordered extents for this inode and |
| 526 | * no dirty pages. We can safely remove it from the |
| 527 | * list of ordered extents |
| 528 | */ |
| 529 | if (RB_EMPTY_ROOT(&tree->tree) && |
| 530 | !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) { |
| 531 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 532 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 533 | spin_unlock(&root->fs_info->ordered_extent_lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 534 | wake_up(&entry->wait); |
Chris Mason | 81d7ed2 | 2008-04-25 08:51:48 -0400 | [diff] [blame] | 535 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 536 | |
Miao Xie | 9afab88 | 2012-10-25 09:41:36 +0000 | [diff] [blame] | 537 | static void btrfs_run_ordered_extent_work(struct btrfs_work *work) |
| 538 | { |
| 539 | struct btrfs_ordered_extent *ordered; |
| 540 | |
| 541 | ordered = container_of(work, struct btrfs_ordered_extent, flush_work); |
| 542 | btrfs_start_ordered_extent(ordered->inode, ordered, 1); |
| 543 | complete(&ordered->completion); |
| 544 | } |
| 545 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 546 | /* |
| 547 | * wait for all the ordered extents in a root. This is done when balancing |
| 548 | * space between drives. |
| 549 | */ |
Liu Bo | 6bbe3a9 | 2012-09-14 02:58:07 -0600 | [diff] [blame] | 550 | void btrfs_wait_ordered_extents(struct btrfs_root *root, int delay_iput) |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 551 | { |
Miao Xie | 9afab88 | 2012-10-25 09:41:36 +0000 | [diff] [blame] | 552 | struct list_head splice, works; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 553 | struct list_head *cur; |
Miao Xie | 9afab88 | 2012-10-25 09:41:36 +0000 | [diff] [blame] | 554 | struct btrfs_ordered_extent *ordered, *next; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 555 | struct inode *inode; |
| 556 | |
| 557 | INIT_LIST_HEAD(&splice); |
Miao Xie | 9afab88 | 2012-10-25 09:41:36 +0000 | [diff] [blame] | 558 | INIT_LIST_HEAD(&works); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 559 | |
Josef Bacik | db1d607 | 2013-03-26 15:29:11 -0400 | [diff] [blame] | 560 | mutex_lock(&root->fs_info->ordered_operations_mutex); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 561 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 562 | list_splice_init(&root->fs_info->ordered_extents, &splice); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 563 | while (!list_empty(&splice)) { |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 564 | cur = splice.next; |
| 565 | ordered = list_entry(cur, struct btrfs_ordered_extent, |
| 566 | root_extent_list); |
| 567 | list_del_init(&ordered->root_extent_list); |
| 568 | atomic_inc(&ordered->refs); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 569 | |
| 570 | /* |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 571 | * the inode may be getting freed (in sys_unlink path). |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 572 | */ |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 573 | inode = igrab(ordered->inode); |
| 574 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 575 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 576 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 577 | if (inode) { |
Miao Xie | 9afab88 | 2012-10-25 09:41:36 +0000 | [diff] [blame] | 578 | ordered->flush_work.func = btrfs_run_ordered_extent_work; |
| 579 | list_add_tail(&ordered->work_list, &works); |
| 580 | btrfs_queue_worker(&root->fs_info->flush_workers, |
| 581 | &ordered->flush_work); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 582 | } else { |
| 583 | btrfs_put_ordered_extent(ordered); |
| 584 | } |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 585 | |
Miao Xie | 9afab88 | 2012-10-25 09:41:36 +0000 | [diff] [blame] | 586 | cond_resched(); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 587 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 588 | } |
| 589 | spin_unlock(&root->fs_info->ordered_extent_lock); |
Miao Xie | 9afab88 | 2012-10-25 09:41:36 +0000 | [diff] [blame] | 590 | |
| 591 | list_for_each_entry_safe(ordered, next, &works, work_list) { |
| 592 | list_del_init(&ordered->work_list); |
| 593 | wait_for_completion(&ordered->completion); |
| 594 | |
| 595 | inode = ordered->inode; |
| 596 | btrfs_put_ordered_extent(ordered); |
| 597 | if (delay_iput) |
| 598 | btrfs_add_delayed_iput(inode); |
| 599 | else |
| 600 | iput(inode); |
| 601 | |
| 602 | cond_resched(); |
| 603 | } |
Josef Bacik | db1d607 | 2013-03-26 15:29:11 -0400 | [diff] [blame] | 604 | mutex_unlock(&root->fs_info->ordered_operations_mutex); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 605 | } |
| 606 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 607 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 608 | * this is used during transaction commit to write all the inodes |
| 609 | * added to the ordered operation list. These files must be fully on |
| 610 | * disk before the transaction commits. |
| 611 | * |
| 612 | * we have two modes here, one is to just start the IO via filemap_flush |
| 613 | * and the other is to wait for all the io. When we wait, we have an |
| 614 | * extra check to make sure the ordered operation list really is empty |
| 615 | * before we return |
| 616 | */ |
Josef Bacik | 569e0f3 | 2013-02-13 11:09:14 -0500 | [diff] [blame] | 617 | int btrfs_run_ordered_operations(struct btrfs_trans_handle *trans, |
| 618 | struct btrfs_root *root, int wait) |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 619 | { |
| 620 | struct btrfs_inode *btrfs_inode; |
| 621 | struct inode *inode; |
Josef Bacik | 569e0f3 | 2013-02-13 11:09:14 -0500 | [diff] [blame] | 622 | struct btrfs_transaction *cur_trans = trans->transaction; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 623 | struct list_head splice; |
Miao Xie | 25287e0 | 2012-10-25 09:31:03 +0000 | [diff] [blame] | 624 | struct list_head works; |
| 625 | struct btrfs_delalloc_work *work, *next; |
| 626 | int ret = 0; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 627 | |
| 628 | INIT_LIST_HEAD(&splice); |
Miao Xie | 25287e0 | 2012-10-25 09:31:03 +0000 | [diff] [blame] | 629 | INIT_LIST_HEAD(&works); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 630 | |
| 631 | mutex_lock(&root->fs_info->ordered_operations_mutex); |
| 632 | spin_lock(&root->fs_info->ordered_extent_lock); |
Josef Bacik | 569e0f3 | 2013-02-13 11:09:14 -0500 | [diff] [blame] | 633 | list_splice_init(&cur_trans->ordered_operations, &splice); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 634 | while (!list_empty(&splice)) { |
| 635 | btrfs_inode = list_entry(splice.next, struct btrfs_inode, |
| 636 | ordered_operations); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 637 | inode = &btrfs_inode->vfs_inode; |
| 638 | |
| 639 | list_del_init(&btrfs_inode->ordered_operations); |
| 640 | |
| 641 | /* |
| 642 | * the inode may be getting freed (in sys_unlink path). |
| 643 | */ |
| 644 | inode = igrab(inode); |
Miao Xie | 25287e0 | 2012-10-25 09:31:03 +0000 | [diff] [blame] | 645 | if (!inode) |
| 646 | continue; |
Miao Xie | 5b947f1 | 2013-01-22 10:52:04 +0000 | [diff] [blame] | 647 | |
| 648 | if (!wait) |
| 649 | list_add_tail(&BTRFS_I(inode)->ordered_operations, |
Josef Bacik | 569e0f3 | 2013-02-13 11:09:14 -0500 | [diff] [blame] | 650 | &cur_trans->ordered_operations); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 651 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 652 | |
Miao Xie | 25287e0 | 2012-10-25 09:31:03 +0000 | [diff] [blame] | 653 | work = btrfs_alloc_delalloc_work(inode, wait, 1); |
| 654 | if (!work) { |
Miao Xie | 5b947f1 | 2013-01-22 10:52:04 +0000 | [diff] [blame] | 655 | spin_lock(&root->fs_info->ordered_extent_lock); |
Miao Xie | 25287e0 | 2012-10-25 09:31:03 +0000 | [diff] [blame] | 656 | if (list_empty(&BTRFS_I(inode)->ordered_operations)) |
| 657 | list_add_tail(&btrfs_inode->ordered_operations, |
| 658 | &splice); |
Miao Xie | 25287e0 | 2012-10-25 09:31:03 +0000 | [diff] [blame] | 659 | list_splice_tail(&splice, |
Josef Bacik | 569e0f3 | 2013-02-13 11:09:14 -0500 | [diff] [blame] | 660 | &cur_trans->ordered_operations); |
Miao Xie | 25287e0 | 2012-10-25 09:31:03 +0000 | [diff] [blame] | 661 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 662 | ret = -ENOMEM; |
| 663 | goto out; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 664 | } |
Miao Xie | 25287e0 | 2012-10-25 09:31:03 +0000 | [diff] [blame] | 665 | list_add_tail(&work->list, &works); |
| 666 | btrfs_queue_worker(&root->fs_info->flush_workers, |
| 667 | &work->work); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 668 | |
| 669 | cond_resched(); |
| 670 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 671 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 672 | spin_unlock(&root->fs_info->ordered_extent_lock); |
Miao Xie | 25287e0 | 2012-10-25 09:31:03 +0000 | [diff] [blame] | 673 | out: |
| 674 | list_for_each_entry_safe(work, next, &works, list) { |
| 675 | list_del_init(&work->list); |
| 676 | btrfs_wait_and_free_delalloc_work(work); |
| 677 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 678 | mutex_unlock(&root->fs_info->ordered_operations_mutex); |
Miao Xie | 25287e0 | 2012-10-25 09:31:03 +0000 | [diff] [blame] | 679 | return ret; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /* |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 683 | * Used to start IO or wait for a given ordered extent to finish. |
| 684 | * |
| 685 | * If wait is one, this effectively waits on page writeback for all the pages |
| 686 | * in the extent, and it waits on the io completion code to insert |
| 687 | * metadata into the btree corresponding to the extent |
| 688 | */ |
| 689 | void btrfs_start_ordered_extent(struct inode *inode, |
| 690 | struct btrfs_ordered_extent *entry, |
| 691 | int wait) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 692 | { |
| 693 | u64 start = entry->file_offset; |
| 694 | u64 end = start + entry->len - 1; |
| 695 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 696 | trace_btrfs_ordered_extent_start(inode, entry); |
| 697 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 698 | /* |
| 699 | * pages in the range can be dirty, clean or writeback. We |
| 700 | * start IO on any dirty ones so the wait doesn't stall waiting |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 701 | * for the flusher thread to find them |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 702 | */ |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 703 | if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags)) |
| 704 | filemap_fdatawrite_range(inode->i_mapping, start, end); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 705 | if (wait) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 706 | wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE, |
| 707 | &entry->flags)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 708 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 709 | } |
| 710 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 711 | /* |
| 712 | * Used to wait on ordered extents across a large range of bytes. |
| 713 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 714 | void btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 715 | { |
| 716 | u64 end; |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 717 | u64 orig_end; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 718 | struct btrfs_ordered_extent *ordered; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 719 | |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 720 | if (start + len < start) { |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 721 | orig_end = INT_LIMIT(loff_t); |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 722 | } else { |
| 723 | orig_end = start + len - 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 724 | if (orig_end > INT_LIMIT(loff_t)) |
| 725 | orig_end = INT_LIMIT(loff_t); |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 726 | } |
Josef Bacik | 551ebb2 | 2012-04-23 14:41:09 -0400 | [diff] [blame] | 727 | |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 728 | /* start IO across the range first to instantiate any delalloc |
| 729 | * extents |
| 730 | */ |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 731 | filemap_fdatawrite_range(inode->i_mapping, start, orig_end); |
| 732 | |
| 733 | /* |
| 734 | * So with compression we will find and lock a dirty page and clear the |
| 735 | * first one as dirty, setup an async extent, and immediately return |
| 736 | * with the entire range locked but with nobody actually marked with |
| 737 | * writeback. So we can't just filemap_write_and_wait_range() and |
| 738 | * expect it to work since it will just kick off a thread to do the |
| 739 | * actual work. So we need to call filemap_fdatawrite_range _again_ |
| 740 | * since it will wait on the page lock, which won't be unlocked until |
| 741 | * after the pages have been marked as writeback and so we're good to go |
| 742 | * from there. We have to do this otherwise we'll miss the ordered |
| 743 | * extents and that results in badness. Please Josef, do not think you |
| 744 | * know better and pull this out at some point in the future, it is |
| 745 | * right and you are wrong. |
| 746 | */ |
| 747 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 748 | &BTRFS_I(inode)->runtime_flags)) |
| 749 | filemap_fdatawrite_range(inode->i_mapping, start, orig_end); |
| 750 | |
| 751 | filemap_fdatawait_range(inode->i_mapping, start, orig_end); |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 752 | |
| 753 | end = orig_end; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 754 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 755 | ordered = btrfs_lookup_first_ordered_extent(inode, end); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 756 | if (!ordered) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 757 | break; |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 758 | if (ordered->file_offset > orig_end) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 759 | btrfs_put_ordered_extent(ordered); |
| 760 | break; |
| 761 | } |
| 762 | if (ordered->file_offset + ordered->len < start) { |
| 763 | btrfs_put_ordered_extent(ordered); |
| 764 | break; |
| 765 | } |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 766 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 767 | end = ordered->file_offset; |
| 768 | btrfs_put_ordered_extent(ordered); |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 769 | if (end == 0 || end == start) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 770 | break; |
| 771 | end--; |
| 772 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 773 | } |
| 774 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 775 | /* |
| 776 | * find an ordered extent corresponding to file_offset. return NULL if |
| 777 | * nothing is found, otherwise take a reference on the extent and return it |
| 778 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 779 | struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode, |
| 780 | u64 file_offset) |
| 781 | { |
| 782 | struct btrfs_ordered_inode_tree *tree; |
| 783 | struct rb_node *node; |
| 784 | struct btrfs_ordered_extent *entry = NULL; |
| 785 | |
| 786 | tree = &BTRFS_I(inode)->ordered_tree; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 787 | spin_lock_irq(&tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 788 | node = tree_search(tree, file_offset); |
| 789 | if (!node) |
| 790 | goto out; |
| 791 | |
| 792 | entry = rb_entry(node, struct btrfs_ordered_extent, rb_node); |
| 793 | if (!offset_in_entry(entry, file_offset)) |
| 794 | entry = NULL; |
| 795 | if (entry) |
| 796 | atomic_inc(&entry->refs); |
| 797 | out: |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 798 | spin_unlock_irq(&tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 799 | return entry; |
| 800 | } |
| 801 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 802 | /* Since the DIO code tries to lock a wide area we need to look for any ordered |
| 803 | * extents that exist in the range, rather than just the start of the range. |
| 804 | */ |
| 805 | struct btrfs_ordered_extent *btrfs_lookup_ordered_range(struct inode *inode, |
| 806 | u64 file_offset, |
| 807 | u64 len) |
| 808 | { |
| 809 | struct btrfs_ordered_inode_tree *tree; |
| 810 | struct rb_node *node; |
| 811 | struct btrfs_ordered_extent *entry = NULL; |
| 812 | |
| 813 | tree = &BTRFS_I(inode)->ordered_tree; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 814 | spin_lock_irq(&tree->lock); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 815 | node = tree_search(tree, file_offset); |
| 816 | if (!node) { |
| 817 | node = tree_search(tree, file_offset + len); |
| 818 | if (!node) |
| 819 | goto out; |
| 820 | } |
| 821 | |
| 822 | while (1) { |
| 823 | entry = rb_entry(node, struct btrfs_ordered_extent, rb_node); |
| 824 | if (range_overlaps(entry, file_offset, len)) |
| 825 | break; |
| 826 | |
| 827 | if (entry->file_offset >= file_offset + len) { |
| 828 | entry = NULL; |
| 829 | break; |
| 830 | } |
| 831 | entry = NULL; |
| 832 | node = rb_next(node); |
| 833 | if (!node) |
| 834 | break; |
| 835 | } |
| 836 | out: |
| 837 | if (entry) |
| 838 | atomic_inc(&entry->refs); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 839 | spin_unlock_irq(&tree->lock); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 840 | return entry; |
| 841 | } |
| 842 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 843 | /* |
| 844 | * lookup and return any extent before 'file_offset'. NULL is returned |
| 845 | * if none is found |
| 846 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 847 | struct btrfs_ordered_extent * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 848 | btrfs_lookup_first_ordered_extent(struct inode *inode, u64 file_offset) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 849 | { |
| 850 | struct btrfs_ordered_inode_tree *tree; |
| 851 | struct rb_node *node; |
| 852 | struct btrfs_ordered_extent *entry = NULL; |
| 853 | |
| 854 | tree = &BTRFS_I(inode)->ordered_tree; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 855 | spin_lock_irq(&tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 856 | node = tree_search(tree, file_offset); |
| 857 | if (!node) |
| 858 | goto out; |
| 859 | |
| 860 | entry = rb_entry(node, struct btrfs_ordered_extent, rb_node); |
| 861 | atomic_inc(&entry->refs); |
| 862 | out: |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 863 | spin_unlock_irq(&tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 864 | return entry; |
| 865 | } |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 866 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 867 | /* |
| 868 | * After an extent is done, call this to conditionally update the on disk |
| 869 | * i_size. i_size is updated to cover any fully written part of the file. |
| 870 | */ |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 871 | int btrfs_ordered_update_i_size(struct inode *inode, u64 offset, |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 872 | struct btrfs_ordered_extent *ordered) |
| 873 | { |
| 874 | struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 875 | u64 disk_i_size; |
| 876 | u64 new_i_size; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 877 | u64 i_size = i_size_read(inode); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 878 | struct rb_node *node; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 879 | struct rb_node *prev = NULL; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 880 | struct btrfs_ordered_extent *test; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 881 | int ret = 1; |
| 882 | |
| 883 | if (ordered) |
| 884 | offset = entry_end(ordered); |
Yan, Zheng | a038fab0 | 2009-12-28 05:01:58 +0000 | [diff] [blame] | 885 | else |
| 886 | offset = ALIGN(offset, BTRFS_I(inode)->root->sectorsize); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 887 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 888 | spin_lock_irq(&tree->lock); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 889 | disk_i_size = BTRFS_I(inode)->disk_i_size; |
| 890 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 891 | /* truncate file */ |
| 892 | if (disk_i_size > i_size) { |
| 893 | BTRFS_I(inode)->disk_i_size = i_size; |
| 894 | ret = 0; |
| 895 | goto out; |
| 896 | } |
| 897 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 898 | /* |
| 899 | * if the disk i_size is already at the inode->i_size, or |
| 900 | * this ordered extent is inside the disk i_size, we're done |
| 901 | */ |
Josef Bacik | 5d1f402 | 2013-01-30 14:17:31 -0500 | [diff] [blame] | 902 | if (disk_i_size == i_size) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 903 | goto out; |
Josef Bacik | 5d1f402 | 2013-01-30 14:17:31 -0500 | [diff] [blame] | 904 | |
| 905 | /* |
| 906 | * We still need to update disk_i_size if outstanding_isize is greater |
| 907 | * than disk_i_size. |
| 908 | */ |
| 909 | if (offset <= disk_i_size && |
| 910 | (!ordered || ordered->outstanding_isize <= disk_i_size)) |
| 911 | goto out; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 912 | |
| 913 | /* |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 914 | * walk backward from this ordered extent to disk_i_size. |
| 915 | * if we find an ordered extent then we can't update disk i_size |
| 916 | * yet |
| 917 | */ |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 918 | if (ordered) { |
| 919 | node = rb_prev(&ordered->rb_node); |
| 920 | } else { |
| 921 | prev = tree_search(tree, offset); |
| 922 | /* |
| 923 | * we insert file extents without involving ordered struct, |
| 924 | * so there should be no ordered struct cover this offset |
| 925 | */ |
| 926 | if (prev) { |
| 927 | test = rb_entry(prev, struct btrfs_ordered_extent, |
| 928 | rb_node); |
| 929 | BUG_ON(offset_in_entry(test, offset)); |
| 930 | } |
| 931 | node = prev; |
| 932 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 933 | for (; node; node = rb_prev(node)) { |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 934 | test = rb_entry(node, struct btrfs_ordered_extent, rb_node); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 935 | |
| 936 | /* We treat this entry as if it doesnt exist */ |
| 937 | if (test_bit(BTRFS_ORDERED_UPDATED_ISIZE, &test->flags)) |
| 938 | continue; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 939 | if (test->file_offset + test->len <= disk_i_size) |
| 940 | break; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 941 | if (test->file_offset >= i_size) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 942 | break; |
Josef Bacik | 59fe4f4 | 2013-01-30 14:31:31 -0500 | [diff] [blame] | 943 | if (entry_end(test) > disk_i_size) { |
Miao Xie | b9a8cc5 | 2012-09-06 04:01:21 -0600 | [diff] [blame] | 944 | /* |
| 945 | * we don't update disk_i_size now, so record this |
| 946 | * undealt i_size. Or we will not know the real |
| 947 | * i_size. |
| 948 | */ |
| 949 | if (test->outstanding_isize < offset) |
| 950 | test->outstanding_isize = offset; |
| 951 | if (ordered && |
| 952 | ordered->outstanding_isize > |
| 953 | test->outstanding_isize) |
| 954 | test->outstanding_isize = |
| 955 | ordered->outstanding_isize; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 956 | goto out; |
Miao Xie | b9a8cc5 | 2012-09-06 04:01:21 -0600 | [diff] [blame] | 957 | } |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 958 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 959 | new_i_size = min_t(u64, offset, i_size); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 960 | |
| 961 | /* |
Miao Xie | b9a8cc5 | 2012-09-06 04:01:21 -0600 | [diff] [blame] | 962 | * Some ordered extents may completed before the current one, and |
| 963 | * we hold the real i_size in ->outstanding_isize. |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 964 | */ |
Miao Xie | b9a8cc5 | 2012-09-06 04:01:21 -0600 | [diff] [blame] | 965 | if (ordered && ordered->outstanding_isize > new_i_size) |
| 966 | new_i_size = min_t(u64, ordered->outstanding_isize, i_size); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 967 | BTRFS_I(inode)->disk_i_size = new_i_size; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 968 | ret = 0; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 969 | out: |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 970 | /* |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 971 | * We need to do this because we can't remove ordered extents until |
| 972 | * after the i_disk_size has been updated and then the inode has been |
| 973 | * updated to reflect the change, so we need to tell anybody who finds |
| 974 | * this ordered extent that we've already done all the real work, we |
| 975 | * just haven't completed all the other work. |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 976 | */ |
| 977 | if (ordered) |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 978 | set_bit(BTRFS_ORDERED_UPDATED_ISIZE, &ordered->flags); |
| 979 | spin_unlock_irq(&tree->lock); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 980 | return ret; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 981 | } |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 982 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 983 | /* |
| 984 | * search the ordered extents for one corresponding to 'offset' and |
| 985 | * try to find a checksum. This is used because we allow pages to |
| 986 | * be reclaimed before their checksum is actually put into the btree |
| 987 | */ |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 988 | int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr, |
| 989 | u32 *sum) |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 990 | { |
| 991 | struct btrfs_ordered_sum *ordered_sum; |
| 992 | struct btrfs_sector_sum *sector_sums; |
| 993 | struct btrfs_ordered_extent *ordered; |
| 994 | struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 995 | unsigned long num_sectors; |
| 996 | unsigned long i; |
| 997 | u32 sectorsize = BTRFS_I(inode)->root->sectorsize; |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 998 | int ret = 1; |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 999 | |
| 1000 | ordered = btrfs_lookup_ordered_extent(inode, offset); |
| 1001 | if (!ordered) |
| 1002 | return 1; |
| 1003 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1004 | spin_lock_irq(&tree->lock); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1005 | list_for_each_entry_reverse(ordered_sum, &ordered->list, list) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1006 | if (disk_bytenr >= ordered_sum->bytenr) { |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 1007 | num_sectors = ordered_sum->len / sectorsize; |
Chris Mason | ed98b56 | 2008-07-22 23:06:42 -0400 | [diff] [blame] | 1008 | sector_sums = ordered_sum->sums; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 1009 | for (i = 0; i < num_sectors; i++) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1010 | if (sector_sums[i].bytenr == disk_bytenr) { |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 1011 | *sum = sector_sums[i].sum; |
| 1012 | ret = 0; |
| 1013 | goto out; |
| 1014 | } |
| 1015 | } |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | out: |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1019 | spin_unlock_irq(&tree->lock); |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 1020 | btrfs_put_ordered_extent(ordered); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1021 | return ret; |
| 1022 | } |
| 1023 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1024 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 1025 | /* |
| 1026 | * add a given inode to the list of inodes that must be fully on |
| 1027 | * disk before a transaction commit finishes. |
| 1028 | * |
| 1029 | * This basically gives us the ext3 style data=ordered mode, and it is mostly |
| 1030 | * used to make sure renamed files are fully on disk. |
| 1031 | * |
| 1032 | * It is a noop if the inode is already fully on disk. |
| 1033 | * |
| 1034 | * If trans is not null, we'll do a friendly check for a transaction that |
| 1035 | * is already flushing things and force the IO down ourselves. |
| 1036 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1037 | void btrfs_add_ordered_operation(struct btrfs_trans_handle *trans, |
| 1038 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 1039 | { |
Josef Bacik | 569e0f3 | 2013-02-13 11:09:14 -0500 | [diff] [blame] | 1040 | struct btrfs_transaction *cur_trans = trans->transaction; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 1041 | u64 last_mod; |
| 1042 | |
| 1043 | last_mod = max(BTRFS_I(inode)->generation, BTRFS_I(inode)->last_trans); |
| 1044 | |
| 1045 | /* |
| 1046 | * if this file hasn't been changed since the last transaction |
| 1047 | * commit, we can safely return without doing anything |
| 1048 | */ |
| 1049 | if (last_mod < root->fs_info->last_trans_committed) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1050 | return; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 1051 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 1052 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 1053 | if (list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 1054 | list_add_tail(&BTRFS_I(inode)->ordered_operations, |
Josef Bacik | 569e0f3 | 2013-02-13 11:09:14 -0500 | [diff] [blame] | 1055 | &cur_trans->ordered_operations); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 1056 | } |
| 1057 | spin_unlock(&root->fs_info->ordered_extent_lock); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 1058 | } |
Miao Xie | 6352b91 | 2012-09-06 04:01:51 -0600 | [diff] [blame] | 1059 | |
| 1060 | int __init ordered_data_init(void) |
| 1061 | { |
| 1062 | btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent", |
| 1063 | sizeof(struct btrfs_ordered_extent), 0, |
| 1064 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 1065 | NULL); |
| 1066 | if (!btrfs_ordered_extent_cache) |
| 1067 | return -ENOMEM; |
Miao Xie | 25287e0 | 2012-10-25 09:31:03 +0000 | [diff] [blame] | 1068 | |
Miao Xie | 6352b91 | 2012-09-06 04:01:51 -0600 | [diff] [blame] | 1069 | return 0; |
| 1070 | } |
| 1071 | |
| 1072 | void ordered_data_exit(void) |
| 1073 | { |
| 1074 | if (btrfs_ordered_extent_cache) |
| 1075 | kmem_cache_destroy(btrfs_ordered_extent_cache); |
| 1076 | } |