1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <linux/slab.h>
7 #include <linux/blkdev.h>
8 #include <linux/writeback.h>
9 #include <linux/sched/mm.h>
12 #include "transaction.h"
13 #include "btrfs_inode.h"
14 #include "extent_io.h"
16 #include "compression.h"
17 #include "delalloc-space.h"
21 static struct kmem_cache *btrfs_ordered_extent_cache;
23 static u64 entry_end(struct btrfs_ordered_extent *entry)
25 if (entry->file_offset + entry->num_bytes < entry->file_offset)
27 return entry->file_offset + entry->num_bytes;
30 /* returns NULL if the insertion worked, or it returns the node it did find
33 static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset,
36 struct rb_node **p = &root->rb_node;
37 struct rb_node *parent = NULL;
38 struct btrfs_ordered_extent *entry;
42 entry = rb_entry(parent, struct btrfs_ordered_extent, rb_node);
44 if (file_offset < entry->file_offset)
46 else if (file_offset >= entry_end(entry))
52 rb_link_node(node, parent, p);
53 rb_insert_color(node, root);
58 * look for a given offset in the tree, and if it can't be found return the
61 static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset,
62 struct rb_node **prev_ret)
64 struct rb_node *n = root->rb_node;
65 struct rb_node *prev = NULL;
67 struct btrfs_ordered_extent *entry;
68 struct btrfs_ordered_extent *prev_entry = NULL;
71 entry = rb_entry(n, struct btrfs_ordered_extent, rb_node);
75 if (file_offset < entry->file_offset)
77 else if (file_offset >= entry_end(entry))
85 while (prev && file_offset >= entry_end(prev_entry)) {
89 prev_entry = rb_entry(test, struct btrfs_ordered_extent,
91 if (file_offset < entry_end(prev_entry))
97 prev_entry = rb_entry(prev, struct btrfs_ordered_extent,
99 while (prev && file_offset < entry_end(prev_entry)) {
100 test = rb_prev(prev);
103 prev_entry = rb_entry(test, struct btrfs_ordered_extent,
111 static int range_overlaps(struct btrfs_ordered_extent *entry, u64 file_offset,
114 if (file_offset + len <= entry->file_offset ||
115 entry->file_offset + entry->num_bytes <= file_offset)
121 * look find the first ordered struct that has this offset, otherwise
122 * the first one less than this offset
124 static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,
127 struct rb_root *root = &tree->tree;
128 struct rb_node *prev = NULL;
130 struct btrfs_ordered_extent *entry;
133 entry = rb_entry(tree->last, struct btrfs_ordered_extent,
135 if (in_range(file_offset, entry->file_offset, entry->num_bytes))
138 ret = __tree_search(root, file_offset, &prev);
147 * Add an ordered extent to the per-inode tree.
149 * @inode: Inode that this extent is for.
150 * @file_offset: Logical offset in file where the extent starts.
151 * @num_bytes: Logical length of extent in file.
152 * @ram_bytes: Full length of unencoded data.
153 * @disk_bytenr: Offset of extent on disk.
154 * @disk_num_bytes: Size of extent on disk.
155 * @offset: Offset into unencoded data where file data starts.
156 * @flags: Flags specifying type of extent (1 << BTRFS_ORDERED_*).
157 * @compress_type: Compression algorithm used for data.
159 * Most of these parameters correspond to &struct btrfs_file_extent_item. The
160 * tree is given a single reference on the ordered extent that was inserted.
162 * Return: 0 or -ENOMEM.
164 int btrfs_add_ordered_extent(struct btrfs_inode *inode, u64 file_offset,
165 u64 num_bytes, u64 ram_bytes, u64 disk_bytenr,
166 u64 disk_num_bytes, u64 offset, unsigned flags,
169 struct btrfs_root *root = inode->root;
170 struct btrfs_fs_info *fs_info = root->fs_info;
171 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
172 struct rb_node *node;
173 struct btrfs_ordered_extent *entry;
177 ((1 << BTRFS_ORDERED_NOCOW) | (1 << BTRFS_ORDERED_PREALLOC))) {
178 /* For nocow write, we can release the qgroup rsv right now */
179 ret = btrfs_qgroup_free_data(inode, NULL, file_offset, num_bytes);
185 * The ordered extent has reserved qgroup space, release now
186 * and pass the reserved number for qgroup_record to free.
188 ret = btrfs_qgroup_release_data(inode, file_offset, num_bytes);
192 entry = kmem_cache_zalloc(btrfs_ordered_extent_cache, GFP_NOFS);
196 entry->file_offset = file_offset;
197 entry->num_bytes = num_bytes;
198 entry->ram_bytes = ram_bytes;
199 entry->disk_bytenr = disk_bytenr;
200 entry->disk_num_bytes = disk_num_bytes;
201 entry->offset = offset;
202 entry->bytes_left = num_bytes;
203 entry->inode = igrab(&inode->vfs_inode);
204 entry->compress_type = compress_type;
205 entry->truncated_len = (u64)-1;
206 entry->qgroup_rsv = ret;
207 entry->physical = (u64)-1;
209 ASSERT((flags & ~BTRFS_ORDERED_TYPE_FLAGS) == 0);
210 entry->flags = flags;
212 percpu_counter_add_batch(&fs_info->ordered_bytes, num_bytes,
213 fs_info->delalloc_batch);
215 /* one ref for the tree */
216 refcount_set(&entry->refs, 1);
217 init_waitqueue_head(&entry->wait);
218 INIT_LIST_HEAD(&entry->list);
219 INIT_LIST_HEAD(&entry->log_list);
220 INIT_LIST_HEAD(&entry->root_extent_list);
221 INIT_LIST_HEAD(&entry->work_list);
222 init_completion(&entry->completion);
224 trace_btrfs_ordered_extent_add(inode, entry);
226 spin_lock_irq(&tree->lock);
227 node = tree_insert(&tree->tree, file_offset,
230 btrfs_panic(fs_info, -EEXIST,
231 "inconsistency in ordered tree at offset %llu",
233 spin_unlock_irq(&tree->lock);
235 spin_lock(&root->ordered_extent_lock);
236 list_add_tail(&entry->root_extent_list,
237 &root->ordered_extents);
238 root->nr_ordered_extents++;
239 if (root->nr_ordered_extents == 1) {
240 spin_lock(&fs_info->ordered_root_lock);
241 BUG_ON(!list_empty(&root->ordered_root));
242 list_add_tail(&root->ordered_root, &fs_info->ordered_roots);
243 spin_unlock(&fs_info->ordered_root_lock);
245 spin_unlock(&root->ordered_extent_lock);
248 * We don't need the count_max_extents here, we can assume that all of
249 * that work has been done at higher layers, so this is truly the
250 * smallest the extent is going to get.
252 spin_lock(&inode->lock);
253 btrfs_mod_outstanding_extents(inode, 1);
254 spin_unlock(&inode->lock);
260 * Add a struct btrfs_ordered_sum into the list of checksums to be inserted
261 * when an ordered extent is finished. If the list covers more than one
262 * ordered extent, it is split across multiples.
264 void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
265 struct btrfs_ordered_sum *sum)
267 struct btrfs_ordered_inode_tree *tree;
269 tree = &BTRFS_I(entry->inode)->ordered_tree;
270 spin_lock_irq(&tree->lock);
271 list_add_tail(&sum->list, &entry->list);
272 spin_unlock_irq(&tree->lock);
276 * Mark all ordered extents io inside the specified range finished.
278 * @page: The invovled page for the opeartion.
279 * For uncompressed buffered IO, the page status also needs to be
280 * updated to indicate whether the pending ordered io is finished.
281 * Can be NULL for direct IO and compressed write.
282 * For these cases, callers are ensured they won't execute the
283 * endio function twice.
284 * @finish_func: The function to be executed when all the IO of an ordered
285 * extent are finished.
287 * This function is called for endio, thus the range must have ordered
288 * extent(s) coveri it.
290 void btrfs_mark_ordered_io_finished(struct btrfs_inode *inode,
291 struct page *page, u64 file_offset,
292 u64 num_bytes, btrfs_func_t finish_func,
295 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
296 struct btrfs_fs_info *fs_info = inode->root->fs_info;
297 struct btrfs_workqueue *wq;
298 struct rb_node *node;
299 struct btrfs_ordered_extent *entry = NULL;
301 u64 cur = file_offset;
303 if (btrfs_is_free_space_inode(inode))
304 wq = fs_info->endio_freespace_worker;
306 wq = fs_info->endio_write_workers;
309 ASSERT(page->mapping && page_offset(page) <= file_offset &&
310 file_offset + num_bytes <= page_offset(page) + PAGE_SIZE);
312 spin_lock_irqsave(&tree->lock, flags);
313 while (cur < file_offset + num_bytes) {
318 node = tree_search(tree, cur);
319 /* No ordered extents at all */
323 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
324 entry_end = entry->file_offset + entry->num_bytes;
330 if (cur >= entry_end) {
331 node = rb_next(node);
332 /* No more ordered extents, exit */
335 entry = rb_entry(node, struct btrfs_ordered_extent,
338 /* Go to next ordered extent and continue */
339 cur = entry->file_offset;
345 * Go to the start of OE.
347 if (cur < entry->file_offset) {
348 cur = entry->file_offset;
353 * Now we are definitely inside one ordered extent.
359 end = min(entry->file_offset + entry->num_bytes,
360 file_offset + num_bytes) - 1;
361 ASSERT(end + 1 - cur < U32_MAX);
366 * Ordered (Private2) bit indicates whether we still
367 * have pending io unfinished for the ordered extent.
369 * If there's no such bit, we need to skip to next range.
371 if (!btrfs_page_test_ordered(fs_info, page, cur, len)) {
375 btrfs_page_clear_ordered(fs_info, page, cur, len);
378 /* Now we're fine to update the accounting */
379 if (unlikely(len > entry->bytes_left)) {
382 "bad ordered extent accounting, root=%llu ino=%llu OE offset=%llu OE len=%llu to_dec=%u left=%llu",
383 inode->root->root_key.objectid,
387 len, entry->bytes_left);
388 entry->bytes_left = 0;
390 entry->bytes_left -= len;
394 set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
397 * All the IO of the ordered extent is finished, we need to queue
398 * the finish_func to be executed.
400 if (entry->bytes_left == 0) {
401 set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
402 cond_wake_up(&entry->wait);
403 refcount_inc(&entry->refs);
404 spin_unlock_irqrestore(&tree->lock, flags);
405 btrfs_init_work(&entry->work, finish_func, NULL, NULL);
406 btrfs_queue_work(wq, &entry->work);
407 spin_lock_irqsave(&tree->lock, flags);
411 spin_unlock_irqrestore(&tree->lock, flags);
415 * Finish IO for one ordered extent across a given range. The range can only
416 * contain one ordered extent.
418 * @cached: The cached ordered extent. If not NULL, we can skip the tree
419 * search and use the ordered extent directly.
420 * Will be also used to store the finished ordered extent.
421 * @file_offset: File offset for the finished IO
422 * @io_size: Length of the finish IO range
424 * Return true if the ordered extent is finished in the range, and update
426 * Return false otherwise.
428 * NOTE: The range can NOT cross multiple ordered extents.
429 * Thus caller should ensure the range doesn't cross ordered extents.
431 bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode,
432 struct btrfs_ordered_extent **cached,
433 u64 file_offset, u64 io_size)
435 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
436 struct rb_node *node;
437 struct btrfs_ordered_extent *entry = NULL;
439 bool finished = false;
441 spin_lock_irqsave(&tree->lock, flags);
442 if (cached && *cached) {
447 node = tree_search(tree, file_offset);
451 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
453 if (!in_range(file_offset, entry->file_offset, entry->num_bytes))
456 if (io_size > entry->bytes_left)
457 btrfs_crit(inode->root->fs_info,
458 "bad ordered accounting left %llu size %llu",
459 entry->bytes_left, io_size);
461 entry->bytes_left -= io_size;
463 if (entry->bytes_left == 0) {
465 * Ensure only one caller can set the flag and finished_ret
468 finished = !test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
469 /* test_and_set_bit implies a barrier */
470 cond_wake_up_nomb(&entry->wait);
473 if (finished && cached && entry) {
475 refcount_inc(&entry->refs);
477 spin_unlock_irqrestore(&tree->lock, flags);
482 * used to drop a reference on an ordered extent. This will free
483 * the extent if the last reference is dropped
485 void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
487 struct list_head *cur;
488 struct btrfs_ordered_sum *sum;
490 trace_btrfs_ordered_extent_put(BTRFS_I(entry->inode), entry);
492 if (refcount_dec_and_test(&entry->refs)) {
493 ASSERT(list_empty(&entry->root_extent_list));
494 ASSERT(list_empty(&entry->log_list));
495 ASSERT(RB_EMPTY_NODE(&entry->rb_node));
497 btrfs_add_delayed_iput(entry->inode);
498 while (!list_empty(&entry->list)) {
499 cur = entry->list.next;
500 sum = list_entry(cur, struct btrfs_ordered_sum, list);
501 list_del(&sum->list);
504 kmem_cache_free(btrfs_ordered_extent_cache, entry);
509 * remove an ordered extent from the tree. No references are dropped
510 * and waiters are woken up.
512 void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode,
513 struct btrfs_ordered_extent *entry)
515 struct btrfs_ordered_inode_tree *tree;
516 struct btrfs_root *root = btrfs_inode->root;
517 struct btrfs_fs_info *fs_info = root->fs_info;
518 struct rb_node *node;
521 /* This is paired with btrfs_add_ordered_extent. */
522 spin_lock(&btrfs_inode->lock);
523 btrfs_mod_outstanding_extents(btrfs_inode, -1);
524 spin_unlock(&btrfs_inode->lock);
525 if (root != fs_info->tree_root) {
528 if (test_bit(BTRFS_ORDERED_ENCODED, &entry->flags))
529 release = entry->disk_num_bytes;
531 release = entry->num_bytes;
532 btrfs_delalloc_release_metadata(btrfs_inode, release, false);
535 percpu_counter_add_batch(&fs_info->ordered_bytes, -entry->num_bytes,
536 fs_info->delalloc_batch);
538 tree = &btrfs_inode->ordered_tree;
539 spin_lock_irq(&tree->lock);
540 node = &entry->rb_node;
541 rb_erase(node, &tree->tree);
543 if (tree->last == node)
545 set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
546 pending = test_and_clear_bit(BTRFS_ORDERED_PENDING, &entry->flags);
547 spin_unlock_irq(&tree->lock);
550 * The current running transaction is waiting on us, we need to let it
551 * know that we're complete and wake it up.
554 struct btrfs_transaction *trans;
557 * The checks for trans are just a formality, it should be set,
558 * but if it isn't we don't want to deref/assert under the spin
559 * lock, so be nice and check if trans is set, but ASSERT() so
560 * if it isn't set a developer will notice.
562 spin_lock(&fs_info->trans_lock);
563 trans = fs_info->running_transaction;
565 refcount_inc(&trans->use_count);
566 spin_unlock(&fs_info->trans_lock);
570 if (atomic_dec_and_test(&trans->pending_ordered))
571 wake_up(&trans->pending_wait);
572 btrfs_put_transaction(trans);
576 spin_lock(&root->ordered_extent_lock);
577 list_del_init(&entry->root_extent_list);
578 root->nr_ordered_extents--;
580 trace_btrfs_ordered_extent_remove(btrfs_inode, entry);
582 if (!root->nr_ordered_extents) {
583 spin_lock(&fs_info->ordered_root_lock);
584 BUG_ON(list_empty(&root->ordered_root));
585 list_del_init(&root->ordered_root);
586 spin_unlock(&fs_info->ordered_root_lock);
588 spin_unlock(&root->ordered_extent_lock);
589 wake_up(&entry->wait);
592 static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
594 struct btrfs_ordered_extent *ordered;
596 ordered = container_of(work, struct btrfs_ordered_extent, flush_work);
597 btrfs_start_ordered_extent(ordered, 1);
598 complete(&ordered->completion);
602 * wait for all the ordered extents in a root. This is done when balancing
603 * space between drives.
605 u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr,
606 const u64 range_start, const u64 range_len)
608 struct btrfs_fs_info *fs_info = root->fs_info;
612 struct btrfs_ordered_extent *ordered, *next;
614 const u64 range_end = range_start + range_len;
616 mutex_lock(&root->ordered_extent_mutex);
617 spin_lock(&root->ordered_extent_lock);
618 list_splice_init(&root->ordered_extents, &splice);
619 while (!list_empty(&splice) && nr) {
620 ordered = list_first_entry(&splice, struct btrfs_ordered_extent,
623 if (range_end <= ordered->disk_bytenr ||
624 ordered->disk_bytenr + ordered->disk_num_bytes <= range_start) {
625 list_move_tail(&ordered->root_extent_list, &skipped);
626 cond_resched_lock(&root->ordered_extent_lock);
630 list_move_tail(&ordered->root_extent_list,
631 &root->ordered_extents);
632 refcount_inc(&ordered->refs);
633 spin_unlock(&root->ordered_extent_lock);
635 btrfs_init_work(&ordered->flush_work,
636 btrfs_run_ordered_extent_work, NULL, NULL);
637 list_add_tail(&ordered->work_list, &works);
638 btrfs_queue_work(fs_info->flush_workers, &ordered->flush_work);
641 spin_lock(&root->ordered_extent_lock);
646 list_splice_tail(&skipped, &root->ordered_extents);
647 list_splice_tail(&splice, &root->ordered_extents);
648 spin_unlock(&root->ordered_extent_lock);
650 list_for_each_entry_safe(ordered, next, &works, work_list) {
651 list_del_init(&ordered->work_list);
652 wait_for_completion(&ordered->completion);
653 btrfs_put_ordered_extent(ordered);
656 mutex_unlock(&root->ordered_extent_mutex);
661 void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr,
662 const u64 range_start, const u64 range_len)
664 struct btrfs_root *root;
665 struct list_head splice;
668 INIT_LIST_HEAD(&splice);
670 mutex_lock(&fs_info->ordered_operations_mutex);
671 spin_lock(&fs_info->ordered_root_lock);
672 list_splice_init(&fs_info->ordered_roots, &splice);
673 while (!list_empty(&splice) && nr) {
674 root = list_first_entry(&splice, struct btrfs_root,
676 root = btrfs_grab_root(root);
678 list_move_tail(&root->ordered_root,
679 &fs_info->ordered_roots);
680 spin_unlock(&fs_info->ordered_root_lock);
682 done = btrfs_wait_ordered_extents(root, nr,
683 range_start, range_len);
684 btrfs_put_root(root);
686 spin_lock(&fs_info->ordered_root_lock);
691 list_splice_tail(&splice, &fs_info->ordered_roots);
692 spin_unlock(&fs_info->ordered_root_lock);
693 mutex_unlock(&fs_info->ordered_operations_mutex);
697 * Used to start IO or wait for a given ordered extent to finish.
699 * If wait is one, this effectively waits on page writeback for all the pages
700 * in the extent, and it waits on the io completion code to insert
701 * metadata into the btree corresponding to the extent
703 void btrfs_start_ordered_extent(struct btrfs_ordered_extent *entry, int wait)
705 u64 start = entry->file_offset;
706 u64 end = start + entry->num_bytes - 1;
707 struct btrfs_inode *inode = BTRFS_I(entry->inode);
709 trace_btrfs_ordered_extent_start(inode, entry);
712 * pages in the range can be dirty, clean or writeback. We
713 * start IO on any dirty ones so the wait doesn't stall waiting
714 * for the flusher thread to find them
716 if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
717 filemap_fdatawrite_range(inode->vfs_inode.i_mapping, start, end);
719 wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
725 * Used to wait on ordered extents across a large range of bytes.
727 int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
733 struct btrfs_ordered_extent *ordered;
735 if (start + len < start) {
736 orig_end = INT_LIMIT(loff_t);
738 orig_end = start + len - 1;
739 if (orig_end > INT_LIMIT(loff_t))
740 orig_end = INT_LIMIT(loff_t);
743 /* start IO across the range first to instantiate any delalloc
746 ret = btrfs_fdatawrite_range(inode, start, orig_end);
751 * If we have a writeback error don't return immediately. Wait first
752 * for any ordered extents that haven't completed yet. This is to make
753 * sure no one can dirty the same page ranges and call writepages()
754 * before the ordered extents complete - to avoid failures (-EEXIST)
755 * when adding the new ordered extents to the ordered tree.
757 ret_wb = filemap_fdatawait_range(inode->i_mapping, start, orig_end);
761 ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode), end);
764 if (ordered->file_offset > orig_end) {
765 btrfs_put_ordered_extent(ordered);
768 if (ordered->file_offset + ordered->num_bytes <= start) {
769 btrfs_put_ordered_extent(ordered);
772 btrfs_start_ordered_extent(ordered, 1);
773 end = ordered->file_offset;
775 * If the ordered extent had an error save the error but don't
776 * exit without waiting first for all other ordered extents in
777 * the range to complete.
779 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags))
781 btrfs_put_ordered_extent(ordered);
782 if (end == 0 || end == start)
786 return ret_wb ? ret_wb : ret;
790 * find an ordered extent corresponding to file_offset. return NULL if
791 * nothing is found, otherwise take a reference on the extent and return it
793 struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,
796 struct btrfs_ordered_inode_tree *tree;
797 struct rb_node *node;
798 struct btrfs_ordered_extent *entry = NULL;
801 tree = &inode->ordered_tree;
802 spin_lock_irqsave(&tree->lock, flags);
803 node = tree_search(tree, file_offset);
807 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
808 if (!in_range(file_offset, entry->file_offset, entry->num_bytes))
811 refcount_inc(&entry->refs);
813 spin_unlock_irqrestore(&tree->lock, flags);
817 /* Since the DIO code tries to lock a wide area we need to look for any ordered
818 * extents that exist in the range, rather than just the start of the range.
820 struct btrfs_ordered_extent *btrfs_lookup_ordered_range(
821 struct btrfs_inode *inode, u64 file_offset, u64 len)
823 struct btrfs_ordered_inode_tree *tree;
824 struct rb_node *node;
825 struct btrfs_ordered_extent *entry = NULL;
827 tree = &inode->ordered_tree;
828 spin_lock_irq(&tree->lock);
829 node = tree_search(tree, file_offset);
831 node = tree_search(tree, file_offset + len);
837 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
838 if (range_overlaps(entry, file_offset, len))
841 if (entry->file_offset >= file_offset + len) {
846 node = rb_next(node);
852 refcount_inc(&entry->refs);
853 spin_unlock_irq(&tree->lock);
858 * Adds all ordered extents to the given list. The list ends up sorted by the
859 * file_offset of the ordered extents.
861 void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode,
862 struct list_head *list)
864 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
867 ASSERT(inode_is_locked(&inode->vfs_inode));
869 spin_lock_irq(&tree->lock);
870 for (n = rb_first(&tree->tree); n; n = rb_next(n)) {
871 struct btrfs_ordered_extent *ordered;
873 ordered = rb_entry(n, struct btrfs_ordered_extent, rb_node);
875 if (test_bit(BTRFS_ORDERED_LOGGED, &ordered->flags))
878 ASSERT(list_empty(&ordered->log_list));
879 list_add_tail(&ordered->log_list, list);
880 refcount_inc(&ordered->refs);
882 spin_unlock_irq(&tree->lock);
886 * lookup and return any extent before 'file_offset'. NULL is returned
889 struct btrfs_ordered_extent *
890 btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset)
892 struct btrfs_ordered_inode_tree *tree;
893 struct rb_node *node;
894 struct btrfs_ordered_extent *entry = NULL;
896 tree = &inode->ordered_tree;
897 spin_lock_irq(&tree->lock);
898 node = tree_search(tree, file_offset);
902 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
903 refcount_inc(&entry->refs);
905 spin_unlock_irq(&tree->lock);
910 * Lookup the first ordered extent that overlaps the range
911 * [@file_offset, @file_offset + @len).
913 * The difference between this and btrfs_lookup_first_ordered_extent() is
914 * that this one won't return any ordered extent that does not overlap the range.
915 * And the difference against btrfs_lookup_ordered_extent() is, this function
916 * ensures the first ordered extent gets returned.
918 struct btrfs_ordered_extent *btrfs_lookup_first_ordered_range(
919 struct btrfs_inode *inode, u64 file_offset, u64 len)
921 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
922 struct rb_node *node;
924 struct rb_node *prev;
925 struct rb_node *next;
926 struct btrfs_ordered_extent *entry = NULL;
928 spin_lock_irq(&tree->lock);
929 node = tree->tree.rb_node;
931 * Here we don't want to use tree_search() which will use tree->last
932 * and screw up the search order.
933 * And __tree_search() can't return the adjacent ordered extents
934 * either, thus here we do our own search.
937 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
939 if (file_offset < entry->file_offset) {
940 node = node->rb_left;
941 } else if (file_offset >= entry_end(entry)) {
942 node = node->rb_right;
945 * Direct hit, got an ordered extent that starts at
956 cur = &entry->rb_node;
957 /* We got an entry around @file_offset, check adjacent entries */
958 if (entry->file_offset < file_offset) {
966 entry = rb_entry(prev, struct btrfs_ordered_extent, rb_node);
967 if (range_overlaps(entry, file_offset, len))
971 entry = rb_entry(next, struct btrfs_ordered_extent, rb_node);
972 if (range_overlaps(entry, file_offset, len))
975 /* No ordered extent in the range */
979 refcount_inc(&entry->refs);
980 spin_unlock_irq(&tree->lock);
985 * btrfs_flush_ordered_range - Lock the passed range and ensures all pending
986 * ordered extents in it are run to completion.
988 * @inode: Inode whose ordered tree is to be searched
989 * @start: Beginning of range to flush
990 * @end: Last byte of range to lock
991 * @cached_state: If passed, will return the extent state responsible for the
992 * locked range. It's the caller's responsibility to free the cached state.
994 * This function always returns with the given range locked, ensuring after it's
995 * called no order extent can be pending.
997 void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,
999 struct extent_state **cached_state)
1001 struct btrfs_ordered_extent *ordered;
1002 struct extent_state *cache = NULL;
1003 struct extent_state **cachedp = &cache;
1006 cachedp = cached_state;
1009 lock_extent_bits(&inode->io_tree, start, end, cachedp);
1010 ordered = btrfs_lookup_ordered_range(inode, start,
1014 * If no external cached_state has been passed then
1015 * decrement the extra ref taken for cachedp since we
1016 * aren't exposing it outside of this function
1019 refcount_dec(&cache->refs);
1022 unlock_extent_cached(&inode->io_tree, start, end, cachedp);
1023 btrfs_start_ordered_extent(ordered, 1);
1024 btrfs_put_ordered_extent(ordered);
1028 static int clone_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pos,
1031 struct inode *inode = ordered->inode;
1032 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1033 u64 file_offset = ordered->file_offset + pos;
1034 u64 disk_bytenr = ordered->disk_bytenr + pos;
1035 unsigned long flags = ordered->flags & BTRFS_ORDERED_TYPE_FLAGS;
1038 * The splitting extent is already counted and will be added again in
1039 * btrfs_add_ordered_extent_*(). Subtract len to avoid double counting.
1041 percpu_counter_add_batch(&fs_info->ordered_bytes, -len,
1042 fs_info->delalloc_batch);
1043 WARN_ON_ONCE(flags & (1 << BTRFS_ORDERED_COMPRESSED));
1044 return btrfs_add_ordered_extent(BTRFS_I(inode), file_offset, len, len,
1045 disk_bytenr, len, 0, flags,
1046 ordered->compress_type);
1049 int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pre,
1052 struct inode *inode = ordered->inode;
1053 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
1054 struct rb_node *node;
1055 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1058 spin_lock_irq(&tree->lock);
1059 /* Remove from tree once */
1060 node = &ordered->rb_node;
1061 rb_erase(node, &tree->tree);
1062 RB_CLEAR_NODE(node);
1063 if (tree->last == node)
1066 ordered->file_offset += pre;
1067 ordered->disk_bytenr += pre;
1068 ordered->num_bytes -= (pre + post);
1069 ordered->disk_num_bytes -= (pre + post);
1070 ordered->bytes_left -= (pre + post);
1072 /* Re-insert the node */
1073 node = tree_insert(&tree->tree, ordered->file_offset, &ordered->rb_node);
1075 btrfs_panic(fs_info, -EEXIST,
1076 "zoned: inconsistency in ordered tree at offset %llu",
1077 ordered->file_offset);
1079 spin_unlock_irq(&tree->lock);
1082 ret = clone_ordered_extent(ordered, 0, pre);
1083 if (ret == 0 && post)
1084 ret = clone_ordered_extent(ordered, pre + ordered->disk_num_bytes,
1090 int __init ordered_data_init(void)
1092 btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent",
1093 sizeof(struct btrfs_ordered_extent), 0,
1096 if (!btrfs_ordered_extent_cache)
1102 void __cold ordered_data_exit(void)
1104 kmem_cache_destroy(btrfs_ordered_extent_cache);