David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Oracle. All rights reserved. |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 4 | */ |
| 5 | |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 6 | #include <linux/slab.h> |
Chris Mason | d6bfde8 | 2008-04-30 13:59:35 -0400 | [diff] [blame] | 7 | #include <linux/blkdev.h> |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 8 | #include <linux/writeback.h> |
Nikolay Borisov | a3d46ae | 2019-04-01 11:29:58 +0300 | [diff] [blame] | 9 | #include <linux/sched/mm.h> |
David Sterba | 602cbe9 | 2019-08-21 18:48:25 +0200 | [diff] [blame] | 10 | #include "misc.h" |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 11 | #include "ctree.h" |
| 12 | #include "transaction.h" |
| 13 | #include "btrfs_inode.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 14 | #include "extent_io.h" |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 15 | #include "disk-io.h" |
Anand Jain | ebb8765 | 2016-03-10 17:26:59 +0800 | [diff] [blame] | 16 | #include "compression.h" |
Josef Bacik | 8673634 | 2019-06-19 15:12:00 -0400 | [diff] [blame] | 17 | #include "delalloc-space.h" |
Qu Wenruo | 7dbeaad | 2020-06-10 09:04:43 +0800 | [diff] [blame] | 18 | #include "qgroup.h" |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 19 | #include "subpage.h" |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 20 | |
Miao Xie | 6352b91 | 2012-09-06 04:01:51 -0600 | [diff] [blame] | 21 | static struct kmem_cache *btrfs_ordered_extent_cache; |
| 22 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 23 | static u64 entry_end(struct btrfs_ordered_extent *entry) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 24 | { |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 25 | if (entry->file_offset + entry->num_bytes < entry->file_offset) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 26 | return (u64)-1; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 27 | return entry->file_offset + entry->num_bytes; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 28 | } |
| 29 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 30 | /* returns NULL if the insertion worked, or it returns the node it did find |
| 31 | * in the tree |
| 32 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 33 | static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset, |
| 34 | struct rb_node *node) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 35 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 36 | struct rb_node **p = &root->rb_node; |
| 37 | struct rb_node *parent = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 38 | struct btrfs_ordered_extent *entry; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 39 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 40 | while (*p) { |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 41 | parent = *p; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 42 | entry = rb_entry(parent, struct btrfs_ordered_extent, rb_node); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 43 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 44 | if (file_offset < entry->file_offset) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 45 | p = &(*p)->rb_left; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 46 | else if (file_offset >= entry_end(entry)) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 47 | p = &(*p)->rb_right; |
| 48 | else |
| 49 | return parent; |
| 50 | } |
| 51 | |
| 52 | rb_link_node(node, parent, p); |
| 53 | rb_insert_color(node, root); |
| 54 | return NULL; |
| 55 | } |
| 56 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 57 | /* |
| 58 | * look for a given offset in the tree, and if it can't be found return the |
| 59 | * first lesser offset |
| 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 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 64 | struct rb_node *n = root->rb_node; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 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 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 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 | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 85 | while (prev && file_offset >= entry_end(prev_entry)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 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); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 99 | while (prev && file_offset < entry_end(prev_entry)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 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 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 111 | static int range_overlaps(struct btrfs_ordered_extent *entry, u64 file_offset, |
| 112 | u64 len) |
| 113 | { |
| 114 | if (file_offset + len <= entry->file_offset || |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 115 | entry->file_offset + entry->num_bytes <= file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 116 | return 0; |
| 117 | return 1; |
| 118 | } |
| 119 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 120 | /* |
| 121 | * look find the first ordered struct that has this offset, otherwise |
| 122 | * the first one less than this offset |
| 123 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 124 | static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree, |
| 125 | u64 file_offset) |
| 126 | { |
| 127 | struct rb_root *root = &tree->tree; |
Chris Mason | c87fb6f | 2011-01-31 19:54:59 -0500 | [diff] [blame] | 128 | struct rb_node *prev = NULL; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 129 | struct rb_node *ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 130 | struct btrfs_ordered_extent *entry; |
| 131 | |
| 132 | if (tree->last) { |
| 133 | entry = rb_entry(tree->last, struct btrfs_ordered_extent, |
| 134 | rb_node); |
Nikolay Borisov | 20bbf20 | 2021-02-17 15:12:49 +0200 | [diff] [blame] | 135 | if (in_range(file_offset, entry->file_offset, entry->num_bytes)) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 136 | return tree->last; |
| 137 | } |
| 138 | ret = __tree_search(root, file_offset, &prev); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 139 | if (!ret) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 140 | ret = prev; |
| 141 | if (ret) |
| 142 | tree->last = ret; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 143 | return ret; |
| 144 | } |
| 145 | |
Qu Wenruo | 7dbeaad | 2020-06-10 09:04:43 +0800 | [diff] [blame] | 146 | /* |
| 147 | * Allocate and add a new ordered_extent into the per-inode tree. |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 148 | * |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 149 | * The tree is given a single reference on the ordered extent that was |
| 150 | * inserted. |
| 151 | */ |
Nikolay Borisov | da69fea | 2020-06-03 08:55:01 +0300 | [diff] [blame] | 152 | static int __btrfs_add_ordered_extent(struct btrfs_inode *inode, u64 file_offset, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 153 | u64 disk_bytenr, u64 num_bytes, |
| 154 | u64 disk_num_bytes, int type, int dio, |
| 155 | int compress_type) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 156 | { |
Nikolay Borisov | da69fea | 2020-06-03 08:55:01 +0300 | [diff] [blame] | 157 | struct btrfs_root *root = inode->root; |
| 158 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 159 | struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 160 | struct rb_node *node; |
| 161 | struct btrfs_ordered_extent *entry; |
Qu Wenruo | 7dbeaad | 2020-06-10 09:04:43 +0800 | [diff] [blame] | 162 | int ret; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 163 | |
Qu Wenruo | 7dbeaad | 2020-06-10 09:04:43 +0800 | [diff] [blame] | 164 | if (type == BTRFS_ORDERED_NOCOW || type == BTRFS_ORDERED_PREALLOC) { |
| 165 | /* For nocow write, we can release the qgroup rsv right now */ |
Nikolay Borisov | 8b8a979 | 2020-06-03 08:55:11 +0300 | [diff] [blame] | 166 | ret = btrfs_qgroup_free_data(inode, NULL, file_offset, num_bytes); |
Qu Wenruo | 7dbeaad | 2020-06-10 09:04:43 +0800 | [diff] [blame] | 167 | if (ret < 0) |
| 168 | return ret; |
| 169 | ret = 0; |
| 170 | } else { |
| 171 | /* |
| 172 | * The ordered extent has reserved qgroup space, release now |
| 173 | * and pass the reserved number for qgroup_record to free. |
| 174 | */ |
Nikolay Borisov | 72b7d15 | 2020-06-03 08:55:18 +0300 | [diff] [blame] | 175 | ret = btrfs_qgroup_release_data(inode, file_offset, num_bytes); |
Qu Wenruo | 7dbeaad | 2020-06-10 09:04:43 +0800 | [diff] [blame] | 176 | if (ret < 0) |
| 177 | return ret; |
| 178 | } |
Miao Xie | 6352b91 | 2012-09-06 04:01:51 -0600 | [diff] [blame] | 179 | entry = kmem_cache_zalloc(btrfs_ordered_extent_cache, GFP_NOFS); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 180 | if (!entry) |
| 181 | return -ENOMEM; |
| 182 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 183 | entry->file_offset = file_offset; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 184 | entry->disk_bytenr = disk_bytenr; |
| 185 | entry->num_bytes = num_bytes; |
| 186 | entry->disk_num_bytes = disk_num_bytes; |
| 187 | entry->bytes_left = num_bytes; |
Nikolay Borisov | da69fea | 2020-06-03 08:55:01 +0300 | [diff] [blame] | 188 | entry->inode = igrab(&inode->vfs_inode); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 189 | entry->compress_type = compress_type; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 190 | entry->truncated_len = (u64)-1; |
Qu Wenruo | 7dbeaad | 2020-06-10 09:04:43 +0800 | [diff] [blame] | 191 | entry->qgroup_rsv = ret; |
Naohiro Aota | d8e3fb1 | 2021-02-04 19:22:05 +0900 | [diff] [blame] | 192 | entry->physical = (u64)-1; |
Qu Wenruo | 3c198fe | 2021-01-21 14:13:54 +0800 | [diff] [blame] | 193 | |
| 194 | ASSERT(type == BTRFS_ORDERED_REGULAR || |
| 195 | type == BTRFS_ORDERED_NOCOW || |
| 196 | type == BTRFS_ORDERED_PREALLOC || |
| 197 | type == BTRFS_ORDERED_COMPRESSED); |
| 198 | set_bit(type, &entry->flags); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 199 | |
Josef Bacik | 5deb17e | 2020-10-09 09:28:20 -0400 | [diff] [blame] | 200 | percpu_counter_add_batch(&fs_info->ordered_bytes, num_bytes, |
| 201 | fs_info->delalloc_batch); |
| 202 | |
| 203 | if (dio) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 204 | set_bit(BTRFS_ORDERED_DIRECT, &entry->flags); |
| 205 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 206 | /* one ref for the tree */ |
Elena Reshetova | e76edab | 2017-03-03 10:55:13 +0200 | [diff] [blame] | 207 | refcount_set(&entry->refs, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 208 | init_waitqueue_head(&entry->wait); |
| 209 | INIT_LIST_HEAD(&entry->list); |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 210 | INIT_LIST_HEAD(&entry->log_list); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 211 | INIT_LIST_HEAD(&entry->root_extent_list); |
Miao Xie | 9afab88 | 2012-10-25 09:41:36 +0000 | [diff] [blame] | 212 | INIT_LIST_HEAD(&entry->work_list); |
| 213 | init_completion(&entry->completion); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 214 | |
Nikolay Borisov | acbf1dd | 2020-08-31 14:42:40 +0300 | [diff] [blame] | 215 | trace_btrfs_ordered_extent_add(inode, entry); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 216 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 217 | spin_lock_irq(&tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 218 | node = tree_insert(&tree->tree, file_offset, |
| 219 | &entry->rb_node); |
Jeff Mahoney | 43c04fb | 2011-10-03 23:22:33 -0400 | [diff] [blame] | 220 | if (node) |
Nikolay Borisov | 511a32b | 2019-11-29 11:38:13 +0200 | [diff] [blame] | 221 | btrfs_panic(fs_info, -EEXIST, |
| 222 | "inconsistency in ordered tree at offset %llu", |
| 223 | file_offset); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 224 | spin_unlock_irq(&tree->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 225 | |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 226 | spin_lock(&root->ordered_extent_lock); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 227 | list_add_tail(&entry->root_extent_list, |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 228 | &root->ordered_extents); |
| 229 | root->nr_ordered_extents++; |
| 230 | if (root->nr_ordered_extents == 1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 231 | spin_lock(&fs_info->ordered_root_lock); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 232 | BUG_ON(!list_empty(&root->ordered_root)); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 233 | list_add_tail(&root->ordered_root, &fs_info->ordered_roots); |
| 234 | spin_unlock(&fs_info->ordered_root_lock); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 235 | } |
| 236 | spin_unlock(&root->ordered_extent_lock); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 237 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 238 | /* |
| 239 | * We don't need the count_max_extents here, we can assume that all of |
| 240 | * that work has been done at higher layers, so this is truly the |
| 241 | * smallest the extent is going to get. |
| 242 | */ |
Nikolay Borisov | da69fea | 2020-06-03 08:55:01 +0300 | [diff] [blame] | 243 | spin_lock(&inode->lock); |
| 244 | btrfs_mod_outstanding_extents(inode, 1); |
| 245 | spin_unlock(&inode->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 246 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 247 | return 0; |
| 248 | } |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 249 | |
Nikolay Borisov | e7fbf60 | 2020-06-03 08:55:13 +0300 | [diff] [blame] | 250 | int btrfs_add_ordered_extent(struct btrfs_inode *inode, u64 file_offset, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 251 | u64 disk_bytenr, u64 num_bytes, u64 disk_num_bytes, |
| 252 | int type) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 253 | { |
Qu Wenruo | 3c198fe | 2021-01-21 14:13:54 +0800 | [diff] [blame] | 254 | ASSERT(type == BTRFS_ORDERED_REGULAR || |
| 255 | type == BTRFS_ORDERED_NOCOW || |
| 256 | type == BTRFS_ORDERED_PREALLOC); |
Nikolay Borisov | e7fbf60 | 2020-06-03 08:55:13 +0300 | [diff] [blame] | 257 | return __btrfs_add_ordered_extent(inode, file_offset, disk_bytenr, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 258 | num_bytes, disk_num_bytes, type, 0, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 259 | BTRFS_COMPRESS_NONE); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 260 | } |
| 261 | |
Nikolay Borisov | c1e0952 | 2020-06-03 08:55:30 +0300 | [diff] [blame] | 262 | int btrfs_add_ordered_extent_dio(struct btrfs_inode *inode, u64 file_offset, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 263 | u64 disk_bytenr, u64 num_bytes, |
| 264 | u64 disk_num_bytes, int type) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 265 | { |
Qu Wenruo | 3c198fe | 2021-01-21 14:13:54 +0800 | [diff] [blame] | 266 | ASSERT(type == BTRFS_ORDERED_REGULAR || |
| 267 | type == BTRFS_ORDERED_NOCOW || |
| 268 | type == BTRFS_ORDERED_PREALLOC); |
Nikolay Borisov | c1e0952 | 2020-06-03 08:55:30 +0300 | [diff] [blame] | 269 | return __btrfs_add_ordered_extent(inode, file_offset, disk_bytenr, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 270 | num_bytes, disk_num_bytes, type, 1, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 271 | BTRFS_COMPRESS_NONE); |
| 272 | } |
| 273 | |
Nikolay Borisov | 4cc6120 | 2020-06-03 08:55:15 +0300 | [diff] [blame] | 274 | int btrfs_add_ordered_extent_compress(struct btrfs_inode *inode, u64 file_offset, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 275 | u64 disk_bytenr, u64 num_bytes, |
Qu Wenruo | 3c198fe | 2021-01-21 14:13:54 +0800 | [diff] [blame] | 276 | u64 disk_num_bytes, int compress_type) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 277 | { |
Qu Wenruo | 3c198fe | 2021-01-21 14:13:54 +0800 | [diff] [blame] | 278 | ASSERT(compress_type != BTRFS_COMPRESS_NONE); |
Nikolay Borisov | 4cc6120 | 2020-06-03 08:55:15 +0300 | [diff] [blame] | 279 | return __btrfs_add_ordered_extent(inode, file_offset, disk_bytenr, |
Qu Wenruo | 3c198fe | 2021-01-21 14:13:54 +0800 | [diff] [blame] | 280 | num_bytes, disk_num_bytes, |
| 281 | BTRFS_ORDERED_COMPRESSED, 0, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 282 | compress_type); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 283 | } |
| 284 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 285 | /* |
| 286 | * 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] | 287 | * when an ordered extent is finished. If the list covers more than one |
| 288 | * ordered extent, it is split across multiples. |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 289 | */ |
Nikolay Borisov | f975626 | 2019-04-10 16:16:11 +0300 | [diff] [blame] | 290 | void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 291 | struct btrfs_ordered_sum *sum) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 292 | { |
| 293 | struct btrfs_ordered_inode_tree *tree; |
Chris Mason | 1b1e213 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 294 | |
Nikolay Borisov | f975626 | 2019-04-10 16:16:11 +0300 | [diff] [blame] | 295 | tree = &BTRFS_I(entry->inode)->ordered_tree; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 296 | spin_lock_irq(&tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 297 | list_add_tail(&sum->list, &entry->list); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 298 | spin_unlock_irq(&tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 299 | } |
| 300 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 301 | /* |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 302 | * Mark all ordered extents io inside the specified range finished. |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 303 | * |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 304 | * @page: The invovled page for the opeartion. |
| 305 | * For uncompressed buffered IO, the page status also needs to be |
| 306 | * updated to indicate whether the pending ordered io is finished. |
| 307 | * Can be NULL for direct IO and compressed write. |
| 308 | * For these cases, callers are ensured they won't execute the |
| 309 | * endio function twice. |
| 310 | * @finish_func: The function to be executed when all the IO of an ordered |
| 311 | * extent are finished. |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 312 | * |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 313 | * This function is called for endio, thus the range must have ordered |
| 314 | * extent(s) coveri it. |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 315 | */ |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 316 | void btrfs_mark_ordered_io_finished(struct btrfs_inode *inode, |
| 317 | struct page *page, u64 file_offset, |
| 318 | u64 num_bytes, btrfs_func_t finish_func, |
| 319 | bool uptodate) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 320 | { |
Nikolay Borisov | 7095821 | 2020-06-03 08:55:23 +0300 | [diff] [blame] | 321 | struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree; |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 322 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 323 | struct btrfs_workqueue *wq; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 324 | struct rb_node *node; |
| 325 | struct btrfs_ordered_extent *entry = NULL; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 326 | unsigned long flags; |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 327 | u64 cur = file_offset; |
| 328 | |
| 329 | if (btrfs_is_free_space_inode(inode)) |
| 330 | wq = fs_info->endio_freespace_worker; |
| 331 | else |
| 332 | wq = fs_info->endio_write_workers; |
| 333 | |
| 334 | if (page) |
| 335 | ASSERT(page->mapping && page_offset(page) <= file_offset && |
| 336 | file_offset + num_bytes <= page_offset(page) + PAGE_SIZE); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 337 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 338 | spin_lock_irqsave(&tree->lock, flags); |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 339 | while (cur < file_offset + num_bytes) { |
| 340 | u64 entry_end; |
| 341 | u64 end; |
| 342 | u32 len; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 343 | |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 344 | node = tree_search(tree, cur); |
| 345 | /* No ordered extents at all */ |
| 346 | if (!node) |
| 347 | break; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 348 | |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 349 | entry = rb_entry(node, struct btrfs_ordered_extent, rb_node); |
| 350 | entry_end = entry->file_offset + entry->num_bytes; |
Qu Wenruo | 58f74b2 | 2020-12-22 13:59:24 +0800 | [diff] [blame] | 351 | /* |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 352 | * |<-- OE --->| | |
| 353 | * cur |
| 354 | * Go to next OE. |
Qu Wenruo | 58f74b2 | 2020-12-22 13:59:24 +0800 | [diff] [blame] | 355 | */ |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 356 | if (cur >= entry_end) { |
| 357 | node = rb_next(node); |
| 358 | /* No more ordered extents, exit */ |
| 359 | if (!node) |
| 360 | break; |
| 361 | entry = rb_entry(node, struct btrfs_ordered_extent, |
| 362 | rb_node); |
| 363 | |
| 364 | /* Go to next ordered extent and continue */ |
| 365 | cur = entry->file_offset; |
| 366 | continue; |
| 367 | } |
| 368 | /* |
| 369 | * | |<--- OE --->| |
| 370 | * cur |
| 371 | * Go to the start of OE. |
| 372 | */ |
| 373 | if (cur < entry->file_offset) { |
| 374 | cur = entry->file_offset; |
| 375 | continue; |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * Now we are definitely inside one ordered extent. |
| 380 | * |
| 381 | * |<--- OE --->| |
| 382 | * | |
| 383 | * cur |
| 384 | */ |
| 385 | end = min(entry->file_offset + entry->num_bytes, |
| 386 | file_offset + num_bytes) - 1; |
| 387 | ASSERT(end + 1 - cur < U32_MAX); |
| 388 | len = end + 1 - cur; |
| 389 | |
| 390 | if (page) { |
| 391 | /* |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 392 | * Ordered (Private2) bit indicates whether we still |
| 393 | * have pending io unfinished for the ordered extent. |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 394 | * |
| 395 | * If there's no such bit, we need to skip to next range. |
| 396 | */ |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 397 | if (!btrfs_page_test_ordered(fs_info, page, cur, len)) { |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 398 | cur += len; |
| 399 | continue; |
| 400 | } |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 401 | btrfs_page_clear_ordered(fs_info, page, cur, len); |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /* Now we're fine to update the accounting */ |
| 405 | if (unlikely(len > entry->bytes_left)) { |
| 406 | WARN_ON(1); |
| 407 | btrfs_crit(fs_info, |
| 408 | "bad ordered extent accounting, root=%llu ino=%llu OE offset=%llu OE len=%llu to_dec=%u left=%llu", |
| 409 | inode->root->root_key.objectid, |
| 410 | btrfs_ino(inode), |
| 411 | entry->file_offset, |
| 412 | entry->num_bytes, |
| 413 | len, entry->bytes_left); |
| 414 | entry->bytes_left = 0; |
| 415 | } else { |
| 416 | entry->bytes_left -= len; |
| 417 | } |
| 418 | |
| 419 | if (!uptodate) |
| 420 | set_bit(BTRFS_ORDERED_IOERR, &entry->flags); |
| 421 | |
| 422 | /* |
| 423 | * All the IO of the ordered extent is finished, we need to queue |
| 424 | * the finish_func to be executed. |
| 425 | */ |
| 426 | if (entry->bytes_left == 0) { |
| 427 | set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags); |
| 428 | cond_wake_up(&entry->wait); |
| 429 | refcount_inc(&entry->refs); |
| 430 | spin_unlock_irqrestore(&tree->lock, flags); |
| 431 | btrfs_init_work(&entry->work, finish_func, NULL, NULL); |
| 432 | btrfs_queue_work(wq, &entry->work); |
| 433 | spin_lock_irqsave(&tree->lock, flags); |
| 434 | } |
| 435 | cur += len; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 436 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 437 | spin_unlock_irqrestore(&tree->lock, flags); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /* |
Qu Wenruo | 58f74b2 | 2020-12-22 13:59:24 +0800 | [diff] [blame] | 441 | * Finish IO for one ordered extent across a given range. The range can only |
| 442 | * contain one ordered extent. |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 443 | * |
Qu Wenruo | 58f74b2 | 2020-12-22 13:59:24 +0800 | [diff] [blame] | 444 | * @cached: The cached ordered extent. If not NULL, we can skip the tree |
| 445 | * search and use the ordered extent directly. |
| 446 | * Will be also used to store the finished ordered extent. |
| 447 | * @file_offset: File offset for the finished IO |
| 448 | * @io_size: Length of the finish IO range |
| 449 | * @uptodate: If the IO finishes without problem |
| 450 | * |
| 451 | * Return true if the ordered extent is finished in the range, and update |
| 452 | * @cached. |
| 453 | * Return false otherwise. |
| 454 | * |
| 455 | * NOTE: The range can NOT cross multiple ordered extents. |
| 456 | * Thus caller should ensure the range doesn't cross ordered extents. |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 457 | */ |
Qu Wenruo | 58f74b2 | 2020-12-22 13:59:24 +0800 | [diff] [blame] | 458 | bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode, |
| 459 | struct btrfs_ordered_extent **cached, |
| 460 | u64 file_offset, u64 io_size, int uptodate) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 461 | { |
Nikolay Borisov | 90c0304 | 2020-08-31 14:42:41 +0300 | [diff] [blame] | 462 | struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 463 | struct rb_node *node; |
Josef Bacik | 5a1a3df | 2010-02-02 20:51:14 +0000 | [diff] [blame] | 464 | struct btrfs_ordered_extent *entry = NULL; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 465 | unsigned long flags; |
Qu Wenruo | 58f74b2 | 2020-12-22 13:59:24 +0800 | [diff] [blame] | 466 | bool finished = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 467 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 468 | spin_lock_irqsave(&tree->lock, flags); |
| 469 | if (cached && *cached) { |
| 470 | entry = *cached; |
| 471 | goto have_entry; |
| 472 | } |
| 473 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 474 | node = tree_search(tree, file_offset); |
Qu Wenruo | 58f74b2 | 2020-12-22 13:59:24 +0800 | [diff] [blame] | 475 | if (!node) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 476 | goto out; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 477 | |
| 478 | entry = rb_entry(node, struct btrfs_ordered_extent, rb_node); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 479 | have_entry: |
Nikolay Borisov | 20bbf20 | 2021-02-17 15:12:49 +0200 | [diff] [blame] | 480 | if (!in_range(file_offset, entry->file_offset, entry->num_bytes)) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 481 | goto out; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 482 | |
Qu Wenruo | 58f74b2 | 2020-12-22 13:59:24 +0800 | [diff] [blame] | 483 | if (io_size > entry->bytes_left) |
Nikolay Borisov | 90c0304 | 2020-08-31 14:42:41 +0300 | [diff] [blame] | 484 | btrfs_crit(inode->root->fs_info, |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 485 | "bad ordered accounting left %llu size %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 486 | entry->bytes_left, io_size); |
Qu Wenruo | 58f74b2 | 2020-12-22 13:59:24 +0800 | [diff] [blame] | 487 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 488 | entry->bytes_left -= io_size; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 489 | if (!uptodate) |
| 490 | set_bit(BTRFS_ORDERED_IOERR, &entry->flags); |
| 491 | |
Miao Xie | af7a650 | 2014-03-06 13:54:56 +0800 | [diff] [blame] | 492 | if (entry->bytes_left == 0) { |
Qu Wenruo | 58f74b2 | 2020-12-22 13:59:24 +0800 | [diff] [blame] | 493 | /* |
| 494 | * Ensure only one caller can set the flag and finished_ret |
| 495 | * accordingly |
| 496 | */ |
| 497 | finished = !test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags); |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 498 | /* test_and_set_bit implies a barrier */ |
| 499 | cond_wake_up_nomb(&entry->wait); |
Miao Xie | af7a650 | 2014-03-06 13:54:56 +0800 | [diff] [blame] | 500 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 501 | out: |
Qu Wenruo | 58f74b2 | 2020-12-22 13:59:24 +0800 | [diff] [blame] | 502 | if (finished && cached && entry) { |
Josef Bacik | 5a1a3df | 2010-02-02 20:51:14 +0000 | [diff] [blame] | 503 | *cached = entry; |
Elena Reshetova | e76edab | 2017-03-03 10:55:13 +0200 | [diff] [blame] | 504 | refcount_inc(&entry->refs); |
Josef Bacik | 5a1a3df | 2010-02-02 20:51:14 +0000 | [diff] [blame] | 505 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 506 | spin_unlock_irqrestore(&tree->lock, flags); |
Qu Wenruo | 58f74b2 | 2020-12-22 13:59:24 +0800 | [diff] [blame] | 507 | return finished; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 508 | } |
| 509 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 510 | /* |
| 511 | * used to drop a reference on an ordered extent. This will free |
| 512 | * the extent if the last reference is dropped |
| 513 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 514 | void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 515 | { |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 516 | struct list_head *cur; |
| 517 | struct btrfs_ordered_sum *sum; |
| 518 | |
Nikolay Borisov | acbf1dd | 2020-08-31 14:42:40 +0300 | [diff] [blame] | 519 | trace_btrfs_ordered_extent_put(BTRFS_I(entry->inode), entry); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 520 | |
Elena Reshetova | e76edab | 2017-03-03 10:55:13 +0200 | [diff] [blame] | 521 | if (refcount_dec_and_test(&entry->refs)) { |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 522 | ASSERT(list_empty(&entry->root_extent_list)); |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 523 | ASSERT(list_empty(&entry->log_list)); |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 524 | ASSERT(RB_EMPTY_NODE(&entry->rb_node)); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 525 | if (entry->inode) |
| 526 | btrfs_add_delayed_iput(entry->inode); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 527 | while (!list_empty(&entry->list)) { |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 528 | cur = entry->list.next; |
| 529 | sum = list_entry(cur, struct btrfs_ordered_sum, list); |
| 530 | list_del(&sum->list); |
Nikolay Borisov | a3d46ae | 2019-04-01 11:29:58 +0300 | [diff] [blame] | 531 | kvfree(sum); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 532 | } |
Miao Xie | 6352b91 | 2012-09-06 04:01:51 -0600 | [diff] [blame] | 533 | kmem_cache_free(btrfs_ordered_extent_cache, entry); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 534 | } |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 535 | } |
| 536 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 537 | /* |
| 538 | * remove an ordered extent from the tree. No references are dropped |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 539 | * and waiters are woken up. |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 540 | */ |
Nikolay Borisov | 71fe0a5 | 2020-09-18 12:15:50 +0300 | [diff] [blame] | 541 | void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode, |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 542 | struct btrfs_ordered_extent *entry) |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 543 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 544 | struct btrfs_ordered_inode_tree *tree; |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 545 | struct btrfs_root *root = btrfs_inode->root; |
Nikolay Borisov | 71fe0a5 | 2020-09-18 12:15:50 +0300 | [diff] [blame] | 546 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 547 | struct rb_node *node; |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 548 | bool pending; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 549 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 550 | /* This is paired with btrfs_add_ordered_extent. */ |
| 551 | spin_lock(&btrfs_inode->lock); |
| 552 | btrfs_mod_outstanding_extents(btrfs_inode, -1); |
| 553 | spin_unlock(&btrfs_inode->lock); |
| 554 | if (root != fs_info->tree_root) |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 555 | btrfs_delalloc_release_metadata(btrfs_inode, entry->num_bytes, |
| 556 | false); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 557 | |
Josef Bacik | 5deb17e | 2020-10-09 09:28:20 -0400 | [diff] [blame] | 558 | percpu_counter_add_batch(&fs_info->ordered_bytes, -entry->num_bytes, |
| 559 | fs_info->delalloc_batch); |
Josef Bacik | 4297ff8 | 2019-04-10 15:56:09 -0400 | [diff] [blame] | 560 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 561 | tree = &btrfs_inode->ordered_tree; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 562 | spin_lock_irq(&tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 563 | node = &entry->rb_node; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 564 | rb_erase(node, &tree->tree); |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 565 | RB_CLEAR_NODE(node); |
Filipe David Borba Manana | 1b8e7e4 | 2013-11-22 18:54:58 +0000 | [diff] [blame] | 566 | if (tree->last == node) |
| 567 | tree->last = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 568 | set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags); |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 569 | pending = test_and_clear_bit(BTRFS_ORDERED_PENDING, &entry->flags); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 570 | spin_unlock_irq(&tree->lock); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 571 | |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 572 | /* |
| 573 | * The current running transaction is waiting on us, we need to let it |
| 574 | * know that we're complete and wake it up. |
| 575 | */ |
| 576 | if (pending) { |
| 577 | struct btrfs_transaction *trans; |
| 578 | |
| 579 | /* |
| 580 | * The checks for trans are just a formality, it should be set, |
| 581 | * but if it isn't we don't want to deref/assert under the spin |
| 582 | * lock, so be nice and check if trans is set, but ASSERT() so |
| 583 | * if it isn't set a developer will notice. |
| 584 | */ |
| 585 | spin_lock(&fs_info->trans_lock); |
| 586 | trans = fs_info->running_transaction; |
| 587 | if (trans) |
| 588 | refcount_inc(&trans->use_count); |
| 589 | spin_unlock(&fs_info->trans_lock); |
| 590 | |
| 591 | ASSERT(trans); |
| 592 | if (trans) { |
| 593 | if (atomic_dec_and_test(&trans->pending_ordered)) |
| 594 | wake_up(&trans->pending_wait); |
| 595 | btrfs_put_transaction(trans); |
| 596 | } |
| 597 | } |
| 598 | |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 599 | spin_lock(&root->ordered_extent_lock); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 600 | list_del_init(&entry->root_extent_list); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 601 | root->nr_ordered_extents--; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 602 | |
Nikolay Borisov | 71fe0a5 | 2020-09-18 12:15:50 +0300 | [diff] [blame] | 603 | trace_btrfs_ordered_extent_remove(btrfs_inode, entry); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 604 | |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 605 | if (!root->nr_ordered_extents) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 606 | spin_lock(&fs_info->ordered_root_lock); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 607 | BUG_ON(list_empty(&root->ordered_root)); |
| 608 | list_del_init(&root->ordered_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 609 | spin_unlock(&fs_info->ordered_root_lock); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 610 | } |
| 611 | spin_unlock(&root->ordered_extent_lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 612 | wake_up(&entry->wait); |
Chris Mason | 81d7ed2 | 2008-04-25 08:51:48 -0400 | [diff] [blame] | 613 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 614 | |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 615 | static void btrfs_run_ordered_extent_work(struct btrfs_work *work) |
Miao Xie | 9afab88 | 2012-10-25 09:41:36 +0000 | [diff] [blame] | 616 | { |
| 617 | struct btrfs_ordered_extent *ordered; |
| 618 | |
| 619 | ordered = container_of(work, struct btrfs_ordered_extent, flush_work); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 620 | btrfs_start_ordered_extent(ordered, 1); |
Miao Xie | 9afab88 | 2012-10-25 09:41:36 +0000 | [diff] [blame] | 621 | complete(&ordered->completion); |
| 622 | } |
| 623 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 624 | /* |
| 625 | * wait for all the ordered extents in a root. This is done when balancing |
| 626 | * space between drives. |
| 627 | */ |
Chris Mason | 6374e57a | 2017-06-23 09:48:21 -0700 | [diff] [blame] | 628 | u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr, |
Filipe Manana | 578def7 | 2016-04-26 15:36:38 +0100 | [diff] [blame] | 629 | const u64 range_start, const u64 range_len) |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 630 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 631 | struct btrfs_fs_info *fs_info = root->fs_info; |
Filipe Manana | 578def7 | 2016-04-26 15:36:38 +0100 | [diff] [blame] | 632 | LIST_HEAD(splice); |
| 633 | LIST_HEAD(skipped); |
| 634 | LIST_HEAD(works); |
Miao Xie | 9afab88 | 2012-10-25 09:41:36 +0000 | [diff] [blame] | 635 | struct btrfs_ordered_extent *ordered, *next; |
Chris Mason | 6374e57a | 2017-06-23 09:48:21 -0700 | [diff] [blame] | 636 | u64 count = 0; |
Filipe Manana | 578def7 | 2016-04-26 15:36:38 +0100 | [diff] [blame] | 637 | const u64 range_end = range_start + range_len; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 638 | |
Miao Xie | 31f3d25 | 2014-03-06 13:55:02 +0800 | [diff] [blame] | 639 | mutex_lock(&root->ordered_extent_mutex); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 640 | spin_lock(&root->ordered_extent_lock); |
| 641 | list_splice_init(&root->ordered_extents, &splice); |
Miao Xie | b024419 | 2013-11-04 23:13:25 +0800 | [diff] [blame] | 642 | while (!list_empty(&splice) && nr) { |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 643 | ordered = list_first_entry(&splice, struct btrfs_ordered_extent, |
| 644 | root_extent_list); |
Filipe Manana | 578def7 | 2016-04-26 15:36:38 +0100 | [diff] [blame] | 645 | |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 646 | if (range_end <= ordered->disk_bytenr || |
| 647 | ordered->disk_bytenr + ordered->disk_num_bytes <= range_start) { |
Filipe Manana | 578def7 | 2016-04-26 15:36:38 +0100 | [diff] [blame] | 648 | list_move_tail(&ordered->root_extent_list, &skipped); |
| 649 | cond_resched_lock(&root->ordered_extent_lock); |
| 650 | continue; |
| 651 | } |
| 652 | |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 653 | list_move_tail(&ordered->root_extent_list, |
| 654 | &root->ordered_extents); |
Elena Reshetova | e76edab | 2017-03-03 10:55:13 +0200 | [diff] [blame] | 655 | refcount_inc(&ordered->refs); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 656 | spin_unlock(&root->ordered_extent_lock); |
| 657 | |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 658 | btrfs_init_work(&ordered->flush_work, |
| 659 | btrfs_run_ordered_extent_work, NULL, NULL); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 660 | list_add_tail(&ordered->work_list, &works); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 661 | btrfs_queue_work(fs_info->flush_workers, &ordered->flush_work); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 662 | |
Miao Xie | 9afab88 | 2012-10-25 09:41:36 +0000 | [diff] [blame] | 663 | cond_resched(); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 664 | spin_lock(&root->ordered_extent_lock); |
Chris Mason | 6374e57a | 2017-06-23 09:48:21 -0700 | [diff] [blame] | 665 | if (nr != U64_MAX) |
Miao Xie | b024419 | 2013-11-04 23:13:25 +0800 | [diff] [blame] | 666 | nr--; |
| 667 | count++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 668 | } |
Filipe Manana | 578def7 | 2016-04-26 15:36:38 +0100 | [diff] [blame] | 669 | list_splice_tail(&skipped, &root->ordered_extents); |
Miao Xie | b024419 | 2013-11-04 23:13:25 +0800 | [diff] [blame] | 670 | list_splice_tail(&splice, &root->ordered_extents); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 671 | spin_unlock(&root->ordered_extent_lock); |
Miao Xie | 9afab88 | 2012-10-25 09:41:36 +0000 | [diff] [blame] | 672 | |
| 673 | list_for_each_entry_safe(ordered, next, &works, work_list) { |
| 674 | list_del_init(&ordered->work_list); |
| 675 | wait_for_completion(&ordered->completion); |
Miao Xie | 9afab88 | 2012-10-25 09:41:36 +0000 | [diff] [blame] | 676 | btrfs_put_ordered_extent(ordered); |
Miao Xie | 9afab88 | 2012-10-25 09:41:36 +0000 | [diff] [blame] | 677 | cond_resched(); |
| 678 | } |
Miao Xie | 31f3d25 | 2014-03-06 13:55:02 +0800 | [diff] [blame] | 679 | mutex_unlock(&root->ordered_extent_mutex); |
Miao Xie | b024419 | 2013-11-04 23:13:25 +0800 | [diff] [blame] | 680 | |
| 681 | return count; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 682 | } |
| 683 | |
Filipe Manana | 042528f | 2019-11-14 18:02:43 +0000 | [diff] [blame] | 684 | void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr, |
Chris Mason | 6374e57a | 2017-06-23 09:48:21 -0700 | [diff] [blame] | 685 | const u64 range_start, const u64 range_len) |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 686 | { |
| 687 | struct btrfs_root *root; |
| 688 | struct list_head splice; |
Chris Mason | 6374e57a | 2017-06-23 09:48:21 -0700 | [diff] [blame] | 689 | u64 done; |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 690 | |
| 691 | INIT_LIST_HEAD(&splice); |
| 692 | |
Miao Xie | 8b9d83c | 2014-03-06 13:54:55 +0800 | [diff] [blame] | 693 | mutex_lock(&fs_info->ordered_operations_mutex); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 694 | spin_lock(&fs_info->ordered_root_lock); |
| 695 | list_splice_init(&fs_info->ordered_roots, &splice); |
Miao Xie | b024419 | 2013-11-04 23:13:25 +0800 | [diff] [blame] | 696 | while (!list_empty(&splice) && nr) { |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 697 | root = list_first_entry(&splice, struct btrfs_root, |
| 698 | ordered_root); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 699 | root = btrfs_grab_root(root); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 700 | BUG_ON(!root); |
| 701 | list_move_tail(&root->ordered_root, |
| 702 | &fs_info->ordered_roots); |
| 703 | spin_unlock(&fs_info->ordered_root_lock); |
| 704 | |
Filipe Manana | 578def7 | 2016-04-26 15:36:38 +0100 | [diff] [blame] | 705 | done = btrfs_wait_ordered_extents(root, nr, |
| 706 | range_start, range_len); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 707 | btrfs_put_root(root); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 708 | |
| 709 | spin_lock(&fs_info->ordered_root_lock); |
Chris Mason | 6374e57a | 2017-06-23 09:48:21 -0700 | [diff] [blame] | 710 | if (nr != U64_MAX) { |
Miao Xie | b024419 | 2013-11-04 23:13:25 +0800 | [diff] [blame] | 711 | nr -= done; |
Miao Xie | b024419 | 2013-11-04 23:13:25 +0800 | [diff] [blame] | 712 | } |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 713 | } |
Miao Xie | 931aa87 | 2013-11-14 17:33:21 +0800 | [diff] [blame] | 714 | list_splice_tail(&splice, &fs_info->ordered_roots); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 715 | spin_unlock(&fs_info->ordered_root_lock); |
Miao Xie | 8b9d83c | 2014-03-06 13:54:55 +0800 | [diff] [blame] | 716 | mutex_unlock(&fs_info->ordered_operations_mutex); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 719 | /* |
| 720 | * Used to start IO or wait for a given ordered extent to finish. |
| 721 | * |
| 722 | * If wait is one, this effectively waits on page writeback for all the pages |
| 723 | * in the extent, and it waits on the io completion code to insert |
| 724 | * metadata into the btree corresponding to the extent |
| 725 | */ |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 726 | void btrfs_start_ordered_extent(struct btrfs_ordered_extent *entry, int wait) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 727 | { |
| 728 | u64 start = entry->file_offset; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 729 | u64 end = start + entry->num_bytes - 1; |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 730 | struct btrfs_inode *inode = BTRFS_I(entry->inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 731 | |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 732 | trace_btrfs_ordered_extent_start(inode, entry); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 733 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 734 | /* |
| 735 | * pages in the range can be dirty, clean or writeback. We |
| 736 | * 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] | 737 | * for the flusher thread to find them |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 738 | */ |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 739 | if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags)) |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 740 | filemap_fdatawrite_range(inode->vfs_inode.i_mapping, start, end); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 741 | if (wait) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 742 | wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE, |
| 743 | &entry->flags)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 744 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 745 | } |
| 746 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 747 | /* |
| 748 | * Used to wait on ordered extents across a large range of bytes. |
| 749 | */ |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 750 | int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 751 | { |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 752 | int ret = 0; |
Filipe Manana | 28aeeac | 2015-05-05 19:03:10 +0100 | [diff] [blame] | 753 | int ret_wb = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 754 | u64 end; |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 755 | u64 orig_end; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 756 | struct btrfs_ordered_extent *ordered; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 757 | |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 758 | if (start + len < start) { |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 759 | orig_end = INT_LIMIT(loff_t); |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 760 | } else { |
| 761 | orig_end = start + len - 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 762 | if (orig_end > INT_LIMIT(loff_t)) |
| 763 | orig_end = INT_LIMIT(loff_t); |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 764 | } |
Josef Bacik | 551ebb2 | 2012-04-23 14:41:09 -0400 | [diff] [blame] | 765 | |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 766 | /* start IO across the range first to instantiate any delalloc |
| 767 | * extents |
| 768 | */ |
Filipe Manana | 728404d | 2014-10-10 09:43:11 +0100 | [diff] [blame] | 769 | ret = btrfs_fdatawrite_range(inode, start, orig_end); |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 770 | if (ret) |
| 771 | return ret; |
Filipe Manana | 728404d | 2014-10-10 09:43:11 +0100 | [diff] [blame] | 772 | |
Filipe Manana | 28aeeac | 2015-05-05 19:03:10 +0100 | [diff] [blame] | 773 | /* |
| 774 | * If we have a writeback error don't return immediately. Wait first |
| 775 | * for any ordered extents that haven't completed yet. This is to make |
| 776 | * sure no one can dirty the same page ranges and call writepages() |
| 777 | * before the ordered extents complete - to avoid failures (-EEXIST) |
| 778 | * when adding the new ordered extents to the ordered tree. |
| 779 | */ |
| 780 | ret_wb = filemap_fdatawait_range(inode->i_mapping, start, orig_end); |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 781 | |
| 782 | end = orig_end; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 783 | while (1) { |
Nikolay Borisov | 6d072c8 | 2020-08-31 14:42:39 +0300 | [diff] [blame] | 784 | ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode), end); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 785 | if (!ordered) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 786 | break; |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 787 | if (ordered->file_offset > orig_end) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 788 | btrfs_put_ordered_extent(ordered); |
| 789 | break; |
| 790 | } |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 791 | if (ordered->file_offset + ordered->num_bytes <= start) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 792 | btrfs_put_ordered_extent(ordered); |
| 793 | break; |
| 794 | } |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 795 | btrfs_start_ordered_extent(ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 796 | end = ordered->file_offset; |
Filipe Manana | e75fd33 | 2020-02-13 12:29:50 +0000 | [diff] [blame] | 797 | /* |
| 798 | * If the ordered extent had an error save the error but don't |
| 799 | * exit without waiting first for all other ordered extents in |
| 800 | * the range to complete. |
| 801 | */ |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 802 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) |
| 803 | ret = -EIO; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 804 | btrfs_put_ordered_extent(ordered); |
Filipe Manana | e75fd33 | 2020-02-13 12:29:50 +0000 | [diff] [blame] | 805 | if (end == 0 || end == start) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 806 | break; |
| 807 | end--; |
| 808 | } |
Filipe Manana | 28aeeac | 2015-05-05 19:03:10 +0100 | [diff] [blame] | 809 | return ret_wb ? ret_wb : ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 810 | } |
| 811 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 812 | /* |
| 813 | * find an ordered extent corresponding to file_offset. return NULL if |
| 814 | * nothing is found, otherwise take a reference on the extent and return it |
| 815 | */ |
Nikolay Borisov | c350437 | 2020-06-03 08:55:03 +0300 | [diff] [blame] | 816 | struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 817 | u64 file_offset) |
| 818 | { |
| 819 | struct btrfs_ordered_inode_tree *tree; |
| 820 | struct rb_node *node; |
| 821 | struct btrfs_ordered_extent *entry = NULL; |
Johannes Thumshirn | 24533f6 | 2021-02-04 19:22:04 +0900 | [diff] [blame] | 822 | unsigned long flags; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 823 | |
Nikolay Borisov | c350437 | 2020-06-03 08:55:03 +0300 | [diff] [blame] | 824 | tree = &inode->ordered_tree; |
Johannes Thumshirn | 24533f6 | 2021-02-04 19:22:04 +0900 | [diff] [blame] | 825 | spin_lock_irqsave(&tree->lock, flags); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 826 | node = tree_search(tree, file_offset); |
| 827 | if (!node) |
| 828 | goto out; |
| 829 | |
| 830 | entry = rb_entry(node, struct btrfs_ordered_extent, rb_node); |
Nikolay Borisov | 20bbf20 | 2021-02-17 15:12:49 +0200 | [diff] [blame] | 831 | if (!in_range(file_offset, entry->file_offset, entry->num_bytes)) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 832 | entry = NULL; |
| 833 | if (entry) |
Elena Reshetova | e76edab | 2017-03-03 10:55:13 +0200 | [diff] [blame] | 834 | refcount_inc(&entry->refs); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 835 | out: |
Johannes Thumshirn | 24533f6 | 2021-02-04 19:22:04 +0900 | [diff] [blame] | 836 | spin_unlock_irqrestore(&tree->lock, flags); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 837 | return entry; |
| 838 | } |
| 839 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 840 | /* Since the DIO code tries to lock a wide area we need to look for any ordered |
| 841 | * extents that exist in the range, rather than just the start of the range. |
| 842 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 843 | struct btrfs_ordered_extent *btrfs_lookup_ordered_range( |
| 844 | struct btrfs_inode *inode, u64 file_offset, u64 len) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 845 | { |
| 846 | struct btrfs_ordered_inode_tree *tree; |
| 847 | struct rb_node *node; |
| 848 | struct btrfs_ordered_extent *entry = NULL; |
| 849 | |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 850 | tree = &inode->ordered_tree; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 851 | spin_lock_irq(&tree->lock); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 852 | node = tree_search(tree, file_offset); |
| 853 | if (!node) { |
| 854 | node = tree_search(tree, file_offset + len); |
| 855 | if (!node) |
| 856 | goto out; |
| 857 | } |
| 858 | |
| 859 | while (1) { |
| 860 | entry = rb_entry(node, struct btrfs_ordered_extent, rb_node); |
| 861 | if (range_overlaps(entry, file_offset, len)) |
| 862 | break; |
| 863 | |
| 864 | if (entry->file_offset >= file_offset + len) { |
| 865 | entry = NULL; |
| 866 | break; |
| 867 | } |
| 868 | entry = NULL; |
| 869 | node = rb_next(node); |
| 870 | if (!node) |
| 871 | break; |
| 872 | } |
| 873 | out: |
| 874 | if (entry) |
Elena Reshetova | e76edab | 2017-03-03 10:55:13 +0200 | [diff] [blame] | 875 | refcount_inc(&entry->refs); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 876 | spin_unlock_irq(&tree->lock); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 877 | return entry; |
| 878 | } |
| 879 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 880 | /* |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 881 | * Adds all ordered extents to the given list. The list ends up sorted by the |
| 882 | * file_offset of the ordered extents. |
| 883 | */ |
| 884 | void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode, |
| 885 | struct list_head *list) |
| 886 | { |
| 887 | struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree; |
| 888 | struct rb_node *n; |
| 889 | |
| 890 | ASSERT(inode_is_locked(&inode->vfs_inode)); |
| 891 | |
| 892 | spin_lock_irq(&tree->lock); |
| 893 | for (n = rb_first(&tree->tree); n; n = rb_next(n)) { |
| 894 | struct btrfs_ordered_extent *ordered; |
| 895 | |
| 896 | ordered = rb_entry(n, struct btrfs_ordered_extent, rb_node); |
| 897 | |
| 898 | if (test_bit(BTRFS_ORDERED_LOGGED, &ordered->flags)) |
| 899 | continue; |
| 900 | |
| 901 | ASSERT(list_empty(&ordered->log_list)); |
| 902 | list_add_tail(&ordered->log_list, list); |
| 903 | refcount_inc(&ordered->refs); |
| 904 | } |
| 905 | spin_unlock_irq(&tree->lock); |
| 906 | } |
| 907 | |
| 908 | /* |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 909 | * lookup and return any extent before 'file_offset'. NULL is returned |
| 910 | * if none is found |
| 911 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 912 | struct btrfs_ordered_extent * |
Nikolay Borisov | 6d072c8 | 2020-08-31 14:42:39 +0300 | [diff] [blame] | 913 | btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 914 | { |
| 915 | struct btrfs_ordered_inode_tree *tree; |
| 916 | struct rb_node *node; |
| 917 | struct btrfs_ordered_extent *entry = NULL; |
| 918 | |
Nikolay Borisov | 6d072c8 | 2020-08-31 14:42:39 +0300 | [diff] [blame] | 919 | tree = &inode->ordered_tree; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 920 | spin_lock_irq(&tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 921 | node = tree_search(tree, file_offset); |
| 922 | if (!node) |
| 923 | goto out; |
| 924 | |
| 925 | entry = rb_entry(node, struct btrfs_ordered_extent, rb_node); |
Elena Reshetova | e76edab | 2017-03-03 10:55:13 +0200 | [diff] [blame] | 926 | refcount_inc(&entry->refs); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 927 | out: |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 928 | spin_unlock_irq(&tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 929 | return entry; |
| 930 | } |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 931 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 932 | /* |
Qu Wenruo | c095f33 | 2021-04-27 15:03:40 +0800 | [diff] [blame] | 933 | * Lookup the first ordered extent that overlaps the range |
| 934 | * [@file_offset, @file_offset + @len). |
| 935 | * |
| 936 | * The difference between this and btrfs_lookup_first_ordered_extent() is |
| 937 | * that this one won't return any ordered extent that does not overlap the range. |
| 938 | * And the difference against btrfs_lookup_ordered_extent() is, this function |
| 939 | * ensures the first ordered extent gets returned. |
| 940 | */ |
| 941 | struct btrfs_ordered_extent *btrfs_lookup_first_ordered_range( |
| 942 | struct btrfs_inode *inode, u64 file_offset, u64 len) |
| 943 | { |
| 944 | struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree; |
| 945 | struct rb_node *node; |
| 946 | struct rb_node *cur; |
| 947 | struct rb_node *prev; |
| 948 | struct rb_node *next; |
| 949 | struct btrfs_ordered_extent *entry = NULL; |
| 950 | |
| 951 | spin_lock_irq(&tree->lock); |
| 952 | node = tree->tree.rb_node; |
| 953 | /* |
| 954 | * Here we don't want to use tree_search() which will use tree->last |
| 955 | * and screw up the search order. |
| 956 | * And __tree_search() can't return the adjacent ordered extents |
| 957 | * either, thus here we do our own search. |
| 958 | */ |
| 959 | while (node) { |
| 960 | entry = rb_entry(node, struct btrfs_ordered_extent, rb_node); |
| 961 | |
| 962 | if (file_offset < entry->file_offset) { |
| 963 | node = node->rb_left; |
| 964 | } else if (file_offset >= entry_end(entry)) { |
| 965 | node = node->rb_right; |
| 966 | } else { |
| 967 | /* |
| 968 | * Direct hit, got an ordered extent that starts at |
| 969 | * @file_offset |
| 970 | */ |
| 971 | goto out; |
| 972 | } |
| 973 | } |
| 974 | if (!entry) { |
| 975 | /* Empty tree */ |
| 976 | goto out; |
| 977 | } |
| 978 | |
| 979 | cur = &entry->rb_node; |
| 980 | /* We got an entry around @file_offset, check adjacent entries */ |
| 981 | if (entry->file_offset < file_offset) { |
| 982 | prev = cur; |
| 983 | next = rb_next(cur); |
| 984 | } else { |
| 985 | prev = rb_prev(cur); |
| 986 | next = cur; |
| 987 | } |
| 988 | if (prev) { |
| 989 | entry = rb_entry(prev, struct btrfs_ordered_extent, rb_node); |
| 990 | if (range_overlaps(entry, file_offset, len)) |
| 991 | goto out; |
| 992 | } |
| 993 | if (next) { |
| 994 | entry = rb_entry(next, struct btrfs_ordered_extent, rb_node); |
| 995 | if (range_overlaps(entry, file_offset, len)) |
| 996 | goto out; |
| 997 | } |
| 998 | /* No ordered extent in the range */ |
| 999 | entry = NULL; |
| 1000 | out: |
| 1001 | if (entry) |
| 1002 | refcount_inc(&entry->refs); |
| 1003 | spin_unlock_irq(&tree->lock); |
| 1004 | return entry; |
| 1005 | } |
| 1006 | |
| 1007 | /* |
Nikolay Borisov | ffa8721 | 2019-05-07 10:19:22 +0300 | [diff] [blame] | 1008 | * btrfs_flush_ordered_range - Lock the passed range and ensures all pending |
| 1009 | * ordered extents in it are run to completion. |
| 1010 | * |
Nikolay Borisov | ffa8721 | 2019-05-07 10:19:22 +0300 | [diff] [blame] | 1011 | * @inode: Inode whose ordered tree is to be searched |
| 1012 | * @start: Beginning of range to flush |
| 1013 | * @end: Last byte of range to lock |
| 1014 | * @cached_state: If passed, will return the extent state responsible for the |
| 1015 | * locked range. It's the caller's responsibility to free the cached state. |
| 1016 | * |
| 1017 | * This function always returns with the given range locked, ensuring after it's |
| 1018 | * called no order extent can be pending. |
| 1019 | */ |
David Sterba | b272ae2 | 2020-02-05 19:09:33 +0100 | [diff] [blame] | 1020 | void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start, |
Nikolay Borisov | ffa8721 | 2019-05-07 10:19:22 +0300 | [diff] [blame] | 1021 | u64 end, |
| 1022 | struct extent_state **cached_state) |
| 1023 | { |
| 1024 | struct btrfs_ordered_extent *ordered; |
Naohiro Aota | a3b46b8 | 2019-07-26 16:47:05 +0900 | [diff] [blame] | 1025 | struct extent_state *cache = NULL; |
| 1026 | struct extent_state **cachedp = &cache; |
Nikolay Borisov | bd80d94 | 2019-05-07 10:19:24 +0300 | [diff] [blame] | 1027 | |
| 1028 | if (cached_state) |
Naohiro Aota | a3b46b8 | 2019-07-26 16:47:05 +0900 | [diff] [blame] | 1029 | cachedp = cached_state; |
Nikolay Borisov | ffa8721 | 2019-05-07 10:19:22 +0300 | [diff] [blame] | 1030 | |
| 1031 | while (1) { |
David Sterba | b272ae2 | 2020-02-05 19:09:33 +0100 | [diff] [blame] | 1032 | lock_extent_bits(&inode->io_tree, start, end, cachedp); |
Nikolay Borisov | ffa8721 | 2019-05-07 10:19:22 +0300 | [diff] [blame] | 1033 | ordered = btrfs_lookup_ordered_range(inode, start, |
| 1034 | end - start + 1); |
Nikolay Borisov | bd80d94 | 2019-05-07 10:19:24 +0300 | [diff] [blame] | 1035 | if (!ordered) { |
| 1036 | /* |
| 1037 | * If no external cached_state has been passed then |
| 1038 | * decrement the extra ref taken for cachedp since we |
| 1039 | * aren't exposing it outside of this function |
| 1040 | */ |
| 1041 | if (!cached_state) |
Naohiro Aota | a3b46b8 | 2019-07-26 16:47:05 +0900 | [diff] [blame] | 1042 | refcount_dec(&cache->refs); |
Nikolay Borisov | ffa8721 | 2019-05-07 10:19:22 +0300 | [diff] [blame] | 1043 | break; |
Nikolay Borisov | bd80d94 | 2019-05-07 10:19:24 +0300 | [diff] [blame] | 1044 | } |
David Sterba | b272ae2 | 2020-02-05 19:09:33 +0100 | [diff] [blame] | 1045 | unlock_extent_cached(&inode->io_tree, start, end, cachedp); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 1046 | btrfs_start_ordered_extent(ordered, 1); |
Nikolay Borisov | ffa8721 | 2019-05-07 10:19:22 +0300 | [diff] [blame] | 1047 | btrfs_put_ordered_extent(ordered); |
| 1048 | } |
| 1049 | } |
| 1050 | |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 1051 | static int clone_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pos, |
| 1052 | u64 len) |
| 1053 | { |
| 1054 | struct inode *inode = ordered->inode; |
| 1055 | u64 file_offset = ordered->file_offset + pos; |
| 1056 | u64 disk_bytenr = ordered->disk_bytenr + pos; |
| 1057 | u64 num_bytes = len; |
| 1058 | u64 disk_num_bytes = len; |
| 1059 | int type; |
| 1060 | unsigned long flags_masked = ordered->flags & ~(1 << BTRFS_ORDERED_DIRECT); |
| 1061 | int compress_type = ordered->compress_type; |
| 1062 | unsigned long weight; |
| 1063 | int ret; |
| 1064 | |
| 1065 | weight = hweight_long(flags_masked); |
| 1066 | WARN_ON_ONCE(weight > 1); |
| 1067 | if (!weight) |
| 1068 | type = 0; |
| 1069 | else |
| 1070 | type = __ffs(flags_masked); |
| 1071 | |
| 1072 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered->flags)) { |
| 1073 | WARN_ON_ONCE(1); |
| 1074 | ret = btrfs_add_ordered_extent_compress(BTRFS_I(inode), |
| 1075 | file_offset, disk_bytenr, num_bytes, |
| 1076 | disk_num_bytes, compress_type); |
| 1077 | } else if (test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) { |
| 1078 | ret = btrfs_add_ordered_extent_dio(BTRFS_I(inode), file_offset, |
| 1079 | disk_bytenr, num_bytes, disk_num_bytes, type); |
| 1080 | } else { |
| 1081 | ret = btrfs_add_ordered_extent(BTRFS_I(inode), file_offset, |
| 1082 | disk_bytenr, num_bytes, disk_num_bytes, type); |
| 1083 | } |
| 1084 | |
| 1085 | return ret; |
| 1086 | } |
| 1087 | |
| 1088 | int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pre, |
| 1089 | u64 post) |
| 1090 | { |
| 1091 | struct inode *inode = ordered->inode; |
| 1092 | struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree; |
| 1093 | struct rb_node *node; |
| 1094 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 1095 | int ret = 0; |
| 1096 | |
| 1097 | spin_lock_irq(&tree->lock); |
| 1098 | /* Remove from tree once */ |
| 1099 | node = &ordered->rb_node; |
| 1100 | rb_erase(node, &tree->tree); |
| 1101 | RB_CLEAR_NODE(node); |
| 1102 | if (tree->last == node) |
| 1103 | tree->last = NULL; |
| 1104 | |
| 1105 | ordered->file_offset += pre; |
| 1106 | ordered->disk_bytenr += pre; |
| 1107 | ordered->num_bytes -= (pre + post); |
| 1108 | ordered->disk_num_bytes -= (pre + post); |
| 1109 | ordered->bytes_left -= (pre + post); |
| 1110 | |
| 1111 | /* Re-insert the node */ |
| 1112 | node = tree_insert(&tree->tree, ordered->file_offset, &ordered->rb_node); |
| 1113 | if (node) |
| 1114 | btrfs_panic(fs_info, -EEXIST, |
| 1115 | "zoned: inconsistency in ordered tree at offset %llu", |
| 1116 | ordered->file_offset); |
| 1117 | |
| 1118 | spin_unlock_irq(&tree->lock); |
| 1119 | |
| 1120 | if (pre) |
| 1121 | ret = clone_ordered_extent(ordered, 0, pre); |
Filipe Manana | adbd914d | 2021-04-21 14:31:50 +0100 | [diff] [blame] | 1122 | if (ret == 0 && post) |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 1123 | ret = clone_ordered_extent(ordered, pre + ordered->disk_num_bytes, |
| 1124 | post); |
| 1125 | |
| 1126 | return ret; |
| 1127 | } |
| 1128 | |
Miao Xie | 6352b91 | 2012-09-06 04:01:51 -0600 | [diff] [blame] | 1129 | int __init ordered_data_init(void) |
| 1130 | { |
| 1131 | btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent", |
| 1132 | sizeof(struct btrfs_ordered_extent), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 1133 | SLAB_MEM_SPREAD, |
Miao Xie | 6352b91 | 2012-09-06 04:01:51 -0600 | [diff] [blame] | 1134 | NULL); |
| 1135 | if (!btrfs_ordered_extent_cache) |
| 1136 | return -ENOMEM; |
Miao Xie | 25287e0 | 2012-10-25 09:31:03 +0000 | [diff] [blame] | 1137 | |
Miao Xie | 6352b91 | 2012-09-06 04:01:51 -0600 | [diff] [blame] | 1138 | return 0; |
| 1139 | } |
| 1140 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 1141 | void __cold ordered_data_exit(void) |
Miao Xie | 6352b91 | 2012-09-06 04:01:51 -0600 | [diff] [blame] | 1142 | { |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 1143 | kmem_cache_destroy(btrfs_ordered_extent_cache); |
Miao Xie | 6352b91 | 2012-09-06 04:01:51 -0600 | [diff] [blame] | 1144 | } |