blob: e13b3d28c063b4f3bf015dc8c5a2a4abcdf044bd [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"
Chris Masondc17ff82008-01-08 15:46:30 -050018
Miao Xie6352b912012-09-06 04:01:51 -060019static struct kmem_cache *btrfs_ordered_extent_cache;
20
Chris Masone6dcd2d2008-07-17 12:53:50 -040021static u64 entry_end(struct btrfs_ordered_extent *entry)
Chris Masondc17ff82008-01-08 15:46:30 -050022{
Omar Sandovalbffe6332019-12-02 17:34:19 -080023 if (entry->file_offset + entry->num_bytes < entry->file_offset)
Chris Masone6dcd2d2008-07-17 12:53:50 -040024 return (u64)-1;
Omar Sandovalbffe6332019-12-02 17:34:19 -080025 return entry->file_offset + entry->num_bytes;
Chris Masondc17ff82008-01-08 15:46:30 -050026}
27
Chris Masond352ac62008-09-29 15:18:18 -040028/* returns NULL if the insertion worked, or it returns the node it did find
29 * in the tree
30 */
Chris Masone6dcd2d2008-07-17 12:53:50 -040031static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset,
32 struct rb_node *node)
Chris Masondc17ff82008-01-08 15:46:30 -050033{
Chris Masond3977122009-01-05 21:25:51 -050034 struct rb_node **p = &root->rb_node;
35 struct rb_node *parent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -040036 struct btrfs_ordered_extent *entry;
Chris Masondc17ff82008-01-08 15:46:30 -050037
Chris Masond3977122009-01-05 21:25:51 -050038 while (*p) {
Chris Masondc17ff82008-01-08 15:46:30 -050039 parent = *p;
Chris Masone6dcd2d2008-07-17 12:53:50 -040040 entry = rb_entry(parent, struct btrfs_ordered_extent, rb_node);
Chris Masondc17ff82008-01-08 15:46:30 -050041
Chris Masone6dcd2d2008-07-17 12:53:50 -040042 if (file_offset < entry->file_offset)
Chris Masondc17ff82008-01-08 15:46:30 -050043 p = &(*p)->rb_left;
Chris Masone6dcd2d2008-07-17 12:53:50 -040044 else if (file_offset >= entry_end(entry))
Chris Masondc17ff82008-01-08 15:46:30 -050045 p = &(*p)->rb_right;
46 else
47 return parent;
48 }
49
50 rb_link_node(node, parent, p);
51 rb_insert_color(node, root);
52 return NULL;
53}
54
Chris Masond352ac62008-09-29 15:18:18 -040055/*
56 * look for a given offset in the tree, and if it can't be found return the
57 * first lesser offset
58 */
Chris Masone6dcd2d2008-07-17 12:53:50 -040059static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset,
60 struct rb_node **prev_ret)
Chris Masondc17ff82008-01-08 15:46:30 -050061{
Chris Masond3977122009-01-05 21:25:51 -050062 struct rb_node *n = root->rb_node;
Chris Masondc17ff82008-01-08 15:46:30 -050063 struct rb_node *prev = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -040064 struct rb_node *test;
65 struct btrfs_ordered_extent *entry;
66 struct btrfs_ordered_extent *prev_entry = NULL;
Chris Masondc17ff82008-01-08 15:46:30 -050067
Chris Masond3977122009-01-05 21:25:51 -050068 while (n) {
Chris Masone6dcd2d2008-07-17 12:53:50 -040069 entry = rb_entry(n, struct btrfs_ordered_extent, rb_node);
Chris Masondc17ff82008-01-08 15:46:30 -050070 prev = n;
71 prev_entry = entry;
Chris Masondc17ff82008-01-08 15:46:30 -050072
Chris Masone6dcd2d2008-07-17 12:53:50 -040073 if (file_offset < entry->file_offset)
Chris Masondc17ff82008-01-08 15:46:30 -050074 n = n->rb_left;
Chris Masone6dcd2d2008-07-17 12:53:50 -040075 else if (file_offset >= entry_end(entry))
Chris Masondc17ff82008-01-08 15:46:30 -050076 n = n->rb_right;
77 else
78 return n;
79 }
80 if (!prev_ret)
81 return NULL;
82
Chris Masond3977122009-01-05 21:25:51 -050083 while (prev && file_offset >= entry_end(prev_entry)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -040084 test = rb_next(prev);
85 if (!test)
86 break;
87 prev_entry = rb_entry(test, struct btrfs_ordered_extent,
88 rb_node);
89 if (file_offset < entry_end(prev_entry))
90 break;
91
92 prev = test;
93 }
94 if (prev)
95 prev_entry = rb_entry(prev, struct btrfs_ordered_extent,
96 rb_node);
Chris Masond3977122009-01-05 21:25:51 -050097 while (prev && file_offset < entry_end(prev_entry)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -040098 test = rb_prev(prev);
99 if (!test)
100 break;
101 prev_entry = rb_entry(test, struct btrfs_ordered_extent,
102 rb_node);
103 prev = test;
Chris Masondc17ff82008-01-08 15:46:30 -0500104 }
105 *prev_ret = prev;
106 return NULL;
107}
108
Chris Masond352ac62008-09-29 15:18:18 -0400109/*
110 * helper to check if a given offset is inside a given entry
111 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400112static int offset_in_entry(struct btrfs_ordered_extent *entry, u64 file_offset)
Chris Masondc17ff82008-01-08 15:46:30 -0500113{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400114 if (file_offset < entry->file_offset ||
Omar Sandovalbffe6332019-12-02 17:34:19 -0800115 entry->file_offset + entry->num_bytes <= file_offset)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400116 return 0;
117 return 1;
118}
119
Josef Bacik4b46fce2010-05-23 11:00:55 -0400120static int range_overlaps(struct btrfs_ordered_extent *entry, u64 file_offset,
121 u64 len)
122{
123 if (file_offset + len <= entry->file_offset ||
Omar Sandovalbffe6332019-12-02 17:34:19 -0800124 entry->file_offset + entry->num_bytes <= file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400125 return 0;
126 return 1;
127}
128
Chris Masond352ac62008-09-29 15:18:18 -0400129/*
130 * look find the first ordered struct that has this offset, otherwise
131 * the first one less than this offset
132 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400133static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,
134 u64 file_offset)
135{
136 struct rb_root *root = &tree->tree;
Chris Masonc87fb6f2011-01-31 19:54:59 -0500137 struct rb_node *prev = NULL;
Chris Masondc17ff82008-01-08 15:46:30 -0500138 struct rb_node *ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400139 struct btrfs_ordered_extent *entry;
140
141 if (tree->last) {
142 entry = rb_entry(tree->last, struct btrfs_ordered_extent,
143 rb_node);
144 if (offset_in_entry(entry, file_offset))
145 return tree->last;
146 }
147 ret = __tree_search(root, file_offset, &prev);
Chris Masondc17ff82008-01-08 15:46:30 -0500148 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400149 ret = prev;
150 if (ret)
151 tree->last = ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500152 return ret;
153}
154
Chris Masoneb84ae02008-07-17 13:53:27 -0400155/* allocate and add a new ordered_extent into the per-inode tree.
Chris Masoneb84ae02008-07-17 13:53:27 -0400156 *
Chris Masoneb84ae02008-07-17 13:53:27 -0400157 * The tree is given a single reference on the ordered extent that was
158 * inserted.
159 */
Josef Bacik4b46fce2010-05-23 11:00:55 -0400160static int __btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800161 u64 disk_bytenr, u64 num_bytes,
162 u64 disk_num_bytes, int type, int dio,
163 int compress_type)
Chris Masondc17ff82008-01-08 15:46:30 -0500164{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400165 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie199c2a92013-05-15 07:48:23 +0000166 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondc17ff82008-01-08 15:46:30 -0500167 struct btrfs_ordered_inode_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400168 struct rb_node *node;
169 struct btrfs_ordered_extent *entry;
Chris Masondc17ff82008-01-08 15:46:30 -0500170
Chris Masone6dcd2d2008-07-17 12:53:50 -0400171 tree = &BTRFS_I(inode)->ordered_tree;
Miao Xie6352b912012-09-06 04:01:51 -0600172 entry = kmem_cache_zalloc(btrfs_ordered_extent_cache, GFP_NOFS);
Chris Masondc17ff82008-01-08 15:46:30 -0500173 if (!entry)
174 return -ENOMEM;
175
Chris Masone6dcd2d2008-07-17 12:53:50 -0400176 entry->file_offset = file_offset;
Omar Sandovalbffe6332019-12-02 17:34:19 -0800177 entry->disk_bytenr = disk_bytenr;
178 entry->num_bytes = num_bytes;
179 entry->disk_num_bytes = disk_num_bytes;
180 entry->bytes_left = num_bytes;
Josef Bacik5fd02042012-05-02 14:00:54 -0400181 entry->inode = igrab(inode);
Li Zefan261507a02010-12-17 14:21:50 +0800182 entry->compress_type = compress_type;
Josef Bacik77cef2e2013-08-29 13:57:21 -0400183 entry->truncated_len = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -0400184 if (type != BTRFS_ORDERED_IO_DONE && type != BTRFS_ORDERED_COMPLETE)
Yan Zheng80ff3852008-10-30 14:20:02 -0400185 set_bit(type, &entry->flags);
Chris Mason3eaa2882008-07-24 11:57:52 -0400186
Josef Bacik4297ff82019-04-10 15:56:09 -0400187 if (dio) {
Omar Sandovalbffe6332019-12-02 17:34:19 -0800188 percpu_counter_add_batch(&fs_info->dio_bytes, num_bytes,
Josef Bacik4297ff82019-04-10 15:56:09 -0400189 fs_info->delalloc_batch);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400190 set_bit(BTRFS_ORDERED_DIRECT, &entry->flags);
Josef Bacik4297ff82019-04-10 15:56:09 -0400191 }
Josef Bacik4b46fce2010-05-23 11:00:55 -0400192
Chris Masone6dcd2d2008-07-17 12:53:50 -0400193 /* one ref for the tree */
Elena Reshetovae76edab2017-03-03 10:55:13 +0200194 refcount_set(&entry->refs, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400195 init_waitqueue_head(&entry->wait);
196 INIT_LIST_HEAD(&entry->list);
Chris Mason3eaa2882008-07-24 11:57:52 -0400197 INIT_LIST_HEAD(&entry->root_extent_list);
Miao Xie9afab882012-10-25 09:41:36 +0000198 INIT_LIST_HEAD(&entry->work_list);
199 init_completion(&entry->completion);
Josef Bacik2ab28f32012-10-12 15:27:49 -0400200 INIT_LIST_HEAD(&entry->log_list);
Josef Bacik50d9aa92014-11-21 14:52:38 -0500201 INIT_LIST_HEAD(&entry->trans_list);
Chris Masondc17ff82008-01-08 15:46:30 -0500202
liubo1abe9b82011-03-24 11:18:59 +0000203 trace_btrfs_ordered_extent_add(inode, entry);
204
Josef Bacik5fd02042012-05-02 14:00:54 -0400205 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400206 node = tree_insert(&tree->tree, file_offset,
207 &entry->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -0400208 if (node)
Nikolay Borisov511a32b2019-11-29 11:38:13 +0200209 btrfs_panic(fs_info, -EEXIST,
210 "inconsistency in ordered tree at offset %llu",
211 file_offset);
Josef Bacik5fd02042012-05-02 14:00:54 -0400212 spin_unlock_irq(&tree->lock);
Chris Masond3977122009-01-05 21:25:51 -0500213
Miao Xie199c2a92013-05-15 07:48:23 +0000214 spin_lock(&root->ordered_extent_lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400215 list_add_tail(&entry->root_extent_list,
Miao Xie199c2a92013-05-15 07:48:23 +0000216 &root->ordered_extents);
217 root->nr_ordered_extents++;
218 if (root->nr_ordered_extents == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400219 spin_lock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +0000220 BUG_ON(!list_empty(&root->ordered_root));
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400221 list_add_tail(&root->ordered_root, &fs_info->ordered_roots);
222 spin_unlock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +0000223 }
224 spin_unlock(&root->ordered_extent_lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400225
Josef Bacik8b62f872017-10-19 14:15:55 -0400226 /*
227 * We don't need the count_max_extents here, we can assume that all of
228 * that work has been done at higher layers, so this is truly the
229 * smallest the extent is going to get.
230 */
231 spin_lock(&BTRFS_I(inode)->lock);
232 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
233 spin_unlock(&BTRFS_I(inode)->lock);
234
Chris Masone6dcd2d2008-07-17 12:53:50 -0400235 return 0;
236}
Chris Masondc17ff82008-01-08 15:46:30 -0500237
Josef Bacik4b46fce2010-05-23 11:00:55 -0400238int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800239 u64 disk_bytenr, u64 num_bytes, u64 disk_num_bytes,
240 int type)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400241{
Omar Sandovalbffe6332019-12-02 17:34:19 -0800242 return __btrfs_add_ordered_extent(inode, file_offset, disk_bytenr,
243 num_bytes, disk_num_bytes, type, 0,
Li Zefan261507a02010-12-17 14:21:50 +0800244 BTRFS_COMPRESS_NONE);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400245}
246
247int btrfs_add_ordered_extent_dio(struct inode *inode, u64 file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800248 u64 disk_bytenr, u64 num_bytes,
249 u64 disk_num_bytes, int type)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400250{
Omar Sandovalbffe6332019-12-02 17:34:19 -0800251 return __btrfs_add_ordered_extent(inode, file_offset, disk_bytenr,
252 num_bytes, disk_num_bytes, type, 1,
Li Zefan261507a02010-12-17 14:21:50 +0800253 BTRFS_COMPRESS_NONE);
254}
255
256int btrfs_add_ordered_extent_compress(struct inode *inode, u64 file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -0800257 u64 disk_bytenr, u64 num_bytes,
258 u64 disk_num_bytes, int type,
259 int compress_type)
Li Zefan261507a02010-12-17 14:21:50 +0800260{
Omar Sandovalbffe6332019-12-02 17:34:19 -0800261 return __btrfs_add_ordered_extent(inode, file_offset, disk_bytenr,
262 num_bytes, disk_num_bytes, type, 0,
Li Zefan261507a02010-12-17 14:21:50 +0800263 compress_type);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400264}
265
Chris Masoneb84ae02008-07-17 13:53:27 -0400266/*
267 * Add a struct btrfs_ordered_sum into the list of checksums to be inserted
Chris Mason3edf7d32008-07-18 06:17:13 -0400268 * when an ordered extent is finished. If the list covers more than one
269 * ordered extent, it is split across multiples.
Chris Masoneb84ae02008-07-17 13:53:27 -0400270 */
Nikolay Borisovf9756262019-04-10 16:16:11 +0300271void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100272 struct btrfs_ordered_sum *sum)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400273{
274 struct btrfs_ordered_inode_tree *tree;
Chris Mason1b1e2132008-06-25 16:01:31 -0400275
Nikolay Borisovf9756262019-04-10 16:16:11 +0300276 tree = &BTRFS_I(entry->inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400277 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400278 list_add_tail(&sum->list, &entry->list);
Josef Bacik5fd02042012-05-02 14:00:54 -0400279 spin_unlock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400280}
281
Chris Masoneb84ae02008-07-17 13:53:27 -0400282/*
283 * this is used to account for finished IO across a given range
Chris Mason163cf092010-11-28 19:56:33 -0500284 * of the file. The IO may span ordered extents. If
285 * a given ordered_extent is completely done, 1 is returned, otherwise
286 * 0.
287 *
288 * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
289 * to make sure this function only returns 1 once for a given ordered extent.
290 *
291 * file_offset is updated to one byte past the range that is recorded as
292 * complete. This allows you to walk forward in the file.
293 */
294int btrfs_dec_test_first_ordered_pending(struct inode *inode,
295 struct btrfs_ordered_extent **cached,
Josef Bacik5fd02042012-05-02 14:00:54 -0400296 u64 *file_offset, u64 io_size, int uptodate)
Chris Mason163cf092010-11-28 19:56:33 -0500297{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400298 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason163cf092010-11-28 19:56:33 -0500299 struct btrfs_ordered_inode_tree *tree;
300 struct rb_node *node;
301 struct btrfs_ordered_extent *entry = NULL;
302 int ret;
Josef Bacik5fd02042012-05-02 14:00:54 -0400303 unsigned long flags;
Chris Mason163cf092010-11-28 19:56:33 -0500304 u64 dec_end;
305 u64 dec_start;
306 u64 to_dec;
307
308 tree = &BTRFS_I(inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400309 spin_lock_irqsave(&tree->lock, flags);
Chris Mason163cf092010-11-28 19:56:33 -0500310 node = tree_search(tree, *file_offset);
311 if (!node) {
312 ret = 1;
313 goto out;
314 }
315
316 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
317 if (!offset_in_entry(entry, *file_offset)) {
318 ret = 1;
319 goto out;
320 }
321
322 dec_start = max(*file_offset, entry->file_offset);
Omar Sandovalbffe6332019-12-02 17:34:19 -0800323 dec_end = min(*file_offset + io_size,
324 entry->file_offset + entry->num_bytes);
Chris Mason163cf092010-11-28 19:56:33 -0500325 *file_offset = dec_end;
326 if (dec_start > dec_end) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400327 btrfs_crit(fs_info, "bad ordering dec_start %llu end %llu",
328 dec_start, dec_end);
Chris Mason163cf092010-11-28 19:56:33 -0500329 }
330 to_dec = dec_end - dec_start;
331 if (to_dec > entry->bytes_left) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400332 btrfs_crit(fs_info,
333 "bad ordered accounting left %llu size %llu",
334 entry->bytes_left, to_dec);
Chris Mason163cf092010-11-28 19:56:33 -0500335 }
336 entry->bytes_left -= to_dec;
Josef Bacik5fd02042012-05-02 14:00:54 -0400337 if (!uptodate)
338 set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
339
Miao Xieaf7a6502014-03-06 13:54:56 +0800340 if (entry->bytes_left == 0) {
Chris Mason163cf092010-11-28 19:56:33 -0500341 ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
David Sterba093258e2018-02-26 16:15:17 +0100342 /* test_and_set_bit implies a barrier */
343 cond_wake_up_nomb(&entry->wait);
Miao Xieaf7a6502014-03-06 13:54:56 +0800344 } else {
Chris Mason163cf092010-11-28 19:56:33 -0500345 ret = 1;
Miao Xieaf7a6502014-03-06 13:54:56 +0800346 }
Chris Mason163cf092010-11-28 19:56:33 -0500347out:
348 if (!ret && cached && entry) {
349 *cached = entry;
Elena Reshetovae76edab2017-03-03 10:55:13 +0200350 refcount_inc(&entry->refs);
Chris Mason163cf092010-11-28 19:56:33 -0500351 }
Josef Bacik5fd02042012-05-02 14:00:54 -0400352 spin_unlock_irqrestore(&tree->lock, flags);
Chris Mason163cf092010-11-28 19:56:33 -0500353 return ret == 0;
354}
355
356/*
357 * this is used to account for finished IO across a given range
Chris Masoneb84ae02008-07-17 13:53:27 -0400358 * of the file. The IO should not span ordered extents. If
359 * a given ordered_extent is completely done, 1 is returned, otherwise
360 * 0.
361 *
362 * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
363 * to make sure this function only returns 1 once for a given ordered extent.
364 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400365int btrfs_dec_test_ordered_pending(struct inode *inode,
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000366 struct btrfs_ordered_extent **cached,
Josef Bacik5fd02042012-05-02 14:00:54 -0400367 u64 file_offset, u64 io_size, int uptodate)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400368{
369 struct btrfs_ordered_inode_tree *tree;
370 struct rb_node *node;
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000371 struct btrfs_ordered_extent *entry = NULL;
Josef Bacik5fd02042012-05-02 14:00:54 -0400372 unsigned long flags;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400373 int ret;
374
375 tree = &BTRFS_I(inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400376 spin_lock_irqsave(&tree->lock, flags);
377 if (cached && *cached) {
378 entry = *cached;
379 goto have_entry;
380 }
381
Chris Masone6dcd2d2008-07-17 12:53:50 -0400382 node = tree_search(tree, file_offset);
383 if (!node) {
384 ret = 1;
385 goto out;
386 }
387
388 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
Josef Bacik5fd02042012-05-02 14:00:54 -0400389have_entry:
Chris Masone6dcd2d2008-07-17 12:53:50 -0400390 if (!offset_in_entry(entry, file_offset)) {
391 ret = 1;
392 goto out;
393 }
394
Chris Mason8b62b722009-09-02 16:53:46 -0400395 if (io_size > entry->bytes_left) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500396 btrfs_crit(BTRFS_I(inode)->root->fs_info,
397 "bad ordered accounting left %llu size %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200398 entry->bytes_left, io_size);
Chris Mason8b62b722009-09-02 16:53:46 -0400399 }
400 entry->bytes_left -= io_size;
Josef Bacik5fd02042012-05-02 14:00:54 -0400401 if (!uptodate)
402 set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
403
Miao Xieaf7a6502014-03-06 13:54:56 +0800404 if (entry->bytes_left == 0) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400405 ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
David Sterba093258e2018-02-26 16:15:17 +0100406 /* test_and_set_bit implies a barrier */
407 cond_wake_up_nomb(&entry->wait);
Miao Xieaf7a6502014-03-06 13:54:56 +0800408 } else {
Chris Mason8b62b722009-09-02 16:53:46 -0400409 ret = 1;
Miao Xieaf7a6502014-03-06 13:54:56 +0800410 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400411out:
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000412 if (!ret && cached && entry) {
413 *cached = entry;
Elena Reshetovae76edab2017-03-03 10:55:13 +0200414 refcount_inc(&entry->refs);
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000415 }
Josef Bacik5fd02042012-05-02 14:00:54 -0400416 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400417 return ret == 0;
418}
419
Chris Masoneb84ae02008-07-17 13:53:27 -0400420/*
421 * used to drop a reference on an ordered extent. This will free
422 * the extent if the last reference is dropped
423 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100424void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400425{
Chris Masonba1da2f2008-07-17 12:54:15 -0400426 struct list_head *cur;
427 struct btrfs_ordered_sum *sum;
428
liubo1abe9b82011-03-24 11:18:59 +0000429 trace_btrfs_ordered_extent_put(entry->inode, entry);
430
Elena Reshetovae76edab2017-03-03 10:55:13 +0200431 if (refcount_dec_and_test(&entry->refs)) {
Filipe Manana61de7182015-07-01 12:13:10 +0100432 ASSERT(list_empty(&entry->log_list));
433 ASSERT(list_empty(&entry->trans_list));
434 ASSERT(list_empty(&entry->root_extent_list));
435 ASSERT(RB_EMPTY_NODE(&entry->rb_node));
Josef Bacik5fd02042012-05-02 14:00:54 -0400436 if (entry->inode)
437 btrfs_add_delayed_iput(entry->inode);
Chris Masond3977122009-01-05 21:25:51 -0500438 while (!list_empty(&entry->list)) {
Chris Masonba1da2f2008-07-17 12:54:15 -0400439 cur = entry->list.next;
440 sum = list_entry(cur, struct btrfs_ordered_sum, list);
441 list_del(&sum->list);
Nikolay Borisova3d46ae2019-04-01 11:29:58 +0300442 kvfree(sum);
Chris Masonba1da2f2008-07-17 12:54:15 -0400443 }
Miao Xie6352b912012-09-06 04:01:51 -0600444 kmem_cache_free(btrfs_ordered_extent_cache, entry);
Chris Masonba1da2f2008-07-17 12:54:15 -0400445 }
Chris Masondc17ff82008-01-08 15:46:30 -0500446}
447
Chris Masoneb84ae02008-07-17 13:53:27 -0400448/*
449 * remove an ordered extent from the tree. No references are dropped
Josef Bacik5fd02042012-05-02 14:00:54 -0400450 * and waiters are woken up.
Chris Masoneb84ae02008-07-17 13:53:27 -0400451 */
Josef Bacik5fd02042012-05-02 14:00:54 -0400452void btrfs_remove_ordered_extent(struct inode *inode,
453 struct btrfs_ordered_extent *entry)
Chris Masondc17ff82008-01-08 15:46:30 -0500454{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400455 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400456 struct btrfs_ordered_inode_tree *tree;
Josef Bacik8b62f872017-10-19 14:15:55 -0400457 struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
458 struct btrfs_root *root = btrfs_inode->root;
Chris Masondc17ff82008-01-08 15:46:30 -0500459 struct rb_node *node;
460
Josef Bacik8b62f872017-10-19 14:15:55 -0400461 /* This is paired with btrfs_add_ordered_extent. */
462 spin_lock(&btrfs_inode->lock);
463 btrfs_mod_outstanding_extents(btrfs_inode, -1);
464 spin_unlock(&btrfs_inode->lock);
465 if (root != fs_info->tree_root)
Omar Sandovalbffe6332019-12-02 17:34:19 -0800466 btrfs_delalloc_release_metadata(btrfs_inode, entry->num_bytes,
467 false);
Josef Bacik8b62f872017-10-19 14:15:55 -0400468
Josef Bacik4297ff82019-04-10 15:56:09 -0400469 if (test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
Omar Sandovalbffe6332019-12-02 17:34:19 -0800470 percpu_counter_add_batch(&fs_info->dio_bytes, -entry->num_bytes,
Josef Bacik4297ff82019-04-10 15:56:09 -0400471 fs_info->delalloc_batch);
472
Josef Bacik8b62f872017-10-19 14:15:55 -0400473 tree = &btrfs_inode->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400474 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400475 node = &entry->rb_node;
Chris Masondc17ff82008-01-08 15:46:30 -0500476 rb_erase(node, &tree->tree);
Filipe Manana61de7182015-07-01 12:13:10 +0100477 RB_CLEAR_NODE(node);
Filipe David Borba Manana1b8e7e42013-11-22 18:54:58 +0000478 if (tree->last == node)
479 tree->last = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400480 set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
Josef Bacik5fd02042012-05-02 14:00:54 -0400481 spin_unlock_irq(&tree->lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400482
Miao Xie199c2a92013-05-15 07:48:23 +0000483 spin_lock(&root->ordered_extent_lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400484 list_del_init(&entry->root_extent_list);
Miao Xie199c2a92013-05-15 07:48:23 +0000485 root->nr_ordered_extents--;
Chris Mason5a3f23d2009-03-31 13:27:11 -0400486
liubo1abe9b82011-03-24 11:18:59 +0000487 trace_btrfs_ordered_extent_remove(inode, entry);
488
Miao Xie199c2a92013-05-15 07:48:23 +0000489 if (!root->nr_ordered_extents) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400490 spin_lock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +0000491 BUG_ON(list_empty(&root->ordered_root));
492 list_del_init(&root->ordered_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400493 spin_unlock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +0000494 }
495 spin_unlock(&root->ordered_extent_lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400496 wake_up(&entry->wait);
Chris Mason81d7ed22008-04-25 08:51:48 -0400497}
Chris Masone6dcd2d2008-07-17 12:53:50 -0400498
Qu Wenruod458b052014-02-28 10:46:19 +0800499static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
Miao Xie9afab882012-10-25 09:41:36 +0000500{
501 struct btrfs_ordered_extent *ordered;
502
503 ordered = container_of(work, struct btrfs_ordered_extent, flush_work);
504 btrfs_start_ordered_extent(ordered->inode, ordered, 1);
505 complete(&ordered->completion);
506}
507
Chris Masond352ac62008-09-29 15:18:18 -0400508/*
509 * wait for all the ordered extents in a root. This is done when balancing
510 * space between drives.
511 */
Chris Mason6374e57a2017-06-23 09:48:21 -0700512u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr,
Filipe Manana578def72016-04-26 15:36:38 +0100513 const u64 range_start, const u64 range_len)
Chris Mason3eaa2882008-07-24 11:57:52 -0400514{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400515 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana578def72016-04-26 15:36:38 +0100516 LIST_HEAD(splice);
517 LIST_HEAD(skipped);
518 LIST_HEAD(works);
Miao Xie9afab882012-10-25 09:41:36 +0000519 struct btrfs_ordered_extent *ordered, *next;
Chris Mason6374e57a2017-06-23 09:48:21 -0700520 u64 count = 0;
Filipe Manana578def72016-04-26 15:36:38 +0100521 const u64 range_end = range_start + range_len;
Chris Mason3eaa2882008-07-24 11:57:52 -0400522
Miao Xie31f3d252014-03-06 13:55:02 +0800523 mutex_lock(&root->ordered_extent_mutex);
Miao Xie199c2a92013-05-15 07:48:23 +0000524 spin_lock(&root->ordered_extent_lock);
525 list_splice_init(&root->ordered_extents, &splice);
Miao Xieb0244192013-11-04 23:13:25 +0800526 while (!list_empty(&splice) && nr) {
Miao Xie199c2a92013-05-15 07:48:23 +0000527 ordered = list_first_entry(&splice, struct btrfs_ordered_extent,
528 root_extent_list);
Filipe Manana578def72016-04-26 15:36:38 +0100529
Omar Sandovalbffe6332019-12-02 17:34:19 -0800530 if (range_end <= ordered->disk_bytenr ||
531 ordered->disk_bytenr + ordered->disk_num_bytes <= range_start) {
Filipe Manana578def72016-04-26 15:36:38 +0100532 list_move_tail(&ordered->root_extent_list, &skipped);
533 cond_resched_lock(&root->ordered_extent_lock);
534 continue;
535 }
536
Miao Xie199c2a92013-05-15 07:48:23 +0000537 list_move_tail(&ordered->root_extent_list,
538 &root->ordered_extents);
Elena Reshetovae76edab2017-03-03 10:55:13 +0200539 refcount_inc(&ordered->refs);
Miao Xie199c2a92013-05-15 07:48:23 +0000540 spin_unlock(&root->ordered_extent_lock);
541
Qu Wenruoa44903a2014-02-28 10:46:09 +0800542 btrfs_init_work(&ordered->flush_work,
543 btrfs_run_ordered_extent_work, NULL, NULL);
Miao Xie199c2a92013-05-15 07:48:23 +0000544 list_add_tail(&ordered->work_list, &works);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400545 btrfs_queue_work(fs_info->flush_workers, &ordered->flush_work);
Miao Xie199c2a92013-05-15 07:48:23 +0000546
Miao Xie9afab882012-10-25 09:41:36 +0000547 cond_resched();
Miao Xie199c2a92013-05-15 07:48:23 +0000548 spin_lock(&root->ordered_extent_lock);
Chris Mason6374e57a2017-06-23 09:48:21 -0700549 if (nr != U64_MAX)
Miao Xieb0244192013-11-04 23:13:25 +0800550 nr--;
551 count++;
Chris Mason3eaa2882008-07-24 11:57:52 -0400552 }
Filipe Manana578def72016-04-26 15:36:38 +0100553 list_splice_tail(&skipped, &root->ordered_extents);
Miao Xieb0244192013-11-04 23:13:25 +0800554 list_splice_tail(&splice, &root->ordered_extents);
Miao Xie199c2a92013-05-15 07:48:23 +0000555 spin_unlock(&root->ordered_extent_lock);
Miao Xie9afab882012-10-25 09:41:36 +0000556
557 list_for_each_entry_safe(ordered, next, &works, work_list) {
558 list_del_init(&ordered->work_list);
559 wait_for_completion(&ordered->completion);
Miao Xie9afab882012-10-25 09:41:36 +0000560 btrfs_put_ordered_extent(ordered);
Miao Xie9afab882012-10-25 09:41:36 +0000561 cond_resched();
562 }
Miao Xie31f3d252014-03-06 13:55:02 +0800563 mutex_unlock(&root->ordered_extent_mutex);
Miao Xieb0244192013-11-04 23:13:25 +0800564
565 return count;
Chris Mason3eaa2882008-07-24 11:57:52 -0400566}
567
Filipe Manana042528f2019-11-14 18:02:43 +0000568void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr,
Chris Mason6374e57a2017-06-23 09:48:21 -0700569 const u64 range_start, const u64 range_len)
Miao Xie199c2a92013-05-15 07:48:23 +0000570{
571 struct btrfs_root *root;
572 struct list_head splice;
Chris Mason6374e57a2017-06-23 09:48:21 -0700573 u64 done;
Miao Xie199c2a92013-05-15 07:48:23 +0000574
575 INIT_LIST_HEAD(&splice);
576
Miao Xie8b9d83c2014-03-06 13:54:55 +0800577 mutex_lock(&fs_info->ordered_operations_mutex);
Miao Xie199c2a92013-05-15 07:48:23 +0000578 spin_lock(&fs_info->ordered_root_lock);
579 list_splice_init(&fs_info->ordered_roots, &splice);
Miao Xieb0244192013-11-04 23:13:25 +0800580 while (!list_empty(&splice) && nr) {
Miao Xie199c2a92013-05-15 07:48:23 +0000581 root = list_first_entry(&splice, struct btrfs_root,
582 ordered_root);
Josef Bacik00246522020-01-24 09:33:01 -0500583 root = btrfs_grab_root(root);
Miao Xie199c2a92013-05-15 07:48:23 +0000584 BUG_ON(!root);
585 list_move_tail(&root->ordered_root,
586 &fs_info->ordered_roots);
587 spin_unlock(&fs_info->ordered_root_lock);
588
Filipe Manana578def72016-04-26 15:36:38 +0100589 done = btrfs_wait_ordered_extents(root, nr,
590 range_start, range_len);
Josef Bacik00246522020-01-24 09:33:01 -0500591 btrfs_put_root(root);
Miao Xie199c2a92013-05-15 07:48:23 +0000592
593 spin_lock(&fs_info->ordered_root_lock);
Chris Mason6374e57a2017-06-23 09:48:21 -0700594 if (nr != U64_MAX) {
Miao Xieb0244192013-11-04 23:13:25 +0800595 nr -= done;
Miao Xieb0244192013-11-04 23:13:25 +0800596 }
Miao Xie199c2a92013-05-15 07:48:23 +0000597 }
Miao Xie931aa872013-11-14 17:33:21 +0800598 list_splice_tail(&splice, &fs_info->ordered_roots);
Miao Xie199c2a92013-05-15 07:48:23 +0000599 spin_unlock(&fs_info->ordered_root_lock);
Miao Xie8b9d83c2014-03-06 13:54:55 +0800600 mutex_unlock(&fs_info->ordered_operations_mutex);
Miao Xie199c2a92013-05-15 07:48:23 +0000601}
602
Chris Masoneb84ae02008-07-17 13:53:27 -0400603/*
604 * Used to start IO or wait for a given ordered extent to finish.
605 *
606 * If wait is one, this effectively waits on page writeback for all the pages
607 * in the extent, and it waits on the io completion code to insert
608 * metadata into the btree corresponding to the extent
609 */
610void btrfs_start_ordered_extent(struct inode *inode,
611 struct btrfs_ordered_extent *entry,
612 int wait)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400613{
614 u64 start = entry->file_offset;
Omar Sandovalbffe6332019-12-02 17:34:19 -0800615 u64 end = start + entry->num_bytes - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400616
liubo1abe9b82011-03-24 11:18:59 +0000617 trace_btrfs_ordered_extent_start(inode, entry);
618
Chris Masoneb84ae02008-07-17 13:53:27 -0400619 /*
620 * pages in the range can be dirty, clean or writeback. We
621 * start IO on any dirty ones so the wait doesn't stall waiting
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300622 * for the flusher thread to find them
Chris Masoneb84ae02008-07-17 13:53:27 -0400623 */
Josef Bacik4b46fce2010-05-23 11:00:55 -0400624 if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
625 filemap_fdatawrite_range(inode->i_mapping, start, end);
Chris Masonc8b97812008-10-29 14:49:59 -0400626 if (wait) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400627 wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
628 &entry->flags));
Chris Masonc8b97812008-10-29 14:49:59 -0400629 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400630}
631
Chris Masoneb84ae02008-07-17 13:53:27 -0400632/*
633 * Used to wait on ordered extents across a large range of bytes.
634 */
Josef Bacik0ef8b722013-10-25 16:13:35 -0400635int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400636{
Josef Bacik0ef8b722013-10-25 16:13:35 -0400637 int ret = 0;
Filipe Manana28aeeac2015-05-05 19:03:10 +0100638 int ret_wb = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400639 u64 end;
Chris Masone5a22172008-07-18 20:42:20 -0400640 u64 orig_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400641 struct btrfs_ordered_extent *ordered;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400642
Chris Masone5a22172008-07-18 20:42:20 -0400643 if (start + len < start) {
Chris Masonf4219502008-07-22 11:18:09 -0400644 orig_end = INT_LIMIT(loff_t);
Chris Masone5a22172008-07-18 20:42:20 -0400645 } else {
646 orig_end = start + len - 1;
Chris Masonf4219502008-07-22 11:18:09 -0400647 if (orig_end > INT_LIMIT(loff_t))
648 orig_end = INT_LIMIT(loff_t);
Chris Masone5a22172008-07-18 20:42:20 -0400649 }
Josef Bacik551ebb22012-04-23 14:41:09 -0400650
Chris Masone5a22172008-07-18 20:42:20 -0400651 /* start IO across the range first to instantiate any delalloc
652 * extents
653 */
Filipe Manana728404d2014-10-10 09:43:11 +0100654 ret = btrfs_fdatawrite_range(inode, start, orig_end);
Josef Bacik0ef8b722013-10-25 16:13:35 -0400655 if (ret)
656 return ret;
Filipe Manana728404d2014-10-10 09:43:11 +0100657
Filipe Manana28aeeac2015-05-05 19:03:10 +0100658 /*
659 * If we have a writeback error don't return immediately. Wait first
660 * for any ordered extents that haven't completed yet. This is to make
661 * sure no one can dirty the same page ranges and call writepages()
662 * before the ordered extents complete - to avoid failures (-EEXIST)
663 * when adding the new ordered extents to the ordered tree.
664 */
665 ret_wb = filemap_fdatawait_range(inode->i_mapping, start, orig_end);
Chris Masonf4219502008-07-22 11:18:09 -0400666
667 end = orig_end;
Chris Masond3977122009-01-05 21:25:51 -0500668 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400669 ordered = btrfs_lookup_first_ordered_extent(inode, end);
Chris Masond3977122009-01-05 21:25:51 -0500670 if (!ordered)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400671 break;
Chris Masone5a22172008-07-18 20:42:20 -0400672 if (ordered->file_offset > orig_end) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400673 btrfs_put_ordered_extent(ordered);
674 break;
675 }
Omar Sandovalbffe6332019-12-02 17:34:19 -0800676 if (ordered->file_offset + ordered->num_bytes <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400677 btrfs_put_ordered_extent(ordered);
678 break;
679 }
Chris Masone5a22172008-07-18 20:42:20 -0400680 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400681 end = ordered->file_offset;
Filipe Mananae75fd332020-02-13 12:29:50 +0000682 /*
683 * If the ordered extent had an error save the error but don't
684 * exit without waiting first for all other ordered extents in
685 * the range to complete.
686 */
Josef Bacik0ef8b722013-10-25 16:13:35 -0400687 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags))
688 ret = -EIO;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400689 btrfs_put_ordered_extent(ordered);
Filipe Mananae75fd332020-02-13 12:29:50 +0000690 if (end == 0 || end == start)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400691 break;
692 end--;
693 }
Filipe Manana28aeeac2015-05-05 19:03:10 +0100694 return ret_wb ? ret_wb : ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400695}
696
Chris Masoneb84ae02008-07-17 13:53:27 -0400697/*
698 * find an ordered extent corresponding to file_offset. return NULL if
699 * nothing is found, otherwise take a reference on the extent and return it
700 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400701struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
702 u64 file_offset)
703{
704 struct btrfs_ordered_inode_tree *tree;
705 struct rb_node *node;
706 struct btrfs_ordered_extent *entry = NULL;
707
708 tree = &BTRFS_I(inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400709 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400710 node = tree_search(tree, file_offset);
711 if (!node)
712 goto out;
713
714 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
715 if (!offset_in_entry(entry, file_offset))
716 entry = NULL;
717 if (entry)
Elena Reshetovae76edab2017-03-03 10:55:13 +0200718 refcount_inc(&entry->refs);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400719out:
Josef Bacik5fd02042012-05-02 14:00:54 -0400720 spin_unlock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400721 return entry;
722}
723
Josef Bacik4b46fce2010-05-23 11:00:55 -0400724/* Since the DIO code tries to lock a wide area we need to look for any ordered
725 * extents that exist in the range, rather than just the start of the range.
726 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +0200727struct btrfs_ordered_extent *btrfs_lookup_ordered_range(
728 struct btrfs_inode *inode, u64 file_offset, u64 len)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400729{
730 struct btrfs_ordered_inode_tree *tree;
731 struct rb_node *node;
732 struct btrfs_ordered_extent *entry = NULL;
733
Nikolay Borisova776c6f2017-02-20 13:50:49 +0200734 tree = &inode->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400735 spin_lock_irq(&tree->lock);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400736 node = tree_search(tree, file_offset);
737 if (!node) {
738 node = tree_search(tree, file_offset + len);
739 if (!node)
740 goto out;
741 }
742
743 while (1) {
744 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
745 if (range_overlaps(entry, file_offset, len))
746 break;
747
748 if (entry->file_offset >= file_offset + len) {
749 entry = NULL;
750 break;
751 }
752 entry = NULL;
753 node = rb_next(node);
754 if (!node)
755 break;
756 }
757out:
758 if (entry)
Elena Reshetovae76edab2017-03-03 10:55:13 +0200759 refcount_inc(&entry->refs);
Josef Bacik5fd02042012-05-02 14:00:54 -0400760 spin_unlock_irq(&tree->lock);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400761 return entry;
762}
763
Chris Masoneb84ae02008-07-17 13:53:27 -0400764/*
765 * lookup and return any extent before 'file_offset'. NULL is returned
766 * if none is found
767 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400768struct btrfs_ordered_extent *
Chris Masond3977122009-01-05 21:25:51 -0500769btrfs_lookup_first_ordered_extent(struct inode *inode, u64 file_offset)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400770{
771 struct btrfs_ordered_inode_tree *tree;
772 struct rb_node *node;
773 struct btrfs_ordered_extent *entry = NULL;
774
775 tree = &BTRFS_I(inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400776 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400777 node = tree_search(tree, file_offset);
778 if (!node)
779 goto out;
780
781 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
Elena Reshetovae76edab2017-03-03 10:55:13 +0200782 refcount_inc(&entry->refs);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400783out:
Josef Bacik5fd02042012-05-02 14:00:54 -0400784 spin_unlock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400785 return entry;
786}
Chris Masondbe674a2008-07-17 12:54:05 -0400787
Chris Masoneb84ae02008-07-17 13:53:27 -0400788/*
Chris Masoneb84ae02008-07-17 13:53:27 -0400789 * search the ordered extents for one corresponding to 'offset' and
790 * try to find a checksum. This is used because we allow pages to
791 * be reclaimed before their checksum is actually put into the btree
792 */
Chris Masond20f7042008-12-08 16:58:54 -0500793int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
Johannes Thumshirn1e25a2e2019-05-22 10:19:01 +0200794 u8 *sum, int len)
Chris Masonba1da2f2008-07-17 12:54:15 -0400795{
Johannes Thumshirn1e25a2e2019-05-22 10:19:01 +0200796 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonba1da2f2008-07-17 12:54:15 -0400797 struct btrfs_ordered_sum *ordered_sum;
Chris Masonba1da2f2008-07-17 12:54:15 -0400798 struct btrfs_ordered_extent *ordered;
799 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
Chris Mason3edf7d32008-07-18 06:17:13 -0400800 unsigned long num_sectors;
801 unsigned long i;
Jeff Mahoneyda170662016-06-15 09:22:56 -0400802 u32 sectorsize = btrfs_inode_sectorsize(inode);
Johannes Thumshirn1e25a2e2019-05-22 10:19:01 +0200803 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
Miao Xiee4100d92013-04-05 07:20:56 +0000804 int index = 0;
Chris Masonba1da2f2008-07-17 12:54:15 -0400805
806 ordered = btrfs_lookup_ordered_extent(inode, offset);
807 if (!ordered)
Miao Xiee4100d92013-04-05 07:20:56 +0000808 return 0;
Chris Masonba1da2f2008-07-17 12:54:15 -0400809
Josef Bacik5fd02042012-05-02 14:00:54 -0400810 spin_lock_irq(&tree->lock);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500811 list_for_each_entry_reverse(ordered_sum, &ordered->list, list) {
Miao Xiee4100d92013-04-05 07:20:56 +0000812 if (disk_bytenr >= ordered_sum->bytenr &&
813 disk_bytenr < ordered_sum->bytenr + ordered_sum->len) {
814 i = (disk_bytenr - ordered_sum->bytenr) >>
815 inode->i_sb->s_blocksize_bits;
Miao Xiee4100d92013-04-05 07:20:56 +0000816 num_sectors = ordered_sum->len >>
817 inode->i_sb->s_blocksize_bits;
Miao Xief51a4a12013-06-19 10:36:09 +0800818 num_sectors = min_t(int, len - index, num_sectors - i);
Johannes Thumshirn1e25a2e2019-05-22 10:19:01 +0200819 memcpy(sum + index, ordered_sum->sums + i * csum_size,
820 num_sectors * csum_size);
Miao Xief51a4a12013-06-19 10:36:09 +0800821
Johannes Thumshirn1e25a2e2019-05-22 10:19:01 +0200822 index += (int)num_sectors * csum_size;
Miao Xief51a4a12013-06-19 10:36:09 +0800823 if (index == len)
824 goto out;
825 disk_bytenr += num_sectors * sectorsize;
Chris Masonba1da2f2008-07-17 12:54:15 -0400826 }
827 }
828out:
Josef Bacik5fd02042012-05-02 14:00:54 -0400829 spin_unlock_irq(&tree->lock);
Chris Mason89642222008-07-24 09:41:53 -0400830 btrfs_put_ordered_extent(ordered);
Miao Xiee4100d92013-04-05 07:20:56 +0000831 return index;
Chris Masonba1da2f2008-07-17 12:54:15 -0400832}
833
Nikolay Borisovffa87212019-05-07 10:19:22 +0300834/*
835 * btrfs_flush_ordered_range - Lock the passed range and ensures all pending
836 * ordered extents in it are run to completion.
837 *
Nikolay Borisovffa87212019-05-07 10:19:22 +0300838 * @inode: Inode whose ordered tree is to be searched
839 * @start: Beginning of range to flush
840 * @end: Last byte of range to lock
841 * @cached_state: If passed, will return the extent state responsible for the
842 * locked range. It's the caller's responsibility to free the cached state.
843 *
844 * This function always returns with the given range locked, ensuring after it's
845 * called no order extent can be pending.
846 */
David Sterbab272ae22020-02-05 19:09:33 +0100847void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,
Nikolay Borisovffa87212019-05-07 10:19:22 +0300848 u64 end,
849 struct extent_state **cached_state)
850{
851 struct btrfs_ordered_extent *ordered;
Naohiro Aotaa3b46b82019-07-26 16:47:05 +0900852 struct extent_state *cache = NULL;
853 struct extent_state **cachedp = &cache;
Nikolay Borisovbd80d942019-05-07 10:19:24 +0300854
855 if (cached_state)
Naohiro Aotaa3b46b82019-07-26 16:47:05 +0900856 cachedp = cached_state;
Nikolay Borisovffa87212019-05-07 10:19:22 +0300857
858 while (1) {
David Sterbab272ae22020-02-05 19:09:33 +0100859 lock_extent_bits(&inode->io_tree, start, end, cachedp);
Nikolay Borisovffa87212019-05-07 10:19:22 +0300860 ordered = btrfs_lookup_ordered_range(inode, start,
861 end - start + 1);
Nikolay Borisovbd80d942019-05-07 10:19:24 +0300862 if (!ordered) {
863 /*
864 * If no external cached_state has been passed then
865 * decrement the extra ref taken for cachedp since we
866 * aren't exposing it outside of this function
867 */
868 if (!cached_state)
Naohiro Aotaa3b46b82019-07-26 16:47:05 +0900869 refcount_dec(&cache->refs);
Nikolay Borisovffa87212019-05-07 10:19:22 +0300870 break;
Nikolay Borisovbd80d942019-05-07 10:19:24 +0300871 }
David Sterbab272ae22020-02-05 19:09:33 +0100872 unlock_extent_cached(&inode->io_tree, start, end, cachedp);
Nikolay Borisovffa87212019-05-07 10:19:22 +0300873 btrfs_start_ordered_extent(&inode->vfs_inode, ordered, 1);
874 btrfs_put_ordered_extent(ordered);
875 }
876}
877
Miao Xie6352b912012-09-06 04:01:51 -0600878int __init ordered_data_init(void)
879{
880 btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent",
881 sizeof(struct btrfs_ordered_extent), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300882 SLAB_MEM_SPREAD,
Miao Xie6352b912012-09-06 04:01:51 -0600883 NULL);
884 if (!btrfs_ordered_extent_cache)
885 return -ENOMEM;
Miao Xie25287e02012-10-25 09:31:03 +0000886
Miao Xie6352b912012-09-06 04:01:51 -0600887 return 0;
888}
889
David Sterbae67c7182018-02-19 17:24:18 +0100890void __cold ordered_data_exit(void)
Miao Xie6352b912012-09-06 04:01:51 -0600891{
Kinglong Mee5598e902016-01-29 21:36:35 +0800892 kmem_cache_destroy(btrfs_ordered_extent_cache);
Miao Xie6352b912012-09-06 04:01:51 -0600893}