blob: 168a5edd939df83ceef9f6306d1ac43d0a9383ce [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;
Yan Zhengd899e052008-10-30 14:25:28 -0400202 if (type != BTRFS_ORDERED_IO_DONE && type != BTRFS_ORDERED_COMPLETE)
Yan Zheng80ff3852008-10-30 14:20:02 -0400203 set_bit(type, &entry->flags);
Chris Mason3eaa2882008-07-24 11:57:52 -0400204
Josef Bacik4297ff82019-04-10 15:56:09 -0400205 if (dio) {
Omar Sandovalbffe6332019-12-02 17:34:19 -0800206 percpu_counter_add_batch(&fs_info->dio_bytes, num_bytes,
Josef Bacik4297ff82019-04-10 15:56:09 -0400207 fs_info->delalloc_batch);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400208 set_bit(BTRFS_ORDERED_DIRECT, &entry->flags);
Josef Bacik4297ff82019-04-10 15:56:09 -0400209 }
Josef Bacik4b46fce2010-05-23 11:00:55 -0400210
Chris Masone6dcd2d2008-07-17 12:53:50 -0400211 /* one ref for the tree */
Elena Reshetovae76edab2017-03-03 10:55:13 +0200212 refcount_set(&entry->refs, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400213 init_waitqueue_head(&entry->wait);
214 INIT_LIST_HEAD(&entry->list);
Filipe Manana48778172020-08-11 12:43:58 +0100215 INIT_LIST_HEAD(&entry->log_list);
Chris Mason3eaa2882008-07-24 11:57:52 -0400216 INIT_LIST_HEAD(&entry->root_extent_list);
Miao Xie9afab882012-10-25 09:41:36 +0000217 INIT_LIST_HEAD(&entry->work_list);
218 init_completion(&entry->completion);
Chris Masondc17ff82008-01-08 15:46:30 -0500219
Nikolay Borisovacbf1dd2020-08-31 14:42:40 +0300220 trace_btrfs_ordered_extent_add(inode, entry);
liubo1abe9b82011-03-24 11:18:59 +0000221
Josef Bacik5fd02042012-05-02 14:00:54 -0400222 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400223 node = tree_insert(&tree->tree, file_offset,
224 &entry->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -0400225 if (node)
Nikolay Borisov511a32b2019-11-29 11:38:13 +0200226 btrfs_panic(fs_info, -EEXIST,
227 "inconsistency in ordered tree at offset %llu",
228 file_offset);
Josef Bacik5fd02042012-05-02 14:00:54 -0400229 spin_unlock_irq(&tree->lock);
Chris Masond3977122009-01-05 21:25:51 -0500230
Miao Xie199c2a92013-05-15 07:48:23 +0000231 spin_lock(&root->ordered_extent_lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400232 list_add_tail(&entry->root_extent_list,
Miao Xie199c2a92013-05-15 07:48:23 +0000233 &root->ordered_extents);
234 root->nr_ordered_extents++;
235 if (root->nr_ordered_extents == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400236 spin_lock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +0000237 BUG_ON(!list_empty(&root->ordered_root));
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400238 list_add_tail(&root->ordered_root, &fs_info->ordered_roots);
239 spin_unlock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +0000240 }
241 spin_unlock(&root->ordered_extent_lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400242
Josef Bacik8b62f872017-10-19 14:15:55 -0400243 /*
244 * We don't need the count_max_extents here, we can assume that all of
245 * that work has been done at higher layers, so this is truly the
246 * smallest the extent is going to get.
247 */
Nikolay Borisovda69fea2020-06-03 08:55:01 +0300248 spin_lock(&inode->lock);
249 btrfs_mod_outstanding_extents(inode, 1);
250 spin_unlock(&inode->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -0400251
Chris Masone6dcd2d2008-07-17 12:53:50 -0400252 return 0;
253}
Chris Masondc17ff82008-01-08 15:46:30 -0500254
Nikolay Borisove7fbf602020-06-03 08:55:13 +0300255int btrfs_add_ordered_extent(struct btrfs_inode *inode, u64 file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800256 u64 disk_bytenr, u64 num_bytes, u64 disk_num_bytes,
257 int type)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400258{
Nikolay Borisove7fbf602020-06-03 08:55:13 +0300259 return __btrfs_add_ordered_extent(inode, file_offset, disk_bytenr,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800260 num_bytes, disk_num_bytes, type, 0,
Li Zefan261507a02010-12-17 14:21:50 +0800261 BTRFS_COMPRESS_NONE);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400262}
263
Nikolay Borisovc1e09522020-06-03 08:55:30 +0300264int btrfs_add_ordered_extent_dio(struct btrfs_inode *inode, u64 file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800265 u64 disk_bytenr, u64 num_bytes,
266 u64 disk_num_bytes, int type)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400267{
Nikolay Borisovc1e09522020-06-03 08:55:30 +0300268 return __btrfs_add_ordered_extent(inode, file_offset, disk_bytenr,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800269 num_bytes, disk_num_bytes, type, 1,
Li Zefan261507a02010-12-17 14:21:50 +0800270 BTRFS_COMPRESS_NONE);
271}
272
Nikolay Borisov4cc61202020-06-03 08:55:15 +0300273int btrfs_add_ordered_extent_compress(struct btrfs_inode *inode, u64 file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800274 u64 disk_bytenr, u64 num_bytes,
275 u64 disk_num_bytes, int type,
276 int compress_type)
Li Zefan261507a02010-12-17 14:21:50 +0800277{
Nikolay Borisov4cc61202020-06-03 08:55:15 +0300278 return __btrfs_add_ordered_extent(inode, file_offset, disk_bytenr,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800279 num_bytes, disk_num_bytes, type, 0,
Li Zefan261507a02010-12-17 14:21:50 +0800280 compress_type);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400281}
282
Chris Masoneb84ae02008-07-17 13:53:27 -0400283/*
284 * Add a struct btrfs_ordered_sum into the list of checksums to be inserted
Chris Mason3edf7d32008-07-18 06:17:13 -0400285 * when an ordered extent is finished. If the list covers more than one
286 * ordered extent, it is split across multiples.
Chris Masoneb84ae02008-07-17 13:53:27 -0400287 */
Nikolay Borisovf9756262019-04-10 16:16:11 +0300288void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100289 struct btrfs_ordered_sum *sum)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400290{
291 struct btrfs_ordered_inode_tree *tree;
Chris Mason1b1e2132008-06-25 16:01:31 -0400292
Nikolay Borisovf9756262019-04-10 16:16:11 +0300293 tree = &BTRFS_I(entry->inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400294 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400295 list_add_tail(&sum->list, &entry->list);
Josef Bacik5fd02042012-05-02 14:00:54 -0400296 spin_unlock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400297}
298
Chris Masoneb84ae02008-07-17 13:53:27 -0400299/*
300 * this is used to account for finished IO across a given range
Chris Mason163cf092010-11-28 19:56:33 -0500301 * of the file. The IO may span ordered extents. If
302 * a given ordered_extent is completely done, 1 is returned, otherwise
303 * 0.
304 *
305 * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
306 * to make sure this function only returns 1 once for a given ordered extent.
307 *
308 * file_offset is updated to one byte past the range that is recorded as
309 * complete. This allows you to walk forward in the file.
310 */
Nikolay Borisov70958212020-06-03 08:55:23 +0300311int btrfs_dec_test_first_ordered_pending(struct btrfs_inode *inode,
Chris Mason163cf092010-11-28 19:56:33 -0500312 struct btrfs_ordered_extent **cached,
Josef Bacik5fd02042012-05-02 14:00:54 -0400313 u64 *file_offset, u64 io_size, int uptodate)
Chris Mason163cf092010-11-28 19:56:33 -0500314{
Nikolay Borisov70958212020-06-03 08:55:23 +0300315 struct btrfs_fs_info *fs_info = inode->root->fs_info;
316 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
Chris Mason163cf092010-11-28 19:56:33 -0500317 struct rb_node *node;
318 struct btrfs_ordered_extent *entry = NULL;
319 int ret;
Josef Bacik5fd02042012-05-02 14:00:54 -0400320 unsigned long flags;
Chris Mason163cf092010-11-28 19:56:33 -0500321 u64 dec_end;
322 u64 dec_start;
323 u64 to_dec;
324
Josef Bacik5fd02042012-05-02 14:00:54 -0400325 spin_lock_irqsave(&tree->lock, flags);
Chris Mason163cf092010-11-28 19:56:33 -0500326 node = tree_search(tree, *file_offset);
327 if (!node) {
328 ret = 1;
329 goto out;
330 }
331
332 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
333 if (!offset_in_entry(entry, *file_offset)) {
334 ret = 1;
335 goto out;
336 }
337
338 dec_start = max(*file_offset, entry->file_offset);
Omar Sandovalbffe6332019-12-02 17:34:19 -0800339 dec_end = min(*file_offset + io_size,
340 entry->file_offset + entry->num_bytes);
Chris Mason163cf092010-11-28 19:56:33 -0500341 *file_offset = dec_end;
342 if (dec_start > dec_end) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400343 btrfs_crit(fs_info, "bad ordering dec_start %llu end %llu",
344 dec_start, dec_end);
Chris Mason163cf092010-11-28 19:56:33 -0500345 }
346 to_dec = dec_end - dec_start;
347 if (to_dec > entry->bytes_left) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400348 btrfs_crit(fs_info,
349 "bad ordered accounting left %llu size %llu",
350 entry->bytes_left, to_dec);
Chris Mason163cf092010-11-28 19:56:33 -0500351 }
352 entry->bytes_left -= to_dec;
Josef Bacik5fd02042012-05-02 14:00:54 -0400353 if (!uptodate)
354 set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
355
Miao Xieaf7a6502014-03-06 13:54:56 +0800356 if (entry->bytes_left == 0) {
Chris Mason163cf092010-11-28 19:56:33 -0500357 ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
David Sterba093258e2018-02-26 16:15:17 +0100358 /* test_and_set_bit implies a barrier */
359 cond_wake_up_nomb(&entry->wait);
Miao Xieaf7a6502014-03-06 13:54:56 +0800360 } else {
Chris Mason163cf092010-11-28 19:56:33 -0500361 ret = 1;
Miao Xieaf7a6502014-03-06 13:54:56 +0800362 }
Chris Mason163cf092010-11-28 19:56:33 -0500363out:
364 if (!ret && cached && entry) {
365 *cached = entry;
Elena Reshetovae76edab2017-03-03 10:55:13 +0200366 refcount_inc(&entry->refs);
Chris Mason163cf092010-11-28 19:56:33 -0500367 }
Josef Bacik5fd02042012-05-02 14:00:54 -0400368 spin_unlock_irqrestore(&tree->lock, flags);
Chris Mason163cf092010-11-28 19:56:33 -0500369 return ret == 0;
370}
371
372/*
373 * this is used to account for finished IO across a given range
Chris Masoneb84ae02008-07-17 13:53:27 -0400374 * of the file. The IO should not span ordered extents. If
375 * a given ordered_extent is completely done, 1 is returned, otherwise
376 * 0.
377 *
378 * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
379 * to make sure this function only returns 1 once for a given ordered extent.
380 */
Nikolay Borisov90c03042020-08-31 14:42:41 +0300381int btrfs_dec_test_ordered_pending(struct btrfs_inode *inode,
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000382 struct btrfs_ordered_extent **cached,
Josef Bacik5fd02042012-05-02 14:00:54 -0400383 u64 file_offset, u64 io_size, int uptodate)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400384{
Nikolay Borisov90c03042020-08-31 14:42:41 +0300385 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400386 struct rb_node *node;
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000387 struct btrfs_ordered_extent *entry = NULL;
Josef Bacik5fd02042012-05-02 14:00:54 -0400388 unsigned long flags;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400389 int ret;
390
Josef Bacik5fd02042012-05-02 14:00:54 -0400391 spin_lock_irqsave(&tree->lock, flags);
392 if (cached && *cached) {
393 entry = *cached;
394 goto have_entry;
395 }
396
Chris Masone6dcd2d2008-07-17 12:53:50 -0400397 node = tree_search(tree, file_offset);
398 if (!node) {
399 ret = 1;
400 goto out;
401 }
402
403 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
Josef Bacik5fd02042012-05-02 14:00:54 -0400404have_entry:
Chris Masone6dcd2d2008-07-17 12:53:50 -0400405 if (!offset_in_entry(entry, file_offset)) {
406 ret = 1;
407 goto out;
408 }
409
Chris Mason8b62b722009-09-02 16:53:46 -0400410 if (io_size > entry->bytes_left) {
Nikolay Borisov90c03042020-08-31 14:42:41 +0300411 btrfs_crit(inode->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -0500412 "bad ordered accounting left %llu size %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200413 entry->bytes_left, io_size);
Chris Mason8b62b722009-09-02 16:53:46 -0400414 }
415 entry->bytes_left -= io_size;
Josef Bacik5fd02042012-05-02 14:00:54 -0400416 if (!uptodate)
417 set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
418
Miao Xieaf7a6502014-03-06 13:54:56 +0800419 if (entry->bytes_left == 0) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400420 ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
David Sterba093258e2018-02-26 16:15:17 +0100421 /* test_and_set_bit implies a barrier */
422 cond_wake_up_nomb(&entry->wait);
Miao Xieaf7a6502014-03-06 13:54:56 +0800423 } else {
Chris Mason8b62b722009-09-02 16:53:46 -0400424 ret = 1;
Miao Xieaf7a6502014-03-06 13:54:56 +0800425 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400426out:
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000427 if (!ret && cached && entry) {
428 *cached = entry;
Elena Reshetovae76edab2017-03-03 10:55:13 +0200429 refcount_inc(&entry->refs);
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000430 }
Josef Bacik5fd02042012-05-02 14:00:54 -0400431 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400432 return ret == 0;
433}
434
Chris Masoneb84ae02008-07-17 13:53:27 -0400435/*
436 * used to drop a reference on an ordered extent. This will free
437 * the extent if the last reference is dropped
438 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100439void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400440{
Chris Masonba1da2f2008-07-17 12:54:15 -0400441 struct list_head *cur;
442 struct btrfs_ordered_sum *sum;
443
Nikolay Borisovacbf1dd2020-08-31 14:42:40 +0300444 trace_btrfs_ordered_extent_put(BTRFS_I(entry->inode), entry);
liubo1abe9b82011-03-24 11:18:59 +0000445
Elena Reshetovae76edab2017-03-03 10:55:13 +0200446 if (refcount_dec_and_test(&entry->refs)) {
Filipe Manana61de7182015-07-01 12:13:10 +0100447 ASSERT(list_empty(&entry->root_extent_list));
Filipe Manana48778172020-08-11 12:43:58 +0100448 ASSERT(list_empty(&entry->log_list));
Filipe Manana61de7182015-07-01 12:13:10 +0100449 ASSERT(RB_EMPTY_NODE(&entry->rb_node));
Josef Bacik5fd02042012-05-02 14:00:54 -0400450 if (entry->inode)
451 btrfs_add_delayed_iput(entry->inode);
Chris Masond3977122009-01-05 21:25:51 -0500452 while (!list_empty(&entry->list)) {
Chris Masonba1da2f2008-07-17 12:54:15 -0400453 cur = entry->list.next;
454 sum = list_entry(cur, struct btrfs_ordered_sum, list);
455 list_del(&sum->list);
Nikolay Borisova3d46ae2019-04-01 11:29:58 +0300456 kvfree(sum);
Chris Masonba1da2f2008-07-17 12:54:15 -0400457 }
Miao Xie6352b912012-09-06 04:01:51 -0600458 kmem_cache_free(btrfs_ordered_extent_cache, entry);
Chris Masonba1da2f2008-07-17 12:54:15 -0400459 }
Chris Masondc17ff82008-01-08 15:46:30 -0500460}
461
Chris Masoneb84ae02008-07-17 13:53:27 -0400462/*
463 * remove an ordered extent from the tree. No references are dropped
Josef Bacik5fd02042012-05-02 14:00:54 -0400464 * and waiters are woken up.
Chris Masoneb84ae02008-07-17 13:53:27 -0400465 */
Josef Bacik5fd02042012-05-02 14:00:54 -0400466void btrfs_remove_ordered_extent(struct inode *inode,
467 struct btrfs_ordered_extent *entry)
Chris Masondc17ff82008-01-08 15:46:30 -0500468{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400469 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400470 struct btrfs_ordered_inode_tree *tree;
Josef Bacik8b62f872017-10-19 14:15:55 -0400471 struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
472 struct btrfs_root *root = btrfs_inode->root;
Chris Masondc17ff82008-01-08 15:46:30 -0500473 struct rb_node *node;
Filipe Manana48778172020-08-11 12:43:58 +0100474 bool pending;
Chris Masondc17ff82008-01-08 15:46:30 -0500475
Josef Bacik8b62f872017-10-19 14:15:55 -0400476 /* This is paired with btrfs_add_ordered_extent. */
477 spin_lock(&btrfs_inode->lock);
478 btrfs_mod_outstanding_extents(btrfs_inode, -1);
479 spin_unlock(&btrfs_inode->lock);
480 if (root != fs_info->tree_root)
Omar Sandovalbffe6332019-12-02 17:34:19 -0800481 btrfs_delalloc_release_metadata(btrfs_inode, entry->num_bytes,
482 false);
Josef Bacik8b62f872017-10-19 14:15:55 -0400483
Josef Bacik4297ff82019-04-10 15:56:09 -0400484 if (test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
Omar Sandovalbffe6332019-12-02 17:34:19 -0800485 percpu_counter_add_batch(&fs_info->dio_bytes, -entry->num_bytes,
Josef Bacik4297ff82019-04-10 15:56:09 -0400486 fs_info->delalloc_batch);
487
Josef Bacik8b62f872017-10-19 14:15:55 -0400488 tree = &btrfs_inode->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400489 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400490 node = &entry->rb_node;
Chris Masondc17ff82008-01-08 15:46:30 -0500491 rb_erase(node, &tree->tree);
Filipe Manana61de7182015-07-01 12:13:10 +0100492 RB_CLEAR_NODE(node);
Filipe David Borba Manana1b8e7e42013-11-22 18:54:58 +0000493 if (tree->last == node)
494 tree->last = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400495 set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
Filipe Manana48778172020-08-11 12:43:58 +0100496 pending = test_and_clear_bit(BTRFS_ORDERED_PENDING, &entry->flags);
Josef Bacik5fd02042012-05-02 14:00:54 -0400497 spin_unlock_irq(&tree->lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400498
Filipe Manana48778172020-08-11 12:43:58 +0100499 /*
500 * The current running transaction is waiting on us, we need to let it
501 * know that we're complete and wake it up.
502 */
503 if (pending) {
504 struct btrfs_transaction *trans;
505
506 /*
507 * The checks for trans are just a formality, it should be set,
508 * but if it isn't we don't want to deref/assert under the spin
509 * lock, so be nice and check if trans is set, but ASSERT() so
510 * if it isn't set a developer will notice.
511 */
512 spin_lock(&fs_info->trans_lock);
513 trans = fs_info->running_transaction;
514 if (trans)
515 refcount_inc(&trans->use_count);
516 spin_unlock(&fs_info->trans_lock);
517
518 ASSERT(trans);
519 if (trans) {
520 if (atomic_dec_and_test(&trans->pending_ordered))
521 wake_up(&trans->pending_wait);
522 btrfs_put_transaction(trans);
523 }
524 }
525
Miao Xie199c2a92013-05-15 07:48:23 +0000526 spin_lock(&root->ordered_extent_lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400527 list_del_init(&entry->root_extent_list);
Miao Xie199c2a92013-05-15 07:48:23 +0000528 root->nr_ordered_extents--;
Chris Mason5a3f23d2009-03-31 13:27:11 -0400529
Nikolay Borisovacbf1dd2020-08-31 14:42:40 +0300530 trace_btrfs_ordered_extent_remove(BTRFS_I(inode), entry);
liubo1abe9b82011-03-24 11:18:59 +0000531
Miao Xie199c2a92013-05-15 07:48:23 +0000532 if (!root->nr_ordered_extents) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400533 spin_lock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +0000534 BUG_ON(list_empty(&root->ordered_root));
535 list_del_init(&root->ordered_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400536 spin_unlock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +0000537 }
538 spin_unlock(&root->ordered_extent_lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400539 wake_up(&entry->wait);
Chris Mason81d7ed22008-04-25 08:51:48 -0400540}
Chris Masone6dcd2d2008-07-17 12:53:50 -0400541
Qu Wenruod458b052014-02-28 10:46:19 +0800542static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
Miao Xie9afab882012-10-25 09:41:36 +0000543{
544 struct btrfs_ordered_extent *ordered;
545
546 ordered = container_of(work, struct btrfs_ordered_extent, flush_work);
547 btrfs_start_ordered_extent(ordered->inode, ordered, 1);
548 complete(&ordered->completion);
549}
550
Chris Masond352ac62008-09-29 15:18:18 -0400551/*
552 * wait for all the ordered extents in a root. This is done when balancing
553 * space between drives.
554 */
Chris Mason6374e57a2017-06-23 09:48:21 -0700555u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr,
Filipe Manana578def72016-04-26 15:36:38 +0100556 const u64 range_start, const u64 range_len)
Chris Mason3eaa2882008-07-24 11:57:52 -0400557{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400558 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana578def72016-04-26 15:36:38 +0100559 LIST_HEAD(splice);
560 LIST_HEAD(skipped);
561 LIST_HEAD(works);
Miao Xie9afab882012-10-25 09:41:36 +0000562 struct btrfs_ordered_extent *ordered, *next;
Chris Mason6374e57a2017-06-23 09:48:21 -0700563 u64 count = 0;
Filipe Manana578def72016-04-26 15:36:38 +0100564 const u64 range_end = range_start + range_len;
Chris Mason3eaa2882008-07-24 11:57:52 -0400565
Miao Xie31f3d252014-03-06 13:55:02 +0800566 mutex_lock(&root->ordered_extent_mutex);
Miao Xie199c2a92013-05-15 07:48:23 +0000567 spin_lock(&root->ordered_extent_lock);
568 list_splice_init(&root->ordered_extents, &splice);
Miao Xieb0244192013-11-04 23:13:25 +0800569 while (!list_empty(&splice) && nr) {
Miao Xie199c2a92013-05-15 07:48:23 +0000570 ordered = list_first_entry(&splice, struct btrfs_ordered_extent,
571 root_extent_list);
Filipe Manana578def72016-04-26 15:36:38 +0100572
Omar Sandovalbffe6332019-12-02 17:34:19 -0800573 if (range_end <= ordered->disk_bytenr ||
574 ordered->disk_bytenr + ordered->disk_num_bytes <= range_start) {
Filipe Manana578def72016-04-26 15:36:38 +0100575 list_move_tail(&ordered->root_extent_list, &skipped);
576 cond_resched_lock(&root->ordered_extent_lock);
577 continue;
578 }
579
Miao Xie199c2a92013-05-15 07:48:23 +0000580 list_move_tail(&ordered->root_extent_list,
581 &root->ordered_extents);
Elena Reshetovae76edab2017-03-03 10:55:13 +0200582 refcount_inc(&ordered->refs);
Miao Xie199c2a92013-05-15 07:48:23 +0000583 spin_unlock(&root->ordered_extent_lock);
584
Qu Wenruoa44903a2014-02-28 10:46:09 +0800585 btrfs_init_work(&ordered->flush_work,
586 btrfs_run_ordered_extent_work, NULL, NULL);
Miao Xie199c2a92013-05-15 07:48:23 +0000587 list_add_tail(&ordered->work_list, &works);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400588 btrfs_queue_work(fs_info->flush_workers, &ordered->flush_work);
Miao Xie199c2a92013-05-15 07:48:23 +0000589
Miao Xie9afab882012-10-25 09:41:36 +0000590 cond_resched();
Miao Xie199c2a92013-05-15 07:48:23 +0000591 spin_lock(&root->ordered_extent_lock);
Chris Mason6374e57a2017-06-23 09:48:21 -0700592 if (nr != U64_MAX)
Miao Xieb0244192013-11-04 23:13:25 +0800593 nr--;
594 count++;
Chris Mason3eaa2882008-07-24 11:57:52 -0400595 }
Filipe Manana578def72016-04-26 15:36:38 +0100596 list_splice_tail(&skipped, &root->ordered_extents);
Miao Xieb0244192013-11-04 23:13:25 +0800597 list_splice_tail(&splice, &root->ordered_extents);
Miao Xie199c2a92013-05-15 07:48:23 +0000598 spin_unlock(&root->ordered_extent_lock);
Miao Xie9afab882012-10-25 09:41:36 +0000599
600 list_for_each_entry_safe(ordered, next, &works, work_list) {
601 list_del_init(&ordered->work_list);
602 wait_for_completion(&ordered->completion);
Miao Xie9afab882012-10-25 09:41:36 +0000603 btrfs_put_ordered_extent(ordered);
Miao Xie9afab882012-10-25 09:41:36 +0000604 cond_resched();
605 }
Miao Xie31f3d252014-03-06 13:55:02 +0800606 mutex_unlock(&root->ordered_extent_mutex);
Miao Xieb0244192013-11-04 23:13:25 +0800607
608 return count;
Chris Mason3eaa2882008-07-24 11:57:52 -0400609}
610
Filipe Manana042528f2019-11-14 18:02:43 +0000611void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr,
Chris Mason6374e57a2017-06-23 09:48:21 -0700612 const u64 range_start, const u64 range_len)
Miao Xie199c2a92013-05-15 07:48:23 +0000613{
614 struct btrfs_root *root;
615 struct list_head splice;
Chris Mason6374e57a2017-06-23 09:48:21 -0700616 u64 done;
Miao Xie199c2a92013-05-15 07:48:23 +0000617
618 INIT_LIST_HEAD(&splice);
619
Miao Xie8b9d83c2014-03-06 13:54:55 +0800620 mutex_lock(&fs_info->ordered_operations_mutex);
Miao Xie199c2a92013-05-15 07:48:23 +0000621 spin_lock(&fs_info->ordered_root_lock);
622 list_splice_init(&fs_info->ordered_roots, &splice);
Miao Xieb0244192013-11-04 23:13:25 +0800623 while (!list_empty(&splice) && nr) {
Miao Xie199c2a92013-05-15 07:48:23 +0000624 root = list_first_entry(&splice, struct btrfs_root,
625 ordered_root);
Josef Bacik00246522020-01-24 09:33:01 -0500626 root = btrfs_grab_root(root);
Miao Xie199c2a92013-05-15 07:48:23 +0000627 BUG_ON(!root);
628 list_move_tail(&root->ordered_root,
629 &fs_info->ordered_roots);
630 spin_unlock(&fs_info->ordered_root_lock);
631
Filipe Manana578def72016-04-26 15:36:38 +0100632 done = btrfs_wait_ordered_extents(root, nr,
633 range_start, range_len);
Josef Bacik00246522020-01-24 09:33:01 -0500634 btrfs_put_root(root);
Miao Xie199c2a92013-05-15 07:48:23 +0000635
636 spin_lock(&fs_info->ordered_root_lock);
Chris Mason6374e57a2017-06-23 09:48:21 -0700637 if (nr != U64_MAX) {
Miao Xieb0244192013-11-04 23:13:25 +0800638 nr -= done;
Miao Xieb0244192013-11-04 23:13:25 +0800639 }
Miao Xie199c2a92013-05-15 07:48:23 +0000640 }
Miao Xie931aa872013-11-14 17:33:21 +0800641 list_splice_tail(&splice, &fs_info->ordered_roots);
Miao Xie199c2a92013-05-15 07:48:23 +0000642 spin_unlock(&fs_info->ordered_root_lock);
Miao Xie8b9d83c2014-03-06 13:54:55 +0800643 mutex_unlock(&fs_info->ordered_operations_mutex);
Miao Xie199c2a92013-05-15 07:48:23 +0000644}
645
Chris Masoneb84ae02008-07-17 13:53:27 -0400646/*
647 * Used to start IO or wait for a given ordered extent to finish.
648 *
649 * If wait is one, this effectively waits on page writeback for all the pages
650 * in the extent, and it waits on the io completion code to insert
651 * metadata into the btree corresponding to the extent
652 */
653void btrfs_start_ordered_extent(struct inode *inode,
654 struct btrfs_ordered_extent *entry,
655 int wait)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400656{
657 u64 start = entry->file_offset;
Omar Sandovalbffe6332019-12-02 17:34:19 -0800658 u64 end = start + entry->num_bytes - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400659
Nikolay Borisovacbf1dd2020-08-31 14:42:40 +0300660 trace_btrfs_ordered_extent_start(BTRFS_I(inode), entry);
liubo1abe9b82011-03-24 11:18:59 +0000661
Chris Masoneb84ae02008-07-17 13:53:27 -0400662 /*
663 * pages in the range can be dirty, clean or writeback. We
664 * start IO on any dirty ones so the wait doesn't stall waiting
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300665 * for the flusher thread to find them
Chris Masoneb84ae02008-07-17 13:53:27 -0400666 */
Josef Bacik4b46fce2010-05-23 11:00:55 -0400667 if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
668 filemap_fdatawrite_range(inode->i_mapping, start, end);
Chris Masonc8b97812008-10-29 14:49:59 -0400669 if (wait) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400670 wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
671 &entry->flags));
Chris Masonc8b97812008-10-29 14:49:59 -0400672 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400673}
674
Chris Masoneb84ae02008-07-17 13:53:27 -0400675/*
676 * Used to wait on ordered extents across a large range of bytes.
677 */
Josef Bacik0ef8b722013-10-25 16:13:35 -0400678int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400679{
Josef Bacik0ef8b722013-10-25 16:13:35 -0400680 int ret = 0;
Filipe Manana28aeeac2015-05-05 19:03:10 +0100681 int ret_wb = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400682 u64 end;
Chris Masone5a22172008-07-18 20:42:20 -0400683 u64 orig_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400684 struct btrfs_ordered_extent *ordered;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400685
Chris Masone5a22172008-07-18 20:42:20 -0400686 if (start + len < start) {
Chris Masonf4219502008-07-22 11:18:09 -0400687 orig_end = INT_LIMIT(loff_t);
Chris Masone5a22172008-07-18 20:42:20 -0400688 } else {
689 orig_end = start + len - 1;
Chris Masonf4219502008-07-22 11:18:09 -0400690 if (orig_end > INT_LIMIT(loff_t))
691 orig_end = INT_LIMIT(loff_t);
Chris Masone5a22172008-07-18 20:42:20 -0400692 }
Josef Bacik551ebb22012-04-23 14:41:09 -0400693
Chris Masone5a22172008-07-18 20:42:20 -0400694 /* start IO across the range first to instantiate any delalloc
695 * extents
696 */
Filipe Manana728404d2014-10-10 09:43:11 +0100697 ret = btrfs_fdatawrite_range(inode, start, orig_end);
Josef Bacik0ef8b722013-10-25 16:13:35 -0400698 if (ret)
699 return ret;
Filipe Manana728404d2014-10-10 09:43:11 +0100700
Filipe Manana28aeeac2015-05-05 19:03:10 +0100701 /*
702 * If we have a writeback error don't return immediately. Wait first
703 * for any ordered extents that haven't completed yet. This is to make
704 * sure no one can dirty the same page ranges and call writepages()
705 * before the ordered extents complete - to avoid failures (-EEXIST)
706 * when adding the new ordered extents to the ordered tree.
707 */
708 ret_wb = filemap_fdatawait_range(inode->i_mapping, start, orig_end);
Chris Masonf4219502008-07-22 11:18:09 -0400709
710 end = orig_end;
Chris Masond3977122009-01-05 21:25:51 -0500711 while (1) {
Nikolay Borisov6d072c82020-08-31 14:42:39 +0300712 ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode), end);
Chris Masond3977122009-01-05 21:25:51 -0500713 if (!ordered)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400714 break;
Chris Masone5a22172008-07-18 20:42:20 -0400715 if (ordered->file_offset > orig_end) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400716 btrfs_put_ordered_extent(ordered);
717 break;
718 }
Omar Sandovalbffe6332019-12-02 17:34:19 -0800719 if (ordered->file_offset + ordered->num_bytes <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400720 btrfs_put_ordered_extent(ordered);
721 break;
722 }
Chris Masone5a22172008-07-18 20:42:20 -0400723 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400724 end = ordered->file_offset;
Filipe Mananae75fd332020-02-13 12:29:50 +0000725 /*
726 * If the ordered extent had an error save the error but don't
727 * exit without waiting first for all other ordered extents in
728 * the range to complete.
729 */
Josef Bacik0ef8b722013-10-25 16:13:35 -0400730 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags))
731 ret = -EIO;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400732 btrfs_put_ordered_extent(ordered);
Filipe Mananae75fd332020-02-13 12:29:50 +0000733 if (end == 0 || end == start)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400734 break;
735 end--;
736 }
Filipe Manana28aeeac2015-05-05 19:03:10 +0100737 return ret_wb ? ret_wb : ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400738}
739
Chris Masoneb84ae02008-07-17 13:53:27 -0400740/*
741 * find an ordered extent corresponding to file_offset. return NULL if
742 * nothing is found, otherwise take a reference on the extent and return it
743 */
Nikolay Borisovc3504372020-06-03 08:55:03 +0300744struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400745 u64 file_offset)
746{
747 struct btrfs_ordered_inode_tree *tree;
748 struct rb_node *node;
749 struct btrfs_ordered_extent *entry = NULL;
750
Nikolay Borisovc3504372020-06-03 08:55:03 +0300751 tree = &inode->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400752 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400753 node = tree_search(tree, file_offset);
754 if (!node)
755 goto out;
756
757 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
758 if (!offset_in_entry(entry, file_offset))
759 entry = NULL;
760 if (entry)
Elena Reshetovae76edab2017-03-03 10:55:13 +0200761 refcount_inc(&entry->refs);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400762out:
Josef Bacik5fd02042012-05-02 14:00:54 -0400763 spin_unlock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400764 return entry;
765}
766
Josef Bacik4b46fce2010-05-23 11:00:55 -0400767/* Since the DIO code tries to lock a wide area we need to look for any ordered
768 * extents that exist in the range, rather than just the start of the range.
769 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +0200770struct btrfs_ordered_extent *btrfs_lookup_ordered_range(
771 struct btrfs_inode *inode, u64 file_offset, u64 len)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400772{
773 struct btrfs_ordered_inode_tree *tree;
774 struct rb_node *node;
775 struct btrfs_ordered_extent *entry = NULL;
776
Nikolay Borisova776c6f2017-02-20 13:50:49 +0200777 tree = &inode->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400778 spin_lock_irq(&tree->lock);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400779 node = tree_search(tree, file_offset);
780 if (!node) {
781 node = tree_search(tree, file_offset + len);
782 if (!node)
783 goto out;
784 }
785
786 while (1) {
787 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
788 if (range_overlaps(entry, file_offset, len))
789 break;
790
791 if (entry->file_offset >= file_offset + len) {
792 entry = NULL;
793 break;
794 }
795 entry = NULL;
796 node = rb_next(node);
797 if (!node)
798 break;
799 }
800out:
801 if (entry)
Elena Reshetovae76edab2017-03-03 10:55:13 +0200802 refcount_inc(&entry->refs);
Josef Bacik5fd02042012-05-02 14:00:54 -0400803 spin_unlock_irq(&tree->lock);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400804 return entry;
805}
806
Chris Masoneb84ae02008-07-17 13:53:27 -0400807/*
Filipe Manana48778172020-08-11 12:43:58 +0100808 * Adds all ordered extents to the given list. The list ends up sorted by the
809 * file_offset of the ordered extents.
810 */
811void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode,
812 struct list_head *list)
813{
814 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
815 struct rb_node *n;
816
817 ASSERT(inode_is_locked(&inode->vfs_inode));
818
819 spin_lock_irq(&tree->lock);
820 for (n = rb_first(&tree->tree); n; n = rb_next(n)) {
821 struct btrfs_ordered_extent *ordered;
822
823 ordered = rb_entry(n, struct btrfs_ordered_extent, rb_node);
824
825 if (test_bit(BTRFS_ORDERED_LOGGED, &ordered->flags))
826 continue;
827
828 ASSERT(list_empty(&ordered->log_list));
829 list_add_tail(&ordered->log_list, list);
830 refcount_inc(&ordered->refs);
831 }
832 spin_unlock_irq(&tree->lock);
833}
834
835/*
Chris Masoneb84ae02008-07-17 13:53:27 -0400836 * lookup and return any extent before 'file_offset'. NULL is returned
837 * if none is found
838 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400839struct btrfs_ordered_extent *
Nikolay Borisov6d072c82020-08-31 14:42:39 +0300840btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400841{
842 struct btrfs_ordered_inode_tree *tree;
843 struct rb_node *node;
844 struct btrfs_ordered_extent *entry = NULL;
845
Nikolay Borisov6d072c82020-08-31 14:42:39 +0300846 tree = &inode->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400847 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400848 node = tree_search(tree, file_offset);
849 if (!node)
850 goto out;
851
852 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
Elena Reshetovae76edab2017-03-03 10:55:13 +0200853 refcount_inc(&entry->refs);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400854out:
Josef Bacik5fd02042012-05-02 14:00:54 -0400855 spin_unlock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400856 return entry;
857}
Chris Masondbe674a2008-07-17 12:54:05 -0400858
Chris Masoneb84ae02008-07-17 13:53:27 -0400859/*
Chris Masoneb84ae02008-07-17 13:53:27 -0400860 * search the ordered extents for one corresponding to 'offset' and
861 * try to find a checksum. This is used because we allow pages to
862 * be reclaimed before their checksum is actually put into the btree
863 */
Chris Masond20f7042008-12-08 16:58:54 -0500864int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
Johannes Thumshirn1e25a2e2019-05-22 10:19:01 +0200865 u8 *sum, int len)
Chris Masonba1da2f2008-07-17 12:54:15 -0400866{
Johannes Thumshirn1e25a2e2019-05-22 10:19:01 +0200867 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonba1da2f2008-07-17 12:54:15 -0400868 struct btrfs_ordered_sum *ordered_sum;
Chris Masonba1da2f2008-07-17 12:54:15 -0400869 struct btrfs_ordered_extent *ordered;
870 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
Chris Mason3edf7d32008-07-18 06:17:13 -0400871 unsigned long num_sectors;
872 unsigned long i;
Jeff Mahoneyda170662016-06-15 09:22:56 -0400873 u32 sectorsize = btrfs_inode_sectorsize(inode);
Johannes Thumshirn1e25a2e2019-05-22 10:19:01 +0200874 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
Miao Xiee4100d92013-04-05 07:20:56 +0000875 int index = 0;
Chris Masonba1da2f2008-07-17 12:54:15 -0400876
Nikolay Borisovc3504372020-06-03 08:55:03 +0300877 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), offset);
Chris Masonba1da2f2008-07-17 12:54:15 -0400878 if (!ordered)
Miao Xiee4100d92013-04-05 07:20:56 +0000879 return 0;
Chris Masonba1da2f2008-07-17 12:54:15 -0400880
Josef Bacik5fd02042012-05-02 14:00:54 -0400881 spin_lock_irq(&tree->lock);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500882 list_for_each_entry_reverse(ordered_sum, &ordered->list, list) {
Miao Xiee4100d92013-04-05 07:20:56 +0000883 if (disk_bytenr >= ordered_sum->bytenr &&
884 disk_bytenr < ordered_sum->bytenr + ordered_sum->len) {
885 i = (disk_bytenr - ordered_sum->bytenr) >>
886 inode->i_sb->s_blocksize_bits;
Miao Xiee4100d92013-04-05 07:20:56 +0000887 num_sectors = ordered_sum->len >>
888 inode->i_sb->s_blocksize_bits;
Miao Xief51a4a12013-06-19 10:36:09 +0800889 num_sectors = min_t(int, len - index, num_sectors - i);
Johannes Thumshirn1e25a2e2019-05-22 10:19:01 +0200890 memcpy(sum + index, ordered_sum->sums + i * csum_size,
891 num_sectors * csum_size);
Miao Xief51a4a12013-06-19 10:36:09 +0800892
Johannes Thumshirn1e25a2e2019-05-22 10:19:01 +0200893 index += (int)num_sectors * csum_size;
Miao Xief51a4a12013-06-19 10:36:09 +0800894 if (index == len)
895 goto out;
896 disk_bytenr += num_sectors * sectorsize;
Chris Masonba1da2f2008-07-17 12:54:15 -0400897 }
898 }
899out:
Josef Bacik5fd02042012-05-02 14:00:54 -0400900 spin_unlock_irq(&tree->lock);
Chris Mason89642222008-07-24 09:41:53 -0400901 btrfs_put_ordered_extent(ordered);
Miao Xiee4100d92013-04-05 07:20:56 +0000902 return index;
Chris Masonba1da2f2008-07-17 12:54:15 -0400903}
904
Nikolay Borisovffa87212019-05-07 10:19:22 +0300905/*
906 * btrfs_flush_ordered_range - Lock the passed range and ensures all pending
907 * ordered extents in it are run to completion.
908 *
Nikolay Borisovffa87212019-05-07 10:19:22 +0300909 * @inode: Inode whose ordered tree is to be searched
910 * @start: Beginning of range to flush
911 * @end: Last byte of range to lock
912 * @cached_state: If passed, will return the extent state responsible for the
913 * locked range. It's the caller's responsibility to free the cached state.
914 *
915 * This function always returns with the given range locked, ensuring after it's
916 * called no order extent can be pending.
917 */
David Sterbab272ae22020-02-05 19:09:33 +0100918void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,
Nikolay Borisovffa87212019-05-07 10:19:22 +0300919 u64 end,
920 struct extent_state **cached_state)
921{
922 struct btrfs_ordered_extent *ordered;
Naohiro Aotaa3b46b82019-07-26 16:47:05 +0900923 struct extent_state *cache = NULL;
924 struct extent_state **cachedp = &cache;
Nikolay Borisovbd80d942019-05-07 10:19:24 +0300925
926 if (cached_state)
Naohiro Aotaa3b46b82019-07-26 16:47:05 +0900927 cachedp = cached_state;
Nikolay Borisovffa87212019-05-07 10:19:22 +0300928
929 while (1) {
David Sterbab272ae22020-02-05 19:09:33 +0100930 lock_extent_bits(&inode->io_tree, start, end, cachedp);
Nikolay Borisovffa87212019-05-07 10:19:22 +0300931 ordered = btrfs_lookup_ordered_range(inode, start,
932 end - start + 1);
Nikolay Borisovbd80d942019-05-07 10:19:24 +0300933 if (!ordered) {
934 /*
935 * If no external cached_state has been passed then
936 * decrement the extra ref taken for cachedp since we
937 * aren't exposing it outside of this function
938 */
939 if (!cached_state)
Naohiro Aotaa3b46b82019-07-26 16:47:05 +0900940 refcount_dec(&cache->refs);
Nikolay Borisovffa87212019-05-07 10:19:22 +0300941 break;
Nikolay Borisovbd80d942019-05-07 10:19:24 +0300942 }
David Sterbab272ae22020-02-05 19:09:33 +0100943 unlock_extent_cached(&inode->io_tree, start, end, cachedp);
Nikolay Borisovffa87212019-05-07 10:19:22 +0300944 btrfs_start_ordered_extent(&inode->vfs_inode, ordered, 1);
945 btrfs_put_ordered_extent(ordered);
946 }
947}
948
Miao Xie6352b912012-09-06 04:01:51 -0600949int __init ordered_data_init(void)
950{
951 btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent",
952 sizeof(struct btrfs_ordered_extent), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300953 SLAB_MEM_SPREAD,
Miao Xie6352b912012-09-06 04:01:51 -0600954 NULL);
955 if (!btrfs_ordered_extent_cache)
956 return -ENOMEM;
Miao Xie25287e02012-10-25 09:31:03 +0000957
Miao Xie6352b912012-09-06 04:01:51 -0600958 return 0;
959}
960
David Sterbae67c7182018-02-19 17:24:18 +0100961void __cold ordered_data_exit(void)
Miao Xie6352b912012-09-06 04:01:51 -0600962{
Kinglong Mee5598e902016-01-29 21:36:35 +0800963 kmem_cache_destroy(btrfs_ordered_extent_cache);
Miao Xie6352b912012-09-06 04:01:51 -0600964}