blob: 6b51fd2ec5ac53fdb255db69c68a34cf4a77f569 [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"
Qu Wenruob945a462021-05-31 16:50:46 +080019#include "subpage.h"
Chris Masondc17ff82008-01-08 15:46:30 -050020
Miao Xie6352b912012-09-06 04:01:51 -060021static struct kmem_cache *btrfs_ordered_extent_cache;
22
Chris Masone6dcd2d2008-07-17 12:53:50 -040023static u64 entry_end(struct btrfs_ordered_extent *entry)
Chris Masondc17ff82008-01-08 15:46:30 -050024{
Omar Sandovalbffe6332019-12-02 17:34:19 -080025 if (entry->file_offset + entry->num_bytes < entry->file_offset)
Chris Masone6dcd2d2008-07-17 12:53:50 -040026 return (u64)-1;
Omar Sandovalbffe6332019-12-02 17:34:19 -080027 return entry->file_offset + entry->num_bytes;
Chris Masondc17ff82008-01-08 15:46:30 -050028}
29
Chris Masond352ac62008-09-29 15:18:18 -040030/* returns NULL if the insertion worked, or it returns the node it did find
31 * in the tree
32 */
Chris Masone6dcd2d2008-07-17 12:53:50 -040033static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset,
34 struct rb_node *node)
Chris Masondc17ff82008-01-08 15:46:30 -050035{
Chris Masond3977122009-01-05 21:25:51 -050036 struct rb_node **p = &root->rb_node;
37 struct rb_node *parent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -040038 struct btrfs_ordered_extent *entry;
Chris Masondc17ff82008-01-08 15:46:30 -050039
Chris Masond3977122009-01-05 21:25:51 -050040 while (*p) {
Chris Masondc17ff82008-01-08 15:46:30 -050041 parent = *p;
Chris Masone6dcd2d2008-07-17 12:53:50 -040042 entry = rb_entry(parent, struct btrfs_ordered_extent, rb_node);
Chris Masondc17ff82008-01-08 15:46:30 -050043
Chris Masone6dcd2d2008-07-17 12:53:50 -040044 if (file_offset < entry->file_offset)
Chris Masondc17ff82008-01-08 15:46:30 -050045 p = &(*p)->rb_left;
Chris Masone6dcd2d2008-07-17 12:53:50 -040046 else if (file_offset >= entry_end(entry))
Chris Masondc17ff82008-01-08 15:46:30 -050047 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 Masond352ac62008-09-29 15:18:18 -040057/*
58 * look for a given offset in the tree, and if it can't be found return the
59 * first lesser offset
60 */
Chris Masone6dcd2d2008-07-17 12:53:50 -040061static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset,
62 struct rb_node **prev_ret)
Chris Masondc17ff82008-01-08 15:46:30 -050063{
Chris Masond3977122009-01-05 21:25:51 -050064 struct rb_node *n = root->rb_node;
Chris Masondc17ff82008-01-08 15:46:30 -050065 struct rb_node *prev = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -040066 struct rb_node *test;
67 struct btrfs_ordered_extent *entry;
68 struct btrfs_ordered_extent *prev_entry = NULL;
Chris Masondc17ff82008-01-08 15:46:30 -050069
Chris Masond3977122009-01-05 21:25:51 -050070 while (n) {
Chris Masone6dcd2d2008-07-17 12:53:50 -040071 entry = rb_entry(n, struct btrfs_ordered_extent, rb_node);
Chris Masondc17ff82008-01-08 15:46:30 -050072 prev = n;
73 prev_entry = entry;
Chris Masondc17ff82008-01-08 15:46:30 -050074
Chris Masone6dcd2d2008-07-17 12:53:50 -040075 if (file_offset < entry->file_offset)
Chris Masondc17ff82008-01-08 15:46:30 -050076 n = n->rb_left;
Chris Masone6dcd2d2008-07-17 12:53:50 -040077 else if (file_offset >= entry_end(entry))
Chris Masondc17ff82008-01-08 15:46:30 -050078 n = n->rb_right;
79 else
80 return n;
81 }
82 if (!prev_ret)
83 return NULL;
84
Chris Masond3977122009-01-05 21:25:51 -050085 while (prev && file_offset >= entry_end(prev_entry)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -040086 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 Masond3977122009-01-05 21:25:51 -050099 while (prev && file_offset < entry_end(prev_entry)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400100 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 Masondc17ff82008-01-08 15:46:30 -0500106 }
107 *prev_ret = prev;
108 return NULL;
109}
110
Josef Bacik4b46fce2010-05-23 11:00:55 -0400111static int range_overlaps(struct btrfs_ordered_extent *entry, u64 file_offset,
112 u64 len)
113{
114 if (file_offset + len <= entry->file_offset ||
Omar Sandovalbffe6332019-12-02 17:34:19 -0800115 entry->file_offset + entry->num_bytes <= file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400116 return 0;
117 return 1;
118}
119
Chris Masond352ac62008-09-29 15:18:18 -0400120/*
121 * look find the first ordered struct that has this offset, otherwise
122 * the first one less than this offset
123 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400124static 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 Masonc87fb6f2011-01-31 19:54:59 -0500128 struct rb_node *prev = NULL;
Chris Masondc17ff82008-01-08 15:46:30 -0500129 struct rb_node *ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400130 struct btrfs_ordered_extent *entry;
131
132 if (tree->last) {
133 entry = rb_entry(tree->last, struct btrfs_ordered_extent,
134 rb_node);
Nikolay Borisov20bbf202021-02-17 15:12:49 +0200135 if (in_range(file_offset, entry->file_offset, entry->num_bytes))
Chris Masone6dcd2d2008-07-17 12:53:50 -0400136 return tree->last;
137 }
138 ret = __tree_search(root, file_offset, &prev);
Chris Masondc17ff82008-01-08 15:46:30 -0500139 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400140 ret = prev;
141 if (ret)
142 tree->last = ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500143 return ret;
144}
145
Qu Wenruo7dbeaad2020-06-10 09:04:43 +0800146/*
147 * Allocate and add a new ordered_extent into the per-inode tree.
Chris Masoneb84ae02008-07-17 13:53:27 -0400148 *
Chris Masoneb84ae02008-07-17 13:53:27 -0400149 * The tree is given a single reference on the ordered extent that was
150 * inserted.
151 */
Nikolay Borisovda69fea2020-06-03 08:55:01 +0300152static int __btrfs_add_ordered_extent(struct btrfs_inode *inode, u64 file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800153 u64 disk_bytenr, u64 num_bytes,
154 u64 disk_num_bytes, int type, int dio,
155 int compress_type)
Chris Masondc17ff82008-01-08 15:46:30 -0500156{
Nikolay Borisovda69fea2020-06-03 08:55:01 +0300157 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 Masone6dcd2d2008-07-17 12:53:50 -0400160 struct rb_node *node;
161 struct btrfs_ordered_extent *entry;
Qu Wenruo7dbeaad2020-06-10 09:04:43 +0800162 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500163
Qu Wenruo7dbeaad2020-06-10 09:04:43 +0800164 if (type == BTRFS_ORDERED_NOCOW || type == BTRFS_ORDERED_PREALLOC) {
165 /* For nocow write, we can release the qgroup rsv right now */
Nikolay Borisov8b8a9792020-06-03 08:55:11 +0300166 ret = btrfs_qgroup_free_data(inode, NULL, file_offset, num_bytes);
Qu Wenruo7dbeaad2020-06-10 09:04:43 +0800167 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 Borisov72b7d152020-06-03 08:55:18 +0300175 ret = btrfs_qgroup_release_data(inode, file_offset, num_bytes);
Qu Wenruo7dbeaad2020-06-10 09:04:43 +0800176 if (ret < 0)
177 return ret;
178 }
Miao Xie6352b912012-09-06 04:01:51 -0600179 entry = kmem_cache_zalloc(btrfs_ordered_extent_cache, GFP_NOFS);
Chris Masondc17ff82008-01-08 15:46:30 -0500180 if (!entry)
181 return -ENOMEM;
182
Chris Masone6dcd2d2008-07-17 12:53:50 -0400183 entry->file_offset = file_offset;
Omar Sandovalbffe6332019-12-02 17:34:19 -0800184 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 Borisovda69fea2020-06-03 08:55:01 +0300188 entry->inode = igrab(&inode->vfs_inode);
Li Zefan261507a02010-12-17 14:21:50 +0800189 entry->compress_type = compress_type;
Josef Bacik77cef2e2013-08-29 13:57:21 -0400190 entry->truncated_len = (u64)-1;
Qu Wenruo7dbeaad2020-06-10 09:04:43 +0800191 entry->qgroup_rsv = ret;
Naohiro Aotad8e3fb12021-02-04 19:22:05 +0900192 entry->physical = (u64)-1;
Qu Wenruo3c198fe2021-01-21 14:13:54 +0800193
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 Mason3eaa2882008-07-24 11:57:52 -0400199
Josef Bacik5deb17e2020-10-09 09:28:20 -0400200 percpu_counter_add_batch(&fs_info->ordered_bytes, num_bytes,
201 fs_info->delalloc_batch);
202
203 if (dio)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400204 set_bit(BTRFS_ORDERED_DIRECT, &entry->flags);
205
Chris Masone6dcd2d2008-07-17 12:53:50 -0400206 /* one ref for the tree */
Elena Reshetovae76edab2017-03-03 10:55:13 +0200207 refcount_set(&entry->refs, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400208 init_waitqueue_head(&entry->wait);
209 INIT_LIST_HEAD(&entry->list);
Filipe Manana48778172020-08-11 12:43:58 +0100210 INIT_LIST_HEAD(&entry->log_list);
Chris Mason3eaa2882008-07-24 11:57:52 -0400211 INIT_LIST_HEAD(&entry->root_extent_list);
Miao Xie9afab882012-10-25 09:41:36 +0000212 INIT_LIST_HEAD(&entry->work_list);
213 init_completion(&entry->completion);
Chris Masondc17ff82008-01-08 15:46:30 -0500214
Nikolay Borisovacbf1dd2020-08-31 14:42:40 +0300215 trace_btrfs_ordered_extent_add(inode, entry);
liubo1abe9b82011-03-24 11:18:59 +0000216
Josef Bacik5fd02042012-05-02 14:00:54 -0400217 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400218 node = tree_insert(&tree->tree, file_offset,
219 &entry->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -0400220 if (node)
Nikolay Borisov511a32b2019-11-29 11:38:13 +0200221 btrfs_panic(fs_info, -EEXIST,
222 "inconsistency in ordered tree at offset %llu",
223 file_offset);
Josef Bacik5fd02042012-05-02 14:00:54 -0400224 spin_unlock_irq(&tree->lock);
Chris Masond3977122009-01-05 21:25:51 -0500225
Miao Xie199c2a92013-05-15 07:48:23 +0000226 spin_lock(&root->ordered_extent_lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400227 list_add_tail(&entry->root_extent_list,
Miao Xie199c2a92013-05-15 07:48:23 +0000228 &root->ordered_extents);
229 root->nr_ordered_extents++;
230 if (root->nr_ordered_extents == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400231 spin_lock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +0000232 BUG_ON(!list_empty(&root->ordered_root));
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400233 list_add_tail(&root->ordered_root, &fs_info->ordered_roots);
234 spin_unlock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +0000235 }
236 spin_unlock(&root->ordered_extent_lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400237
Josef Bacik8b62f872017-10-19 14:15:55 -0400238 /*
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 Borisovda69fea2020-06-03 08:55:01 +0300243 spin_lock(&inode->lock);
244 btrfs_mod_outstanding_extents(inode, 1);
245 spin_unlock(&inode->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -0400246
Chris Masone6dcd2d2008-07-17 12:53:50 -0400247 return 0;
248}
Chris Masondc17ff82008-01-08 15:46:30 -0500249
Nikolay Borisove7fbf602020-06-03 08:55:13 +0300250int btrfs_add_ordered_extent(struct btrfs_inode *inode, u64 file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800251 u64 disk_bytenr, u64 num_bytes, u64 disk_num_bytes,
252 int type)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400253{
Qu Wenruo3c198fe2021-01-21 14:13:54 +0800254 ASSERT(type == BTRFS_ORDERED_REGULAR ||
255 type == BTRFS_ORDERED_NOCOW ||
256 type == BTRFS_ORDERED_PREALLOC);
Nikolay Borisove7fbf602020-06-03 08:55:13 +0300257 return __btrfs_add_ordered_extent(inode, file_offset, disk_bytenr,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800258 num_bytes, disk_num_bytes, type, 0,
Li Zefan261507a02010-12-17 14:21:50 +0800259 BTRFS_COMPRESS_NONE);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400260}
261
Nikolay Borisovc1e09522020-06-03 08:55:30 +0300262int btrfs_add_ordered_extent_dio(struct btrfs_inode *inode, u64 file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800263 u64 disk_bytenr, u64 num_bytes,
264 u64 disk_num_bytes, 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 Borisovc1e09522020-06-03 08:55:30 +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, 1,
Li Zefan261507a02010-12-17 14:21:50 +0800271 BTRFS_COMPRESS_NONE);
272}
273
Nikolay Borisov4cc61202020-06-03 08:55:15 +0300274int btrfs_add_ordered_extent_compress(struct btrfs_inode *inode, u64 file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800275 u64 disk_bytenr, u64 num_bytes,
Qu Wenruo3c198fe2021-01-21 14:13:54 +0800276 u64 disk_num_bytes, int compress_type)
Li Zefan261507a02010-12-17 14:21:50 +0800277{
Qu Wenruo3c198fe2021-01-21 14:13:54 +0800278 ASSERT(compress_type != BTRFS_COMPRESS_NONE);
Nikolay Borisov4cc61202020-06-03 08:55:15 +0300279 return __btrfs_add_ordered_extent(inode, file_offset, disk_bytenr,
Qu Wenruo3c198fe2021-01-21 14:13:54 +0800280 num_bytes, disk_num_bytes,
281 BTRFS_ORDERED_COMPRESSED, 0,
Li Zefan261507a02010-12-17 14:21:50 +0800282 compress_type);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400283}
284
Chris Masoneb84ae02008-07-17 13:53:27 -0400285/*
286 * Add a struct btrfs_ordered_sum into the list of checksums to be inserted
Chris Mason3edf7d32008-07-18 06:17:13 -0400287 * when an ordered extent is finished. If the list covers more than one
288 * ordered extent, it is split across multiples.
Chris Masoneb84ae02008-07-17 13:53:27 -0400289 */
Nikolay Borisovf9756262019-04-10 16:16:11 +0300290void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100291 struct btrfs_ordered_sum *sum)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400292{
293 struct btrfs_ordered_inode_tree *tree;
Chris Mason1b1e2132008-06-25 16:01:31 -0400294
Nikolay Borisovf9756262019-04-10 16:16:11 +0300295 tree = &BTRFS_I(entry->inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400296 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400297 list_add_tail(&sum->list, &entry->list);
Josef Bacik5fd02042012-05-02 14:00:54 -0400298 spin_unlock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400299}
300
Chris Masoneb84ae02008-07-17 13:53:27 -0400301/*
Qu Wenruoe65f1522021-04-01 15:15:06 +0800302 * Mark all ordered extents io inside the specified range finished.
Chris Mason163cf092010-11-28 19:56:33 -0500303 *
Qu Wenruoe65f1522021-04-01 15:15:06 +0800304 * @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 Mason163cf092010-11-28 19:56:33 -0500312 *
Qu Wenruoe65f1522021-04-01 15:15:06 +0800313 * This function is called for endio, thus the range must have ordered
314 * extent(s) coveri it.
Chris Mason163cf092010-11-28 19:56:33 -0500315 */
Qu Wenruoe65f1522021-04-01 15:15:06 +0800316void 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 Mason163cf092010-11-28 19:56:33 -0500320{
Nikolay Borisov70958212020-06-03 08:55:23 +0300321 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
Qu Wenruoe65f1522021-04-01 15:15:06 +0800322 struct btrfs_fs_info *fs_info = inode->root->fs_info;
323 struct btrfs_workqueue *wq;
Chris Mason163cf092010-11-28 19:56:33 -0500324 struct rb_node *node;
325 struct btrfs_ordered_extent *entry = NULL;
Josef Bacik5fd02042012-05-02 14:00:54 -0400326 unsigned long flags;
Qu Wenruoe65f1522021-04-01 15:15:06 +0800327 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 Mason163cf092010-11-28 19:56:33 -0500337
Josef Bacik5fd02042012-05-02 14:00:54 -0400338 spin_lock_irqsave(&tree->lock, flags);
Qu Wenruoe65f1522021-04-01 15:15:06 +0800339 while (cur < file_offset + num_bytes) {
340 u64 entry_end;
341 u64 end;
342 u32 len;
Chris Mason163cf092010-11-28 19:56:33 -0500343
Qu Wenruoe65f1522021-04-01 15:15:06 +0800344 node = tree_search(tree, cur);
345 /* No ordered extents at all */
346 if (!node)
347 break;
Chris Mason163cf092010-11-28 19:56:33 -0500348
Qu Wenruoe65f1522021-04-01 15:15:06 +0800349 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
350 entry_end = entry->file_offset + entry->num_bytes;
Qu Wenruo58f74b22020-12-22 13:59:24 +0800351 /*
Qu Wenruoe65f1522021-04-01 15:15:06 +0800352 * |<-- OE --->| |
353 * cur
354 * Go to next OE.
Qu Wenruo58f74b22020-12-22 13:59:24 +0800355 */
Qu Wenruoe65f1522021-04-01 15:15:06 +0800356 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 Wenruof57ad932021-04-07 19:22:13 +0800392 * Ordered (Private2) bit indicates whether we still
393 * have pending io unfinished for the ordered extent.
Qu Wenruoe65f1522021-04-01 15:15:06 +0800394 *
395 * If there's no such bit, we need to skip to next range.
396 */
Qu Wenruob945a462021-05-31 16:50:46 +0800397 if (!btrfs_page_test_ordered(fs_info, page, cur, len)) {
Qu Wenruoe65f1522021-04-01 15:15:06 +0800398 cur += len;
399 continue;
400 }
Qu Wenruob945a462021-05-31 16:50:46 +0800401 btrfs_page_clear_ordered(fs_info, page, cur, len);
Qu Wenruoe65f1522021-04-01 15:15:06 +0800402 }
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 Mason163cf092010-11-28 19:56:33 -0500436 }
Josef Bacik5fd02042012-05-02 14:00:54 -0400437 spin_unlock_irqrestore(&tree->lock, flags);
Chris Mason163cf092010-11-28 19:56:33 -0500438}
439
440/*
Qu Wenruo58f74b22020-12-22 13:59:24 +0800441 * Finish IO for one ordered extent across a given range. The range can only
442 * contain one ordered extent.
Chris Masoneb84ae02008-07-17 13:53:27 -0400443 *
Qu Wenruo58f74b22020-12-22 13:59:24 +0800444 * @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
Qu Wenruo58f74b22020-12-22 13:59:24 +0800449 *
450 * Return true if the ordered extent is finished in the range, and update
451 * @cached.
452 * Return false otherwise.
453 *
454 * NOTE: The range can NOT cross multiple ordered extents.
455 * Thus caller should ensure the range doesn't cross ordered extents.
Chris Masoneb84ae02008-07-17 13:53:27 -0400456 */
Qu Wenruo58f74b22020-12-22 13:59:24 +0800457bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode,
458 struct btrfs_ordered_extent **cached,
David Sterbaf41b6ba2021-07-26 14:15:10 +0200459 u64 file_offset, u64 io_size)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400460{
Nikolay Borisov90c03042020-08-31 14:42:41 +0300461 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400462 struct rb_node *node;
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000463 struct btrfs_ordered_extent *entry = NULL;
Josef Bacik5fd02042012-05-02 14:00:54 -0400464 unsigned long flags;
Qu Wenruo58f74b22020-12-22 13:59:24 +0800465 bool finished = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400466
Josef Bacik5fd02042012-05-02 14:00:54 -0400467 spin_lock_irqsave(&tree->lock, flags);
468 if (cached && *cached) {
469 entry = *cached;
470 goto have_entry;
471 }
472
Chris Masone6dcd2d2008-07-17 12:53:50 -0400473 node = tree_search(tree, file_offset);
Qu Wenruo58f74b22020-12-22 13:59:24 +0800474 if (!node)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400475 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400476
477 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
Josef Bacik5fd02042012-05-02 14:00:54 -0400478have_entry:
Nikolay Borisov20bbf202021-02-17 15:12:49 +0200479 if (!in_range(file_offset, entry->file_offset, entry->num_bytes))
Chris Masone6dcd2d2008-07-17 12:53:50 -0400480 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400481
Qu Wenruo58f74b22020-12-22 13:59:24 +0800482 if (io_size > entry->bytes_left)
Nikolay Borisov90c03042020-08-31 14:42:41 +0300483 btrfs_crit(inode->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -0500484 "bad ordered accounting left %llu size %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200485 entry->bytes_left, io_size);
Qu Wenruo58f74b22020-12-22 13:59:24 +0800486
Chris Mason8b62b722009-09-02 16:53:46 -0400487 entry->bytes_left -= io_size;
Josef Bacik5fd02042012-05-02 14:00:54 -0400488
Miao Xieaf7a6502014-03-06 13:54:56 +0800489 if (entry->bytes_left == 0) {
Qu Wenruo58f74b22020-12-22 13:59:24 +0800490 /*
491 * Ensure only one caller can set the flag and finished_ret
492 * accordingly
493 */
494 finished = !test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
David Sterba093258e2018-02-26 16:15:17 +0100495 /* test_and_set_bit implies a barrier */
496 cond_wake_up_nomb(&entry->wait);
Miao Xieaf7a6502014-03-06 13:54:56 +0800497 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400498out:
Qu Wenruo58f74b22020-12-22 13:59:24 +0800499 if (finished && cached && entry) {
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000500 *cached = entry;
Elena Reshetovae76edab2017-03-03 10:55:13 +0200501 refcount_inc(&entry->refs);
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000502 }
Josef Bacik5fd02042012-05-02 14:00:54 -0400503 spin_unlock_irqrestore(&tree->lock, flags);
Qu Wenruo58f74b22020-12-22 13:59:24 +0800504 return finished;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400505}
506
Chris Masoneb84ae02008-07-17 13:53:27 -0400507/*
508 * used to drop a reference on an ordered extent. This will free
509 * the extent if the last reference is dropped
510 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100511void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400512{
Chris Masonba1da2f2008-07-17 12:54:15 -0400513 struct list_head *cur;
514 struct btrfs_ordered_sum *sum;
515
Nikolay Borisovacbf1dd2020-08-31 14:42:40 +0300516 trace_btrfs_ordered_extent_put(BTRFS_I(entry->inode), entry);
liubo1abe9b82011-03-24 11:18:59 +0000517
Elena Reshetovae76edab2017-03-03 10:55:13 +0200518 if (refcount_dec_and_test(&entry->refs)) {
Filipe Manana61de7182015-07-01 12:13:10 +0100519 ASSERT(list_empty(&entry->root_extent_list));
Filipe Manana48778172020-08-11 12:43:58 +0100520 ASSERT(list_empty(&entry->log_list));
Filipe Manana61de7182015-07-01 12:13:10 +0100521 ASSERT(RB_EMPTY_NODE(&entry->rb_node));
Josef Bacik5fd02042012-05-02 14:00:54 -0400522 if (entry->inode)
523 btrfs_add_delayed_iput(entry->inode);
Chris Masond3977122009-01-05 21:25:51 -0500524 while (!list_empty(&entry->list)) {
Chris Masonba1da2f2008-07-17 12:54:15 -0400525 cur = entry->list.next;
526 sum = list_entry(cur, struct btrfs_ordered_sum, list);
527 list_del(&sum->list);
Nikolay Borisova3d46ae2019-04-01 11:29:58 +0300528 kvfree(sum);
Chris Masonba1da2f2008-07-17 12:54:15 -0400529 }
Miao Xie6352b912012-09-06 04:01:51 -0600530 kmem_cache_free(btrfs_ordered_extent_cache, entry);
Chris Masonba1da2f2008-07-17 12:54:15 -0400531 }
Chris Masondc17ff82008-01-08 15:46:30 -0500532}
533
Chris Masoneb84ae02008-07-17 13:53:27 -0400534/*
535 * remove an ordered extent from the tree. No references are dropped
Josef Bacik5fd02042012-05-02 14:00:54 -0400536 * and waiters are woken up.
Chris Masoneb84ae02008-07-17 13:53:27 -0400537 */
Nikolay Borisov71fe0a52020-09-18 12:15:50 +0300538void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode,
Josef Bacik5fd02042012-05-02 14:00:54 -0400539 struct btrfs_ordered_extent *entry)
Chris Masondc17ff82008-01-08 15:46:30 -0500540{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400541 struct btrfs_ordered_inode_tree *tree;
Josef Bacik8b62f872017-10-19 14:15:55 -0400542 struct btrfs_root *root = btrfs_inode->root;
Nikolay Borisov71fe0a52020-09-18 12:15:50 +0300543 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masondc17ff82008-01-08 15:46:30 -0500544 struct rb_node *node;
Filipe Manana48778172020-08-11 12:43:58 +0100545 bool pending;
Chris Masondc17ff82008-01-08 15:46:30 -0500546
Josef Bacik8b62f872017-10-19 14:15:55 -0400547 /* This is paired with btrfs_add_ordered_extent. */
548 spin_lock(&btrfs_inode->lock);
549 btrfs_mod_outstanding_extents(btrfs_inode, -1);
550 spin_unlock(&btrfs_inode->lock);
551 if (root != fs_info->tree_root)
Omar Sandovalbffe6332019-12-02 17:34:19 -0800552 btrfs_delalloc_release_metadata(btrfs_inode, entry->num_bytes,
553 false);
Josef Bacik8b62f872017-10-19 14:15:55 -0400554
Josef Bacik5deb17e2020-10-09 09:28:20 -0400555 percpu_counter_add_batch(&fs_info->ordered_bytes, -entry->num_bytes,
556 fs_info->delalloc_batch);
Josef Bacik4297ff82019-04-10 15:56:09 -0400557
Josef Bacik8b62f872017-10-19 14:15:55 -0400558 tree = &btrfs_inode->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400559 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400560 node = &entry->rb_node;
Chris Masondc17ff82008-01-08 15:46:30 -0500561 rb_erase(node, &tree->tree);
Filipe Manana61de7182015-07-01 12:13:10 +0100562 RB_CLEAR_NODE(node);
Filipe David Borba Manana1b8e7e42013-11-22 18:54:58 +0000563 if (tree->last == node)
564 tree->last = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400565 set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
Filipe Manana48778172020-08-11 12:43:58 +0100566 pending = test_and_clear_bit(BTRFS_ORDERED_PENDING, &entry->flags);
Josef Bacik5fd02042012-05-02 14:00:54 -0400567 spin_unlock_irq(&tree->lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400568
Filipe Manana48778172020-08-11 12:43:58 +0100569 /*
570 * The current running transaction is waiting on us, we need to let it
571 * know that we're complete and wake it up.
572 */
573 if (pending) {
574 struct btrfs_transaction *trans;
575
576 /*
577 * The checks for trans are just a formality, it should be set,
578 * but if it isn't we don't want to deref/assert under the spin
579 * lock, so be nice and check if trans is set, but ASSERT() so
580 * if it isn't set a developer will notice.
581 */
582 spin_lock(&fs_info->trans_lock);
583 trans = fs_info->running_transaction;
584 if (trans)
585 refcount_inc(&trans->use_count);
586 spin_unlock(&fs_info->trans_lock);
587
588 ASSERT(trans);
589 if (trans) {
590 if (atomic_dec_and_test(&trans->pending_ordered))
591 wake_up(&trans->pending_wait);
592 btrfs_put_transaction(trans);
593 }
594 }
595
Miao Xie199c2a92013-05-15 07:48:23 +0000596 spin_lock(&root->ordered_extent_lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400597 list_del_init(&entry->root_extent_list);
Miao Xie199c2a92013-05-15 07:48:23 +0000598 root->nr_ordered_extents--;
Chris Mason5a3f23d2009-03-31 13:27:11 -0400599
Nikolay Borisov71fe0a52020-09-18 12:15:50 +0300600 trace_btrfs_ordered_extent_remove(btrfs_inode, entry);
liubo1abe9b82011-03-24 11:18:59 +0000601
Miao Xie199c2a92013-05-15 07:48:23 +0000602 if (!root->nr_ordered_extents) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400603 spin_lock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +0000604 BUG_ON(list_empty(&root->ordered_root));
605 list_del_init(&root->ordered_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400606 spin_unlock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +0000607 }
608 spin_unlock(&root->ordered_extent_lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400609 wake_up(&entry->wait);
Chris Mason81d7ed22008-04-25 08:51:48 -0400610}
Chris Masone6dcd2d2008-07-17 12:53:50 -0400611
Qu Wenruod458b052014-02-28 10:46:19 +0800612static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
Miao Xie9afab882012-10-25 09:41:36 +0000613{
614 struct btrfs_ordered_extent *ordered;
615
616 ordered = container_of(work, struct btrfs_ordered_extent, flush_work);
Nikolay Borisovc0a43602020-09-18 12:15:53 +0300617 btrfs_start_ordered_extent(ordered, 1);
Miao Xie9afab882012-10-25 09:41:36 +0000618 complete(&ordered->completion);
619}
620
Chris Masond352ac62008-09-29 15:18:18 -0400621/*
622 * wait for all the ordered extents in a root. This is done when balancing
623 * space between drives.
624 */
Chris Mason6374e57a2017-06-23 09:48:21 -0700625u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr,
Filipe Manana578def72016-04-26 15:36:38 +0100626 const u64 range_start, const u64 range_len)
Chris Mason3eaa2882008-07-24 11:57:52 -0400627{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400628 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana578def72016-04-26 15:36:38 +0100629 LIST_HEAD(splice);
630 LIST_HEAD(skipped);
631 LIST_HEAD(works);
Miao Xie9afab882012-10-25 09:41:36 +0000632 struct btrfs_ordered_extent *ordered, *next;
Chris Mason6374e57a2017-06-23 09:48:21 -0700633 u64 count = 0;
Filipe Manana578def72016-04-26 15:36:38 +0100634 const u64 range_end = range_start + range_len;
Chris Mason3eaa2882008-07-24 11:57:52 -0400635
Miao Xie31f3d252014-03-06 13:55:02 +0800636 mutex_lock(&root->ordered_extent_mutex);
Miao Xie199c2a92013-05-15 07:48:23 +0000637 spin_lock(&root->ordered_extent_lock);
638 list_splice_init(&root->ordered_extents, &splice);
Miao Xieb0244192013-11-04 23:13:25 +0800639 while (!list_empty(&splice) && nr) {
Miao Xie199c2a92013-05-15 07:48:23 +0000640 ordered = list_first_entry(&splice, struct btrfs_ordered_extent,
641 root_extent_list);
Filipe Manana578def72016-04-26 15:36:38 +0100642
Omar Sandovalbffe6332019-12-02 17:34:19 -0800643 if (range_end <= ordered->disk_bytenr ||
644 ordered->disk_bytenr + ordered->disk_num_bytes <= range_start) {
Filipe Manana578def72016-04-26 15:36:38 +0100645 list_move_tail(&ordered->root_extent_list, &skipped);
646 cond_resched_lock(&root->ordered_extent_lock);
647 continue;
648 }
649
Miao Xie199c2a92013-05-15 07:48:23 +0000650 list_move_tail(&ordered->root_extent_list,
651 &root->ordered_extents);
Elena Reshetovae76edab2017-03-03 10:55:13 +0200652 refcount_inc(&ordered->refs);
Miao Xie199c2a92013-05-15 07:48:23 +0000653 spin_unlock(&root->ordered_extent_lock);
654
Qu Wenruoa44903a2014-02-28 10:46:09 +0800655 btrfs_init_work(&ordered->flush_work,
656 btrfs_run_ordered_extent_work, NULL, NULL);
Miao Xie199c2a92013-05-15 07:48:23 +0000657 list_add_tail(&ordered->work_list, &works);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400658 btrfs_queue_work(fs_info->flush_workers, &ordered->flush_work);
Miao Xie199c2a92013-05-15 07:48:23 +0000659
Miao Xie9afab882012-10-25 09:41:36 +0000660 cond_resched();
Miao Xie199c2a92013-05-15 07:48:23 +0000661 spin_lock(&root->ordered_extent_lock);
Chris Mason6374e57a2017-06-23 09:48:21 -0700662 if (nr != U64_MAX)
Miao Xieb0244192013-11-04 23:13:25 +0800663 nr--;
664 count++;
Chris Mason3eaa2882008-07-24 11:57:52 -0400665 }
Filipe Manana578def72016-04-26 15:36:38 +0100666 list_splice_tail(&skipped, &root->ordered_extents);
Miao Xieb0244192013-11-04 23:13:25 +0800667 list_splice_tail(&splice, &root->ordered_extents);
Miao Xie199c2a92013-05-15 07:48:23 +0000668 spin_unlock(&root->ordered_extent_lock);
Miao Xie9afab882012-10-25 09:41:36 +0000669
670 list_for_each_entry_safe(ordered, next, &works, work_list) {
671 list_del_init(&ordered->work_list);
672 wait_for_completion(&ordered->completion);
Miao Xie9afab882012-10-25 09:41:36 +0000673 btrfs_put_ordered_extent(ordered);
Miao Xie9afab882012-10-25 09:41:36 +0000674 cond_resched();
675 }
Miao Xie31f3d252014-03-06 13:55:02 +0800676 mutex_unlock(&root->ordered_extent_mutex);
Miao Xieb0244192013-11-04 23:13:25 +0800677
678 return count;
Chris Mason3eaa2882008-07-24 11:57:52 -0400679}
680
Filipe Manana042528f2019-11-14 18:02:43 +0000681void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr,
Chris Mason6374e57a2017-06-23 09:48:21 -0700682 const u64 range_start, const u64 range_len)
Miao Xie199c2a92013-05-15 07:48:23 +0000683{
684 struct btrfs_root *root;
685 struct list_head splice;
Chris Mason6374e57a2017-06-23 09:48:21 -0700686 u64 done;
Miao Xie199c2a92013-05-15 07:48:23 +0000687
688 INIT_LIST_HEAD(&splice);
689
Miao Xie8b9d83c2014-03-06 13:54:55 +0800690 mutex_lock(&fs_info->ordered_operations_mutex);
Miao Xie199c2a92013-05-15 07:48:23 +0000691 spin_lock(&fs_info->ordered_root_lock);
692 list_splice_init(&fs_info->ordered_roots, &splice);
Miao Xieb0244192013-11-04 23:13:25 +0800693 while (!list_empty(&splice) && nr) {
Miao Xie199c2a92013-05-15 07:48:23 +0000694 root = list_first_entry(&splice, struct btrfs_root,
695 ordered_root);
Josef Bacik00246522020-01-24 09:33:01 -0500696 root = btrfs_grab_root(root);
Miao Xie199c2a92013-05-15 07:48:23 +0000697 BUG_ON(!root);
698 list_move_tail(&root->ordered_root,
699 &fs_info->ordered_roots);
700 spin_unlock(&fs_info->ordered_root_lock);
701
Filipe Manana578def72016-04-26 15:36:38 +0100702 done = btrfs_wait_ordered_extents(root, nr,
703 range_start, range_len);
Josef Bacik00246522020-01-24 09:33:01 -0500704 btrfs_put_root(root);
Miao Xie199c2a92013-05-15 07:48:23 +0000705
706 spin_lock(&fs_info->ordered_root_lock);
Chris Mason6374e57a2017-06-23 09:48:21 -0700707 if (nr != U64_MAX) {
Miao Xieb0244192013-11-04 23:13:25 +0800708 nr -= done;
Miao Xieb0244192013-11-04 23:13:25 +0800709 }
Miao Xie199c2a92013-05-15 07:48:23 +0000710 }
Miao Xie931aa872013-11-14 17:33:21 +0800711 list_splice_tail(&splice, &fs_info->ordered_roots);
Miao Xie199c2a92013-05-15 07:48:23 +0000712 spin_unlock(&fs_info->ordered_root_lock);
Miao Xie8b9d83c2014-03-06 13:54:55 +0800713 mutex_unlock(&fs_info->ordered_operations_mutex);
Miao Xie199c2a92013-05-15 07:48:23 +0000714}
715
Chris Masoneb84ae02008-07-17 13:53:27 -0400716/*
717 * Used to start IO or wait for a given ordered extent to finish.
718 *
719 * If wait is one, this effectively waits on page writeback for all the pages
720 * in the extent, and it waits on the io completion code to insert
721 * metadata into the btree corresponding to the extent
722 */
Nikolay Borisovc0a43602020-09-18 12:15:53 +0300723void btrfs_start_ordered_extent(struct btrfs_ordered_extent *entry, int wait)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400724{
725 u64 start = entry->file_offset;
Omar Sandovalbffe6332019-12-02 17:34:19 -0800726 u64 end = start + entry->num_bytes - 1;
Nikolay Borisovc0a43602020-09-18 12:15:53 +0300727 struct btrfs_inode *inode = BTRFS_I(entry->inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400728
Nikolay Borisovc0a43602020-09-18 12:15:53 +0300729 trace_btrfs_ordered_extent_start(inode, entry);
liubo1abe9b82011-03-24 11:18:59 +0000730
Chris Masoneb84ae02008-07-17 13:53:27 -0400731 /*
732 * pages in the range can be dirty, clean or writeback. We
733 * start IO on any dirty ones so the wait doesn't stall waiting
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300734 * for the flusher thread to find them
Chris Masoneb84ae02008-07-17 13:53:27 -0400735 */
Josef Bacik4b46fce2010-05-23 11:00:55 -0400736 if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +0300737 filemap_fdatawrite_range(inode->vfs_inode.i_mapping, start, end);
Chris Masonc8b97812008-10-29 14:49:59 -0400738 if (wait) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400739 wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
740 &entry->flags));
Chris Masonc8b97812008-10-29 14:49:59 -0400741 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400742}
743
Chris Masoneb84ae02008-07-17 13:53:27 -0400744/*
745 * Used to wait on ordered extents across a large range of bytes.
746 */
Josef Bacik0ef8b722013-10-25 16:13:35 -0400747int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400748{
Josef Bacik0ef8b722013-10-25 16:13:35 -0400749 int ret = 0;
Filipe Manana28aeeac2015-05-05 19:03:10 +0100750 int ret_wb = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400751 u64 end;
Chris Masone5a22172008-07-18 20:42:20 -0400752 u64 orig_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400753 struct btrfs_ordered_extent *ordered;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400754
Chris Masone5a22172008-07-18 20:42:20 -0400755 if (start + len < start) {
Chris Masonf4219502008-07-22 11:18:09 -0400756 orig_end = INT_LIMIT(loff_t);
Chris Masone5a22172008-07-18 20:42:20 -0400757 } else {
758 orig_end = start + len - 1;
Chris Masonf4219502008-07-22 11:18:09 -0400759 if (orig_end > INT_LIMIT(loff_t))
760 orig_end = INT_LIMIT(loff_t);
Chris Masone5a22172008-07-18 20:42:20 -0400761 }
Josef Bacik551ebb22012-04-23 14:41:09 -0400762
Chris Masone5a22172008-07-18 20:42:20 -0400763 /* start IO across the range first to instantiate any delalloc
764 * extents
765 */
Filipe Manana728404d2014-10-10 09:43:11 +0100766 ret = btrfs_fdatawrite_range(inode, start, orig_end);
Josef Bacik0ef8b722013-10-25 16:13:35 -0400767 if (ret)
768 return ret;
Filipe Manana728404d2014-10-10 09:43:11 +0100769
Filipe Manana28aeeac2015-05-05 19:03:10 +0100770 /*
771 * If we have a writeback error don't return immediately. Wait first
772 * for any ordered extents that haven't completed yet. This is to make
773 * sure no one can dirty the same page ranges and call writepages()
774 * before the ordered extents complete - to avoid failures (-EEXIST)
775 * when adding the new ordered extents to the ordered tree.
776 */
777 ret_wb = filemap_fdatawait_range(inode->i_mapping, start, orig_end);
Chris Masonf4219502008-07-22 11:18:09 -0400778
779 end = orig_end;
Chris Masond3977122009-01-05 21:25:51 -0500780 while (1) {
Nikolay Borisov6d072c82020-08-31 14:42:39 +0300781 ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode), end);
Chris Masond3977122009-01-05 21:25:51 -0500782 if (!ordered)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400783 break;
Chris Masone5a22172008-07-18 20:42:20 -0400784 if (ordered->file_offset > orig_end) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400785 btrfs_put_ordered_extent(ordered);
786 break;
787 }
Omar Sandovalbffe6332019-12-02 17:34:19 -0800788 if (ordered->file_offset + ordered->num_bytes <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400789 btrfs_put_ordered_extent(ordered);
790 break;
791 }
Nikolay Borisovc0a43602020-09-18 12:15:53 +0300792 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400793 end = ordered->file_offset;
Filipe Mananae75fd332020-02-13 12:29:50 +0000794 /*
795 * If the ordered extent had an error save the error but don't
796 * exit without waiting first for all other ordered extents in
797 * the range to complete.
798 */
Josef Bacik0ef8b722013-10-25 16:13:35 -0400799 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags))
800 ret = -EIO;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400801 btrfs_put_ordered_extent(ordered);
Filipe Mananae75fd332020-02-13 12:29:50 +0000802 if (end == 0 || end == start)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400803 break;
804 end--;
805 }
Filipe Manana28aeeac2015-05-05 19:03:10 +0100806 return ret_wb ? ret_wb : ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400807}
808
Chris Masoneb84ae02008-07-17 13:53:27 -0400809/*
810 * find an ordered extent corresponding to file_offset. return NULL if
811 * nothing is found, otherwise take a reference on the extent and return it
812 */
Nikolay Borisovc3504372020-06-03 08:55:03 +0300813struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400814 u64 file_offset)
815{
816 struct btrfs_ordered_inode_tree *tree;
817 struct rb_node *node;
818 struct btrfs_ordered_extent *entry = NULL;
Johannes Thumshirn24533f62021-02-04 19:22:04 +0900819 unsigned long flags;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400820
Nikolay Borisovc3504372020-06-03 08:55:03 +0300821 tree = &inode->ordered_tree;
Johannes Thumshirn24533f62021-02-04 19:22:04 +0900822 spin_lock_irqsave(&tree->lock, flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400823 node = tree_search(tree, file_offset);
824 if (!node)
825 goto out;
826
827 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
Nikolay Borisov20bbf202021-02-17 15:12:49 +0200828 if (!in_range(file_offset, entry->file_offset, entry->num_bytes))
Chris Masone6dcd2d2008-07-17 12:53:50 -0400829 entry = NULL;
830 if (entry)
Elena Reshetovae76edab2017-03-03 10:55:13 +0200831 refcount_inc(&entry->refs);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400832out:
Johannes Thumshirn24533f62021-02-04 19:22:04 +0900833 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400834 return entry;
835}
836
Josef Bacik4b46fce2010-05-23 11:00:55 -0400837/* Since the DIO code tries to lock a wide area we need to look for any ordered
838 * extents that exist in the range, rather than just the start of the range.
839 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +0200840struct btrfs_ordered_extent *btrfs_lookup_ordered_range(
841 struct btrfs_inode *inode, u64 file_offset, u64 len)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400842{
843 struct btrfs_ordered_inode_tree *tree;
844 struct rb_node *node;
845 struct btrfs_ordered_extent *entry = NULL;
846
Nikolay Borisova776c6f2017-02-20 13:50:49 +0200847 tree = &inode->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400848 spin_lock_irq(&tree->lock);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400849 node = tree_search(tree, file_offset);
850 if (!node) {
851 node = tree_search(tree, file_offset + len);
852 if (!node)
853 goto out;
854 }
855
856 while (1) {
857 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
858 if (range_overlaps(entry, file_offset, len))
859 break;
860
861 if (entry->file_offset >= file_offset + len) {
862 entry = NULL;
863 break;
864 }
865 entry = NULL;
866 node = rb_next(node);
867 if (!node)
868 break;
869 }
870out:
871 if (entry)
Elena Reshetovae76edab2017-03-03 10:55:13 +0200872 refcount_inc(&entry->refs);
Josef Bacik5fd02042012-05-02 14:00:54 -0400873 spin_unlock_irq(&tree->lock);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400874 return entry;
875}
876
Chris Masoneb84ae02008-07-17 13:53:27 -0400877/*
Filipe Manana48778172020-08-11 12:43:58 +0100878 * Adds all ordered extents to the given list. The list ends up sorted by the
879 * file_offset of the ordered extents.
880 */
881void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode,
882 struct list_head *list)
883{
884 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
885 struct rb_node *n;
886
887 ASSERT(inode_is_locked(&inode->vfs_inode));
888
889 spin_lock_irq(&tree->lock);
890 for (n = rb_first(&tree->tree); n; n = rb_next(n)) {
891 struct btrfs_ordered_extent *ordered;
892
893 ordered = rb_entry(n, struct btrfs_ordered_extent, rb_node);
894
895 if (test_bit(BTRFS_ORDERED_LOGGED, &ordered->flags))
896 continue;
897
898 ASSERT(list_empty(&ordered->log_list));
899 list_add_tail(&ordered->log_list, list);
900 refcount_inc(&ordered->refs);
901 }
902 spin_unlock_irq(&tree->lock);
903}
904
905/*
Chris Masoneb84ae02008-07-17 13:53:27 -0400906 * lookup and return any extent before 'file_offset'. NULL is returned
907 * if none is found
908 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400909struct btrfs_ordered_extent *
Nikolay Borisov6d072c82020-08-31 14:42:39 +0300910btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400911{
912 struct btrfs_ordered_inode_tree *tree;
913 struct rb_node *node;
914 struct btrfs_ordered_extent *entry = NULL;
915
Nikolay Borisov6d072c82020-08-31 14:42:39 +0300916 tree = &inode->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400917 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400918 node = tree_search(tree, file_offset);
919 if (!node)
920 goto out;
921
922 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
Elena Reshetovae76edab2017-03-03 10:55:13 +0200923 refcount_inc(&entry->refs);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400924out:
Josef Bacik5fd02042012-05-02 14:00:54 -0400925 spin_unlock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400926 return entry;
927}
Chris Masondbe674a2008-07-17 12:54:05 -0400928
Chris Masoneb84ae02008-07-17 13:53:27 -0400929/*
Qu Wenruoc095f332021-04-27 15:03:40 +0800930 * Lookup the first ordered extent that overlaps the range
931 * [@file_offset, @file_offset + @len).
932 *
933 * The difference between this and btrfs_lookup_first_ordered_extent() is
934 * that this one won't return any ordered extent that does not overlap the range.
935 * And the difference against btrfs_lookup_ordered_extent() is, this function
936 * ensures the first ordered extent gets returned.
937 */
938struct btrfs_ordered_extent *btrfs_lookup_first_ordered_range(
939 struct btrfs_inode *inode, u64 file_offset, u64 len)
940{
941 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
942 struct rb_node *node;
943 struct rb_node *cur;
944 struct rb_node *prev;
945 struct rb_node *next;
946 struct btrfs_ordered_extent *entry = NULL;
947
948 spin_lock_irq(&tree->lock);
949 node = tree->tree.rb_node;
950 /*
951 * Here we don't want to use tree_search() which will use tree->last
952 * and screw up the search order.
953 * And __tree_search() can't return the adjacent ordered extents
954 * either, thus here we do our own search.
955 */
956 while (node) {
957 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
958
959 if (file_offset < entry->file_offset) {
960 node = node->rb_left;
961 } else if (file_offset >= entry_end(entry)) {
962 node = node->rb_right;
963 } else {
964 /*
965 * Direct hit, got an ordered extent that starts at
966 * @file_offset
967 */
968 goto out;
969 }
970 }
971 if (!entry) {
972 /* Empty tree */
973 goto out;
974 }
975
976 cur = &entry->rb_node;
977 /* We got an entry around @file_offset, check adjacent entries */
978 if (entry->file_offset < file_offset) {
979 prev = cur;
980 next = rb_next(cur);
981 } else {
982 prev = rb_prev(cur);
983 next = cur;
984 }
985 if (prev) {
986 entry = rb_entry(prev, struct btrfs_ordered_extent, rb_node);
987 if (range_overlaps(entry, file_offset, len))
988 goto out;
989 }
990 if (next) {
991 entry = rb_entry(next, struct btrfs_ordered_extent, rb_node);
992 if (range_overlaps(entry, file_offset, len))
993 goto out;
994 }
995 /* No ordered extent in the range */
996 entry = NULL;
997out:
998 if (entry)
999 refcount_inc(&entry->refs);
1000 spin_unlock_irq(&tree->lock);
1001 return entry;
1002}
1003
1004/*
Nikolay Borisovffa87212019-05-07 10:19:22 +03001005 * btrfs_flush_ordered_range - Lock the passed range and ensures all pending
1006 * ordered extents in it are run to completion.
1007 *
Nikolay Borisovffa87212019-05-07 10:19:22 +03001008 * @inode: Inode whose ordered tree is to be searched
1009 * @start: Beginning of range to flush
1010 * @end: Last byte of range to lock
1011 * @cached_state: If passed, will return the extent state responsible for the
1012 * locked range. It's the caller's responsibility to free the cached state.
1013 *
1014 * This function always returns with the given range locked, ensuring after it's
1015 * called no order extent can be pending.
1016 */
David Sterbab272ae22020-02-05 19:09:33 +01001017void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,
Nikolay Borisovffa87212019-05-07 10:19:22 +03001018 u64 end,
1019 struct extent_state **cached_state)
1020{
1021 struct btrfs_ordered_extent *ordered;
Naohiro Aotaa3b46b82019-07-26 16:47:05 +09001022 struct extent_state *cache = NULL;
1023 struct extent_state **cachedp = &cache;
Nikolay Borisovbd80d942019-05-07 10:19:24 +03001024
1025 if (cached_state)
Naohiro Aotaa3b46b82019-07-26 16:47:05 +09001026 cachedp = cached_state;
Nikolay Borisovffa87212019-05-07 10:19:22 +03001027
1028 while (1) {
David Sterbab272ae22020-02-05 19:09:33 +01001029 lock_extent_bits(&inode->io_tree, start, end, cachedp);
Nikolay Borisovffa87212019-05-07 10:19:22 +03001030 ordered = btrfs_lookup_ordered_range(inode, start,
1031 end - start + 1);
Nikolay Borisovbd80d942019-05-07 10:19:24 +03001032 if (!ordered) {
1033 /*
1034 * If no external cached_state has been passed then
1035 * decrement the extra ref taken for cachedp since we
1036 * aren't exposing it outside of this function
1037 */
1038 if (!cached_state)
Naohiro Aotaa3b46b82019-07-26 16:47:05 +09001039 refcount_dec(&cache->refs);
Nikolay Borisovffa87212019-05-07 10:19:22 +03001040 break;
Nikolay Borisovbd80d942019-05-07 10:19:24 +03001041 }
David Sterbab272ae22020-02-05 19:09:33 +01001042 unlock_extent_cached(&inode->io_tree, start, end, cachedp);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03001043 btrfs_start_ordered_extent(ordered, 1);
Nikolay Borisovffa87212019-05-07 10:19:22 +03001044 btrfs_put_ordered_extent(ordered);
1045 }
1046}
1047
Naohiro Aotad22002f2021-02-04 19:22:00 +09001048static int clone_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pos,
1049 u64 len)
1050{
1051 struct inode *inode = ordered->inode;
Naohiro Aotaf79645d2021-09-07 00:04:28 +09001052 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
Naohiro Aotad22002f2021-02-04 19:22:00 +09001053 u64 file_offset = ordered->file_offset + pos;
1054 u64 disk_bytenr = ordered->disk_bytenr + pos;
1055 u64 num_bytes = len;
1056 u64 disk_num_bytes = len;
1057 int type;
1058 unsigned long flags_masked = ordered->flags & ~(1 << BTRFS_ORDERED_DIRECT);
1059 int compress_type = ordered->compress_type;
1060 unsigned long weight;
1061 int ret;
1062
1063 weight = hweight_long(flags_masked);
1064 WARN_ON_ONCE(weight > 1);
1065 if (!weight)
1066 type = 0;
1067 else
1068 type = __ffs(flags_masked);
1069
Naohiro Aotaf79645d2021-09-07 00:04:28 +09001070 /*
1071 * The splitting extent is already counted and will be added again
1072 * in btrfs_add_ordered_extent_*(). Subtract num_bytes to avoid
1073 * double counting.
1074 */
1075 percpu_counter_add_batch(&fs_info->ordered_bytes, -num_bytes,
1076 fs_info->delalloc_batch);
Naohiro Aotad22002f2021-02-04 19:22:00 +09001077 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered->flags)) {
1078 WARN_ON_ONCE(1);
1079 ret = btrfs_add_ordered_extent_compress(BTRFS_I(inode),
1080 file_offset, disk_bytenr, num_bytes,
1081 disk_num_bytes, compress_type);
1082 } else if (test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
1083 ret = btrfs_add_ordered_extent_dio(BTRFS_I(inode), file_offset,
1084 disk_bytenr, num_bytes, disk_num_bytes, type);
1085 } else {
1086 ret = btrfs_add_ordered_extent(BTRFS_I(inode), file_offset,
1087 disk_bytenr, num_bytes, disk_num_bytes, type);
1088 }
1089
1090 return ret;
1091}
1092
1093int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pre,
1094 u64 post)
1095{
1096 struct inode *inode = ordered->inode;
1097 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
1098 struct rb_node *node;
1099 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1100 int ret = 0;
1101
1102 spin_lock_irq(&tree->lock);
1103 /* Remove from tree once */
1104 node = &ordered->rb_node;
1105 rb_erase(node, &tree->tree);
1106 RB_CLEAR_NODE(node);
1107 if (tree->last == node)
1108 tree->last = NULL;
1109
1110 ordered->file_offset += pre;
1111 ordered->disk_bytenr += pre;
1112 ordered->num_bytes -= (pre + post);
1113 ordered->disk_num_bytes -= (pre + post);
1114 ordered->bytes_left -= (pre + post);
1115
1116 /* Re-insert the node */
1117 node = tree_insert(&tree->tree, ordered->file_offset, &ordered->rb_node);
1118 if (node)
1119 btrfs_panic(fs_info, -EEXIST,
1120 "zoned: inconsistency in ordered tree at offset %llu",
1121 ordered->file_offset);
1122
1123 spin_unlock_irq(&tree->lock);
1124
1125 if (pre)
1126 ret = clone_ordered_extent(ordered, 0, pre);
Filipe Mananaadbd914d2021-04-21 14:31:50 +01001127 if (ret == 0 && post)
Naohiro Aotad22002f2021-02-04 19:22:00 +09001128 ret = clone_ordered_extent(ordered, pre + ordered->disk_num_bytes,
1129 post);
1130
1131 return ret;
1132}
1133
Miao Xie6352b912012-09-06 04:01:51 -06001134int __init ordered_data_init(void)
1135{
1136 btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent",
1137 sizeof(struct btrfs_ordered_extent), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03001138 SLAB_MEM_SPREAD,
Miao Xie6352b912012-09-06 04:01:51 -06001139 NULL);
1140 if (!btrfs_ordered_extent_cache)
1141 return -ENOMEM;
Miao Xie25287e02012-10-25 09:31:03 +00001142
Miao Xie6352b912012-09-06 04:01:51 -06001143 return 0;
1144}
1145
David Sterbae67c7182018-02-19 17:24:18 +01001146void __cold ordered_data_exit(void)
Miao Xie6352b912012-09-06 04:01:51 -06001147{
Kinglong Mee5598e902016-01-29 21:36:35 +08001148 kmem_cache_destroy(btrfs_ordered_extent_cache);
Miao Xie6352b912012-09-06 04:01:51 -06001149}