Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 */
5
6#include <linux/slab.h>
7#include <linux/blkdev.h>
8#include <linux/writeback.h>
9#include <linux/sched/mm.h>
10#include "misc.h"
11#include "ctree.h"
12#include "transaction.h"
13#include "btrfs_inode.h"
14#include "extent_io.h"
15#include "disk-io.h"
16#include "compression.h"
17#include "delalloc-space.h"
18
19static struct kmem_cache *btrfs_ordered_extent_cache;
20
21static u64 entry_end(struct btrfs_ordered_extent *entry)
22{
23 if (entry->file_offset + entry->len < entry->file_offset)
24 return (u64)-1;
25 return entry->file_offset + entry->len;
26}
27
28/* returns NULL if the insertion worked, or it returns the node it did find
29 * in the tree
30 */
31static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset,
32 struct rb_node *node)
33{
34 struct rb_node **p = &root->rb_node;
35 struct rb_node *parent = NULL;
36 struct btrfs_ordered_extent *entry;
37
38 while (*p) {
39 parent = *p;
40 entry = rb_entry(parent, struct btrfs_ordered_extent, rb_node);
41
42 if (file_offset < entry->file_offset)
43 p = &(*p)->rb_left;
44 else if (file_offset >= entry_end(entry))
45 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
55static void ordered_data_tree_panic(struct inode *inode, int errno,
56 u64 offset)
57{
58 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
59 btrfs_panic(fs_info, errno,
60 "Inconsistency in ordered tree at offset %llu", offset);
61}
62
63/*
64 * look for a given offset in the tree, and if it can't be found return the
65 * first lesser offset
66 */
67static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset,
68 struct rb_node **prev_ret)
69{
70 struct rb_node *n = root->rb_node;
71 struct rb_node *prev = NULL;
72 struct rb_node *test;
73 struct btrfs_ordered_extent *entry;
74 struct btrfs_ordered_extent *prev_entry = NULL;
75
76 while (n) {
77 entry = rb_entry(n, struct btrfs_ordered_extent, rb_node);
78 prev = n;
79 prev_entry = entry;
80
81 if (file_offset < entry->file_offset)
82 n = n->rb_left;
83 else if (file_offset >= entry_end(entry))
84 n = n->rb_right;
85 else
86 return n;
87 }
88 if (!prev_ret)
89 return NULL;
90
91 while (prev && file_offset >= entry_end(prev_entry)) {
92 test = rb_next(prev);
93 if (!test)
94 break;
95 prev_entry = rb_entry(test, struct btrfs_ordered_extent,
96 rb_node);
97 if (file_offset < entry_end(prev_entry))
98 break;
99
100 prev = test;
101 }
102 if (prev)
103 prev_entry = rb_entry(prev, struct btrfs_ordered_extent,
104 rb_node);
105 while (prev && file_offset < entry_end(prev_entry)) {
106 test = rb_prev(prev);
107 if (!test)
108 break;
109 prev_entry = rb_entry(test, struct btrfs_ordered_extent,
110 rb_node);
111 prev = test;
112 }
113 *prev_ret = prev;
114 return NULL;
115}
116
117/*
118 * helper to check if a given offset is inside a given entry
119 */
120static int offset_in_entry(struct btrfs_ordered_extent *entry, u64 file_offset)
121{
122 if (file_offset < entry->file_offset ||
123 entry->file_offset + entry->len <= file_offset)
124 return 0;
125 return 1;
126}
127
128static int range_overlaps(struct btrfs_ordered_extent *entry, u64 file_offset,
129 u64 len)
130{
131 if (file_offset + len <= entry->file_offset ||
132 entry->file_offset + entry->len <= file_offset)
133 return 0;
134 return 1;
135}
136
137/*
138 * look find the first ordered struct that has this offset, otherwise
139 * the first one less than this offset
140 */
141static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,
142 u64 file_offset)
143{
144 struct rb_root *root = &tree->tree;
145 struct rb_node *prev = NULL;
146 struct rb_node *ret;
147 struct btrfs_ordered_extent *entry;
148
149 if (tree->last) {
150 entry = rb_entry(tree->last, struct btrfs_ordered_extent,
151 rb_node);
152 if (offset_in_entry(entry, file_offset))
153 return tree->last;
154 }
155 ret = __tree_search(root, file_offset, &prev);
156 if (!ret)
157 ret = prev;
158 if (ret)
159 tree->last = ret;
160 return ret;
161}
162
163/* allocate and add a new ordered_extent into the per-inode tree.
164 * file_offset is the logical offset in the file
165 *
166 * start is the disk block number of an extent already reserved in the
167 * extent allocation tree
168 *
169 * len is the length of the extent
170 *
171 * The tree is given a single reference on the ordered extent that was
172 * inserted.
173 */
174static int __btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
175 u64 start, u64 len, u64 disk_len,
176 int type, int dio, int compress_type)
177{
178 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
179 struct btrfs_root *root = BTRFS_I(inode)->root;
180 struct btrfs_ordered_inode_tree *tree;
181 struct rb_node *node;
182 struct btrfs_ordered_extent *entry;
183
184 tree = &BTRFS_I(inode)->ordered_tree;
185 entry = kmem_cache_zalloc(btrfs_ordered_extent_cache, GFP_NOFS);
186 if (!entry)
187 return -ENOMEM;
188
189 entry->file_offset = file_offset;
190 entry->start = start;
191 entry->len = len;
192 entry->disk_len = disk_len;
193 entry->bytes_left = len;
194 entry->inode = igrab(inode);
195 entry->compress_type = compress_type;
196 entry->truncated_len = (u64)-1;
197 if (type != BTRFS_ORDERED_IO_DONE && type != BTRFS_ORDERED_COMPLETE)
198 set_bit(type, &entry->flags);
199
200 if (dio) {
201 percpu_counter_add_batch(&fs_info->dio_bytes, len,
202 fs_info->delalloc_batch);
203 set_bit(BTRFS_ORDERED_DIRECT, &entry->flags);
204 }
205
206 /* one ref for the tree */
207 refcount_set(&entry->refs, 1);
208 init_waitqueue_head(&entry->wait);
209 INIT_LIST_HEAD(&entry->list);
210 INIT_LIST_HEAD(&entry->root_extent_list);
211 INIT_LIST_HEAD(&entry->work_list);
212 init_completion(&entry->completion);
213 INIT_LIST_HEAD(&entry->log_list);
214 INIT_LIST_HEAD(&entry->trans_list);
215
216 trace_btrfs_ordered_extent_add(inode, entry);
217
218 spin_lock_irq(&tree->lock);
219 node = tree_insert(&tree->tree, file_offset,
220 &entry->rb_node);
221 if (node)
222 ordered_data_tree_panic(inode, -EEXIST, file_offset);
223 spin_unlock_irq(&tree->lock);
224
225 spin_lock(&root->ordered_extent_lock);
226 list_add_tail(&entry->root_extent_list,
227 &root->ordered_extents);
228 root->nr_ordered_extents++;
229 if (root->nr_ordered_extents == 1) {
230 spin_lock(&fs_info->ordered_root_lock);
231 BUG_ON(!list_empty(&root->ordered_root));
232 list_add_tail(&root->ordered_root, &fs_info->ordered_roots);
233 spin_unlock(&fs_info->ordered_root_lock);
234 }
235 spin_unlock(&root->ordered_extent_lock);
236
237 /*
238 * We don't need the count_max_extents here, we can assume that all of
239 * that work has been done at higher layers, so this is truly the
240 * smallest the extent is going to get.
241 */
242 spin_lock(&BTRFS_I(inode)->lock);
243 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
244 spin_unlock(&BTRFS_I(inode)->lock);
245
246 return 0;
247}
248
249int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
250 u64 start, u64 len, u64 disk_len, int type)
251{
252 return __btrfs_add_ordered_extent(inode, file_offset, start, len,
253 disk_len, type, 0,
254 BTRFS_COMPRESS_NONE);
255}
256
257int btrfs_add_ordered_extent_dio(struct inode *inode, u64 file_offset,
258 u64 start, u64 len, u64 disk_len, int type)
259{
260 return __btrfs_add_ordered_extent(inode, file_offset, start, len,
261 disk_len, type, 1,
262 BTRFS_COMPRESS_NONE);
263}
264
265int btrfs_add_ordered_extent_compress(struct inode *inode, u64 file_offset,
266 u64 start, u64 len, u64 disk_len,
267 int type, int compress_type)
268{
269 return __btrfs_add_ordered_extent(inode, file_offset, start, len,
270 disk_len, type, 0,
271 compress_type);
272}
273
274/*
275 * Add a struct btrfs_ordered_sum into the list of checksums to be inserted
276 * when an ordered extent is finished. If the list covers more than one
277 * ordered extent, it is split across multiples.
278 */
279void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
280 struct btrfs_ordered_sum *sum)
281{
282 struct btrfs_ordered_inode_tree *tree;
283
284 tree = &BTRFS_I(entry->inode)->ordered_tree;
285 spin_lock_irq(&tree->lock);
286 list_add_tail(&sum->list, &entry->list);
287 spin_unlock_irq(&tree->lock);
288}
289
290/*
291 * this is used to account for finished IO across a given range
292 * of the file. The IO may span ordered extents. If
293 * a given ordered_extent is completely done, 1 is returned, otherwise
294 * 0.
295 *
296 * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
297 * to make sure this function only returns 1 once for a given ordered extent.
298 *
299 * file_offset is updated to one byte past the range that is recorded as
300 * complete. This allows you to walk forward in the file.
301 */
302int btrfs_dec_test_first_ordered_pending(struct inode *inode,
303 struct btrfs_ordered_extent **cached,
304 u64 *file_offset, u64 io_size, int uptodate)
305{
306 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
307 struct btrfs_ordered_inode_tree *tree;
308 struct rb_node *node;
309 struct btrfs_ordered_extent *entry = NULL;
310 int ret;
311 unsigned long flags;
312 u64 dec_end;
313 u64 dec_start;
314 u64 to_dec;
315
316 tree = &BTRFS_I(inode)->ordered_tree;
317 spin_lock_irqsave(&tree->lock, flags);
318 node = tree_search(tree, *file_offset);
319 if (!node) {
320 ret = 1;
321 goto out;
322 }
323
324 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
325 if (!offset_in_entry(entry, *file_offset)) {
326 ret = 1;
327 goto out;
328 }
329
330 dec_start = max(*file_offset, entry->file_offset);
331 dec_end = min(*file_offset + io_size, entry->file_offset +
332 entry->len);
333 *file_offset = dec_end;
334 if (dec_start > dec_end) {
335 btrfs_crit(fs_info, "bad ordering dec_start %llu end %llu",
336 dec_start, dec_end);
337 }
338 to_dec = dec_end - dec_start;
339 if (to_dec > entry->bytes_left) {
340 btrfs_crit(fs_info,
341 "bad ordered accounting left %llu size %llu",
342 entry->bytes_left, to_dec);
343 }
344 entry->bytes_left -= to_dec;
345 if (!uptodate)
346 set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
347
348 if (entry->bytes_left == 0) {
349 ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
350 /* test_and_set_bit implies a barrier */
351 cond_wake_up_nomb(&entry->wait);
352 } else {
353 ret = 1;
354 }
355out:
356 if (!ret && cached && entry) {
357 *cached = entry;
358 refcount_inc(&entry->refs);
359 }
360 spin_unlock_irqrestore(&tree->lock, flags);
361 return ret == 0;
362}
363
364/*
365 * this is used to account for finished IO across a given range
366 * of the file. The IO should not span ordered extents. If
367 * a given ordered_extent is completely done, 1 is returned, otherwise
368 * 0.
369 *
370 * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
371 * to make sure this function only returns 1 once for a given ordered extent.
372 */
373int btrfs_dec_test_ordered_pending(struct inode *inode,
374 struct btrfs_ordered_extent **cached,
375 u64 file_offset, u64 io_size, int uptodate)
376{
377 struct btrfs_ordered_inode_tree *tree;
378 struct rb_node *node;
379 struct btrfs_ordered_extent *entry = NULL;
380 unsigned long flags;
381 int ret;
382
383 tree = &BTRFS_I(inode)->ordered_tree;
384 spin_lock_irqsave(&tree->lock, flags);
385 if (cached && *cached) {
386 entry = *cached;
387 goto have_entry;
388 }
389
390 node = tree_search(tree, file_offset);
391 if (!node) {
392 ret = 1;
393 goto out;
394 }
395
396 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
397have_entry:
398 if (!offset_in_entry(entry, file_offset)) {
399 ret = 1;
400 goto out;
401 }
402
403 if (io_size > entry->bytes_left) {
404 btrfs_crit(BTRFS_I(inode)->root->fs_info,
405 "bad ordered accounting left %llu size %llu",
406 entry->bytes_left, io_size);
407 }
408 entry->bytes_left -= io_size;
409 if (!uptodate)
410 set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
411
412 if (entry->bytes_left == 0) {
413 ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
414 /* test_and_set_bit implies a barrier */
415 cond_wake_up_nomb(&entry->wait);
416 } else {
417 ret = 1;
418 }
419out:
420 if (!ret && cached && entry) {
421 *cached = entry;
422 refcount_inc(&entry->refs);
423 }
424 spin_unlock_irqrestore(&tree->lock, flags);
425 return ret == 0;
426}
427
428/*
429 * used to drop a reference on an ordered extent. This will free
430 * the extent if the last reference is dropped
431 */
432void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
433{
434 struct list_head *cur;
435 struct btrfs_ordered_sum *sum;
436
437 trace_btrfs_ordered_extent_put(entry->inode, entry);
438
439 if (refcount_dec_and_test(&entry->refs)) {
440 ASSERT(list_empty(&entry->log_list));
441 ASSERT(list_empty(&entry->trans_list));
442 ASSERT(list_empty(&entry->root_extent_list));
443 ASSERT(RB_EMPTY_NODE(&entry->rb_node));
444 if (entry->inode)
445 btrfs_add_delayed_iput(entry->inode);
446 while (!list_empty(&entry->list)) {
447 cur = entry->list.next;
448 sum = list_entry(cur, struct btrfs_ordered_sum, list);
449 list_del(&sum->list);
450 kvfree(sum);
451 }
452 kmem_cache_free(btrfs_ordered_extent_cache, entry);
453 }
454}
455
456/*
457 * remove an ordered extent from the tree. No references are dropped
458 * and waiters are woken up.
459 */
460void btrfs_remove_ordered_extent(struct inode *inode,
461 struct btrfs_ordered_extent *entry)
462{
463 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
464 struct btrfs_ordered_inode_tree *tree;
465 struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
466 struct btrfs_root *root = btrfs_inode->root;
467 struct rb_node *node;
468
469 /* This is paired with btrfs_add_ordered_extent. */
470 spin_lock(&btrfs_inode->lock);
471 btrfs_mod_outstanding_extents(btrfs_inode, -1);
472 spin_unlock(&btrfs_inode->lock);
473 if (root != fs_info->tree_root)
474 btrfs_delalloc_release_metadata(btrfs_inode, entry->len, false);
475
476 if (test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
477 percpu_counter_add_batch(&fs_info->dio_bytes, -entry->len,
478 fs_info->delalloc_batch);
479
480 tree = &btrfs_inode->ordered_tree;
481 spin_lock_irq(&tree->lock);
482 node = &entry->rb_node;
483 rb_erase(node, &tree->tree);
484 RB_CLEAR_NODE(node);
485 if (tree->last == node)
486 tree->last = NULL;
487 set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
488 spin_unlock_irq(&tree->lock);
489
490 spin_lock(&root->ordered_extent_lock);
491 list_del_init(&entry->root_extent_list);
492 root->nr_ordered_extents--;
493
494 trace_btrfs_ordered_extent_remove(inode, entry);
495
496 if (!root->nr_ordered_extents) {
497 spin_lock(&fs_info->ordered_root_lock);
498 BUG_ON(list_empty(&root->ordered_root));
499 list_del_init(&root->ordered_root);
500 spin_unlock(&fs_info->ordered_root_lock);
501 }
502 spin_unlock(&root->ordered_extent_lock);
503 wake_up(&entry->wait);
504}
505
506static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
507{
508 struct btrfs_ordered_extent *ordered;
509
510 ordered = container_of(work, struct btrfs_ordered_extent, flush_work);
511 btrfs_start_ordered_extent(ordered->inode, ordered, 1);
512 complete(&ordered->completion);
513}
514
515/*
516 * wait for all the ordered extents in a root. This is done when balancing
517 * space between drives.
518 */
519u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr,
520 const u64 range_start, const u64 range_len)
521{
522 struct btrfs_fs_info *fs_info = root->fs_info;
523 LIST_HEAD(splice);
524 LIST_HEAD(skipped);
525 LIST_HEAD(works);
526 struct btrfs_ordered_extent *ordered, *next;
527 u64 count = 0;
528 const u64 range_end = range_start + range_len;
529
530 mutex_lock(&root->ordered_extent_mutex);
531 spin_lock(&root->ordered_extent_lock);
532 list_splice_init(&root->ordered_extents, &splice);
533 while (!list_empty(&splice) && nr) {
534 ordered = list_first_entry(&splice, struct btrfs_ordered_extent,
535 root_extent_list);
536
537 if (range_end <= ordered->start ||
538 ordered->start + ordered->disk_len <= range_start) {
539 list_move_tail(&ordered->root_extent_list, &skipped);
540 cond_resched_lock(&root->ordered_extent_lock);
541 continue;
542 }
543
544 list_move_tail(&ordered->root_extent_list,
545 &root->ordered_extents);
546 refcount_inc(&ordered->refs);
547 spin_unlock(&root->ordered_extent_lock);
548
549 btrfs_init_work(&ordered->flush_work,
550 btrfs_flush_delalloc_helper,
551 btrfs_run_ordered_extent_work, NULL, NULL);
552 list_add_tail(&ordered->work_list, &works);
553 btrfs_queue_work(fs_info->flush_workers, &ordered->flush_work);
554
555 cond_resched();
556 spin_lock(&root->ordered_extent_lock);
557 if (nr != U64_MAX)
558 nr--;
559 count++;
560 }
561 list_splice_tail(&skipped, &root->ordered_extents);
562 list_splice_tail(&splice, &root->ordered_extents);
563 spin_unlock(&root->ordered_extent_lock);
564
565 list_for_each_entry_safe(ordered, next, &works, work_list) {
566 list_del_init(&ordered->work_list);
567 wait_for_completion(&ordered->completion);
568 btrfs_put_ordered_extent(ordered);
569 cond_resched();
570 }
571 mutex_unlock(&root->ordered_extent_mutex);
572
573 return count;
574}
575
576u64 btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr,
577 const u64 range_start, const u64 range_len)
578{
579 struct btrfs_root *root;
580 struct list_head splice;
581 u64 total_done = 0;
582 u64 done;
583
584 INIT_LIST_HEAD(&splice);
585
586 mutex_lock(&fs_info->ordered_operations_mutex);
587 spin_lock(&fs_info->ordered_root_lock);
588 list_splice_init(&fs_info->ordered_roots, &splice);
589 while (!list_empty(&splice) && nr) {
590 root = list_first_entry(&splice, struct btrfs_root,
591 ordered_root);
592 root = btrfs_grab_fs_root(root);
593 BUG_ON(!root);
594 list_move_tail(&root->ordered_root,
595 &fs_info->ordered_roots);
596 spin_unlock(&fs_info->ordered_root_lock);
597
598 done = btrfs_wait_ordered_extents(root, nr,
599 range_start, range_len);
600 btrfs_put_fs_root(root);
601 total_done += done;
602
603 spin_lock(&fs_info->ordered_root_lock);
604 if (nr != U64_MAX) {
605 nr -= done;
606 }
607 }
608 list_splice_tail(&splice, &fs_info->ordered_roots);
609 spin_unlock(&fs_info->ordered_root_lock);
610 mutex_unlock(&fs_info->ordered_operations_mutex);
611
612 return total_done;
613}
614
615/*
616 * Used to start IO or wait for a given ordered extent to finish.
617 *
618 * If wait is one, this effectively waits on page writeback for all the pages
619 * in the extent, and it waits on the io completion code to insert
620 * metadata into the btree corresponding to the extent
621 */
622void btrfs_start_ordered_extent(struct inode *inode,
623 struct btrfs_ordered_extent *entry,
624 int wait)
625{
626 u64 start = entry->file_offset;
627 u64 end = start + entry->len - 1;
628
629 trace_btrfs_ordered_extent_start(inode, entry);
630
631 /*
632 * pages in the range can be dirty, clean or writeback. We
633 * start IO on any dirty ones so the wait doesn't stall waiting
634 * for the flusher thread to find them
635 */
636 if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
637 filemap_fdatawrite_range(inode->i_mapping, start, end);
638 if (wait) {
639 wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
640 &entry->flags));
641 }
642}
643
644/*
645 * Used to wait on ordered extents across a large range of bytes.
646 */
647int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
648{
649 int ret = 0;
650 int ret_wb = 0;
651 u64 end;
652 u64 orig_end;
653 struct btrfs_ordered_extent *ordered;
654
655 if (start + len < start) {
656 orig_end = INT_LIMIT(loff_t);
657 } else {
658 orig_end = start + len - 1;
659 if (orig_end > INT_LIMIT(loff_t))
660 orig_end = INT_LIMIT(loff_t);
661 }
662
663 /* start IO across the range first to instantiate any delalloc
664 * extents
665 */
666 ret = btrfs_fdatawrite_range(inode, start, orig_end);
667 if (ret)
668 return ret;
669
670 /*
671 * If we have a writeback error don't return immediately. Wait first
672 * for any ordered extents that haven't completed yet. This is to make
673 * sure no one can dirty the same page ranges and call writepages()
674 * before the ordered extents complete - to avoid failures (-EEXIST)
675 * when adding the new ordered extents to the ordered tree.
676 */
677 ret_wb = filemap_fdatawait_range(inode->i_mapping, start, orig_end);
678
679 end = orig_end;
680 while (1) {
681 ordered = btrfs_lookup_first_ordered_extent(inode, end);
682 if (!ordered)
683 break;
684 if (ordered->file_offset > orig_end) {
685 btrfs_put_ordered_extent(ordered);
686 break;
687 }
688 if (ordered->file_offset + ordered->len <= start) {
689 btrfs_put_ordered_extent(ordered);
690 break;
691 }
692 btrfs_start_ordered_extent(inode, ordered, 1);
693 end = ordered->file_offset;
694 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags))
695 ret = -EIO;
696 btrfs_put_ordered_extent(ordered);
697 if (ret || end == 0 || end == start)
698 break;
699 end--;
700 }
701 return ret_wb ? ret_wb : ret;
702}
703
704/*
705 * find an ordered extent corresponding to file_offset. return NULL if
706 * nothing is found, otherwise take a reference on the extent and return it
707 */
708struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
709 u64 file_offset)
710{
711 struct btrfs_ordered_inode_tree *tree;
712 struct rb_node *node;
713 struct btrfs_ordered_extent *entry = NULL;
714
715 tree = &BTRFS_I(inode)->ordered_tree;
716 spin_lock_irq(&tree->lock);
717 node = tree_search(tree, file_offset);
718 if (!node)
719 goto out;
720
721 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
722 if (!offset_in_entry(entry, file_offset))
723 entry = NULL;
724 if (entry)
725 refcount_inc(&entry->refs);
726out:
727 spin_unlock_irq(&tree->lock);
728 return entry;
729}
730
731/* Since the DIO code tries to lock a wide area we need to look for any ordered
732 * extents that exist in the range, rather than just the start of the range.
733 */
734struct btrfs_ordered_extent *btrfs_lookup_ordered_range(
735 struct btrfs_inode *inode, u64 file_offset, u64 len)
736{
737 struct btrfs_ordered_inode_tree *tree;
738 struct rb_node *node;
739 struct btrfs_ordered_extent *entry = NULL;
740
741 tree = &inode->ordered_tree;
742 spin_lock_irq(&tree->lock);
743 node = tree_search(tree, file_offset);
744 if (!node) {
745 node = tree_search(tree, file_offset + len);
746 if (!node)
747 goto out;
748 }
749
750 while (1) {
751 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
752 if (range_overlaps(entry, file_offset, len))
753 break;
754
755 if (entry->file_offset >= file_offset + len) {
756 entry = NULL;
757 break;
758 }
759 entry = NULL;
760 node = rb_next(node);
761 if (!node)
762 break;
763 }
764out:
765 if (entry)
766 refcount_inc(&entry->refs);
767 spin_unlock_irq(&tree->lock);
768 return entry;
769}
770
771/*
772 * lookup and return any extent before 'file_offset'. NULL is returned
773 * if none is found
774 */
775struct btrfs_ordered_extent *
776btrfs_lookup_first_ordered_extent(struct inode *inode, u64 file_offset)
777{
778 struct btrfs_ordered_inode_tree *tree;
779 struct rb_node *node;
780 struct btrfs_ordered_extent *entry = NULL;
781
782 tree = &BTRFS_I(inode)->ordered_tree;
783 spin_lock_irq(&tree->lock);
784 node = tree_search(tree, file_offset);
785 if (!node)
786 goto out;
787
788 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
789 refcount_inc(&entry->refs);
790out:
791 spin_unlock_irq(&tree->lock);
792 return entry;
793}
794
795/*
796 * After an extent is done, call this to conditionally update the on disk
797 * i_size. i_size is updated to cover any fully written part of the file.
798 */
799int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
800 struct btrfs_ordered_extent *ordered)
801{
802 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
803 u64 disk_i_size;
804 u64 new_i_size;
805 u64 i_size = i_size_read(inode);
806 struct rb_node *node;
807 struct rb_node *prev = NULL;
808 struct btrfs_ordered_extent *test;
809 int ret = 1;
810 u64 orig_offset = offset;
811
812 spin_lock_irq(&tree->lock);
813 if (ordered) {
814 offset = entry_end(ordered);
815 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags))
816 offset = min(offset,
817 ordered->file_offset +
818 ordered->truncated_len);
819 } else {
820 offset = ALIGN(offset, btrfs_inode_sectorsize(inode));
821 }
822 disk_i_size = BTRFS_I(inode)->disk_i_size;
823
824 /*
825 * truncate file.
826 * If ordered is not NULL, then this is called from endio and
827 * disk_i_size will be updated by either truncate itself or any
828 * in-flight IOs which are inside the disk_i_size.
829 *
830 * Because btrfs_setsize() may set i_size with disk_i_size if truncate
831 * fails somehow, we need to make sure we have a precise disk_i_size by
832 * updating it as usual.
833 *
834 */
835 if (!ordered && disk_i_size > i_size) {
836 BTRFS_I(inode)->disk_i_size = orig_offset;
837 ret = 0;
838 goto out;
839 }
840
841 /*
842 * if the disk i_size is already at the inode->i_size, or
843 * this ordered extent is inside the disk i_size, we're done
844 */
845 if (disk_i_size == i_size)
846 goto out;
847
848 /*
849 * We still need to update disk_i_size if outstanding_isize is greater
850 * than disk_i_size.
851 */
852 if (offset <= disk_i_size &&
853 (!ordered || ordered->outstanding_isize <= disk_i_size))
854 goto out;
855
856 /*
857 * walk backward from this ordered extent to disk_i_size.
858 * if we find an ordered extent then we can't update disk i_size
859 * yet
860 */
861 if (ordered) {
862 node = rb_prev(&ordered->rb_node);
863 } else {
864 prev = tree_search(tree, offset);
865 /*
866 * we insert file extents without involving ordered struct,
867 * so there should be no ordered struct cover this offset
868 */
869 if (prev) {
870 test = rb_entry(prev, struct btrfs_ordered_extent,
871 rb_node);
872 BUG_ON(offset_in_entry(test, offset));
873 }
874 node = prev;
875 }
876 for (; node; node = rb_prev(node)) {
877 test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
878
879 /* We treat this entry as if it doesn't exist */
880 if (test_bit(BTRFS_ORDERED_UPDATED_ISIZE, &test->flags))
881 continue;
882
883 if (entry_end(test) <= disk_i_size)
884 break;
885 if (test->file_offset >= i_size)
886 break;
887
888 /*
889 * We don't update disk_i_size now, so record this undealt
890 * i_size. Or we will not know the real i_size.
891 */
892 if (test->outstanding_isize < offset)
893 test->outstanding_isize = offset;
894 if (ordered &&
895 ordered->outstanding_isize > test->outstanding_isize)
896 test->outstanding_isize = ordered->outstanding_isize;
897 goto out;
898 }
899 new_i_size = min_t(u64, offset, i_size);
900
901 /*
902 * Some ordered extents may completed before the current one, and
903 * we hold the real i_size in ->outstanding_isize.
904 */
905 if (ordered && ordered->outstanding_isize > new_i_size)
906 new_i_size = min_t(u64, ordered->outstanding_isize, i_size);
907 BTRFS_I(inode)->disk_i_size = new_i_size;
908 ret = 0;
909out:
910 /*
911 * We need to do this because we can't remove ordered extents until
912 * after the i_disk_size has been updated and then the inode has been
913 * updated to reflect the change, so we need to tell anybody who finds
914 * this ordered extent that we've already done all the real work, we
915 * just haven't completed all the other work.
916 */
917 if (ordered)
918 set_bit(BTRFS_ORDERED_UPDATED_ISIZE, &ordered->flags);
919 spin_unlock_irq(&tree->lock);
920 return ret;
921}
922
923/*
924 * search the ordered extents for one corresponding to 'offset' and
925 * try to find a checksum. This is used because we allow pages to
926 * be reclaimed before their checksum is actually put into the btree
927 */
928int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
929 u8 *sum, int len)
930{
931 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
932 struct btrfs_ordered_sum *ordered_sum;
933 struct btrfs_ordered_extent *ordered;
934 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
935 unsigned long num_sectors;
936 unsigned long i;
937 u32 sectorsize = btrfs_inode_sectorsize(inode);
938 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
939 int index = 0;
940
941 ordered = btrfs_lookup_ordered_extent(inode, offset);
942 if (!ordered)
943 return 0;
944
945 spin_lock_irq(&tree->lock);
946 list_for_each_entry_reverse(ordered_sum, &ordered->list, list) {
947 if (disk_bytenr >= ordered_sum->bytenr &&
948 disk_bytenr < ordered_sum->bytenr + ordered_sum->len) {
949 i = (disk_bytenr - ordered_sum->bytenr) >>
950 inode->i_sb->s_blocksize_bits;
951 num_sectors = ordered_sum->len >>
952 inode->i_sb->s_blocksize_bits;
953 num_sectors = min_t(int, len - index, num_sectors - i);
954 memcpy(sum + index, ordered_sum->sums + i * csum_size,
955 num_sectors * csum_size);
956
957 index += (int)num_sectors * csum_size;
958 if (index == len)
959 goto out;
960 disk_bytenr += num_sectors * sectorsize;
961 }
962 }
963out:
964 spin_unlock_irq(&tree->lock);
965 btrfs_put_ordered_extent(ordered);
966 return index;
967}
968
969/*
970 * btrfs_flush_ordered_range - Lock the passed range and ensures all pending
971 * ordered extents in it are run to completion.
972 *
973 * @tree: IO tree used for locking out other users of the range
974 * @inode: Inode whose ordered tree is to be searched
975 * @start: Beginning of range to flush
976 * @end: Last byte of range to lock
977 * @cached_state: If passed, will return the extent state responsible for the
978 * locked range. It's the caller's responsibility to free the cached state.
979 *
980 * This function always returns with the given range locked, ensuring after it's
981 * called no order extent can be pending.
982 */
983void btrfs_lock_and_flush_ordered_range(struct extent_io_tree *tree,
984 struct btrfs_inode *inode, u64 start,
985 u64 end,
986 struct extent_state **cached_state)
987{
988 struct btrfs_ordered_extent *ordered;
989 struct extent_state *cache = NULL;
990 struct extent_state **cachedp = &cache;
991
992 if (cached_state)
993 cachedp = cached_state;
994
995 while (1) {
996 lock_extent_bits(tree, start, end, cachedp);
997 ordered = btrfs_lookup_ordered_range(inode, start,
998 end - start + 1);
999 if (!ordered) {
1000 /*
1001 * If no external cached_state has been passed then
1002 * decrement the extra ref taken for cachedp since we
1003 * aren't exposing it outside of this function
1004 */
1005 if (!cached_state)
1006 refcount_dec(&cache->refs);
1007 break;
1008 }
1009 unlock_extent_cached(tree, start, end, cachedp);
1010 btrfs_start_ordered_extent(&inode->vfs_inode, ordered, 1);
1011 btrfs_put_ordered_extent(ordered);
1012 }
1013}
1014
1015int __init ordered_data_init(void)
1016{
1017 btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent",
1018 sizeof(struct btrfs_ordered_extent), 0,
1019 SLAB_MEM_SPREAD,
1020 NULL);
1021 if (!btrfs_ordered_extent_cache)
1022 return -ENOMEM;
1023
1024 return 0;
1025}
1026
1027void __cold ordered_data_exit(void)
1028{
1029 kmem_cache_destroy(btrfs_ordered_extent_cache);
1030}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 */
5
6#include <linux/slab.h>
7#include <linux/blkdev.h>
8#include <linux/writeback.h>
9#include <linux/sched/mm.h>
10#include "messages.h"
11#include "misc.h"
12#include "ctree.h"
13#include "transaction.h"
14#include "btrfs_inode.h"
15#include "extent_io.h"
16#include "disk-io.h"
17#include "compression.h"
18#include "delalloc-space.h"
19#include "qgroup.h"
20#include "subpage.h"
21#include "file.h"
22#include "super.h"
23
24static struct kmem_cache *btrfs_ordered_extent_cache;
25
26static u64 entry_end(struct btrfs_ordered_extent *entry)
27{
28 if (entry->file_offset + entry->num_bytes < entry->file_offset)
29 return (u64)-1;
30 return entry->file_offset + entry->num_bytes;
31}
32
33/* returns NULL if the insertion worked, or it returns the node it did find
34 * in the tree
35 */
36static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset,
37 struct rb_node *node)
38{
39 struct rb_node **p = &root->rb_node;
40 struct rb_node *parent = NULL;
41 struct btrfs_ordered_extent *entry;
42
43 while (*p) {
44 parent = *p;
45 entry = rb_entry(parent, struct btrfs_ordered_extent, rb_node);
46
47 if (file_offset < entry->file_offset)
48 p = &(*p)->rb_left;
49 else if (file_offset >= entry_end(entry))
50 p = &(*p)->rb_right;
51 else
52 return parent;
53 }
54
55 rb_link_node(node, parent, p);
56 rb_insert_color(node, root);
57 return NULL;
58}
59
60/*
61 * look for a given offset in the tree, and if it can't be found return the
62 * first lesser offset
63 */
64static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset,
65 struct rb_node **prev_ret)
66{
67 struct rb_node *n = root->rb_node;
68 struct rb_node *prev = NULL;
69 struct rb_node *test;
70 struct btrfs_ordered_extent *entry;
71 struct btrfs_ordered_extent *prev_entry = NULL;
72
73 while (n) {
74 entry = rb_entry(n, struct btrfs_ordered_extent, rb_node);
75 prev = n;
76 prev_entry = entry;
77
78 if (file_offset < entry->file_offset)
79 n = n->rb_left;
80 else if (file_offset >= entry_end(entry))
81 n = n->rb_right;
82 else
83 return n;
84 }
85 if (!prev_ret)
86 return NULL;
87
88 while (prev && file_offset >= entry_end(prev_entry)) {
89 test = rb_next(prev);
90 if (!test)
91 break;
92 prev_entry = rb_entry(test, struct btrfs_ordered_extent,
93 rb_node);
94 if (file_offset < entry_end(prev_entry))
95 break;
96
97 prev = test;
98 }
99 if (prev)
100 prev_entry = rb_entry(prev, struct btrfs_ordered_extent,
101 rb_node);
102 while (prev && file_offset < entry_end(prev_entry)) {
103 test = rb_prev(prev);
104 if (!test)
105 break;
106 prev_entry = rb_entry(test, struct btrfs_ordered_extent,
107 rb_node);
108 prev = test;
109 }
110 *prev_ret = prev;
111 return NULL;
112}
113
114static int range_overlaps(struct btrfs_ordered_extent *entry, u64 file_offset,
115 u64 len)
116{
117 if (file_offset + len <= entry->file_offset ||
118 entry->file_offset + entry->num_bytes <= file_offset)
119 return 0;
120 return 1;
121}
122
123/*
124 * look find the first ordered struct that has this offset, otherwise
125 * the first one less than this offset
126 */
127static inline struct rb_node *ordered_tree_search(struct btrfs_inode *inode,
128 u64 file_offset)
129{
130 struct rb_node *prev = NULL;
131 struct rb_node *ret;
132 struct btrfs_ordered_extent *entry;
133
134 if (inode->ordered_tree_last) {
135 entry = rb_entry(inode->ordered_tree_last, struct btrfs_ordered_extent,
136 rb_node);
137 if (in_range(file_offset, entry->file_offset, entry->num_bytes))
138 return inode->ordered_tree_last;
139 }
140 ret = __tree_search(&inode->ordered_tree, file_offset, &prev);
141 if (!ret)
142 ret = prev;
143 if (ret)
144 inode->ordered_tree_last = ret;
145 return ret;
146}
147
148static struct btrfs_ordered_extent *alloc_ordered_extent(
149 struct btrfs_inode *inode, u64 file_offset, u64 num_bytes,
150 u64 ram_bytes, u64 disk_bytenr, u64 disk_num_bytes,
151 u64 offset, unsigned long flags, int compress_type)
152{
153 struct btrfs_ordered_extent *entry;
154 int ret;
155 u64 qgroup_rsv = 0;
156
157 if (flags &
158 ((1 << BTRFS_ORDERED_NOCOW) | (1 << BTRFS_ORDERED_PREALLOC))) {
159 /* For nocow write, we can release the qgroup rsv right now */
160 ret = btrfs_qgroup_free_data(inode, NULL, file_offset, num_bytes, &qgroup_rsv);
161 if (ret < 0)
162 return ERR_PTR(ret);
163 } else {
164 /*
165 * The ordered extent has reserved qgroup space, release now
166 * and pass the reserved number for qgroup_record to free.
167 */
168 ret = btrfs_qgroup_release_data(inode, file_offset, num_bytes, &qgroup_rsv);
169 if (ret < 0)
170 return ERR_PTR(ret);
171 }
172 entry = kmem_cache_zalloc(btrfs_ordered_extent_cache, GFP_NOFS);
173 if (!entry)
174 return ERR_PTR(-ENOMEM);
175
176 entry->file_offset = file_offset;
177 entry->num_bytes = num_bytes;
178 entry->ram_bytes = ram_bytes;
179 entry->disk_bytenr = disk_bytenr;
180 entry->disk_num_bytes = disk_num_bytes;
181 entry->offset = offset;
182 entry->bytes_left = num_bytes;
183 entry->inode = igrab(&inode->vfs_inode);
184 entry->compress_type = compress_type;
185 entry->truncated_len = (u64)-1;
186 entry->qgroup_rsv = qgroup_rsv;
187 entry->flags = flags;
188 refcount_set(&entry->refs, 1);
189 init_waitqueue_head(&entry->wait);
190 INIT_LIST_HEAD(&entry->list);
191 INIT_LIST_HEAD(&entry->log_list);
192 INIT_LIST_HEAD(&entry->root_extent_list);
193 INIT_LIST_HEAD(&entry->work_list);
194 INIT_LIST_HEAD(&entry->bioc_list);
195 init_completion(&entry->completion);
196
197 /*
198 * We don't need the count_max_extents here, we can assume that all of
199 * that work has been done at higher layers, so this is truly the
200 * smallest the extent is going to get.
201 */
202 spin_lock(&inode->lock);
203 btrfs_mod_outstanding_extents(inode, 1);
204 spin_unlock(&inode->lock);
205
206 return entry;
207}
208
209static void insert_ordered_extent(struct btrfs_ordered_extent *entry)
210{
211 struct btrfs_inode *inode = BTRFS_I(entry->inode);
212 struct btrfs_root *root = inode->root;
213 struct btrfs_fs_info *fs_info = root->fs_info;
214 struct rb_node *node;
215
216 trace_btrfs_ordered_extent_add(inode, entry);
217
218 percpu_counter_add_batch(&fs_info->ordered_bytes, entry->num_bytes,
219 fs_info->delalloc_batch);
220
221 /* One ref for the tree. */
222 refcount_inc(&entry->refs);
223
224 spin_lock_irq(&inode->ordered_tree_lock);
225 node = tree_insert(&inode->ordered_tree, entry->file_offset,
226 &entry->rb_node);
227 if (node)
228 btrfs_panic(fs_info, -EEXIST,
229 "inconsistency in ordered tree at offset %llu",
230 entry->file_offset);
231 spin_unlock_irq(&inode->ordered_tree_lock);
232
233 spin_lock(&root->ordered_extent_lock);
234 list_add_tail(&entry->root_extent_list,
235 &root->ordered_extents);
236 root->nr_ordered_extents++;
237 if (root->nr_ordered_extents == 1) {
238 spin_lock(&fs_info->ordered_root_lock);
239 BUG_ON(!list_empty(&root->ordered_root));
240 list_add_tail(&root->ordered_root, &fs_info->ordered_roots);
241 spin_unlock(&fs_info->ordered_root_lock);
242 }
243 spin_unlock(&root->ordered_extent_lock);
244}
245
246/*
247 * Add an ordered extent to the per-inode tree.
248 *
249 * @inode: Inode that this extent is for.
250 * @file_offset: Logical offset in file where the extent starts.
251 * @num_bytes: Logical length of extent in file.
252 * @ram_bytes: Full length of unencoded data.
253 * @disk_bytenr: Offset of extent on disk.
254 * @disk_num_bytes: Size of extent on disk.
255 * @offset: Offset into unencoded data where file data starts.
256 * @flags: Flags specifying type of extent (1 << BTRFS_ORDERED_*).
257 * @compress_type: Compression algorithm used for data.
258 *
259 * Most of these parameters correspond to &struct btrfs_file_extent_item. The
260 * tree is given a single reference on the ordered extent that was inserted, and
261 * the returned pointer is given a second reference.
262 *
263 * Return: the new ordered extent or error pointer.
264 */
265struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
266 struct btrfs_inode *inode, u64 file_offset,
267 u64 num_bytes, u64 ram_bytes, u64 disk_bytenr,
268 u64 disk_num_bytes, u64 offset, unsigned long flags,
269 int compress_type)
270{
271 struct btrfs_ordered_extent *entry;
272
273 ASSERT((flags & ~BTRFS_ORDERED_TYPE_FLAGS) == 0);
274
275 entry = alloc_ordered_extent(inode, file_offset, num_bytes, ram_bytes,
276 disk_bytenr, disk_num_bytes, offset, flags,
277 compress_type);
278 if (!IS_ERR(entry))
279 insert_ordered_extent(entry);
280 return entry;
281}
282
283/*
284 * Add a struct btrfs_ordered_sum into the list of checksums to be inserted
285 * when an ordered extent is finished. If the list covers more than one
286 * ordered extent, it is split across multiples.
287 */
288void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
289 struct btrfs_ordered_sum *sum)
290{
291 struct btrfs_inode *inode = BTRFS_I(entry->inode);
292
293 spin_lock_irq(&inode->ordered_tree_lock);
294 list_add_tail(&sum->list, &entry->list);
295 spin_unlock_irq(&inode->ordered_tree_lock);
296}
297
298static void finish_ordered_fn(struct btrfs_work *work)
299{
300 struct btrfs_ordered_extent *ordered_extent;
301
302 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
303 btrfs_finish_ordered_io(ordered_extent);
304}
305
306static bool can_finish_ordered_extent(struct btrfs_ordered_extent *ordered,
307 struct page *page, u64 file_offset,
308 u64 len, bool uptodate)
309{
310 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
311 struct btrfs_fs_info *fs_info = inode->root->fs_info;
312
313 lockdep_assert_held(&inode->ordered_tree_lock);
314
315 if (page) {
316 ASSERT(page->mapping);
317 ASSERT(page_offset(page) <= file_offset);
318 ASSERT(file_offset + len <= page_offset(page) + PAGE_SIZE);
319
320 /*
321 * Ordered (Private2) bit indicates whether we still have
322 * pending io unfinished for the ordered extent.
323 *
324 * If there's no such bit, we need to skip to next range.
325 */
326 if (!btrfs_folio_test_ordered(fs_info, page_folio(page),
327 file_offset, len))
328 return false;
329 btrfs_folio_clear_ordered(fs_info, page_folio(page), file_offset, len);
330 }
331
332 /* Now we're fine to update the accounting. */
333 if (WARN_ON_ONCE(len > ordered->bytes_left)) {
334 btrfs_crit(fs_info,
335"bad ordered extent accounting, root=%llu ino=%llu OE offset=%llu OE len=%llu to_dec=%llu left=%llu",
336 inode->root->root_key.objectid, btrfs_ino(inode),
337 ordered->file_offset, ordered->num_bytes,
338 len, ordered->bytes_left);
339 ordered->bytes_left = 0;
340 } else {
341 ordered->bytes_left -= len;
342 }
343
344 if (!uptodate)
345 set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
346
347 if (ordered->bytes_left)
348 return false;
349
350 /*
351 * All the IO of the ordered extent is finished, we need to queue
352 * the finish_func to be executed.
353 */
354 set_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags);
355 cond_wake_up(&ordered->wait);
356 refcount_inc(&ordered->refs);
357 trace_btrfs_ordered_extent_mark_finished(inode, ordered);
358 return true;
359}
360
361static void btrfs_queue_ordered_fn(struct btrfs_ordered_extent *ordered)
362{
363 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
364 struct btrfs_fs_info *fs_info = inode->root->fs_info;
365 struct btrfs_workqueue *wq = btrfs_is_free_space_inode(inode) ?
366 fs_info->endio_freespace_worker : fs_info->endio_write_workers;
367
368 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL);
369 btrfs_queue_work(wq, &ordered->work);
370}
371
372bool btrfs_finish_ordered_extent(struct btrfs_ordered_extent *ordered,
373 struct page *page, u64 file_offset, u64 len,
374 bool uptodate)
375{
376 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
377 unsigned long flags;
378 bool ret;
379
380 trace_btrfs_finish_ordered_extent(inode, file_offset, len, uptodate);
381
382 spin_lock_irqsave(&inode->ordered_tree_lock, flags);
383 ret = can_finish_ordered_extent(ordered, page, file_offset, len, uptodate);
384 spin_unlock_irqrestore(&inode->ordered_tree_lock, flags);
385
386 if (ret)
387 btrfs_queue_ordered_fn(ordered);
388 return ret;
389}
390
391/*
392 * Mark all ordered extents io inside the specified range finished.
393 *
394 * @page: The involved page for the operation.
395 * For uncompressed buffered IO, the page status also needs to be
396 * updated to indicate whether the pending ordered io is finished.
397 * Can be NULL for direct IO and compressed write.
398 * For these cases, callers are ensured they won't execute the
399 * endio function twice.
400 *
401 * This function is called for endio, thus the range must have ordered
402 * extent(s) covering it.
403 */
404void btrfs_mark_ordered_io_finished(struct btrfs_inode *inode,
405 struct page *page, u64 file_offset,
406 u64 num_bytes, bool uptodate)
407{
408 struct rb_node *node;
409 struct btrfs_ordered_extent *entry = NULL;
410 unsigned long flags;
411 u64 cur = file_offset;
412
413 trace_btrfs_writepage_end_io_hook(inode, file_offset,
414 file_offset + num_bytes - 1,
415 uptodate);
416
417 spin_lock_irqsave(&inode->ordered_tree_lock, flags);
418 while (cur < file_offset + num_bytes) {
419 u64 entry_end;
420 u64 end;
421 u32 len;
422
423 node = ordered_tree_search(inode, cur);
424 /* No ordered extents at all */
425 if (!node)
426 break;
427
428 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
429 entry_end = entry->file_offset + entry->num_bytes;
430 /*
431 * |<-- OE --->| |
432 * cur
433 * Go to next OE.
434 */
435 if (cur >= entry_end) {
436 node = rb_next(node);
437 /* No more ordered extents, exit */
438 if (!node)
439 break;
440 entry = rb_entry(node, struct btrfs_ordered_extent,
441 rb_node);
442
443 /* Go to next ordered extent and continue */
444 cur = entry->file_offset;
445 continue;
446 }
447 /*
448 * | |<--- OE --->|
449 * cur
450 * Go to the start of OE.
451 */
452 if (cur < entry->file_offset) {
453 cur = entry->file_offset;
454 continue;
455 }
456
457 /*
458 * Now we are definitely inside one ordered extent.
459 *
460 * |<--- OE --->|
461 * |
462 * cur
463 */
464 end = min(entry->file_offset + entry->num_bytes,
465 file_offset + num_bytes) - 1;
466 ASSERT(end + 1 - cur < U32_MAX);
467 len = end + 1 - cur;
468
469 if (can_finish_ordered_extent(entry, page, cur, len, uptodate)) {
470 spin_unlock_irqrestore(&inode->ordered_tree_lock, flags);
471 btrfs_queue_ordered_fn(entry);
472 spin_lock_irqsave(&inode->ordered_tree_lock, flags);
473 }
474 cur += len;
475 }
476 spin_unlock_irqrestore(&inode->ordered_tree_lock, flags);
477}
478
479/*
480 * Finish IO for one ordered extent across a given range. The range can only
481 * contain one ordered extent.
482 *
483 * @cached: The cached ordered extent. If not NULL, we can skip the tree
484 * search and use the ordered extent directly.
485 * Will be also used to store the finished ordered extent.
486 * @file_offset: File offset for the finished IO
487 * @io_size: Length of the finish IO range
488 *
489 * Return true if the ordered extent is finished in the range, and update
490 * @cached.
491 * Return false otherwise.
492 *
493 * NOTE: The range can NOT cross multiple ordered extents.
494 * Thus caller should ensure the range doesn't cross ordered extents.
495 */
496bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode,
497 struct btrfs_ordered_extent **cached,
498 u64 file_offset, u64 io_size)
499{
500 struct rb_node *node;
501 struct btrfs_ordered_extent *entry = NULL;
502 unsigned long flags;
503 bool finished = false;
504
505 spin_lock_irqsave(&inode->ordered_tree_lock, flags);
506 if (cached && *cached) {
507 entry = *cached;
508 goto have_entry;
509 }
510
511 node = ordered_tree_search(inode, file_offset);
512 if (!node)
513 goto out;
514
515 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
516have_entry:
517 if (!in_range(file_offset, entry->file_offset, entry->num_bytes))
518 goto out;
519
520 if (io_size > entry->bytes_left)
521 btrfs_crit(inode->root->fs_info,
522 "bad ordered accounting left %llu size %llu",
523 entry->bytes_left, io_size);
524
525 entry->bytes_left -= io_size;
526
527 if (entry->bytes_left == 0) {
528 /*
529 * Ensure only one caller can set the flag and finished_ret
530 * accordingly
531 */
532 finished = !test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
533 /* test_and_set_bit implies a barrier */
534 cond_wake_up_nomb(&entry->wait);
535 }
536out:
537 if (finished && cached && entry) {
538 *cached = entry;
539 refcount_inc(&entry->refs);
540 trace_btrfs_ordered_extent_dec_test_pending(inode, entry);
541 }
542 spin_unlock_irqrestore(&inode->ordered_tree_lock, flags);
543 return finished;
544}
545
546/*
547 * used to drop a reference on an ordered extent. This will free
548 * the extent if the last reference is dropped
549 */
550void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
551{
552 struct list_head *cur;
553 struct btrfs_ordered_sum *sum;
554
555 trace_btrfs_ordered_extent_put(BTRFS_I(entry->inode), entry);
556
557 if (refcount_dec_and_test(&entry->refs)) {
558 ASSERT(list_empty(&entry->root_extent_list));
559 ASSERT(list_empty(&entry->log_list));
560 ASSERT(RB_EMPTY_NODE(&entry->rb_node));
561 if (entry->inode)
562 btrfs_add_delayed_iput(BTRFS_I(entry->inode));
563 while (!list_empty(&entry->list)) {
564 cur = entry->list.next;
565 sum = list_entry(cur, struct btrfs_ordered_sum, list);
566 list_del(&sum->list);
567 kvfree(sum);
568 }
569 kmem_cache_free(btrfs_ordered_extent_cache, entry);
570 }
571}
572
573/*
574 * remove an ordered extent from the tree. No references are dropped
575 * and waiters are woken up.
576 */
577void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode,
578 struct btrfs_ordered_extent *entry)
579{
580 struct btrfs_root *root = btrfs_inode->root;
581 struct btrfs_fs_info *fs_info = root->fs_info;
582 struct rb_node *node;
583 bool pending;
584 bool freespace_inode;
585
586 /*
587 * If this is a free space inode the thread has not acquired the ordered
588 * extents lockdep map.
589 */
590 freespace_inode = btrfs_is_free_space_inode(btrfs_inode);
591
592 btrfs_lockdep_acquire(fs_info, btrfs_trans_pending_ordered);
593 /* This is paired with btrfs_alloc_ordered_extent. */
594 spin_lock(&btrfs_inode->lock);
595 btrfs_mod_outstanding_extents(btrfs_inode, -1);
596 spin_unlock(&btrfs_inode->lock);
597 if (root != fs_info->tree_root) {
598 u64 release;
599
600 if (test_bit(BTRFS_ORDERED_ENCODED, &entry->flags))
601 release = entry->disk_num_bytes;
602 else
603 release = entry->num_bytes;
604 btrfs_delalloc_release_metadata(btrfs_inode, release,
605 test_bit(BTRFS_ORDERED_IOERR,
606 &entry->flags));
607 }
608
609 percpu_counter_add_batch(&fs_info->ordered_bytes, -entry->num_bytes,
610 fs_info->delalloc_batch);
611
612 spin_lock_irq(&btrfs_inode->ordered_tree_lock);
613 node = &entry->rb_node;
614 rb_erase(node, &btrfs_inode->ordered_tree);
615 RB_CLEAR_NODE(node);
616 if (btrfs_inode->ordered_tree_last == node)
617 btrfs_inode->ordered_tree_last = NULL;
618 set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
619 pending = test_and_clear_bit(BTRFS_ORDERED_PENDING, &entry->flags);
620 spin_unlock_irq(&btrfs_inode->ordered_tree_lock);
621
622 /*
623 * The current running transaction is waiting on us, we need to let it
624 * know that we're complete and wake it up.
625 */
626 if (pending) {
627 struct btrfs_transaction *trans;
628
629 /*
630 * The checks for trans are just a formality, it should be set,
631 * but if it isn't we don't want to deref/assert under the spin
632 * lock, so be nice and check if trans is set, but ASSERT() so
633 * if it isn't set a developer will notice.
634 */
635 spin_lock(&fs_info->trans_lock);
636 trans = fs_info->running_transaction;
637 if (trans)
638 refcount_inc(&trans->use_count);
639 spin_unlock(&fs_info->trans_lock);
640
641 ASSERT(trans || BTRFS_FS_ERROR(fs_info));
642 if (trans) {
643 if (atomic_dec_and_test(&trans->pending_ordered))
644 wake_up(&trans->pending_wait);
645 btrfs_put_transaction(trans);
646 }
647 }
648
649 btrfs_lockdep_release(fs_info, btrfs_trans_pending_ordered);
650
651 spin_lock(&root->ordered_extent_lock);
652 list_del_init(&entry->root_extent_list);
653 root->nr_ordered_extents--;
654
655 trace_btrfs_ordered_extent_remove(btrfs_inode, entry);
656
657 if (!root->nr_ordered_extents) {
658 spin_lock(&fs_info->ordered_root_lock);
659 BUG_ON(list_empty(&root->ordered_root));
660 list_del_init(&root->ordered_root);
661 spin_unlock(&fs_info->ordered_root_lock);
662 }
663 spin_unlock(&root->ordered_extent_lock);
664 wake_up(&entry->wait);
665 if (!freespace_inode)
666 btrfs_lockdep_release(fs_info, btrfs_ordered_extent);
667}
668
669static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
670{
671 struct btrfs_ordered_extent *ordered;
672
673 ordered = container_of(work, struct btrfs_ordered_extent, flush_work);
674 btrfs_start_ordered_extent(ordered);
675 complete(&ordered->completion);
676}
677
678/*
679 * wait for all the ordered extents in a root. This is done when balancing
680 * space between drives.
681 */
682u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr,
683 const u64 range_start, const u64 range_len)
684{
685 struct btrfs_fs_info *fs_info = root->fs_info;
686 LIST_HEAD(splice);
687 LIST_HEAD(skipped);
688 LIST_HEAD(works);
689 struct btrfs_ordered_extent *ordered, *next;
690 u64 count = 0;
691 const u64 range_end = range_start + range_len;
692
693 mutex_lock(&root->ordered_extent_mutex);
694 spin_lock(&root->ordered_extent_lock);
695 list_splice_init(&root->ordered_extents, &splice);
696 while (!list_empty(&splice) && nr) {
697 ordered = list_first_entry(&splice, struct btrfs_ordered_extent,
698 root_extent_list);
699
700 if (range_end <= ordered->disk_bytenr ||
701 ordered->disk_bytenr + ordered->disk_num_bytes <= range_start) {
702 list_move_tail(&ordered->root_extent_list, &skipped);
703 cond_resched_lock(&root->ordered_extent_lock);
704 continue;
705 }
706
707 list_move_tail(&ordered->root_extent_list,
708 &root->ordered_extents);
709 refcount_inc(&ordered->refs);
710 spin_unlock(&root->ordered_extent_lock);
711
712 btrfs_init_work(&ordered->flush_work,
713 btrfs_run_ordered_extent_work, NULL);
714 list_add_tail(&ordered->work_list, &works);
715 btrfs_queue_work(fs_info->flush_workers, &ordered->flush_work);
716
717 cond_resched();
718 spin_lock(&root->ordered_extent_lock);
719 if (nr != U64_MAX)
720 nr--;
721 count++;
722 }
723 list_splice_tail(&skipped, &root->ordered_extents);
724 list_splice_tail(&splice, &root->ordered_extents);
725 spin_unlock(&root->ordered_extent_lock);
726
727 list_for_each_entry_safe(ordered, next, &works, work_list) {
728 list_del_init(&ordered->work_list);
729 wait_for_completion(&ordered->completion);
730 btrfs_put_ordered_extent(ordered);
731 cond_resched();
732 }
733 mutex_unlock(&root->ordered_extent_mutex);
734
735 return count;
736}
737
738void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr,
739 const u64 range_start, const u64 range_len)
740{
741 struct btrfs_root *root;
742 LIST_HEAD(splice);
743 u64 done;
744
745 mutex_lock(&fs_info->ordered_operations_mutex);
746 spin_lock(&fs_info->ordered_root_lock);
747 list_splice_init(&fs_info->ordered_roots, &splice);
748 while (!list_empty(&splice) && nr) {
749 root = list_first_entry(&splice, struct btrfs_root,
750 ordered_root);
751 root = btrfs_grab_root(root);
752 BUG_ON(!root);
753 list_move_tail(&root->ordered_root,
754 &fs_info->ordered_roots);
755 spin_unlock(&fs_info->ordered_root_lock);
756
757 done = btrfs_wait_ordered_extents(root, nr,
758 range_start, range_len);
759 btrfs_put_root(root);
760
761 spin_lock(&fs_info->ordered_root_lock);
762 if (nr != U64_MAX) {
763 nr -= done;
764 }
765 }
766 list_splice_tail(&splice, &fs_info->ordered_roots);
767 spin_unlock(&fs_info->ordered_root_lock);
768 mutex_unlock(&fs_info->ordered_operations_mutex);
769}
770
771/*
772 * Start IO and wait for a given ordered extent to finish.
773 *
774 * Wait on page writeback for all the pages in the extent and the IO completion
775 * code to insert metadata into the btree corresponding to the extent.
776 */
777void btrfs_start_ordered_extent(struct btrfs_ordered_extent *entry)
778{
779 u64 start = entry->file_offset;
780 u64 end = start + entry->num_bytes - 1;
781 struct btrfs_inode *inode = BTRFS_I(entry->inode);
782 bool freespace_inode;
783
784 trace_btrfs_ordered_extent_start(inode, entry);
785
786 /*
787 * If this is a free space inode do not take the ordered extents lockdep
788 * map.
789 */
790 freespace_inode = btrfs_is_free_space_inode(inode);
791
792 /*
793 * pages in the range can be dirty, clean or writeback. We
794 * start IO on any dirty ones so the wait doesn't stall waiting
795 * for the flusher thread to find them
796 */
797 if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
798 filemap_fdatawrite_range(inode->vfs_inode.i_mapping, start, end);
799
800 if (!freespace_inode)
801 btrfs_might_wait_for_event(inode->root->fs_info, btrfs_ordered_extent);
802 wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE, &entry->flags));
803}
804
805/*
806 * Used to wait on ordered extents across a large range of bytes.
807 */
808int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
809{
810 int ret = 0;
811 int ret_wb = 0;
812 u64 end;
813 u64 orig_end;
814 struct btrfs_ordered_extent *ordered;
815
816 if (start + len < start) {
817 orig_end = OFFSET_MAX;
818 } else {
819 orig_end = start + len - 1;
820 if (orig_end > OFFSET_MAX)
821 orig_end = OFFSET_MAX;
822 }
823
824 /* start IO across the range first to instantiate any delalloc
825 * extents
826 */
827 ret = btrfs_fdatawrite_range(inode, start, orig_end);
828 if (ret)
829 return ret;
830
831 /*
832 * If we have a writeback error don't return immediately. Wait first
833 * for any ordered extents that haven't completed yet. This is to make
834 * sure no one can dirty the same page ranges and call writepages()
835 * before the ordered extents complete - to avoid failures (-EEXIST)
836 * when adding the new ordered extents to the ordered tree.
837 */
838 ret_wb = filemap_fdatawait_range(inode->i_mapping, start, orig_end);
839
840 end = orig_end;
841 while (1) {
842 ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode), end);
843 if (!ordered)
844 break;
845 if (ordered->file_offset > orig_end) {
846 btrfs_put_ordered_extent(ordered);
847 break;
848 }
849 if (ordered->file_offset + ordered->num_bytes <= start) {
850 btrfs_put_ordered_extent(ordered);
851 break;
852 }
853 btrfs_start_ordered_extent(ordered);
854 end = ordered->file_offset;
855 /*
856 * If the ordered extent had an error save the error but don't
857 * exit without waiting first for all other ordered extents in
858 * the range to complete.
859 */
860 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags))
861 ret = -EIO;
862 btrfs_put_ordered_extent(ordered);
863 if (end == 0 || end == start)
864 break;
865 end--;
866 }
867 return ret_wb ? ret_wb : ret;
868}
869
870/*
871 * find an ordered extent corresponding to file_offset. return NULL if
872 * nothing is found, otherwise take a reference on the extent and return it
873 */
874struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,
875 u64 file_offset)
876{
877 struct rb_node *node;
878 struct btrfs_ordered_extent *entry = NULL;
879 unsigned long flags;
880
881 spin_lock_irqsave(&inode->ordered_tree_lock, flags);
882 node = ordered_tree_search(inode, file_offset);
883 if (!node)
884 goto out;
885
886 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
887 if (!in_range(file_offset, entry->file_offset, entry->num_bytes))
888 entry = NULL;
889 if (entry) {
890 refcount_inc(&entry->refs);
891 trace_btrfs_ordered_extent_lookup(inode, entry);
892 }
893out:
894 spin_unlock_irqrestore(&inode->ordered_tree_lock, flags);
895 return entry;
896}
897
898/* Since the DIO code tries to lock a wide area we need to look for any ordered
899 * extents that exist in the range, rather than just the start of the range.
900 */
901struct btrfs_ordered_extent *btrfs_lookup_ordered_range(
902 struct btrfs_inode *inode, u64 file_offset, u64 len)
903{
904 struct rb_node *node;
905 struct btrfs_ordered_extent *entry = NULL;
906
907 spin_lock_irq(&inode->ordered_tree_lock);
908 node = ordered_tree_search(inode, file_offset);
909 if (!node) {
910 node = ordered_tree_search(inode, file_offset + len);
911 if (!node)
912 goto out;
913 }
914
915 while (1) {
916 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
917 if (range_overlaps(entry, file_offset, len))
918 break;
919
920 if (entry->file_offset >= file_offset + len) {
921 entry = NULL;
922 break;
923 }
924 entry = NULL;
925 node = rb_next(node);
926 if (!node)
927 break;
928 }
929out:
930 if (entry) {
931 refcount_inc(&entry->refs);
932 trace_btrfs_ordered_extent_lookup_range(inode, entry);
933 }
934 spin_unlock_irq(&inode->ordered_tree_lock);
935 return entry;
936}
937
938/*
939 * Adds all ordered extents to the given list. The list ends up sorted by the
940 * file_offset of the ordered extents.
941 */
942void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode,
943 struct list_head *list)
944{
945 struct rb_node *n;
946
947 ASSERT(inode_is_locked(&inode->vfs_inode));
948
949 spin_lock_irq(&inode->ordered_tree_lock);
950 for (n = rb_first(&inode->ordered_tree); n; n = rb_next(n)) {
951 struct btrfs_ordered_extent *ordered;
952
953 ordered = rb_entry(n, struct btrfs_ordered_extent, rb_node);
954
955 if (test_bit(BTRFS_ORDERED_LOGGED, &ordered->flags))
956 continue;
957
958 ASSERT(list_empty(&ordered->log_list));
959 list_add_tail(&ordered->log_list, list);
960 refcount_inc(&ordered->refs);
961 trace_btrfs_ordered_extent_lookup_for_logging(inode, ordered);
962 }
963 spin_unlock_irq(&inode->ordered_tree_lock);
964}
965
966/*
967 * lookup and return any extent before 'file_offset'. NULL is returned
968 * if none is found
969 */
970struct btrfs_ordered_extent *
971btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset)
972{
973 struct rb_node *node;
974 struct btrfs_ordered_extent *entry = NULL;
975
976 spin_lock_irq(&inode->ordered_tree_lock);
977 node = ordered_tree_search(inode, file_offset);
978 if (!node)
979 goto out;
980
981 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
982 refcount_inc(&entry->refs);
983 trace_btrfs_ordered_extent_lookup_first(inode, entry);
984out:
985 spin_unlock_irq(&inode->ordered_tree_lock);
986 return entry;
987}
988
989/*
990 * Lookup the first ordered extent that overlaps the range
991 * [@file_offset, @file_offset + @len).
992 *
993 * The difference between this and btrfs_lookup_first_ordered_extent() is
994 * that this one won't return any ordered extent that does not overlap the range.
995 * And the difference against btrfs_lookup_ordered_extent() is, this function
996 * ensures the first ordered extent gets returned.
997 */
998struct btrfs_ordered_extent *btrfs_lookup_first_ordered_range(
999 struct btrfs_inode *inode, u64 file_offset, u64 len)
1000{
1001 struct rb_node *node;
1002 struct rb_node *cur;
1003 struct rb_node *prev;
1004 struct rb_node *next;
1005 struct btrfs_ordered_extent *entry = NULL;
1006
1007 spin_lock_irq(&inode->ordered_tree_lock);
1008 node = inode->ordered_tree.rb_node;
1009 /*
1010 * Here we don't want to use tree_search() which will use tree->last
1011 * and screw up the search order.
1012 * And __tree_search() can't return the adjacent ordered extents
1013 * either, thus here we do our own search.
1014 */
1015 while (node) {
1016 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
1017
1018 if (file_offset < entry->file_offset) {
1019 node = node->rb_left;
1020 } else if (file_offset >= entry_end(entry)) {
1021 node = node->rb_right;
1022 } else {
1023 /*
1024 * Direct hit, got an ordered extent that starts at
1025 * @file_offset
1026 */
1027 goto out;
1028 }
1029 }
1030 if (!entry) {
1031 /* Empty tree */
1032 goto out;
1033 }
1034
1035 cur = &entry->rb_node;
1036 /* We got an entry around @file_offset, check adjacent entries */
1037 if (entry->file_offset < file_offset) {
1038 prev = cur;
1039 next = rb_next(cur);
1040 } else {
1041 prev = rb_prev(cur);
1042 next = cur;
1043 }
1044 if (prev) {
1045 entry = rb_entry(prev, struct btrfs_ordered_extent, rb_node);
1046 if (range_overlaps(entry, file_offset, len))
1047 goto out;
1048 }
1049 if (next) {
1050 entry = rb_entry(next, struct btrfs_ordered_extent, rb_node);
1051 if (range_overlaps(entry, file_offset, len))
1052 goto out;
1053 }
1054 /* No ordered extent in the range */
1055 entry = NULL;
1056out:
1057 if (entry) {
1058 refcount_inc(&entry->refs);
1059 trace_btrfs_ordered_extent_lookup_first_range(inode, entry);
1060 }
1061
1062 spin_unlock_irq(&inode->ordered_tree_lock);
1063 return entry;
1064}
1065
1066/*
1067 * Lock the passed range and ensures all pending ordered extents in it are run
1068 * to completion.
1069 *
1070 * @inode: Inode whose ordered tree is to be searched
1071 * @start: Beginning of range to flush
1072 * @end: Last byte of range to lock
1073 * @cached_state: If passed, will return the extent state responsible for the
1074 * locked range. It's the caller's responsibility to free the
1075 * cached state.
1076 *
1077 * Always return with the given range locked, ensuring after it's called no
1078 * order extent can be pending.
1079 */
1080void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,
1081 u64 end,
1082 struct extent_state **cached_state)
1083{
1084 struct btrfs_ordered_extent *ordered;
1085 struct extent_state *cache = NULL;
1086 struct extent_state **cachedp = &cache;
1087
1088 if (cached_state)
1089 cachedp = cached_state;
1090
1091 while (1) {
1092 lock_extent(&inode->io_tree, start, end, cachedp);
1093 ordered = btrfs_lookup_ordered_range(inode, start,
1094 end - start + 1);
1095 if (!ordered) {
1096 /*
1097 * If no external cached_state has been passed then
1098 * decrement the extra ref taken for cachedp since we
1099 * aren't exposing it outside of this function
1100 */
1101 if (!cached_state)
1102 refcount_dec(&cache->refs);
1103 break;
1104 }
1105 unlock_extent(&inode->io_tree, start, end, cachedp);
1106 btrfs_start_ordered_extent(ordered);
1107 btrfs_put_ordered_extent(ordered);
1108 }
1109}
1110
1111/*
1112 * Lock the passed range and ensure all pending ordered extents in it are run
1113 * to completion in nowait mode.
1114 *
1115 * Return true if btrfs_lock_ordered_range does not return any extents,
1116 * otherwise false.
1117 */
1118bool btrfs_try_lock_ordered_range(struct btrfs_inode *inode, u64 start, u64 end,
1119 struct extent_state **cached_state)
1120{
1121 struct btrfs_ordered_extent *ordered;
1122
1123 if (!try_lock_extent(&inode->io_tree, start, end, cached_state))
1124 return false;
1125
1126 ordered = btrfs_lookup_ordered_range(inode, start, end - start + 1);
1127 if (!ordered)
1128 return true;
1129
1130 btrfs_put_ordered_extent(ordered);
1131 unlock_extent(&inode->io_tree, start, end, cached_state);
1132
1133 return false;
1134}
1135
1136/* Split out a new ordered extent for this first @len bytes of @ordered. */
1137struct btrfs_ordered_extent *btrfs_split_ordered_extent(
1138 struct btrfs_ordered_extent *ordered, u64 len)
1139{
1140 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
1141 struct btrfs_root *root = inode->root;
1142 struct btrfs_fs_info *fs_info = root->fs_info;
1143 u64 file_offset = ordered->file_offset;
1144 u64 disk_bytenr = ordered->disk_bytenr;
1145 unsigned long flags = ordered->flags;
1146 struct btrfs_ordered_sum *sum, *tmpsum;
1147 struct btrfs_ordered_extent *new;
1148 struct rb_node *node;
1149 u64 offset = 0;
1150
1151 trace_btrfs_ordered_extent_split(inode, ordered);
1152
1153 ASSERT(!(flags & (1U << BTRFS_ORDERED_COMPRESSED)));
1154
1155 /*
1156 * The entire bio must be covered by the ordered extent, but we can't
1157 * reduce the original extent to a zero length either.
1158 */
1159 if (WARN_ON_ONCE(len >= ordered->num_bytes))
1160 return ERR_PTR(-EINVAL);
1161 /* We cannot split partially completed ordered extents. */
1162 if (ordered->bytes_left) {
1163 ASSERT(!(flags & ~BTRFS_ORDERED_TYPE_FLAGS));
1164 if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes))
1165 return ERR_PTR(-EINVAL);
1166 }
1167 /* We cannot split a compressed ordered extent. */
1168 if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes))
1169 return ERR_PTR(-EINVAL);
1170
1171 new = alloc_ordered_extent(inode, file_offset, len, len, disk_bytenr,
1172 len, 0, flags, ordered->compress_type);
1173 if (IS_ERR(new))
1174 return new;
1175
1176 /* One ref for the tree. */
1177 refcount_inc(&new->refs);
1178
1179 spin_lock_irq(&root->ordered_extent_lock);
1180 spin_lock(&inode->ordered_tree_lock);
1181 /* Remove from tree once */
1182 node = &ordered->rb_node;
1183 rb_erase(node, &inode->ordered_tree);
1184 RB_CLEAR_NODE(node);
1185 if (inode->ordered_tree_last == node)
1186 inode->ordered_tree_last = NULL;
1187
1188 ordered->file_offset += len;
1189 ordered->disk_bytenr += len;
1190 ordered->num_bytes -= len;
1191 ordered->disk_num_bytes -= len;
1192
1193 if (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags)) {
1194 ASSERT(ordered->bytes_left == 0);
1195 new->bytes_left = 0;
1196 } else {
1197 ordered->bytes_left -= len;
1198 }
1199
1200 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags)) {
1201 if (ordered->truncated_len > len) {
1202 ordered->truncated_len -= len;
1203 } else {
1204 new->truncated_len = ordered->truncated_len;
1205 ordered->truncated_len = 0;
1206 }
1207 }
1208
1209 list_for_each_entry_safe(sum, tmpsum, &ordered->list, list) {
1210 if (offset == len)
1211 break;
1212 list_move_tail(&sum->list, &new->list);
1213 offset += sum->len;
1214 }
1215
1216 /* Re-insert the node */
1217 node = tree_insert(&inode->ordered_tree, ordered->file_offset,
1218 &ordered->rb_node);
1219 if (node)
1220 btrfs_panic(fs_info, -EEXIST,
1221 "zoned: inconsistency in ordered tree at offset %llu",
1222 ordered->file_offset);
1223
1224 node = tree_insert(&inode->ordered_tree, new->file_offset, &new->rb_node);
1225 if (node)
1226 btrfs_panic(fs_info, -EEXIST,
1227 "zoned: inconsistency in ordered tree at offset %llu",
1228 new->file_offset);
1229 spin_unlock(&inode->ordered_tree_lock);
1230
1231 list_add_tail(&new->root_extent_list, &root->ordered_extents);
1232 root->nr_ordered_extents++;
1233 spin_unlock_irq(&root->ordered_extent_lock);
1234 return new;
1235}
1236
1237int __init ordered_data_init(void)
1238{
1239 btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent",
1240 sizeof(struct btrfs_ordered_extent), 0,
1241 SLAB_MEM_SPREAD,
1242 NULL);
1243 if (!btrfs_ordered_extent_cache)
1244 return -ENOMEM;
1245
1246 return 0;
1247}
1248
1249void __cold ordered_data_exit(void)
1250{
1251 kmem_cache_destroy(btrfs_ordered_extent_cache);
1252}