blob: 985a215584370efd95388dd41e8e150999d184bb [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Masondc17ff82008-01-08 15:46:30 -05002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Masondc17ff82008-01-08 15:46:30 -05004 */
5
Chris Masondc17ff82008-01-08 15:46:30 -05006#include <linux/slab.h>
Chris Masond6bfde82008-04-30 13:59:35 -04007#include <linux/blkdev.h>
Chris Masonf4219502008-07-22 11:18:09 -04008#include <linux/writeback.h>
Nikolay Borisova3d46ae2019-04-01 11:29:58 +03009#include <linux/sched/mm.h>
David Sterba602cbe92019-08-21 18:48:25 +020010#include "misc.h"
Chris Masondc17ff82008-01-08 15:46:30 -050011#include "ctree.h"
12#include "transaction.h"
13#include "btrfs_inode.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040014#include "extent_io.h"
Miao Xie199c2a92013-05-15 07:48:23 +000015#include "disk-io.h"
Anand Jainebb87652016-03-10 17:26:59 +080016#include "compression.h"
Josef Bacik86736342019-06-19 15:12:00 -040017#include "delalloc-space.h"
Qu Wenruo7dbeaad2020-06-10 09:04:43 +080018#include "qgroup.h"
Chris Masondc17ff82008-01-08 15:46:30 -050019
Miao Xie6352b912012-09-06 04:01:51 -060020static struct kmem_cache *btrfs_ordered_extent_cache;
21
Chris Masone6dcd2d2008-07-17 12:53:50 -040022static u64 entry_end(struct btrfs_ordered_extent *entry)
Chris Masondc17ff82008-01-08 15:46:30 -050023{
Omar Sandovalbffe6332019-12-02 17:34:19 -080024 if (entry->file_offset + entry->num_bytes < entry->file_offset)
Chris Masone6dcd2d2008-07-17 12:53:50 -040025 return (u64)-1;
Omar Sandovalbffe6332019-12-02 17:34:19 -080026 return entry->file_offset + entry->num_bytes;
Chris Masondc17ff82008-01-08 15:46:30 -050027}
28
Chris Masond352ac62008-09-29 15:18:18 -040029/* returns NULL if the insertion worked, or it returns the node it did find
30 * in the tree
31 */
Chris Masone6dcd2d2008-07-17 12:53:50 -040032static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset,
33 struct rb_node *node)
Chris Masondc17ff82008-01-08 15:46:30 -050034{
Chris Masond3977122009-01-05 21:25:51 -050035 struct rb_node **p = &root->rb_node;
36 struct rb_node *parent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -040037 struct btrfs_ordered_extent *entry;
Chris Masondc17ff82008-01-08 15:46:30 -050038
Chris Masond3977122009-01-05 21:25:51 -050039 while (*p) {
Chris Masondc17ff82008-01-08 15:46:30 -050040 parent = *p;
Chris Masone6dcd2d2008-07-17 12:53:50 -040041 entry = rb_entry(parent, struct btrfs_ordered_extent, rb_node);
Chris Masondc17ff82008-01-08 15:46:30 -050042
Chris Masone6dcd2d2008-07-17 12:53:50 -040043 if (file_offset < entry->file_offset)
Chris Masondc17ff82008-01-08 15:46:30 -050044 p = &(*p)->rb_left;
Chris Masone6dcd2d2008-07-17 12:53:50 -040045 else if (file_offset >= entry_end(entry))
Chris Masondc17ff82008-01-08 15:46:30 -050046 p = &(*p)->rb_right;
47 else
48 return parent;
49 }
50
51 rb_link_node(node, parent, p);
52 rb_insert_color(node, root);
53 return NULL;
54}
55
Chris Masond352ac62008-09-29 15:18:18 -040056/*
57 * look for a given offset in the tree, and if it can't be found return the
58 * first lesser offset
59 */
Chris Masone6dcd2d2008-07-17 12:53:50 -040060static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset,
61 struct rb_node **prev_ret)
Chris Masondc17ff82008-01-08 15:46:30 -050062{
Chris Masond3977122009-01-05 21:25:51 -050063 struct rb_node *n = root->rb_node;
Chris Masondc17ff82008-01-08 15:46:30 -050064 struct rb_node *prev = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -040065 struct rb_node *test;
66 struct btrfs_ordered_extent *entry;
67 struct btrfs_ordered_extent *prev_entry = NULL;
Chris Masondc17ff82008-01-08 15:46:30 -050068
Chris Masond3977122009-01-05 21:25:51 -050069 while (n) {
Chris Masone6dcd2d2008-07-17 12:53:50 -040070 entry = rb_entry(n, struct btrfs_ordered_extent, rb_node);
Chris Masondc17ff82008-01-08 15:46:30 -050071 prev = n;
72 prev_entry = entry;
Chris Masondc17ff82008-01-08 15:46:30 -050073
Chris Masone6dcd2d2008-07-17 12:53:50 -040074 if (file_offset < entry->file_offset)
Chris Masondc17ff82008-01-08 15:46:30 -050075 n = n->rb_left;
Chris Masone6dcd2d2008-07-17 12:53:50 -040076 else if (file_offset >= entry_end(entry))
Chris Masondc17ff82008-01-08 15:46:30 -050077 n = n->rb_right;
78 else
79 return n;
80 }
81 if (!prev_ret)
82 return NULL;
83
Chris Masond3977122009-01-05 21:25:51 -050084 while (prev && file_offset >= entry_end(prev_entry)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -040085 test = rb_next(prev);
86 if (!test)
87 break;
88 prev_entry = rb_entry(test, struct btrfs_ordered_extent,
89 rb_node);
90 if (file_offset < entry_end(prev_entry))
91 break;
92
93 prev = test;
94 }
95 if (prev)
96 prev_entry = rb_entry(prev, struct btrfs_ordered_extent,
97 rb_node);
Chris Masond3977122009-01-05 21:25:51 -050098 while (prev && file_offset < entry_end(prev_entry)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -040099 test = rb_prev(prev);
100 if (!test)
101 break;
102 prev_entry = rb_entry(test, struct btrfs_ordered_extent,
103 rb_node);
104 prev = test;
Chris Masondc17ff82008-01-08 15:46:30 -0500105 }
106 *prev_ret = prev;
107 return NULL;
108}
109
Chris Masond352ac62008-09-29 15:18:18 -0400110/*
111 * helper to check if a given offset is inside a given entry
112 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400113static int offset_in_entry(struct btrfs_ordered_extent *entry, u64 file_offset)
Chris Masondc17ff82008-01-08 15:46:30 -0500114{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400115 if (file_offset < entry->file_offset ||
Omar Sandovalbffe6332019-12-02 17:34:19 -0800116 entry->file_offset + entry->num_bytes <= file_offset)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400117 return 0;
118 return 1;
119}
120
Josef Bacik4b46fce2010-05-23 11:00:55 -0400121static int range_overlaps(struct btrfs_ordered_extent *entry, u64 file_offset,
122 u64 len)
123{
124 if (file_offset + len <= entry->file_offset ||
Omar Sandovalbffe6332019-12-02 17:34:19 -0800125 entry->file_offset + entry->num_bytes <= file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400126 return 0;
127 return 1;
128}
129
Chris Masond352ac62008-09-29 15:18:18 -0400130/*
131 * look find the first ordered struct that has this offset, otherwise
132 * the first one less than this offset
133 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400134static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,
135 u64 file_offset)
136{
137 struct rb_root *root = &tree->tree;
Chris Masonc87fb6f2011-01-31 19:54:59 -0500138 struct rb_node *prev = NULL;
Chris Masondc17ff82008-01-08 15:46:30 -0500139 struct rb_node *ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400140 struct btrfs_ordered_extent *entry;
141
142 if (tree->last) {
143 entry = rb_entry(tree->last, struct btrfs_ordered_extent,
144 rb_node);
145 if (offset_in_entry(entry, file_offset))
146 return tree->last;
147 }
148 ret = __tree_search(root, file_offset, &prev);
Chris Masondc17ff82008-01-08 15:46:30 -0500149 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400150 ret = prev;
151 if (ret)
152 tree->last = ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500153 return ret;
154}
155
Qu Wenruo7dbeaad2020-06-10 09:04:43 +0800156/*
157 * Allocate and add a new ordered_extent into the per-inode tree.
Chris Masoneb84ae02008-07-17 13:53:27 -0400158 *
Chris Masoneb84ae02008-07-17 13:53:27 -0400159 * The tree is given a single reference on the ordered extent that was
160 * inserted.
161 */
Nikolay Borisovda69fea2020-06-03 08:55:01 +0300162static int __btrfs_add_ordered_extent(struct btrfs_inode *inode, u64 file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800163 u64 disk_bytenr, u64 num_bytes,
164 u64 disk_num_bytes, int type, int dio,
165 int compress_type)
Chris Masondc17ff82008-01-08 15:46:30 -0500166{
Nikolay Borisovda69fea2020-06-03 08:55:01 +0300167 struct btrfs_root *root = inode->root;
168 struct btrfs_fs_info *fs_info = root->fs_info;
169 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400170 struct rb_node *node;
171 struct btrfs_ordered_extent *entry;
Qu Wenruo7dbeaad2020-06-10 09:04:43 +0800172 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500173
Qu Wenruo7dbeaad2020-06-10 09:04:43 +0800174 if (type == BTRFS_ORDERED_NOCOW || type == BTRFS_ORDERED_PREALLOC) {
175 /* For nocow write, we can release the qgroup rsv right now */
Nikolay Borisov8b8a9792020-06-03 08:55:11 +0300176 ret = btrfs_qgroup_free_data(inode, NULL, file_offset, num_bytes);
Qu Wenruo7dbeaad2020-06-10 09:04:43 +0800177 if (ret < 0)
178 return ret;
179 ret = 0;
180 } else {
181 /*
182 * The ordered extent has reserved qgroup space, release now
183 * and pass the reserved number for qgroup_record to free.
184 */
Nikolay Borisov72b7d152020-06-03 08:55:18 +0300185 ret = btrfs_qgroup_release_data(inode, file_offset, num_bytes);
Qu Wenruo7dbeaad2020-06-10 09:04:43 +0800186 if (ret < 0)
187 return ret;
188 }
Miao Xie6352b912012-09-06 04:01:51 -0600189 entry = kmem_cache_zalloc(btrfs_ordered_extent_cache, GFP_NOFS);
Chris Masondc17ff82008-01-08 15:46:30 -0500190 if (!entry)
191 return -ENOMEM;
192
Chris Masone6dcd2d2008-07-17 12:53:50 -0400193 entry->file_offset = file_offset;
Omar Sandovalbffe6332019-12-02 17:34:19 -0800194 entry->disk_bytenr = disk_bytenr;
195 entry->num_bytes = num_bytes;
196 entry->disk_num_bytes = disk_num_bytes;
197 entry->bytes_left = num_bytes;
Nikolay Borisovda69fea2020-06-03 08:55:01 +0300198 entry->inode = igrab(&inode->vfs_inode);
Li Zefan261507a02010-12-17 14:21:50 +0800199 entry->compress_type = compress_type;
Josef Bacik77cef2e2013-08-29 13:57:21 -0400200 entry->truncated_len = (u64)-1;
Qu Wenruo7dbeaad2020-06-10 09:04:43 +0800201 entry->qgroup_rsv = ret;
Naohiro Aotad8e3fb12021-02-04 19:22:05 +0900202 entry->physical = (u64)-1;
203 entry->disk = NULL;
204 entry->partno = (u8)-1;
Qu Wenruo3c198fe2021-01-21 14:13:54 +0800205
206 ASSERT(type == BTRFS_ORDERED_REGULAR ||
207 type == BTRFS_ORDERED_NOCOW ||
208 type == BTRFS_ORDERED_PREALLOC ||
209 type == BTRFS_ORDERED_COMPRESSED);
210 set_bit(type, &entry->flags);
Chris Mason3eaa2882008-07-24 11:57:52 -0400211
Josef Bacik5deb17e2020-10-09 09:28:20 -0400212 percpu_counter_add_batch(&fs_info->ordered_bytes, num_bytes,
213 fs_info->delalloc_batch);
214
215 if (dio)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400216 set_bit(BTRFS_ORDERED_DIRECT, &entry->flags);
217
Chris Masone6dcd2d2008-07-17 12:53:50 -0400218 /* one ref for the tree */
Elena Reshetovae76edab2017-03-03 10:55:13 +0200219 refcount_set(&entry->refs, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400220 init_waitqueue_head(&entry->wait);
221 INIT_LIST_HEAD(&entry->list);
Filipe Manana48778172020-08-11 12:43:58 +0100222 INIT_LIST_HEAD(&entry->log_list);
Chris Mason3eaa2882008-07-24 11:57:52 -0400223 INIT_LIST_HEAD(&entry->root_extent_list);
Miao Xie9afab882012-10-25 09:41:36 +0000224 INIT_LIST_HEAD(&entry->work_list);
225 init_completion(&entry->completion);
Chris Masondc17ff82008-01-08 15:46:30 -0500226
Nikolay Borisovacbf1dd2020-08-31 14:42:40 +0300227 trace_btrfs_ordered_extent_add(inode, entry);
liubo1abe9b82011-03-24 11:18:59 +0000228
Josef Bacik5fd02042012-05-02 14:00:54 -0400229 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400230 node = tree_insert(&tree->tree, file_offset,
231 &entry->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -0400232 if (node)
Nikolay Borisov511a32b2019-11-29 11:38:13 +0200233 btrfs_panic(fs_info, -EEXIST,
234 "inconsistency in ordered tree at offset %llu",
235 file_offset);
Josef Bacik5fd02042012-05-02 14:00:54 -0400236 spin_unlock_irq(&tree->lock);
Chris Masond3977122009-01-05 21:25:51 -0500237
Miao Xie199c2a92013-05-15 07:48:23 +0000238 spin_lock(&root->ordered_extent_lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400239 list_add_tail(&entry->root_extent_list,
Miao Xie199c2a92013-05-15 07:48:23 +0000240 &root->ordered_extents);
241 root->nr_ordered_extents++;
242 if (root->nr_ordered_extents == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400243 spin_lock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +0000244 BUG_ON(!list_empty(&root->ordered_root));
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400245 list_add_tail(&root->ordered_root, &fs_info->ordered_roots);
246 spin_unlock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +0000247 }
248 spin_unlock(&root->ordered_extent_lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400249
Josef Bacik8b62f872017-10-19 14:15:55 -0400250 /*
251 * We don't need the count_max_extents here, we can assume that all of
252 * that work has been done at higher layers, so this is truly the
253 * smallest the extent is going to get.
254 */
Nikolay Borisovda69fea2020-06-03 08:55:01 +0300255 spin_lock(&inode->lock);
256 btrfs_mod_outstanding_extents(inode, 1);
257 spin_unlock(&inode->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -0400258
Chris Masone6dcd2d2008-07-17 12:53:50 -0400259 return 0;
260}
Chris Masondc17ff82008-01-08 15:46:30 -0500261
Nikolay Borisove7fbf602020-06-03 08:55:13 +0300262int btrfs_add_ordered_extent(struct btrfs_inode *inode, u64 file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800263 u64 disk_bytenr, u64 num_bytes, u64 disk_num_bytes,
264 int type)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400265{
Qu Wenruo3c198fe2021-01-21 14:13:54 +0800266 ASSERT(type == BTRFS_ORDERED_REGULAR ||
267 type == BTRFS_ORDERED_NOCOW ||
268 type == BTRFS_ORDERED_PREALLOC);
Nikolay Borisove7fbf602020-06-03 08:55:13 +0300269 return __btrfs_add_ordered_extent(inode, file_offset, disk_bytenr,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800270 num_bytes, disk_num_bytes, type, 0,
Li Zefan261507a02010-12-17 14:21:50 +0800271 BTRFS_COMPRESS_NONE);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400272}
273
Nikolay Borisovc1e09522020-06-03 08:55:30 +0300274int btrfs_add_ordered_extent_dio(struct btrfs_inode *inode, u64 file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800275 u64 disk_bytenr, u64 num_bytes,
276 u64 disk_num_bytes, int type)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400277{
Qu Wenruo3c198fe2021-01-21 14:13:54 +0800278 ASSERT(type == BTRFS_ORDERED_REGULAR ||
279 type == BTRFS_ORDERED_NOCOW ||
280 type == BTRFS_ORDERED_PREALLOC);
Nikolay Borisovc1e09522020-06-03 08:55:30 +0300281 return __btrfs_add_ordered_extent(inode, file_offset, disk_bytenr,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800282 num_bytes, disk_num_bytes, type, 1,
Li Zefan261507a02010-12-17 14:21:50 +0800283 BTRFS_COMPRESS_NONE);
284}
285
Nikolay Borisov4cc61202020-06-03 08:55:15 +0300286int btrfs_add_ordered_extent_compress(struct btrfs_inode *inode, u64 file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800287 u64 disk_bytenr, u64 num_bytes,
Qu Wenruo3c198fe2021-01-21 14:13:54 +0800288 u64 disk_num_bytes, int compress_type)
Li Zefan261507a02010-12-17 14:21:50 +0800289{
Qu Wenruo3c198fe2021-01-21 14:13:54 +0800290 ASSERT(compress_type != BTRFS_COMPRESS_NONE);
Nikolay Borisov4cc61202020-06-03 08:55:15 +0300291 return __btrfs_add_ordered_extent(inode, file_offset, disk_bytenr,
Qu Wenruo3c198fe2021-01-21 14:13:54 +0800292 num_bytes, disk_num_bytes,
293 BTRFS_ORDERED_COMPRESSED, 0,
Li Zefan261507a02010-12-17 14:21:50 +0800294 compress_type);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400295}
296
Chris Masoneb84ae02008-07-17 13:53:27 -0400297/*
298 * Add a struct btrfs_ordered_sum into the list of checksums to be inserted
Chris Mason3edf7d32008-07-18 06:17:13 -0400299 * when an ordered extent is finished. If the list covers more than one
300 * ordered extent, it is split across multiples.
Chris Masoneb84ae02008-07-17 13:53:27 -0400301 */
Nikolay Borisovf9756262019-04-10 16:16:11 +0300302void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100303 struct btrfs_ordered_sum *sum)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400304{
305 struct btrfs_ordered_inode_tree *tree;
Chris Mason1b1e2132008-06-25 16:01:31 -0400306
Nikolay Borisovf9756262019-04-10 16:16:11 +0300307 tree = &BTRFS_I(entry->inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400308 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400309 list_add_tail(&sum->list, &entry->list);
Josef Bacik5fd02042012-05-02 14:00:54 -0400310 spin_unlock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400311}
312
Chris Masoneb84ae02008-07-17 13:53:27 -0400313/*
Qu Wenruo58f74b22020-12-22 13:59:24 +0800314 * Finish IO for one ordered extent across a given range. The range can
315 * contain several ordered extents.
Chris Mason163cf092010-11-28 19:56:33 -0500316 *
Qu Wenruo58f74b22020-12-22 13:59:24 +0800317 * @found_ret: Return the finished ordered extent
318 * @file_offset: File offset for the finished IO
319 * Will also be updated to one byte past the range that is
320 * recordered as finished. This allows caller to walk forward.
321 * @io_size: Length of the finish IO range
322 * @uptodate: If the IO finished without problem
Chris Mason163cf092010-11-28 19:56:33 -0500323 *
Qu Wenruo58f74b22020-12-22 13:59:24 +0800324 * Return true if any ordered extent is finished in the range, and update
325 * @found_ret and @file_offset.
326 * Return false otherwise.
327 *
328 * NOTE: Although The range can cross multiple ordered extents, only one
329 * ordered extent will be updated during one call. The caller is responsible to
330 * iterate all ordered extents in the range.
Chris Mason163cf092010-11-28 19:56:33 -0500331 */
Qu Wenruo58f74b22020-12-22 13:59:24 +0800332bool btrfs_dec_test_first_ordered_pending(struct btrfs_inode *inode,
333 struct btrfs_ordered_extent **finished_ret,
Josef Bacik5fd02042012-05-02 14:00:54 -0400334 u64 *file_offset, u64 io_size, int uptodate)
Chris Mason163cf092010-11-28 19:56:33 -0500335{
Nikolay Borisov70958212020-06-03 08:55:23 +0300336 struct btrfs_fs_info *fs_info = inode->root->fs_info;
337 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
Chris Mason163cf092010-11-28 19:56:33 -0500338 struct rb_node *node;
339 struct btrfs_ordered_extent *entry = NULL;
Qu Wenruo58f74b22020-12-22 13:59:24 +0800340 bool finished = false;
Josef Bacik5fd02042012-05-02 14:00:54 -0400341 unsigned long flags;
Chris Mason163cf092010-11-28 19:56:33 -0500342 u64 dec_end;
343 u64 dec_start;
344 u64 to_dec;
345
Josef Bacik5fd02042012-05-02 14:00:54 -0400346 spin_lock_irqsave(&tree->lock, flags);
Chris Mason163cf092010-11-28 19:56:33 -0500347 node = tree_search(tree, *file_offset);
Qu Wenruo58f74b22020-12-22 13:59:24 +0800348 if (!node)
Chris Mason163cf092010-11-28 19:56:33 -0500349 goto out;
Chris Mason163cf092010-11-28 19:56:33 -0500350
351 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
Qu Wenruo58f74b22020-12-22 13:59:24 +0800352 if (!offset_in_entry(entry, *file_offset))
Chris Mason163cf092010-11-28 19:56:33 -0500353 goto out;
Chris Mason163cf092010-11-28 19:56:33 -0500354
355 dec_start = max(*file_offset, entry->file_offset);
Omar Sandovalbffe6332019-12-02 17:34:19 -0800356 dec_end = min(*file_offset + io_size,
357 entry->file_offset + entry->num_bytes);
Chris Mason163cf092010-11-28 19:56:33 -0500358 *file_offset = dec_end;
359 if (dec_start > dec_end) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400360 btrfs_crit(fs_info, "bad ordering dec_start %llu end %llu",
361 dec_start, dec_end);
Chris Mason163cf092010-11-28 19:56:33 -0500362 }
363 to_dec = dec_end - dec_start;
364 if (to_dec > entry->bytes_left) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400365 btrfs_crit(fs_info,
366 "bad ordered accounting left %llu size %llu",
367 entry->bytes_left, to_dec);
Chris Mason163cf092010-11-28 19:56:33 -0500368 }
369 entry->bytes_left -= to_dec;
Josef Bacik5fd02042012-05-02 14:00:54 -0400370 if (!uptodate)
371 set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
372
Miao Xieaf7a6502014-03-06 13:54:56 +0800373 if (entry->bytes_left == 0) {
Qu Wenruo58f74b22020-12-22 13:59:24 +0800374 /*
375 * Ensure only one caller can set the flag and finished_ret
376 * accordingly
377 */
378 finished = !test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
David Sterba093258e2018-02-26 16:15:17 +0100379 /* test_and_set_bit implies a barrier */
380 cond_wake_up_nomb(&entry->wait);
Miao Xieaf7a6502014-03-06 13:54:56 +0800381 }
Chris Mason163cf092010-11-28 19:56:33 -0500382out:
Qu Wenruo58f74b22020-12-22 13:59:24 +0800383 if (finished && finished_ret && entry) {
384 *finished_ret = entry;
Elena Reshetovae76edab2017-03-03 10:55:13 +0200385 refcount_inc(&entry->refs);
Chris Mason163cf092010-11-28 19:56:33 -0500386 }
Josef Bacik5fd02042012-05-02 14:00:54 -0400387 spin_unlock_irqrestore(&tree->lock, flags);
Qu Wenruo58f74b22020-12-22 13:59:24 +0800388 return finished;
Chris Mason163cf092010-11-28 19:56:33 -0500389}
390
391/*
Qu Wenruo58f74b22020-12-22 13:59:24 +0800392 * Finish IO for one ordered extent across a given range. The range can only
393 * contain one ordered extent.
Chris Masoneb84ae02008-07-17 13:53:27 -0400394 *
Qu Wenruo58f74b22020-12-22 13:59:24 +0800395 * @cached: The cached ordered extent. If not NULL, we can skip the tree
396 * search and use the ordered extent directly.
397 * Will be also used to store the finished ordered extent.
398 * @file_offset: File offset for the finished IO
399 * @io_size: Length of the finish IO range
400 * @uptodate: If the IO finishes without problem
401 *
402 * Return true if the ordered extent is finished in the range, and update
403 * @cached.
404 * Return false otherwise.
405 *
406 * NOTE: The range can NOT cross multiple ordered extents.
407 * Thus caller should ensure the range doesn't cross ordered extents.
Chris Masoneb84ae02008-07-17 13:53:27 -0400408 */
Qu Wenruo58f74b22020-12-22 13:59:24 +0800409bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode,
410 struct btrfs_ordered_extent **cached,
411 u64 file_offset, u64 io_size, int uptodate)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400412{
Nikolay Borisov90c03042020-08-31 14:42:41 +0300413 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400414 struct rb_node *node;
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000415 struct btrfs_ordered_extent *entry = NULL;
Josef Bacik5fd02042012-05-02 14:00:54 -0400416 unsigned long flags;
Qu Wenruo58f74b22020-12-22 13:59:24 +0800417 bool finished = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400418
Josef Bacik5fd02042012-05-02 14:00:54 -0400419 spin_lock_irqsave(&tree->lock, flags);
420 if (cached && *cached) {
421 entry = *cached;
422 goto have_entry;
423 }
424
Chris Masone6dcd2d2008-07-17 12:53:50 -0400425 node = tree_search(tree, file_offset);
Qu Wenruo58f74b22020-12-22 13:59:24 +0800426 if (!node)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400427 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400428
429 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
Josef Bacik5fd02042012-05-02 14:00:54 -0400430have_entry:
Qu Wenruo58f74b22020-12-22 13:59:24 +0800431 if (!offset_in_entry(entry, file_offset))
Chris Masone6dcd2d2008-07-17 12:53:50 -0400432 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400433
Qu Wenruo58f74b22020-12-22 13:59:24 +0800434 if (io_size > entry->bytes_left)
Nikolay Borisov90c03042020-08-31 14:42:41 +0300435 btrfs_crit(inode->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -0500436 "bad ordered accounting left %llu size %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200437 entry->bytes_left, io_size);
Qu Wenruo58f74b22020-12-22 13:59:24 +0800438
Chris Mason8b62b722009-09-02 16:53:46 -0400439 entry->bytes_left -= io_size;
Josef Bacik5fd02042012-05-02 14:00:54 -0400440 if (!uptodate)
441 set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
442
Miao Xieaf7a6502014-03-06 13:54:56 +0800443 if (entry->bytes_left == 0) {
Qu Wenruo58f74b22020-12-22 13:59:24 +0800444 /*
445 * Ensure only one caller can set the flag and finished_ret
446 * accordingly
447 */
448 finished = !test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
David Sterba093258e2018-02-26 16:15:17 +0100449 /* test_and_set_bit implies a barrier */
450 cond_wake_up_nomb(&entry->wait);
Miao Xieaf7a6502014-03-06 13:54:56 +0800451 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400452out:
Qu Wenruo58f74b22020-12-22 13:59:24 +0800453 if (finished && cached && entry) {
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000454 *cached = entry;
Elena Reshetovae76edab2017-03-03 10:55:13 +0200455 refcount_inc(&entry->refs);
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000456 }
Josef Bacik5fd02042012-05-02 14:00:54 -0400457 spin_unlock_irqrestore(&tree->lock, flags);
Qu Wenruo58f74b22020-12-22 13:59:24 +0800458 return finished;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400459}
460
Chris Masoneb84ae02008-07-17 13:53:27 -0400461/*
462 * used to drop a reference on an ordered extent. This will free
463 * the extent if the last reference is dropped
464 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100465void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400466{
Chris Masonba1da2f2008-07-17 12:54:15 -0400467 struct list_head *cur;
468 struct btrfs_ordered_sum *sum;
469
Nikolay Borisovacbf1dd2020-08-31 14:42:40 +0300470 trace_btrfs_ordered_extent_put(BTRFS_I(entry->inode), entry);
liubo1abe9b82011-03-24 11:18:59 +0000471
Elena Reshetovae76edab2017-03-03 10:55:13 +0200472 if (refcount_dec_and_test(&entry->refs)) {
Filipe Manana61de7182015-07-01 12:13:10 +0100473 ASSERT(list_empty(&entry->root_extent_list));
Filipe Manana48778172020-08-11 12:43:58 +0100474 ASSERT(list_empty(&entry->log_list));
Filipe Manana61de7182015-07-01 12:13:10 +0100475 ASSERT(RB_EMPTY_NODE(&entry->rb_node));
Josef Bacik5fd02042012-05-02 14:00:54 -0400476 if (entry->inode)
477 btrfs_add_delayed_iput(entry->inode);
Chris Masond3977122009-01-05 21:25:51 -0500478 while (!list_empty(&entry->list)) {
Chris Masonba1da2f2008-07-17 12:54:15 -0400479 cur = entry->list.next;
480 sum = list_entry(cur, struct btrfs_ordered_sum, list);
481 list_del(&sum->list);
Nikolay Borisova3d46ae2019-04-01 11:29:58 +0300482 kvfree(sum);
Chris Masonba1da2f2008-07-17 12:54:15 -0400483 }
Miao Xie6352b912012-09-06 04:01:51 -0600484 kmem_cache_free(btrfs_ordered_extent_cache, entry);
Chris Masonba1da2f2008-07-17 12:54:15 -0400485 }
Chris Masondc17ff82008-01-08 15:46:30 -0500486}
487
Chris Masoneb84ae02008-07-17 13:53:27 -0400488/*
489 * remove an ordered extent from the tree. No references are dropped
Josef Bacik5fd02042012-05-02 14:00:54 -0400490 * and waiters are woken up.
Chris Masoneb84ae02008-07-17 13:53:27 -0400491 */
Nikolay Borisov71fe0a52020-09-18 12:15:50 +0300492void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode,
Josef Bacik5fd02042012-05-02 14:00:54 -0400493 struct btrfs_ordered_extent *entry)
Chris Masondc17ff82008-01-08 15:46:30 -0500494{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400495 struct btrfs_ordered_inode_tree *tree;
Josef Bacik8b62f872017-10-19 14:15:55 -0400496 struct btrfs_root *root = btrfs_inode->root;
Nikolay Borisov71fe0a52020-09-18 12:15:50 +0300497 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masondc17ff82008-01-08 15:46:30 -0500498 struct rb_node *node;
Filipe Manana48778172020-08-11 12:43:58 +0100499 bool pending;
Chris Masondc17ff82008-01-08 15:46:30 -0500500
Josef Bacik8b62f872017-10-19 14:15:55 -0400501 /* This is paired with btrfs_add_ordered_extent. */
502 spin_lock(&btrfs_inode->lock);
503 btrfs_mod_outstanding_extents(btrfs_inode, -1);
504 spin_unlock(&btrfs_inode->lock);
505 if (root != fs_info->tree_root)
Omar Sandovalbffe6332019-12-02 17:34:19 -0800506 btrfs_delalloc_release_metadata(btrfs_inode, entry->num_bytes,
507 false);
Josef Bacik8b62f872017-10-19 14:15:55 -0400508
Josef Bacik5deb17e2020-10-09 09:28:20 -0400509 percpu_counter_add_batch(&fs_info->ordered_bytes, -entry->num_bytes,
510 fs_info->delalloc_batch);
Josef Bacik4297ff82019-04-10 15:56:09 -0400511
Josef Bacik8b62f872017-10-19 14:15:55 -0400512 tree = &btrfs_inode->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400513 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400514 node = &entry->rb_node;
Chris Masondc17ff82008-01-08 15:46:30 -0500515 rb_erase(node, &tree->tree);
Filipe Manana61de7182015-07-01 12:13:10 +0100516 RB_CLEAR_NODE(node);
Filipe David Borba Manana1b8e7e42013-11-22 18:54:58 +0000517 if (tree->last == node)
518 tree->last = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400519 set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
Filipe Manana48778172020-08-11 12:43:58 +0100520 pending = test_and_clear_bit(BTRFS_ORDERED_PENDING, &entry->flags);
Josef Bacik5fd02042012-05-02 14:00:54 -0400521 spin_unlock_irq(&tree->lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400522
Filipe Manana48778172020-08-11 12:43:58 +0100523 /*
524 * The current running transaction is waiting on us, we need to let it
525 * know that we're complete and wake it up.
526 */
527 if (pending) {
528 struct btrfs_transaction *trans;
529
530 /*
531 * The checks for trans are just a formality, it should be set,
532 * but if it isn't we don't want to deref/assert under the spin
533 * lock, so be nice and check if trans is set, but ASSERT() so
534 * if it isn't set a developer will notice.
535 */
536 spin_lock(&fs_info->trans_lock);
537 trans = fs_info->running_transaction;
538 if (trans)
539 refcount_inc(&trans->use_count);
540 spin_unlock(&fs_info->trans_lock);
541
542 ASSERT(trans);
543 if (trans) {
544 if (atomic_dec_and_test(&trans->pending_ordered))
545 wake_up(&trans->pending_wait);
546 btrfs_put_transaction(trans);
547 }
548 }
549
Miao Xie199c2a92013-05-15 07:48:23 +0000550 spin_lock(&root->ordered_extent_lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400551 list_del_init(&entry->root_extent_list);
Miao Xie199c2a92013-05-15 07:48:23 +0000552 root->nr_ordered_extents--;
Chris Mason5a3f23d2009-03-31 13:27:11 -0400553
Nikolay Borisov71fe0a52020-09-18 12:15:50 +0300554 trace_btrfs_ordered_extent_remove(btrfs_inode, entry);
liubo1abe9b82011-03-24 11:18:59 +0000555
Miao Xie199c2a92013-05-15 07:48:23 +0000556 if (!root->nr_ordered_extents) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400557 spin_lock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +0000558 BUG_ON(list_empty(&root->ordered_root));
559 list_del_init(&root->ordered_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400560 spin_unlock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +0000561 }
562 spin_unlock(&root->ordered_extent_lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400563 wake_up(&entry->wait);
Chris Mason81d7ed22008-04-25 08:51:48 -0400564}
Chris Masone6dcd2d2008-07-17 12:53:50 -0400565
Qu Wenruod458b052014-02-28 10:46:19 +0800566static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
Miao Xie9afab882012-10-25 09:41:36 +0000567{
568 struct btrfs_ordered_extent *ordered;
569
570 ordered = container_of(work, struct btrfs_ordered_extent, flush_work);
Nikolay Borisovc0a43602020-09-18 12:15:53 +0300571 btrfs_start_ordered_extent(ordered, 1);
Miao Xie9afab882012-10-25 09:41:36 +0000572 complete(&ordered->completion);
573}
574
Chris Masond352ac62008-09-29 15:18:18 -0400575/*
576 * wait for all the ordered extents in a root. This is done when balancing
577 * space between drives.
578 */
Chris Mason6374e57a2017-06-23 09:48:21 -0700579u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr,
Filipe Manana578def72016-04-26 15:36:38 +0100580 const u64 range_start, const u64 range_len)
Chris Mason3eaa2882008-07-24 11:57:52 -0400581{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400582 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana578def72016-04-26 15:36:38 +0100583 LIST_HEAD(splice);
584 LIST_HEAD(skipped);
585 LIST_HEAD(works);
Miao Xie9afab882012-10-25 09:41:36 +0000586 struct btrfs_ordered_extent *ordered, *next;
Chris Mason6374e57a2017-06-23 09:48:21 -0700587 u64 count = 0;
Filipe Manana578def72016-04-26 15:36:38 +0100588 const u64 range_end = range_start + range_len;
Chris Mason3eaa2882008-07-24 11:57:52 -0400589
Miao Xie31f3d252014-03-06 13:55:02 +0800590 mutex_lock(&root->ordered_extent_mutex);
Miao Xie199c2a92013-05-15 07:48:23 +0000591 spin_lock(&root->ordered_extent_lock);
592 list_splice_init(&root->ordered_extents, &splice);
Miao Xieb0244192013-11-04 23:13:25 +0800593 while (!list_empty(&splice) && nr) {
Miao Xie199c2a92013-05-15 07:48:23 +0000594 ordered = list_first_entry(&splice, struct btrfs_ordered_extent,
595 root_extent_list);
Filipe Manana578def72016-04-26 15:36:38 +0100596
Omar Sandovalbffe6332019-12-02 17:34:19 -0800597 if (range_end <= ordered->disk_bytenr ||
598 ordered->disk_bytenr + ordered->disk_num_bytes <= range_start) {
Filipe Manana578def72016-04-26 15:36:38 +0100599 list_move_tail(&ordered->root_extent_list, &skipped);
600 cond_resched_lock(&root->ordered_extent_lock);
601 continue;
602 }
603
Miao Xie199c2a92013-05-15 07:48:23 +0000604 list_move_tail(&ordered->root_extent_list,
605 &root->ordered_extents);
Elena Reshetovae76edab2017-03-03 10:55:13 +0200606 refcount_inc(&ordered->refs);
Miao Xie199c2a92013-05-15 07:48:23 +0000607 spin_unlock(&root->ordered_extent_lock);
608
Qu Wenruoa44903a2014-02-28 10:46:09 +0800609 btrfs_init_work(&ordered->flush_work,
610 btrfs_run_ordered_extent_work, NULL, NULL);
Miao Xie199c2a92013-05-15 07:48:23 +0000611 list_add_tail(&ordered->work_list, &works);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400612 btrfs_queue_work(fs_info->flush_workers, &ordered->flush_work);
Miao Xie199c2a92013-05-15 07:48:23 +0000613
Miao Xie9afab882012-10-25 09:41:36 +0000614 cond_resched();
Miao Xie199c2a92013-05-15 07:48:23 +0000615 spin_lock(&root->ordered_extent_lock);
Chris Mason6374e57a2017-06-23 09:48:21 -0700616 if (nr != U64_MAX)
Miao Xieb0244192013-11-04 23:13:25 +0800617 nr--;
618 count++;
Chris Mason3eaa2882008-07-24 11:57:52 -0400619 }
Filipe Manana578def72016-04-26 15:36:38 +0100620 list_splice_tail(&skipped, &root->ordered_extents);
Miao Xieb0244192013-11-04 23:13:25 +0800621 list_splice_tail(&splice, &root->ordered_extents);
Miao Xie199c2a92013-05-15 07:48:23 +0000622 spin_unlock(&root->ordered_extent_lock);
Miao Xie9afab882012-10-25 09:41:36 +0000623
624 list_for_each_entry_safe(ordered, next, &works, work_list) {
625 list_del_init(&ordered->work_list);
626 wait_for_completion(&ordered->completion);
Miao Xie9afab882012-10-25 09:41:36 +0000627 btrfs_put_ordered_extent(ordered);
Miao Xie9afab882012-10-25 09:41:36 +0000628 cond_resched();
629 }
Miao Xie31f3d252014-03-06 13:55:02 +0800630 mutex_unlock(&root->ordered_extent_mutex);
Miao Xieb0244192013-11-04 23:13:25 +0800631
632 return count;
Chris Mason3eaa2882008-07-24 11:57:52 -0400633}
634
Filipe Manana042528f2019-11-14 18:02:43 +0000635void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr,
Chris Mason6374e57a2017-06-23 09:48:21 -0700636 const u64 range_start, const u64 range_len)
Miao Xie199c2a92013-05-15 07:48:23 +0000637{
638 struct btrfs_root *root;
639 struct list_head splice;
Chris Mason6374e57a2017-06-23 09:48:21 -0700640 u64 done;
Miao Xie199c2a92013-05-15 07:48:23 +0000641
642 INIT_LIST_HEAD(&splice);
643
Miao Xie8b9d83c2014-03-06 13:54:55 +0800644 mutex_lock(&fs_info->ordered_operations_mutex);
Miao Xie199c2a92013-05-15 07:48:23 +0000645 spin_lock(&fs_info->ordered_root_lock);
646 list_splice_init(&fs_info->ordered_roots, &splice);
Miao Xieb0244192013-11-04 23:13:25 +0800647 while (!list_empty(&splice) && nr) {
Miao Xie199c2a92013-05-15 07:48:23 +0000648 root = list_first_entry(&splice, struct btrfs_root,
649 ordered_root);
Josef Bacik00246522020-01-24 09:33:01 -0500650 root = btrfs_grab_root(root);
Miao Xie199c2a92013-05-15 07:48:23 +0000651 BUG_ON(!root);
652 list_move_tail(&root->ordered_root,
653 &fs_info->ordered_roots);
654 spin_unlock(&fs_info->ordered_root_lock);
655
Filipe Manana578def72016-04-26 15:36:38 +0100656 done = btrfs_wait_ordered_extents(root, nr,
657 range_start, range_len);
Josef Bacik00246522020-01-24 09:33:01 -0500658 btrfs_put_root(root);
Miao Xie199c2a92013-05-15 07:48:23 +0000659
660 spin_lock(&fs_info->ordered_root_lock);
Chris Mason6374e57a2017-06-23 09:48:21 -0700661 if (nr != U64_MAX) {
Miao Xieb0244192013-11-04 23:13:25 +0800662 nr -= done;
Miao Xieb0244192013-11-04 23:13:25 +0800663 }
Miao Xie199c2a92013-05-15 07:48:23 +0000664 }
Miao Xie931aa872013-11-14 17:33:21 +0800665 list_splice_tail(&splice, &fs_info->ordered_roots);
Miao Xie199c2a92013-05-15 07:48:23 +0000666 spin_unlock(&fs_info->ordered_root_lock);
Miao Xie8b9d83c2014-03-06 13:54:55 +0800667 mutex_unlock(&fs_info->ordered_operations_mutex);
Miao Xie199c2a92013-05-15 07:48:23 +0000668}
669
Chris Masoneb84ae02008-07-17 13:53:27 -0400670/*
671 * Used to start IO or wait for a given ordered extent to finish.
672 *
673 * If wait is one, this effectively waits on page writeback for all the pages
674 * in the extent, and it waits on the io completion code to insert
675 * metadata into the btree corresponding to the extent
676 */
Nikolay Borisovc0a43602020-09-18 12:15:53 +0300677void btrfs_start_ordered_extent(struct btrfs_ordered_extent *entry, int wait)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400678{
679 u64 start = entry->file_offset;
Omar Sandovalbffe6332019-12-02 17:34:19 -0800680 u64 end = start + entry->num_bytes - 1;
Nikolay Borisovc0a43602020-09-18 12:15:53 +0300681 struct btrfs_inode *inode = BTRFS_I(entry->inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400682
Nikolay Borisovc0a43602020-09-18 12:15:53 +0300683 trace_btrfs_ordered_extent_start(inode, entry);
liubo1abe9b82011-03-24 11:18:59 +0000684
Chris Masoneb84ae02008-07-17 13:53:27 -0400685 /*
686 * pages in the range can be dirty, clean or writeback. We
687 * start IO on any dirty ones so the wait doesn't stall waiting
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300688 * for the flusher thread to find them
Chris Masoneb84ae02008-07-17 13:53:27 -0400689 */
Josef Bacik4b46fce2010-05-23 11:00:55 -0400690 if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +0300691 filemap_fdatawrite_range(inode->vfs_inode.i_mapping, start, end);
Chris Masonc8b97812008-10-29 14:49:59 -0400692 if (wait) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400693 wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
694 &entry->flags));
Chris Masonc8b97812008-10-29 14:49:59 -0400695 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400696}
697
Chris Masoneb84ae02008-07-17 13:53:27 -0400698/*
699 * Used to wait on ordered extents across a large range of bytes.
700 */
Josef Bacik0ef8b722013-10-25 16:13:35 -0400701int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400702{
Josef Bacik0ef8b722013-10-25 16:13:35 -0400703 int ret = 0;
Filipe Manana28aeeac2015-05-05 19:03:10 +0100704 int ret_wb = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400705 u64 end;
Chris Masone5a22172008-07-18 20:42:20 -0400706 u64 orig_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400707 struct btrfs_ordered_extent *ordered;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400708
Chris Masone5a22172008-07-18 20:42:20 -0400709 if (start + len < start) {
Chris Masonf4219502008-07-22 11:18:09 -0400710 orig_end = INT_LIMIT(loff_t);
Chris Masone5a22172008-07-18 20:42:20 -0400711 } else {
712 orig_end = start + len - 1;
Chris Masonf4219502008-07-22 11:18:09 -0400713 if (orig_end > INT_LIMIT(loff_t))
714 orig_end = INT_LIMIT(loff_t);
Chris Masone5a22172008-07-18 20:42:20 -0400715 }
Josef Bacik551ebb22012-04-23 14:41:09 -0400716
Chris Masone5a22172008-07-18 20:42:20 -0400717 /* start IO across the range first to instantiate any delalloc
718 * extents
719 */
Filipe Manana728404d2014-10-10 09:43:11 +0100720 ret = btrfs_fdatawrite_range(inode, start, orig_end);
Josef Bacik0ef8b722013-10-25 16:13:35 -0400721 if (ret)
722 return ret;
Filipe Manana728404d2014-10-10 09:43:11 +0100723
Filipe Manana28aeeac2015-05-05 19:03:10 +0100724 /*
725 * If we have a writeback error don't return immediately. Wait first
726 * for any ordered extents that haven't completed yet. This is to make
727 * sure no one can dirty the same page ranges and call writepages()
728 * before the ordered extents complete - to avoid failures (-EEXIST)
729 * when adding the new ordered extents to the ordered tree.
730 */
731 ret_wb = filemap_fdatawait_range(inode->i_mapping, start, orig_end);
Chris Masonf4219502008-07-22 11:18:09 -0400732
733 end = orig_end;
Chris Masond3977122009-01-05 21:25:51 -0500734 while (1) {
Nikolay Borisov6d072c82020-08-31 14:42:39 +0300735 ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode), end);
Chris Masond3977122009-01-05 21:25:51 -0500736 if (!ordered)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400737 break;
Chris Masone5a22172008-07-18 20:42:20 -0400738 if (ordered->file_offset > orig_end) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400739 btrfs_put_ordered_extent(ordered);
740 break;
741 }
Omar Sandovalbffe6332019-12-02 17:34:19 -0800742 if (ordered->file_offset + ordered->num_bytes <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400743 btrfs_put_ordered_extent(ordered);
744 break;
745 }
Nikolay Borisovc0a43602020-09-18 12:15:53 +0300746 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400747 end = ordered->file_offset;
Filipe Mananae75fd332020-02-13 12:29:50 +0000748 /*
749 * If the ordered extent had an error save the error but don't
750 * exit without waiting first for all other ordered extents in
751 * the range to complete.
752 */
Josef Bacik0ef8b722013-10-25 16:13:35 -0400753 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags))
754 ret = -EIO;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400755 btrfs_put_ordered_extent(ordered);
Filipe Mananae75fd332020-02-13 12:29:50 +0000756 if (end == 0 || end == start)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400757 break;
758 end--;
759 }
Filipe Manana28aeeac2015-05-05 19:03:10 +0100760 return ret_wb ? ret_wb : ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400761}
762
Chris Masoneb84ae02008-07-17 13:53:27 -0400763/*
764 * find an ordered extent corresponding to file_offset. return NULL if
765 * nothing is found, otherwise take a reference on the extent and return it
766 */
Nikolay Borisovc3504372020-06-03 08:55:03 +0300767struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400768 u64 file_offset)
769{
770 struct btrfs_ordered_inode_tree *tree;
771 struct rb_node *node;
772 struct btrfs_ordered_extent *entry = NULL;
Johannes Thumshirn24533f62021-02-04 19:22:04 +0900773 unsigned long flags;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400774
Nikolay Borisovc3504372020-06-03 08:55:03 +0300775 tree = &inode->ordered_tree;
Johannes Thumshirn24533f62021-02-04 19:22:04 +0900776 spin_lock_irqsave(&tree->lock, flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400777 node = tree_search(tree, file_offset);
778 if (!node)
779 goto out;
780
781 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
782 if (!offset_in_entry(entry, file_offset))
783 entry = NULL;
784 if (entry)
Elena Reshetovae76edab2017-03-03 10:55:13 +0200785 refcount_inc(&entry->refs);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400786out:
Johannes Thumshirn24533f62021-02-04 19:22:04 +0900787 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400788 return entry;
789}
790
Josef Bacik4b46fce2010-05-23 11:00:55 -0400791/* Since the DIO code tries to lock a wide area we need to look for any ordered
792 * extents that exist in the range, rather than just the start of the range.
793 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +0200794struct btrfs_ordered_extent *btrfs_lookup_ordered_range(
795 struct btrfs_inode *inode, u64 file_offset, u64 len)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400796{
797 struct btrfs_ordered_inode_tree *tree;
798 struct rb_node *node;
799 struct btrfs_ordered_extent *entry = NULL;
800
Nikolay Borisova776c6f2017-02-20 13:50:49 +0200801 tree = &inode->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400802 spin_lock_irq(&tree->lock);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400803 node = tree_search(tree, file_offset);
804 if (!node) {
805 node = tree_search(tree, file_offset + len);
806 if (!node)
807 goto out;
808 }
809
810 while (1) {
811 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
812 if (range_overlaps(entry, file_offset, len))
813 break;
814
815 if (entry->file_offset >= file_offset + len) {
816 entry = NULL;
817 break;
818 }
819 entry = NULL;
820 node = rb_next(node);
821 if (!node)
822 break;
823 }
824out:
825 if (entry)
Elena Reshetovae76edab2017-03-03 10:55:13 +0200826 refcount_inc(&entry->refs);
Josef Bacik5fd02042012-05-02 14:00:54 -0400827 spin_unlock_irq(&tree->lock);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400828 return entry;
829}
830
Chris Masoneb84ae02008-07-17 13:53:27 -0400831/*
Filipe Manana48778172020-08-11 12:43:58 +0100832 * Adds all ordered extents to the given list. The list ends up sorted by the
833 * file_offset of the ordered extents.
834 */
835void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode,
836 struct list_head *list)
837{
838 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
839 struct rb_node *n;
840
841 ASSERT(inode_is_locked(&inode->vfs_inode));
842
843 spin_lock_irq(&tree->lock);
844 for (n = rb_first(&tree->tree); n; n = rb_next(n)) {
845 struct btrfs_ordered_extent *ordered;
846
847 ordered = rb_entry(n, struct btrfs_ordered_extent, rb_node);
848
849 if (test_bit(BTRFS_ORDERED_LOGGED, &ordered->flags))
850 continue;
851
852 ASSERT(list_empty(&ordered->log_list));
853 list_add_tail(&ordered->log_list, list);
854 refcount_inc(&ordered->refs);
855 }
856 spin_unlock_irq(&tree->lock);
857}
858
859/*
Chris Masoneb84ae02008-07-17 13:53:27 -0400860 * lookup and return any extent before 'file_offset'. NULL is returned
861 * if none is found
862 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400863struct btrfs_ordered_extent *
Nikolay Borisov6d072c82020-08-31 14:42:39 +0300864btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400865{
866 struct btrfs_ordered_inode_tree *tree;
867 struct rb_node *node;
868 struct btrfs_ordered_extent *entry = NULL;
869
Nikolay Borisov6d072c82020-08-31 14:42:39 +0300870 tree = &inode->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400871 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400872 node = tree_search(tree, file_offset);
873 if (!node)
874 goto out;
875
876 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
Elena Reshetovae76edab2017-03-03 10:55:13 +0200877 refcount_inc(&entry->refs);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400878out:
Josef Bacik5fd02042012-05-02 14:00:54 -0400879 spin_unlock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400880 return entry;
881}
Chris Masondbe674a2008-07-17 12:54:05 -0400882
Chris Masoneb84ae02008-07-17 13:53:27 -0400883/*
Nikolay Borisovffa87212019-05-07 10:19:22 +0300884 * btrfs_flush_ordered_range - Lock the passed range and ensures all pending
885 * ordered extents in it are run to completion.
886 *
Nikolay Borisovffa87212019-05-07 10:19:22 +0300887 * @inode: Inode whose ordered tree is to be searched
888 * @start: Beginning of range to flush
889 * @end: Last byte of range to lock
890 * @cached_state: If passed, will return the extent state responsible for the
891 * locked range. It's the caller's responsibility to free the cached state.
892 *
893 * This function always returns with the given range locked, ensuring after it's
894 * called no order extent can be pending.
895 */
David Sterbab272ae22020-02-05 19:09:33 +0100896void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,
Nikolay Borisovffa87212019-05-07 10:19:22 +0300897 u64 end,
898 struct extent_state **cached_state)
899{
900 struct btrfs_ordered_extent *ordered;
Naohiro Aotaa3b46b82019-07-26 16:47:05 +0900901 struct extent_state *cache = NULL;
902 struct extent_state **cachedp = &cache;
Nikolay Borisovbd80d942019-05-07 10:19:24 +0300903
904 if (cached_state)
Naohiro Aotaa3b46b82019-07-26 16:47:05 +0900905 cachedp = cached_state;
Nikolay Borisovffa87212019-05-07 10:19:22 +0300906
907 while (1) {
David Sterbab272ae22020-02-05 19:09:33 +0100908 lock_extent_bits(&inode->io_tree, start, end, cachedp);
Nikolay Borisovffa87212019-05-07 10:19:22 +0300909 ordered = btrfs_lookup_ordered_range(inode, start,
910 end - start + 1);
Nikolay Borisovbd80d942019-05-07 10:19:24 +0300911 if (!ordered) {
912 /*
913 * If no external cached_state has been passed then
914 * decrement the extra ref taken for cachedp since we
915 * aren't exposing it outside of this function
916 */
917 if (!cached_state)
Naohiro Aotaa3b46b82019-07-26 16:47:05 +0900918 refcount_dec(&cache->refs);
Nikolay Borisovffa87212019-05-07 10:19:22 +0300919 break;
Nikolay Borisovbd80d942019-05-07 10:19:24 +0300920 }
David Sterbab272ae22020-02-05 19:09:33 +0100921 unlock_extent_cached(&inode->io_tree, start, end, cachedp);
Nikolay Borisovc0a43602020-09-18 12:15:53 +0300922 btrfs_start_ordered_extent(ordered, 1);
Nikolay Borisovffa87212019-05-07 10:19:22 +0300923 btrfs_put_ordered_extent(ordered);
924 }
925}
926
Naohiro Aotad22002f2021-02-04 19:22:00 +0900927static int clone_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pos,
928 u64 len)
929{
930 struct inode *inode = ordered->inode;
931 u64 file_offset = ordered->file_offset + pos;
932 u64 disk_bytenr = ordered->disk_bytenr + pos;
933 u64 num_bytes = len;
934 u64 disk_num_bytes = len;
935 int type;
936 unsigned long flags_masked = ordered->flags & ~(1 << BTRFS_ORDERED_DIRECT);
937 int compress_type = ordered->compress_type;
938 unsigned long weight;
939 int ret;
940
941 weight = hweight_long(flags_masked);
942 WARN_ON_ONCE(weight > 1);
943 if (!weight)
944 type = 0;
945 else
946 type = __ffs(flags_masked);
947
948 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered->flags)) {
949 WARN_ON_ONCE(1);
950 ret = btrfs_add_ordered_extent_compress(BTRFS_I(inode),
951 file_offset, disk_bytenr, num_bytes,
952 disk_num_bytes, compress_type);
953 } else if (test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
954 ret = btrfs_add_ordered_extent_dio(BTRFS_I(inode), file_offset,
955 disk_bytenr, num_bytes, disk_num_bytes, type);
956 } else {
957 ret = btrfs_add_ordered_extent(BTRFS_I(inode), file_offset,
958 disk_bytenr, num_bytes, disk_num_bytes, type);
959 }
960
961 return ret;
962}
963
964int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pre,
965 u64 post)
966{
967 struct inode *inode = ordered->inode;
968 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
969 struct rb_node *node;
970 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
971 int ret = 0;
972
973 spin_lock_irq(&tree->lock);
974 /* Remove from tree once */
975 node = &ordered->rb_node;
976 rb_erase(node, &tree->tree);
977 RB_CLEAR_NODE(node);
978 if (tree->last == node)
979 tree->last = NULL;
980
981 ordered->file_offset += pre;
982 ordered->disk_bytenr += pre;
983 ordered->num_bytes -= (pre + post);
984 ordered->disk_num_bytes -= (pre + post);
985 ordered->bytes_left -= (pre + post);
986
987 /* Re-insert the node */
988 node = tree_insert(&tree->tree, ordered->file_offset, &ordered->rb_node);
989 if (node)
990 btrfs_panic(fs_info, -EEXIST,
991 "zoned: inconsistency in ordered tree at offset %llu",
992 ordered->file_offset);
993
994 spin_unlock_irq(&tree->lock);
995
996 if (pre)
997 ret = clone_ordered_extent(ordered, 0, pre);
998 if (post)
999 ret = clone_ordered_extent(ordered, pre + ordered->disk_num_bytes,
1000 post);
1001
1002 return ret;
1003}
1004
Miao Xie6352b912012-09-06 04:01:51 -06001005int __init ordered_data_init(void)
1006{
1007 btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent",
1008 sizeof(struct btrfs_ordered_extent), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03001009 SLAB_MEM_SPREAD,
Miao Xie6352b912012-09-06 04:01:51 -06001010 NULL);
1011 if (!btrfs_ordered_extent_cache)
1012 return -ENOMEM;
Miao Xie25287e02012-10-25 09:31:03 +00001013
Miao Xie6352b912012-09-06 04:01:51 -06001014 return 0;
1015}
1016
David Sterbae67c7182018-02-19 17:24:18 +01001017void __cold ordered_data_exit(void)
Miao Xie6352b912012-09-06 04:01:51 -06001018{
Kinglong Mee5598e902016-01-29 21:36:35 +08001019 kmem_cache_destroy(btrfs_ordered_extent_cache);
Miao Xie6352b912012-09-06 04:01:51 -06001020}