1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
7 #include <linux/pagemap.h>
8 #include <linux/time.h>
9 #include <linux/init.h>
10 #include <linux/string.h>
11 #include <linux/backing-dev.h>
12 #include <linux/falloc.h>
13 #include <linux/writeback.h>
14 #include <linux/compat.h>
15 #include <linux/slab.h>
16 #include <linux/btrfs.h>
17 #include <linux/uio.h>
18 #include <linux/iversion.h>
19 #include <linux/fsverity.h>
22 #include "transaction.h"
23 #include "btrfs_inode.h"
24 #include "print-tree.h"
29 #include "compression.h"
30 #include "delalloc-space.h"
34 static struct kmem_cache *btrfs_inode_defrag_cachep;
36 * when auto defrag is enabled we
37 * queue up these defrag structs to remember which
38 * inodes need defragging passes
41 struct rb_node rb_node;
45 * transid where the defrag was added, we search for
46 * extents newer than this
54 * The extent size threshold for autodefrag.
56 * This value is different for compressed/non-compressed extents,
57 * thus needs to be passed from higher layer.
58 * (aka, inode_should_defrag())
63 static int __compare_inode_defrag(struct inode_defrag *defrag1,
64 struct inode_defrag *defrag2)
66 if (defrag1->root > defrag2->root)
68 else if (defrag1->root < defrag2->root)
70 else if (defrag1->ino > defrag2->ino)
72 else if (defrag1->ino < defrag2->ino)
78 /* pop a record for an inode into the defrag tree. The lock
79 * must be held already
81 * If you're inserting a record for an older transid than an
82 * existing record, the transid already in the tree is lowered
84 * If an existing record is found the defrag item you
87 static int __btrfs_add_inode_defrag(struct btrfs_inode *inode,
88 struct inode_defrag *defrag)
90 struct btrfs_fs_info *fs_info = inode->root->fs_info;
91 struct inode_defrag *entry;
93 struct rb_node *parent = NULL;
96 p = &fs_info->defrag_inodes.rb_node;
99 entry = rb_entry(parent, struct inode_defrag, rb_node);
101 ret = __compare_inode_defrag(defrag, entry);
103 p = &parent->rb_left;
105 p = &parent->rb_right;
107 /* if we're reinserting an entry for
108 * an old defrag run, make sure to
109 * lower the transid of our existing record
111 if (defrag->transid < entry->transid)
112 entry->transid = defrag->transid;
113 entry->extent_thresh = min(defrag->extent_thresh,
114 entry->extent_thresh);
118 set_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags);
119 rb_link_node(&defrag->rb_node, parent, p);
120 rb_insert_color(&defrag->rb_node, &fs_info->defrag_inodes);
124 static inline int __need_auto_defrag(struct btrfs_fs_info *fs_info)
126 if (!btrfs_test_opt(fs_info, AUTO_DEFRAG))
129 if (btrfs_fs_closing(fs_info))
136 * insert a defrag record for this inode if auto defrag is
139 int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
140 struct btrfs_inode *inode, u32 extent_thresh)
142 struct btrfs_root *root = inode->root;
143 struct btrfs_fs_info *fs_info = root->fs_info;
144 struct inode_defrag *defrag;
148 if (!__need_auto_defrag(fs_info))
151 if (test_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags))
155 transid = trans->transid;
157 transid = inode->root->last_trans;
159 defrag = kmem_cache_zalloc(btrfs_inode_defrag_cachep, GFP_NOFS);
163 defrag->ino = btrfs_ino(inode);
164 defrag->transid = transid;
165 defrag->root = root->root_key.objectid;
166 defrag->extent_thresh = extent_thresh;
168 spin_lock(&fs_info->defrag_inodes_lock);
169 if (!test_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags)) {
171 * If we set IN_DEFRAG flag and evict the inode from memory,
172 * and then re-read this inode, this new inode doesn't have
173 * IN_DEFRAG flag. At the case, we may find the existed defrag.
175 ret = __btrfs_add_inode_defrag(inode, defrag);
177 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
179 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
181 spin_unlock(&fs_info->defrag_inodes_lock);
186 * pick the defragable inode that we want, if it doesn't exist, we will get
189 static struct inode_defrag *
190 btrfs_pick_defrag_inode(struct btrfs_fs_info *fs_info, u64 root, u64 ino)
192 struct inode_defrag *entry = NULL;
193 struct inode_defrag tmp;
195 struct rb_node *parent = NULL;
201 spin_lock(&fs_info->defrag_inodes_lock);
202 p = fs_info->defrag_inodes.rb_node;
205 entry = rb_entry(parent, struct inode_defrag, rb_node);
207 ret = __compare_inode_defrag(&tmp, entry);
211 p = parent->rb_right;
216 if (parent && __compare_inode_defrag(&tmp, entry) > 0) {
217 parent = rb_next(parent);
219 entry = rb_entry(parent, struct inode_defrag, rb_node);
225 rb_erase(parent, &fs_info->defrag_inodes);
226 spin_unlock(&fs_info->defrag_inodes_lock);
230 void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info)
232 struct inode_defrag *defrag;
233 struct rb_node *node;
235 spin_lock(&fs_info->defrag_inodes_lock);
236 node = rb_first(&fs_info->defrag_inodes);
238 rb_erase(node, &fs_info->defrag_inodes);
239 defrag = rb_entry(node, struct inode_defrag, rb_node);
240 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
242 cond_resched_lock(&fs_info->defrag_inodes_lock);
244 node = rb_first(&fs_info->defrag_inodes);
246 spin_unlock(&fs_info->defrag_inodes_lock);
249 #define BTRFS_DEFRAG_BATCH 1024
251 static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
252 struct inode_defrag *defrag)
254 struct btrfs_root *inode_root;
256 struct btrfs_ioctl_defrag_range_args range;
261 if (test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state))
263 if (!__need_auto_defrag(fs_info))
267 inode_root = btrfs_get_fs_root(fs_info, defrag->root, true);
268 if (IS_ERR(inode_root)) {
269 ret = PTR_ERR(inode_root);
273 inode = btrfs_iget(fs_info->sb, defrag->ino, inode_root);
274 btrfs_put_root(inode_root);
276 ret = PTR_ERR(inode);
280 if (cur >= i_size_read(inode)) {
285 /* do a chunk of defrag */
286 clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
287 memset(&range, 0, sizeof(range));
290 range.extent_thresh = defrag->extent_thresh;
292 sb_start_write(fs_info->sb);
293 ret = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
295 sb_end_write(fs_info->sb);
301 cur = max(cur + fs_info->sectorsize, range.start);
305 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
310 * run through the list of inodes in the FS that need
313 int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info)
315 struct inode_defrag *defrag;
317 u64 root_objectid = 0;
319 atomic_inc(&fs_info->defrag_running);
321 /* Pause the auto defragger. */
322 if (test_bit(BTRFS_FS_STATE_REMOUNTING,
326 if (!__need_auto_defrag(fs_info))
329 /* find an inode to defrag */
330 defrag = btrfs_pick_defrag_inode(fs_info, root_objectid,
333 if (root_objectid || first_ino) {
342 first_ino = defrag->ino + 1;
343 root_objectid = defrag->root;
345 __btrfs_run_defrag_inode(fs_info, defrag);
347 atomic_dec(&fs_info->defrag_running);
350 * during unmount, we use the transaction_wait queue to
351 * wait for the defragger to stop
353 wake_up(&fs_info->transaction_wait);
357 /* simple helper to fault in pages and copy. This should go away
358 * and be replaced with calls into generic code.
360 static noinline int btrfs_copy_from_user(loff_t pos, size_t write_bytes,
361 struct page **prepared_pages,
365 size_t total_copied = 0;
367 int offset = offset_in_page(pos);
369 while (write_bytes > 0) {
370 size_t count = min_t(size_t,
371 PAGE_SIZE - offset, write_bytes);
372 struct page *page = prepared_pages[pg];
374 * Copy data from userspace to the current page
376 copied = copy_page_from_iter_atomic(page, offset, count, i);
378 /* Flush processor's dcache for this page */
379 flush_dcache_page(page);
382 * if we get a partial write, we can end up with
383 * partially up to date pages. These add
384 * a lot of complexity, so make sure they don't
385 * happen by forcing this copy to be retried.
387 * The rest of the btrfs_file_write code will fall
388 * back to page at a time copies after we return 0.
390 if (unlikely(copied < count)) {
391 if (!PageUptodate(page)) {
392 iov_iter_revert(i, copied);
399 write_bytes -= copied;
400 total_copied += copied;
402 if (offset == PAGE_SIZE) {
411 * unlocks pages after btrfs_file_write is done with them
413 static void btrfs_drop_pages(struct btrfs_fs_info *fs_info,
414 struct page **pages, size_t num_pages,
418 u64 block_start = round_down(pos, fs_info->sectorsize);
419 u64 block_len = round_up(pos + copied, fs_info->sectorsize) - block_start;
421 ASSERT(block_len <= U32_MAX);
422 for (i = 0; i < num_pages; i++) {
423 /* page checked is some magic around finding pages that
424 * have been modified without going through btrfs_set_page_dirty
425 * clear it here. There should be no need to mark the pages
426 * accessed as prepare_pages should have marked them accessed
427 * in prepare_pages via find_or_create_page()
429 btrfs_page_clamp_clear_checked(fs_info, pages[i], block_start,
431 unlock_page(pages[i]);
437 * After btrfs_copy_from_user(), update the following things for delalloc:
438 * - Mark newly dirtied pages as DELALLOC in the io tree.
439 * Used to advise which range is to be written back.
440 * - Mark modified pages as Uptodate/Dirty and not needing COW fixup
441 * - Update inode size for past EOF write
443 int btrfs_dirty_pages(struct btrfs_inode *inode, struct page **pages,
444 size_t num_pages, loff_t pos, size_t write_bytes,
445 struct extent_state **cached, bool noreserve)
447 struct btrfs_fs_info *fs_info = inode->root->fs_info;
452 u64 end_of_last_block;
453 u64 end_pos = pos + write_bytes;
454 loff_t isize = i_size_read(&inode->vfs_inode);
455 unsigned int extra_bits = 0;
457 if (write_bytes == 0)
461 extra_bits |= EXTENT_NORESERVE;
463 start_pos = round_down(pos, fs_info->sectorsize);
464 num_bytes = round_up(write_bytes + pos - start_pos,
465 fs_info->sectorsize);
466 ASSERT(num_bytes <= U32_MAX);
468 end_of_last_block = start_pos + num_bytes - 1;
471 * The pages may have already been dirty, clear out old accounting so
472 * we can set things up properly
474 clear_extent_bit(&inode->io_tree, start_pos, end_of_last_block,
475 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
478 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
483 for (i = 0; i < num_pages; i++) {
484 struct page *p = pages[i];
486 btrfs_page_clamp_set_uptodate(fs_info, p, start_pos, num_bytes);
487 btrfs_page_clamp_clear_checked(fs_info, p, start_pos, num_bytes);
488 btrfs_page_clamp_set_dirty(fs_info, p, start_pos, num_bytes);
492 * we've only changed i_size in ram, and we haven't updated
493 * the disk i_size. There is no need to log the inode
497 i_size_write(&inode->vfs_inode, end_pos);
502 * this drops all the extents in the cache that intersect the range
503 * [start, end]. Existing extents are split as required.
505 void btrfs_drop_extent_cache(struct btrfs_inode *inode, u64 start, u64 end,
508 struct extent_map *em;
509 struct extent_map *split = NULL;
510 struct extent_map *split2 = NULL;
511 struct extent_map_tree *em_tree = &inode->extent_tree;
512 u64 len = end - start + 1;
520 WARN_ON(end < start);
521 if (end == (u64)-1) {
530 split = alloc_extent_map();
532 split2 = alloc_extent_map();
533 if (!split || !split2)
536 write_lock(&em_tree->lock);
537 em = lookup_extent_mapping(em_tree, start, len);
539 write_unlock(&em_tree->lock);
543 gen = em->generation;
544 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
545 if (testend && em->start + em->len >= start + len) {
547 write_unlock(&em_tree->lock);
550 start = em->start + em->len;
552 len = start + len - (em->start + em->len);
554 write_unlock(&em_tree->lock);
557 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
558 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
559 clear_bit(EXTENT_FLAG_LOGGING, &flags);
560 modified = !list_empty(&em->list);
564 if (em->start < start) {
565 split->start = em->start;
566 split->len = start - em->start;
568 if (em->block_start < EXTENT_MAP_LAST_BYTE) {
569 split->orig_start = em->orig_start;
570 split->block_start = em->block_start;
573 split->block_len = em->block_len;
575 split->block_len = split->len;
576 split->orig_block_len = max(split->block_len,
578 split->ram_bytes = em->ram_bytes;
580 split->orig_start = split->start;
581 split->block_len = 0;
582 split->block_start = em->block_start;
583 split->orig_block_len = 0;
584 split->ram_bytes = split->len;
587 split->generation = gen;
588 split->flags = flags;
589 split->compress_type = em->compress_type;
590 replace_extent_mapping(em_tree, em, split, modified);
591 free_extent_map(split);
595 if (testend && em->start + em->len > start + len) {
596 u64 diff = start + len - em->start;
598 split->start = start + len;
599 split->len = em->start + em->len - (start + len);
600 split->flags = flags;
601 split->compress_type = em->compress_type;
602 split->generation = gen;
604 if (em->block_start < EXTENT_MAP_LAST_BYTE) {
605 split->orig_block_len = max(em->block_len,
608 split->ram_bytes = em->ram_bytes;
610 split->block_len = em->block_len;
611 split->block_start = em->block_start;
612 split->orig_start = em->orig_start;
614 split->block_len = split->len;
615 split->block_start = em->block_start
617 split->orig_start = em->orig_start;
620 split->ram_bytes = split->len;
621 split->orig_start = split->start;
622 split->block_len = 0;
623 split->block_start = em->block_start;
624 split->orig_block_len = 0;
627 if (extent_map_in_tree(em)) {
628 replace_extent_mapping(em_tree, em, split,
631 ret = add_extent_mapping(em_tree, split,
633 ASSERT(ret == 0); /* Logic error */
635 free_extent_map(split);
639 if (extent_map_in_tree(em))
640 remove_extent_mapping(em_tree, em);
641 write_unlock(&em_tree->lock);
645 /* once for the tree*/
649 free_extent_map(split);
651 free_extent_map(split2);
655 * this is very complex, but the basic idea is to drop all extents
656 * in the range start - end. hint_block is filled in with a block number
657 * that would be a good hint to the block allocator for this file.
659 * If an extent intersects the range but is not entirely inside the range
660 * it is either truncated or split. Anything entirely inside the range
661 * is deleted from the tree.
663 * Note: the VFS' inode number of bytes is not updated, it's up to the caller
664 * to deal with that. We set the field 'bytes_found' of the arguments structure
665 * with the number of allocated bytes found in the target range, so that the
666 * caller can update the inode's number of bytes in an atomic way when
667 * replacing extents in a range to avoid races with stat(2).
669 int btrfs_drop_extents(struct btrfs_trans_handle *trans,
670 struct btrfs_root *root, struct btrfs_inode *inode,
671 struct btrfs_drop_extents_args *args)
673 struct btrfs_fs_info *fs_info = root->fs_info;
674 struct extent_buffer *leaf;
675 struct btrfs_file_extent_item *fi;
676 struct btrfs_ref ref = { 0 };
677 struct btrfs_key key;
678 struct btrfs_key new_key;
679 u64 ino = btrfs_ino(inode);
680 u64 search_start = args->start;
683 u64 extent_offset = 0;
685 u64 last_end = args->start;
691 int modify_tree = -1;
694 int leafs_visited = 0;
695 struct btrfs_path *path = args->path;
697 args->bytes_found = 0;
698 args->extent_inserted = false;
700 /* Must always have a path if ->replace_extent is true */
701 ASSERT(!(args->replace_extent && !args->path));
704 path = btrfs_alloc_path();
711 if (args->drop_cache)
712 btrfs_drop_extent_cache(inode, args->start, args->end - 1, 0);
714 if (args->start >= inode->disk_i_size && !args->replace_extent)
717 update_refs = (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID);
720 ret = btrfs_lookup_file_extent(trans, root, path, ino,
721 search_start, modify_tree);
724 if (ret > 0 && path->slots[0] > 0 && search_start == args->start) {
725 leaf = path->nodes[0];
726 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
727 if (key.objectid == ino &&
728 key.type == BTRFS_EXTENT_DATA_KEY)
734 leaf = path->nodes[0];
735 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
737 ret = btrfs_next_leaf(root, path);
745 leaf = path->nodes[0];
749 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
751 if (key.objectid > ino)
753 if (WARN_ON_ONCE(key.objectid < ino) ||
754 key.type < BTRFS_EXTENT_DATA_KEY) {
759 if (key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= args->end)
762 fi = btrfs_item_ptr(leaf, path->slots[0],
763 struct btrfs_file_extent_item);
764 extent_type = btrfs_file_extent_type(leaf, fi);
766 if (extent_type == BTRFS_FILE_EXTENT_REG ||
767 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
768 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
769 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
770 extent_offset = btrfs_file_extent_offset(leaf, fi);
771 extent_end = key.offset +
772 btrfs_file_extent_num_bytes(leaf, fi);
773 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
774 extent_end = key.offset +
775 btrfs_file_extent_ram_bytes(leaf, fi);
782 * Don't skip extent items representing 0 byte lengths. They
783 * used to be created (bug) if while punching holes we hit
784 * -ENOSPC condition. So if we find one here, just ensure we
785 * delete it, otherwise we would insert a new file extent item
786 * with the same key (offset) as that 0 bytes length file
787 * extent item in the call to setup_items_for_insert() later
790 if (extent_end == key.offset && extent_end >= search_start) {
791 last_end = extent_end;
792 goto delete_extent_item;
795 if (extent_end <= search_start) {
801 search_start = max(key.offset, args->start);
802 if (recow || !modify_tree) {
804 btrfs_release_path(path);
809 * | - range to drop - |
810 * | -------- extent -------- |
812 if (args->start > key.offset && args->end < extent_end) {
814 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
819 memcpy(&new_key, &key, sizeof(new_key));
820 new_key.offset = args->start;
821 ret = btrfs_duplicate_item(trans, root, path,
823 if (ret == -EAGAIN) {
824 btrfs_release_path(path);
830 leaf = path->nodes[0];
831 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
832 struct btrfs_file_extent_item);
833 btrfs_set_file_extent_num_bytes(leaf, fi,
834 args->start - key.offset);
836 fi = btrfs_item_ptr(leaf, path->slots[0],
837 struct btrfs_file_extent_item);
839 extent_offset += args->start - key.offset;
840 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
841 btrfs_set_file_extent_num_bytes(leaf, fi,
842 extent_end - args->start);
843 btrfs_mark_buffer_dirty(leaf);
845 if (update_refs && disk_bytenr > 0) {
846 btrfs_init_generic_ref(&ref,
847 BTRFS_ADD_DELAYED_REF,
848 disk_bytenr, num_bytes, 0);
849 btrfs_init_data_ref(&ref,
850 root->root_key.objectid,
852 args->start - extent_offset,
854 ret = btrfs_inc_extent_ref(trans, &ref);
855 BUG_ON(ret); /* -ENOMEM */
857 key.offset = args->start;
860 * From here on out we will have actually dropped something, so
861 * last_end can be updated.
863 last_end = extent_end;
866 * | ---- range to drop ----- |
867 * | -------- extent -------- |
869 if (args->start <= key.offset && args->end < extent_end) {
870 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
875 memcpy(&new_key, &key, sizeof(new_key));
876 new_key.offset = args->end;
877 btrfs_set_item_key_safe(fs_info, path, &new_key);
879 extent_offset += args->end - key.offset;
880 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
881 btrfs_set_file_extent_num_bytes(leaf, fi,
882 extent_end - args->end);
883 btrfs_mark_buffer_dirty(leaf);
884 if (update_refs && disk_bytenr > 0)
885 args->bytes_found += args->end - key.offset;
889 search_start = extent_end;
891 * | ---- range to drop ----- |
892 * | -------- extent -------- |
894 if (args->start > key.offset && args->end >= extent_end) {
896 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
901 btrfs_set_file_extent_num_bytes(leaf, fi,
902 args->start - key.offset);
903 btrfs_mark_buffer_dirty(leaf);
904 if (update_refs && disk_bytenr > 0)
905 args->bytes_found += extent_end - args->start;
906 if (args->end == extent_end)
914 * | ---- range to drop ----- |
915 * | ------ extent ------ |
917 if (args->start <= key.offset && args->end >= extent_end) {
920 del_slot = path->slots[0];
923 BUG_ON(del_slot + del_nr != path->slots[0]);
928 extent_type == BTRFS_FILE_EXTENT_INLINE) {
929 args->bytes_found += extent_end - key.offset;
930 extent_end = ALIGN(extent_end,
931 fs_info->sectorsize);
932 } else if (update_refs && disk_bytenr > 0) {
933 btrfs_init_generic_ref(&ref,
934 BTRFS_DROP_DELAYED_REF,
935 disk_bytenr, num_bytes, 0);
936 btrfs_init_data_ref(&ref,
937 root->root_key.objectid,
939 key.offset - extent_offset, 0,
941 ret = btrfs_free_extent(trans, &ref);
942 BUG_ON(ret); /* -ENOMEM */
943 args->bytes_found += extent_end - key.offset;
946 if (args->end == extent_end)
949 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
954 ret = btrfs_del_items(trans, root, path, del_slot,
957 btrfs_abort_transaction(trans, ret);
964 btrfs_release_path(path);
971 if (!ret && del_nr > 0) {
973 * Set path->slots[0] to first slot, so that after the delete
974 * if items are move off from our leaf to its immediate left or
975 * right neighbor leafs, we end up with a correct and adjusted
976 * path->slots[0] for our insertion (if args->replace_extent).
978 path->slots[0] = del_slot;
979 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
981 btrfs_abort_transaction(trans, ret);
984 leaf = path->nodes[0];
986 * If btrfs_del_items() was called, it might have deleted a leaf, in
987 * which case it unlocked our path, so check path->locks[0] matches a
990 if (!ret && args->replace_extent && leafs_visited == 1 &&
991 path->locks[0] == BTRFS_WRITE_LOCK &&
992 btrfs_leaf_free_space(leaf) >=
993 sizeof(struct btrfs_item) + args->extent_item_size) {
996 key.type = BTRFS_EXTENT_DATA_KEY;
997 key.offset = args->start;
998 if (!del_nr && path->slots[0] < btrfs_header_nritems(leaf)) {
999 struct btrfs_key slot_key;
1001 btrfs_item_key_to_cpu(leaf, &slot_key, path->slots[0]);
1002 if (btrfs_comp_cpu_keys(&key, &slot_key) > 0)
1005 btrfs_setup_item_for_insert(root, path, &key, args->extent_item_size);
1006 args->extent_inserted = true;
1010 btrfs_free_path(path);
1011 else if (!args->extent_inserted)
1012 btrfs_release_path(path);
1014 args->drop_end = found ? min(args->end, last_end) : args->end;
1019 static int extent_mergeable(struct extent_buffer *leaf, int slot,
1020 u64 objectid, u64 bytenr, u64 orig_offset,
1021 u64 *start, u64 *end)
1023 struct btrfs_file_extent_item *fi;
1024 struct btrfs_key key;
1027 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
1030 btrfs_item_key_to_cpu(leaf, &key, slot);
1031 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
1034 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
1035 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
1036 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
1037 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
1038 btrfs_file_extent_compression(leaf, fi) ||
1039 btrfs_file_extent_encryption(leaf, fi) ||
1040 btrfs_file_extent_other_encoding(leaf, fi))
1043 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
1044 if ((*start && *start != key.offset) || (*end && *end != extent_end))
1047 *start = key.offset;
1053 * Mark extent in the range start - end as written.
1055 * This changes extent type from 'pre-allocated' to 'regular'. If only
1056 * part of extent is marked as written, the extent will be split into
1059 int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
1060 struct btrfs_inode *inode, u64 start, u64 end)
1062 struct btrfs_fs_info *fs_info = trans->fs_info;
1063 struct btrfs_root *root = inode->root;
1064 struct extent_buffer *leaf;
1065 struct btrfs_path *path;
1066 struct btrfs_file_extent_item *fi;
1067 struct btrfs_ref ref = { 0 };
1068 struct btrfs_key key;
1069 struct btrfs_key new_key;
1081 u64 ino = btrfs_ino(inode);
1083 path = btrfs_alloc_path();
1090 key.type = BTRFS_EXTENT_DATA_KEY;
1093 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1096 if (ret > 0 && path->slots[0] > 0)
1099 leaf = path->nodes[0];
1100 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1101 if (key.objectid != ino ||
1102 key.type != BTRFS_EXTENT_DATA_KEY) {
1104 btrfs_abort_transaction(trans, ret);
1107 fi = btrfs_item_ptr(leaf, path->slots[0],
1108 struct btrfs_file_extent_item);
1109 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_PREALLOC) {
1111 btrfs_abort_transaction(trans, ret);
1114 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
1115 if (key.offset > start || extent_end < end) {
1117 btrfs_abort_transaction(trans, ret);
1121 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1122 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1123 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
1124 memcpy(&new_key, &key, sizeof(new_key));
1126 if (start == key.offset && end < extent_end) {
1129 if (extent_mergeable(leaf, path->slots[0] - 1,
1130 ino, bytenr, orig_offset,
1131 &other_start, &other_end)) {
1132 new_key.offset = end;
1133 btrfs_set_item_key_safe(fs_info, path, &new_key);
1134 fi = btrfs_item_ptr(leaf, path->slots[0],
1135 struct btrfs_file_extent_item);
1136 btrfs_set_file_extent_generation(leaf, fi,
1138 btrfs_set_file_extent_num_bytes(leaf, fi,
1140 btrfs_set_file_extent_offset(leaf, fi,
1142 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
1143 struct btrfs_file_extent_item);
1144 btrfs_set_file_extent_generation(leaf, fi,
1146 btrfs_set_file_extent_num_bytes(leaf, fi,
1148 btrfs_mark_buffer_dirty(leaf);
1153 if (start > key.offset && end == extent_end) {
1156 if (extent_mergeable(leaf, path->slots[0] + 1,
1157 ino, bytenr, orig_offset,
1158 &other_start, &other_end)) {
1159 fi = btrfs_item_ptr(leaf, path->slots[0],
1160 struct btrfs_file_extent_item);
1161 btrfs_set_file_extent_num_bytes(leaf, fi,
1162 start - key.offset);
1163 btrfs_set_file_extent_generation(leaf, fi,
1166 new_key.offset = start;
1167 btrfs_set_item_key_safe(fs_info, path, &new_key);
1169 fi = btrfs_item_ptr(leaf, path->slots[0],
1170 struct btrfs_file_extent_item);
1171 btrfs_set_file_extent_generation(leaf, fi,
1173 btrfs_set_file_extent_num_bytes(leaf, fi,
1175 btrfs_set_file_extent_offset(leaf, fi,
1176 start - orig_offset);
1177 btrfs_mark_buffer_dirty(leaf);
1182 while (start > key.offset || end < extent_end) {
1183 if (key.offset == start)
1186 new_key.offset = split;
1187 ret = btrfs_duplicate_item(trans, root, path, &new_key);
1188 if (ret == -EAGAIN) {
1189 btrfs_release_path(path);
1193 btrfs_abort_transaction(trans, ret);
1197 leaf = path->nodes[0];
1198 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
1199 struct btrfs_file_extent_item);
1200 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1201 btrfs_set_file_extent_num_bytes(leaf, fi,
1202 split - key.offset);
1204 fi = btrfs_item_ptr(leaf, path->slots[0],
1205 struct btrfs_file_extent_item);
1207 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1208 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
1209 btrfs_set_file_extent_num_bytes(leaf, fi,
1210 extent_end - split);
1211 btrfs_mark_buffer_dirty(leaf);
1213 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, bytenr,
1215 btrfs_init_data_ref(&ref, root->root_key.objectid, ino,
1216 orig_offset, 0, false);
1217 ret = btrfs_inc_extent_ref(trans, &ref);
1219 btrfs_abort_transaction(trans, ret);
1223 if (split == start) {
1226 if (start != key.offset) {
1228 btrfs_abort_transaction(trans, ret);
1239 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
1241 btrfs_init_data_ref(&ref, root->root_key.objectid, ino, orig_offset,
1243 if (extent_mergeable(leaf, path->slots[0] + 1,
1244 ino, bytenr, orig_offset,
1245 &other_start, &other_end)) {
1247 btrfs_release_path(path);
1250 extent_end = other_end;
1251 del_slot = path->slots[0] + 1;
1253 ret = btrfs_free_extent(trans, &ref);
1255 btrfs_abort_transaction(trans, ret);
1261 if (extent_mergeable(leaf, path->slots[0] - 1,
1262 ino, bytenr, orig_offset,
1263 &other_start, &other_end)) {
1265 btrfs_release_path(path);
1268 key.offset = other_start;
1269 del_slot = path->slots[0];
1271 ret = btrfs_free_extent(trans, &ref);
1273 btrfs_abort_transaction(trans, ret);
1278 fi = btrfs_item_ptr(leaf, path->slots[0],
1279 struct btrfs_file_extent_item);
1280 btrfs_set_file_extent_type(leaf, fi,
1281 BTRFS_FILE_EXTENT_REG);
1282 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1283 btrfs_mark_buffer_dirty(leaf);
1285 fi = btrfs_item_ptr(leaf, del_slot - 1,
1286 struct btrfs_file_extent_item);
1287 btrfs_set_file_extent_type(leaf, fi,
1288 BTRFS_FILE_EXTENT_REG);
1289 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1290 btrfs_set_file_extent_num_bytes(leaf, fi,
1291 extent_end - key.offset);
1292 btrfs_mark_buffer_dirty(leaf);
1294 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
1296 btrfs_abort_transaction(trans, ret);
1301 btrfs_free_path(path);
1306 * on error we return an unlocked page and the error value
1307 * on success we return a locked page and 0
1309 static int prepare_uptodate_page(struct inode *inode,
1310 struct page *page, u64 pos,
1311 bool force_uptodate)
1315 if (((pos & (PAGE_SIZE - 1)) || force_uptodate) &&
1316 !PageUptodate(page)) {
1317 ret = btrfs_readpage(NULL, page);
1321 if (!PageUptodate(page)) {
1327 * Since btrfs_readpage() will unlock the page before it
1328 * returns, there is a window where btrfs_releasepage() can be
1329 * called to release the page. Here we check both inode
1330 * mapping and PagePrivate() to make sure the page was not
1333 * The private flag check is essential for subpage as we need
1334 * to store extra bitmap using page->private.
1336 if (page->mapping != inode->i_mapping || !PagePrivate(page)) {
1345 * this just gets pages into the page cache and locks them down.
1347 static noinline int prepare_pages(struct inode *inode, struct page **pages,
1348 size_t num_pages, loff_t pos,
1349 size_t write_bytes, bool force_uptodate)
1352 unsigned long index = pos >> PAGE_SHIFT;
1353 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1357 for (i = 0; i < num_pages; i++) {
1359 pages[i] = find_or_create_page(inode->i_mapping, index + i,
1360 mask | __GFP_WRITE);
1367 err = set_page_extent_mapped(pages[i]);
1374 err = prepare_uptodate_page(inode, pages[i], pos,
1376 if (!err && i == num_pages - 1)
1377 err = prepare_uptodate_page(inode, pages[i],
1378 pos + write_bytes, false);
1381 if (err == -EAGAIN) {
1388 wait_on_page_writeback(pages[i]);
1393 while (faili >= 0) {
1394 unlock_page(pages[faili]);
1395 put_page(pages[faili]);
1403 * This function locks the extent and properly waits for data=ordered extents
1404 * to finish before allowing the pages to be modified if need.
1407 * 1 - the extent is locked
1408 * 0 - the extent is not locked, and everything is OK
1409 * -EAGAIN - need re-prepare the pages
1410 * the other < 0 number - Something wrong happens
1413 lock_and_cleanup_extent_if_need(struct btrfs_inode *inode, struct page **pages,
1414 size_t num_pages, loff_t pos,
1416 u64 *lockstart, u64 *lockend,
1417 struct extent_state **cached_state)
1419 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1425 start_pos = round_down(pos, fs_info->sectorsize);
1426 last_pos = round_up(pos + write_bytes, fs_info->sectorsize) - 1;
1428 if (start_pos < inode->vfs_inode.i_size) {
1429 struct btrfs_ordered_extent *ordered;
1431 lock_extent_bits(&inode->io_tree, start_pos, last_pos,
1433 ordered = btrfs_lookup_ordered_range(inode, start_pos,
1434 last_pos - start_pos + 1);
1436 ordered->file_offset + ordered->num_bytes > start_pos &&
1437 ordered->file_offset <= last_pos) {
1438 unlock_extent_cached(&inode->io_tree, start_pos,
1439 last_pos, cached_state);
1440 for (i = 0; i < num_pages; i++) {
1441 unlock_page(pages[i]);
1444 btrfs_start_ordered_extent(ordered, 1);
1445 btrfs_put_ordered_extent(ordered);
1449 btrfs_put_ordered_extent(ordered);
1451 *lockstart = start_pos;
1452 *lockend = last_pos;
1457 * We should be called after prepare_pages() which should have locked
1458 * all pages in the range.
1460 for (i = 0; i < num_pages; i++)
1461 WARN_ON(!PageLocked(pages[i]));
1466 static int check_can_nocow(struct btrfs_inode *inode, loff_t pos,
1467 size_t *write_bytes, bool nowait)
1469 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1470 struct btrfs_root *root = inode->root;
1471 u64 lockstart, lockend;
1475 if (!(inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)))
1478 if (!nowait && !btrfs_drew_try_write_lock(&root->snapshot_lock))
1481 lockstart = round_down(pos, fs_info->sectorsize);
1482 lockend = round_up(pos + *write_bytes,
1483 fs_info->sectorsize) - 1;
1484 num_bytes = lockend - lockstart + 1;
1487 struct btrfs_ordered_extent *ordered;
1489 if (!try_lock_extent(&inode->io_tree, lockstart, lockend))
1492 ordered = btrfs_lookup_ordered_range(inode, lockstart,
1495 btrfs_put_ordered_extent(ordered);
1500 btrfs_lock_and_flush_ordered_range(inode, lockstart,
1504 ret = can_nocow_extent(&inode->vfs_inode, lockstart, &num_bytes,
1505 NULL, NULL, NULL, false);
1509 btrfs_drew_write_unlock(&root->snapshot_lock);
1511 *write_bytes = min_t(size_t, *write_bytes ,
1512 num_bytes - pos + lockstart);
1515 unlock_extent(&inode->io_tree, lockstart, lockend);
1520 static int check_nocow_nolock(struct btrfs_inode *inode, loff_t pos,
1521 size_t *write_bytes)
1523 return check_can_nocow(inode, pos, write_bytes, true);
1527 * Check if we can do nocow write into the range [@pos, @pos + @write_bytes)
1530 * @write_bytes: The length to write, will be updated to the nocow writeable
1533 * This function will flush ordered extents in the range to ensure proper
1537 * >0 and update @write_bytes if we can do nocow write
1538 * 0 if we can't do nocow write
1539 * -EAGAIN if we can't get the needed lock or there are ordered extents
1540 * for * (nowait == true) case
1541 * <0 if other error happened
1543 * NOTE: Callers need to release the lock by btrfs_check_nocow_unlock().
1545 int btrfs_check_nocow_lock(struct btrfs_inode *inode, loff_t pos,
1546 size_t *write_bytes)
1548 return check_can_nocow(inode, pos, write_bytes, false);
1551 void btrfs_check_nocow_unlock(struct btrfs_inode *inode)
1553 btrfs_drew_write_unlock(&inode->root->snapshot_lock);
1556 static void update_time_for_write(struct inode *inode)
1558 struct timespec64 now;
1560 if (IS_NOCMTIME(inode))
1563 now = current_time(inode);
1564 if (!timespec64_equal(&inode->i_mtime, &now))
1565 inode->i_mtime = now;
1567 if (!timespec64_equal(&inode->i_ctime, &now))
1568 inode->i_ctime = now;
1570 if (IS_I_VERSION(inode))
1571 inode_inc_iversion(inode);
1574 static int btrfs_write_check(struct kiocb *iocb, struct iov_iter *from,
1577 struct file *file = iocb->ki_filp;
1578 struct inode *inode = file_inode(file);
1579 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1580 loff_t pos = iocb->ki_pos;
1585 if (iocb->ki_flags & IOCB_NOWAIT) {
1586 size_t nocow_bytes = count;
1588 /* We will allocate space in case nodatacow is not set, so bail */
1589 if (check_nocow_nolock(BTRFS_I(inode), pos, &nocow_bytes) <= 0)
1592 * There are holes in the range or parts of the range that must
1593 * be COWed (shared extents, RO block groups, etc), so just bail
1596 if (nocow_bytes < count)
1600 current->backing_dev_info = inode_to_bdi(inode);
1601 ret = file_remove_privs(file);
1606 * We reserve space for updating the inode when we reserve space for the
1607 * extent we are going to write, so we will enospc out there. We don't
1608 * need to start yet another transaction to update the inode as we will
1609 * update the inode when we finish writing whatever data we write.
1611 update_time_for_write(inode);
1613 start_pos = round_down(pos, fs_info->sectorsize);
1614 oldsize = i_size_read(inode);
1615 if (start_pos > oldsize) {
1616 /* Expand hole size to cover write data, preventing empty gap */
1617 loff_t end_pos = round_up(pos + count, fs_info->sectorsize);
1619 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, end_pos);
1621 current->backing_dev_info = NULL;
1629 static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb,
1632 struct file *file = iocb->ki_filp;
1634 struct inode *inode = file_inode(file);
1635 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1636 struct page **pages = NULL;
1637 struct extent_changeset *data_reserved = NULL;
1638 u64 release_bytes = 0;
1641 size_t num_written = 0;
1644 bool only_release_metadata = false;
1645 bool force_page_uptodate = false;
1646 loff_t old_isize = i_size_read(inode);
1647 unsigned int ilock_flags = 0;
1649 if (iocb->ki_flags & IOCB_NOWAIT)
1650 ilock_flags |= BTRFS_ILOCK_TRY;
1652 ret = btrfs_inode_lock(inode, ilock_flags);
1656 ret = generic_write_checks(iocb, i);
1660 ret = btrfs_write_check(iocb, i, ret);
1665 nrptrs = min(DIV_ROUND_UP(iov_iter_count(i), PAGE_SIZE),
1666 PAGE_SIZE / (sizeof(struct page *)));
1667 nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
1668 nrptrs = max(nrptrs, 8);
1669 pages = kmalloc_array(nrptrs, sizeof(struct page *), GFP_KERNEL);
1675 while (iov_iter_count(i) > 0) {
1676 struct extent_state *cached_state = NULL;
1677 size_t offset = offset_in_page(pos);
1678 size_t sector_offset;
1679 size_t write_bytes = min(iov_iter_count(i),
1680 nrptrs * (size_t)PAGE_SIZE -
1683 size_t reserve_bytes;
1686 size_t dirty_sectors;
1691 * Fault pages before locking them in prepare_pages
1692 * to avoid recursive lock
1694 if (unlikely(fault_in_iov_iter_readable(i, write_bytes))) {
1699 only_release_metadata = false;
1700 sector_offset = pos & (fs_info->sectorsize - 1);
1702 extent_changeset_release(data_reserved);
1703 ret = btrfs_check_data_free_space(BTRFS_I(inode),
1704 &data_reserved, pos,
1708 * If we don't have to COW at the offset, reserve
1709 * metadata only. write_bytes may get smaller than
1712 if (btrfs_check_nocow_lock(BTRFS_I(inode), pos,
1714 only_release_metadata = true;
1719 num_pages = DIV_ROUND_UP(write_bytes + offset, PAGE_SIZE);
1720 WARN_ON(num_pages > nrptrs);
1721 reserve_bytes = round_up(write_bytes + sector_offset,
1722 fs_info->sectorsize);
1723 WARN_ON(reserve_bytes == 0);
1724 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode),
1727 if (!only_release_metadata)
1728 btrfs_free_reserved_data_space(BTRFS_I(inode),
1732 btrfs_check_nocow_unlock(BTRFS_I(inode));
1736 release_bytes = reserve_bytes;
1739 * This is going to setup the pages array with the number of
1740 * pages we want, so we don't really need to worry about the
1741 * contents of pages from loop to loop
1743 ret = prepare_pages(inode, pages, num_pages,
1745 force_page_uptodate);
1747 btrfs_delalloc_release_extents(BTRFS_I(inode),
1752 extents_locked = lock_and_cleanup_extent_if_need(
1753 BTRFS_I(inode), pages,
1754 num_pages, pos, write_bytes, &lockstart,
1755 &lockend, &cached_state);
1756 if (extents_locked < 0) {
1757 if (extents_locked == -EAGAIN)
1759 btrfs_delalloc_release_extents(BTRFS_I(inode),
1761 ret = extents_locked;
1765 copied = btrfs_copy_from_user(pos, write_bytes, pages, i);
1767 num_sectors = BTRFS_BYTES_TO_BLKS(fs_info, reserve_bytes);
1768 dirty_sectors = round_up(copied + sector_offset,
1769 fs_info->sectorsize);
1770 dirty_sectors = BTRFS_BYTES_TO_BLKS(fs_info, dirty_sectors);
1773 * if we have trouble faulting in the pages, fall
1774 * back to one page at a time
1776 if (copied < write_bytes)
1780 force_page_uptodate = true;
1784 force_page_uptodate = false;
1785 dirty_pages = DIV_ROUND_UP(copied + offset,
1789 if (num_sectors > dirty_sectors) {
1790 /* release everything except the sectors we dirtied */
1791 release_bytes -= dirty_sectors << fs_info->sectorsize_bits;
1792 if (only_release_metadata) {
1793 btrfs_delalloc_release_metadata(BTRFS_I(inode),
1794 release_bytes, true);
1798 __pos = round_down(pos,
1799 fs_info->sectorsize) +
1800 (dirty_pages << PAGE_SHIFT);
1801 btrfs_delalloc_release_space(BTRFS_I(inode),
1802 data_reserved, __pos,
1803 release_bytes, true);
1807 release_bytes = round_up(copied + sector_offset,
1808 fs_info->sectorsize);
1810 ret = btrfs_dirty_pages(BTRFS_I(inode), pages,
1811 dirty_pages, pos, copied,
1812 &cached_state, only_release_metadata);
1815 * If we have not locked the extent range, because the range's
1816 * start offset is >= i_size, we might still have a non-NULL
1817 * cached extent state, acquired while marking the extent range
1818 * as delalloc through btrfs_dirty_pages(). Therefore free any
1819 * possible cached extent state to avoid a memory leak.
1822 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1823 lockstart, lockend, &cached_state);
1825 free_extent_state(cached_state);
1827 btrfs_delalloc_release_extents(BTRFS_I(inode), reserve_bytes);
1829 btrfs_drop_pages(fs_info, pages, num_pages, pos, copied);
1834 if (only_release_metadata)
1835 btrfs_check_nocow_unlock(BTRFS_I(inode));
1837 btrfs_drop_pages(fs_info, pages, num_pages, pos, copied);
1841 balance_dirty_pages_ratelimited(inode->i_mapping);
1844 num_written += copied;
1849 if (release_bytes) {
1850 if (only_release_metadata) {
1851 btrfs_check_nocow_unlock(BTRFS_I(inode));
1852 btrfs_delalloc_release_metadata(BTRFS_I(inode),
1853 release_bytes, true);
1855 btrfs_delalloc_release_space(BTRFS_I(inode),
1857 round_down(pos, fs_info->sectorsize),
1858 release_bytes, true);
1862 extent_changeset_free(data_reserved);
1863 if (num_written > 0) {
1864 pagecache_isize_extended(inode, old_isize, iocb->ki_pos);
1865 iocb->ki_pos += num_written;
1868 btrfs_inode_unlock(inode, ilock_flags);
1869 return num_written ? num_written : ret;
1872 static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
1873 const struct iov_iter *iter, loff_t offset)
1875 const u32 blocksize_mask = fs_info->sectorsize - 1;
1877 if (offset & blocksize_mask)
1880 if (iov_iter_alignment(iter) & blocksize_mask)
1886 static ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from)
1888 const bool is_sync_write = (iocb->ki_flags & IOCB_DSYNC);
1889 struct file *file = iocb->ki_filp;
1890 struct inode *inode = file_inode(file);
1891 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1893 ssize_t written = 0;
1894 ssize_t written_buffered;
1895 size_t prev_left = 0;
1898 unsigned int ilock_flags = 0;
1900 if (iocb->ki_flags & IOCB_NOWAIT)
1901 ilock_flags |= BTRFS_ILOCK_TRY;
1903 /* If the write DIO is within EOF, use a shared lock */
1904 if (iocb->ki_pos + iov_iter_count(from) <= i_size_read(inode))
1905 ilock_flags |= BTRFS_ILOCK_SHARED;
1908 err = btrfs_inode_lock(inode, ilock_flags);
1912 err = generic_write_checks(iocb, from);
1914 btrfs_inode_unlock(inode, ilock_flags);
1918 err = btrfs_write_check(iocb, from, err);
1920 btrfs_inode_unlock(inode, ilock_flags);
1926 * Re-check since file size may have changed just before taking the
1927 * lock or pos may have changed because of O_APPEND in generic_write_check()
1929 if ((ilock_flags & BTRFS_ILOCK_SHARED) &&
1930 pos + iov_iter_count(from) > i_size_read(inode)) {
1931 btrfs_inode_unlock(inode, ilock_flags);
1932 ilock_flags &= ~BTRFS_ILOCK_SHARED;
1936 if (check_direct_IO(fs_info, from, pos)) {
1937 btrfs_inode_unlock(inode, ilock_flags);
1942 * We remove IOCB_DSYNC so that we don't deadlock when iomap_dio_rw()
1943 * calls generic_write_sync() (through iomap_dio_complete()), because
1944 * that results in calling fsync (btrfs_sync_file()) which will try to
1945 * lock the inode in exclusive/write mode.
1948 iocb->ki_flags &= ~IOCB_DSYNC;
1951 * The iov_iter can be mapped to the same file range we are writing to.
1952 * If that's the case, then we will deadlock in the iomap code, because
1953 * it first calls our callback btrfs_dio_iomap_begin(), which will create
1954 * an ordered extent, and after that it will fault in the pages that the
1955 * iov_iter refers to. During the fault in we end up in the readahead
1956 * pages code (starting at btrfs_readahead()), which will lock the range,
1957 * find that ordered extent and then wait for it to complete (at
1958 * btrfs_lock_and_flush_ordered_range()), resulting in a deadlock since
1959 * obviously the ordered extent can never complete as we didn't submit
1960 * yet the respective bio(s). This always happens when the buffer is
1961 * memory mapped to the same file range, since the iomap DIO code always
1962 * invalidates pages in the target file range (after starting and waiting
1963 * for any writeback).
1965 * So here we disable page faults in the iov_iter and then retry if we
1966 * got -EFAULT, faulting in the pages before the retry.
1969 from->nofault = true;
1970 err = iomap_dio_rw(iocb, from, &btrfs_dio_iomap_ops, &btrfs_dio_ops,
1971 IOMAP_DIO_PARTIAL, written);
1972 from->nofault = false;
1974 /* No increment (+=) because iomap returns a cumulative value. */
1978 if (iov_iter_count(from) > 0 && (err == -EFAULT || err > 0)) {
1979 const size_t left = iov_iter_count(from);
1981 * We have more data left to write. Try to fault in as many as
1982 * possible of the remainder pages and retry. We do this without
1983 * releasing and locking again the inode, to prevent races with
1986 * Also, in case the iov refers to pages in the file range of the
1987 * file we want to write to (due to a mmap), we could enter an
1988 * infinite loop if we retry after faulting the pages in, since
1989 * iomap will invalidate any pages in the range early on, before
1990 * it tries to fault in the pages of the iov. So we keep track of
1991 * how much was left of iov in the previous EFAULT and fallback
1992 * to buffered IO in case we haven't made any progress.
1994 if (left == prev_left) {
1997 fault_in_iov_iter_readable(from, left);
2003 btrfs_inode_unlock(inode, ilock_flags);
2006 * Add back IOCB_DSYNC. Our caller, btrfs_file_write_iter(), will do
2007 * the fsync (call generic_write_sync()).
2010 iocb->ki_flags |= IOCB_DSYNC;
2012 /* If 'err' is -ENOTBLK then it means we must fallback to buffered IO. */
2013 if ((err < 0 && err != -ENOTBLK) || !iov_iter_count(from))
2018 written_buffered = btrfs_buffered_write(iocb, from);
2019 if (written_buffered < 0) {
2020 err = written_buffered;
2024 * Ensure all data is persisted. We want the next direct IO read to be
2025 * able to read what was just written.
2027 endbyte = pos + written_buffered - 1;
2028 err = btrfs_fdatawrite_range(inode, pos, endbyte);
2031 err = filemap_fdatawait_range(inode->i_mapping, pos, endbyte);
2034 written += written_buffered;
2035 iocb->ki_pos = pos + written_buffered;
2036 invalidate_mapping_pages(file->f_mapping, pos >> PAGE_SHIFT,
2037 endbyte >> PAGE_SHIFT);
2039 return err < 0 ? err : written;
2042 static ssize_t btrfs_file_write_iter(struct kiocb *iocb,
2043 struct iov_iter *from)
2045 struct file *file = iocb->ki_filp;
2046 struct btrfs_inode *inode = BTRFS_I(file_inode(file));
2047 ssize_t num_written = 0;
2048 const bool sync = iocb->ki_flags & IOCB_DSYNC;
2051 * If the fs flips readonly due to some impossible error, although we
2052 * have opened a file as writable, we have to stop this write operation
2053 * to ensure consistency.
2055 if (BTRFS_FS_ERROR(inode->root->fs_info))
2058 if (!(iocb->ki_flags & IOCB_DIRECT) &&
2059 (iocb->ki_flags & IOCB_NOWAIT))
2063 atomic_inc(&inode->sync_writers);
2065 if (iocb->ki_flags & IOCB_DIRECT)
2066 num_written = btrfs_direct_write(iocb, from);
2068 num_written = btrfs_buffered_write(iocb, from);
2070 btrfs_set_inode_last_sub_trans(inode);
2072 if (num_written > 0)
2073 num_written = generic_write_sync(iocb, num_written);
2076 atomic_dec(&inode->sync_writers);
2078 current->backing_dev_info = NULL;
2082 int btrfs_release_file(struct inode *inode, struct file *filp)
2084 struct btrfs_file_private *private = filp->private_data;
2086 if (private && private->filldir_buf)
2087 kfree(private->filldir_buf);
2089 filp->private_data = NULL;
2092 * Set by setattr when we are about to truncate a file from a non-zero
2093 * size to a zero size. This tries to flush down new bytes that may
2094 * have been written if the application were using truncate to replace
2097 if (test_and_clear_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
2098 &BTRFS_I(inode)->runtime_flags))
2099 filemap_flush(inode->i_mapping);
2103 static int start_ordered_ops(struct inode *inode, loff_t start, loff_t end)
2106 struct blk_plug plug;
2109 * This is only called in fsync, which would do synchronous writes, so
2110 * a plug can merge adjacent IOs as much as possible. Esp. in case of
2111 * multiple disks using raid profile, a large IO can be split to
2112 * several segments of stripe length (currently 64K).
2114 blk_start_plug(&plug);
2115 atomic_inc(&BTRFS_I(inode)->sync_writers);
2116 ret = btrfs_fdatawrite_range(inode, start, end);
2117 atomic_dec(&BTRFS_I(inode)->sync_writers);
2118 blk_finish_plug(&plug);
2123 static inline bool skip_inode_logging(const struct btrfs_log_ctx *ctx)
2125 struct btrfs_inode *inode = BTRFS_I(ctx->inode);
2126 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2128 if (btrfs_inode_in_log(inode, fs_info->generation) &&
2129 list_empty(&ctx->ordered_extents))
2133 * If we are doing a fast fsync we can not bail out if the inode's
2134 * last_trans is <= then the last committed transaction, because we only
2135 * update the last_trans of the inode during ordered extent completion,
2136 * and for a fast fsync we don't wait for that, we only wait for the
2137 * writeback to complete.
2139 if (inode->last_trans <= fs_info->last_trans_committed &&
2140 (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) ||
2141 list_empty(&ctx->ordered_extents)))
2148 * fsync call for both files and directories. This logs the inode into
2149 * the tree log instead of forcing full commits whenever possible.
2151 * It needs to call filemap_fdatawait so that all ordered extent updates are
2152 * in the metadata btree are up to date for copying to the log.
2154 * It drops the inode mutex before doing the tree log commit. This is an
2155 * important optimization for directories because holding the mutex prevents
2156 * new operations on the dir while we write to disk.
2158 int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
2160 struct dentry *dentry = file_dentry(file);
2161 struct inode *inode = d_inode(dentry);
2162 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2163 struct btrfs_root *root = BTRFS_I(inode)->root;
2164 struct btrfs_trans_handle *trans;
2165 struct btrfs_log_ctx ctx;
2170 trace_btrfs_sync_file(file, datasync);
2172 btrfs_init_log_ctx(&ctx, inode);
2175 * Always set the range to a full range, otherwise we can get into
2176 * several problems, from missing file extent items to represent holes
2177 * when not using the NO_HOLES feature, to log tree corruption due to
2178 * races between hole detection during logging and completion of ordered
2179 * extents outside the range, to missing checksums due to ordered extents
2180 * for which we flushed only a subset of their pages.
2184 len = (u64)LLONG_MAX + 1;
2187 * We write the dirty pages in the range and wait until they complete
2188 * out of the ->i_mutex. If so, we can flush the dirty pages by
2189 * multi-task, and make the performance up. See
2190 * btrfs_wait_ordered_range for an explanation of the ASYNC check.
2192 ret = start_ordered_ops(inode, start, end);
2196 btrfs_inode_lock(inode, BTRFS_ILOCK_MMAP);
2198 atomic_inc(&root->log_batch);
2201 * Always check for the full sync flag while holding the inode's lock,
2202 * to avoid races with other tasks. The flag must be either set all the
2203 * time during logging or always off all the time while logging.
2205 full_sync = test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2206 &BTRFS_I(inode)->runtime_flags);
2209 * Before we acquired the inode's lock and the mmap lock, someone may
2210 * have dirtied more pages in the target range. We need to make sure
2211 * that writeback for any such pages does not start while we are logging
2212 * the inode, because if it does, any of the following might happen when
2213 * we are not doing a full inode sync:
2215 * 1) We log an extent after its writeback finishes but before its
2216 * checksums are added to the csum tree, leading to -EIO errors
2217 * when attempting to read the extent after a log replay.
2219 * 2) We can end up logging an extent before its writeback finishes.
2220 * Therefore after the log replay we will have a file extent item
2221 * pointing to an unwritten extent (and no data checksums as well).
2223 * So trigger writeback for any eventual new dirty pages and then we
2224 * wait for all ordered extents to complete below.
2226 ret = start_ordered_ops(inode, start, end);
2228 btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
2233 * We have to do this here to avoid the priority inversion of waiting on
2234 * IO of a lower priority task while holding a transaction open.
2236 * For a full fsync we wait for the ordered extents to complete while
2237 * for a fast fsync we wait just for writeback to complete, and then
2238 * attach the ordered extents to the transaction so that a transaction
2239 * commit waits for their completion, to avoid data loss if we fsync,
2240 * the current transaction commits before the ordered extents complete
2241 * and a power failure happens right after that.
2243 * For zoned filesystem, if a write IO uses a ZONE_APPEND command, the
2244 * logical address recorded in the ordered extent may change. We need
2245 * to wait for the IO to stabilize the logical address.
2247 if (full_sync || btrfs_is_zoned(fs_info)) {
2248 ret = btrfs_wait_ordered_range(inode, start, len);
2251 * Get our ordered extents as soon as possible to avoid doing
2252 * checksum lookups in the csum tree, and use instead the
2253 * checksums attached to the ordered extents.
2255 btrfs_get_ordered_extents_for_logging(BTRFS_I(inode),
2256 &ctx.ordered_extents);
2257 ret = filemap_fdatawait_range(inode->i_mapping, start, end);
2261 goto out_release_extents;
2263 atomic_inc(&root->log_batch);
2266 if (skip_inode_logging(&ctx)) {
2268 * We've had everything committed since the last time we were
2269 * modified so clear this flag in case it was set for whatever
2270 * reason, it's no longer relevant.
2272 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2273 &BTRFS_I(inode)->runtime_flags);
2275 * An ordered extent might have started before and completed
2276 * already with io errors, in which case the inode was not
2277 * updated and we end up here. So check the inode's mapping
2278 * for any errors that might have happened since we last
2279 * checked called fsync.
2281 ret = filemap_check_wb_err(inode->i_mapping, file->f_wb_err);
2282 goto out_release_extents;
2286 * We use start here because we will need to wait on the IO to complete
2287 * in btrfs_sync_log, which could require joining a transaction (for
2288 * example checking cross references in the nocow path). If we use join
2289 * here we could get into a situation where we're waiting on IO to
2290 * happen that is blocked on a transaction trying to commit. With start
2291 * we inc the extwriter counter, so we wait for all extwriters to exit
2292 * before we start blocking joiners. This comment is to keep somebody
2293 * from thinking they are super smart and changing this to
2294 * btrfs_join_transaction *cough*Josef*cough*.
2296 trans = btrfs_start_transaction(root, 0);
2297 if (IS_ERR(trans)) {
2298 ret = PTR_ERR(trans);
2299 goto out_release_extents;
2301 trans->in_fsync = true;
2303 ret = btrfs_log_dentry_safe(trans, dentry, &ctx);
2304 btrfs_release_log_ctx_extents(&ctx);
2306 /* Fallthrough and commit/free transaction. */
2310 /* we've logged all the items and now have a consistent
2311 * version of the file in the log. It is possible that
2312 * someone will come in and modify the file, but that's
2313 * fine because the log is consistent on disk, and we
2314 * have references to all of the file's extents
2316 * It is possible that someone will come in and log the
2317 * file again, but that will end up using the synchronization
2318 * inside btrfs_sync_log to keep things safe.
2320 btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
2322 if (ret != BTRFS_NO_LOG_SYNC) {
2324 ret = btrfs_sync_log(trans, root, &ctx);
2326 ret = btrfs_end_transaction(trans);
2331 ret = btrfs_wait_ordered_range(inode, start, len);
2333 btrfs_end_transaction(trans);
2337 ret = btrfs_commit_transaction(trans);
2339 ret = btrfs_end_transaction(trans);
2342 ASSERT(list_empty(&ctx.list));
2343 err = file_check_and_advance_wb_err(file);
2346 return ret > 0 ? -EIO : ret;
2348 out_release_extents:
2349 btrfs_release_log_ctx_extents(&ctx);
2350 btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
2354 static const struct vm_operations_struct btrfs_file_vm_ops = {
2355 .fault = filemap_fault,
2356 .map_pages = filemap_map_pages,
2357 .page_mkwrite = btrfs_page_mkwrite,
2360 static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
2362 struct address_space *mapping = filp->f_mapping;
2364 if (!mapping->a_ops->readpage)
2367 file_accessed(filp);
2368 vma->vm_ops = &btrfs_file_vm_ops;
2373 static int hole_mergeable(struct btrfs_inode *inode, struct extent_buffer *leaf,
2374 int slot, u64 start, u64 end)
2376 struct btrfs_file_extent_item *fi;
2377 struct btrfs_key key;
2379 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
2382 btrfs_item_key_to_cpu(leaf, &key, slot);
2383 if (key.objectid != btrfs_ino(inode) ||
2384 key.type != BTRFS_EXTENT_DATA_KEY)
2387 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
2389 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2392 if (btrfs_file_extent_disk_bytenr(leaf, fi))
2395 if (key.offset == end)
2397 if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
2402 static int fill_holes(struct btrfs_trans_handle *trans,
2403 struct btrfs_inode *inode,
2404 struct btrfs_path *path, u64 offset, u64 end)
2406 struct btrfs_fs_info *fs_info = trans->fs_info;
2407 struct btrfs_root *root = inode->root;
2408 struct extent_buffer *leaf;
2409 struct btrfs_file_extent_item *fi;
2410 struct extent_map *hole_em;
2411 struct extent_map_tree *em_tree = &inode->extent_tree;
2412 struct btrfs_key key;
2415 if (btrfs_fs_incompat(fs_info, NO_HOLES))
2418 key.objectid = btrfs_ino(inode);
2419 key.type = BTRFS_EXTENT_DATA_KEY;
2420 key.offset = offset;
2422 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2425 * We should have dropped this offset, so if we find it then
2426 * something has gone horribly wrong.
2433 leaf = path->nodes[0];
2434 if (hole_mergeable(inode, leaf, path->slots[0] - 1, offset, end)) {
2438 fi = btrfs_item_ptr(leaf, path->slots[0],
2439 struct btrfs_file_extent_item);
2440 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
2442 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2443 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2444 btrfs_set_file_extent_offset(leaf, fi, 0);
2445 btrfs_mark_buffer_dirty(leaf);
2449 if (hole_mergeable(inode, leaf, path->slots[0], offset, end)) {
2452 key.offset = offset;
2453 btrfs_set_item_key_safe(fs_info, path, &key);
2454 fi = btrfs_item_ptr(leaf, path->slots[0],
2455 struct btrfs_file_extent_item);
2456 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
2458 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2459 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2460 btrfs_set_file_extent_offset(leaf, fi, 0);
2461 btrfs_mark_buffer_dirty(leaf);
2464 btrfs_release_path(path);
2466 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
2467 offset, 0, 0, end - offset, 0, end - offset, 0, 0, 0);
2472 btrfs_release_path(path);
2474 hole_em = alloc_extent_map();
2476 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
2477 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
2479 hole_em->start = offset;
2480 hole_em->len = end - offset;
2481 hole_em->ram_bytes = hole_em->len;
2482 hole_em->orig_start = offset;
2484 hole_em->block_start = EXTENT_MAP_HOLE;
2485 hole_em->block_len = 0;
2486 hole_em->orig_block_len = 0;
2487 hole_em->compress_type = BTRFS_COMPRESS_NONE;
2488 hole_em->generation = trans->transid;
2491 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
2492 write_lock(&em_tree->lock);
2493 ret = add_extent_mapping(em_tree, hole_em, 1);
2494 write_unlock(&em_tree->lock);
2495 } while (ret == -EEXIST);
2496 free_extent_map(hole_em);
2498 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2499 &inode->runtime_flags);
2506 * Find a hole extent on given inode and change start/len to the end of hole
2507 * extent.(hole/vacuum extent whose em->start <= start &&
2508 * em->start + em->len > start)
2509 * When a hole extent is found, return 1 and modify start/len.
2511 static int find_first_non_hole(struct btrfs_inode *inode, u64 *start, u64 *len)
2513 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2514 struct extent_map *em;
2517 em = btrfs_get_extent(inode, NULL, 0,
2518 round_down(*start, fs_info->sectorsize),
2519 round_up(*len, fs_info->sectorsize));
2523 /* Hole or vacuum extent(only exists in no-hole mode) */
2524 if (em->block_start == EXTENT_MAP_HOLE) {
2526 *len = em->start + em->len > *start + *len ?
2527 0 : *start + *len - em->start - em->len;
2528 *start = em->start + em->len;
2530 free_extent_map(em);
2534 static int btrfs_punch_hole_lock_range(struct inode *inode,
2535 const u64 lockstart,
2537 struct extent_state **cached_state)
2540 * For subpage case, if the range is not at page boundary, we could
2541 * have pages at the leading/tailing part of the range.
2542 * This could lead to dead loop since filemap_range_has_page()
2543 * will always return true.
2544 * So here we need to do extra page alignment for
2545 * filemap_range_has_page().
2547 const u64 page_lockstart = round_up(lockstart, PAGE_SIZE);
2548 const u64 page_lockend = round_down(lockend + 1, PAGE_SIZE) - 1;
2551 struct btrfs_ordered_extent *ordered;
2554 truncate_pagecache_range(inode, lockstart, lockend);
2556 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2558 ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode),
2562 * We need to make sure we have no ordered extents in this range
2563 * and nobody raced in and read a page in this range, if we did
2564 * we need to try again.
2567 (ordered->file_offset + ordered->num_bytes <= lockstart ||
2568 ordered->file_offset > lockend)) &&
2569 !filemap_range_has_page(inode->i_mapping,
2570 page_lockstart, page_lockend)) {
2572 btrfs_put_ordered_extent(ordered);
2576 btrfs_put_ordered_extent(ordered);
2577 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
2578 lockend, cached_state);
2579 ret = btrfs_wait_ordered_range(inode, lockstart,
2580 lockend - lockstart + 1);
2587 static int btrfs_insert_replace_extent(struct btrfs_trans_handle *trans,
2588 struct btrfs_inode *inode,
2589 struct btrfs_path *path,
2590 struct btrfs_replace_extent_info *extent_info,
2591 const u64 replace_len,
2592 const u64 bytes_to_drop)
2594 struct btrfs_fs_info *fs_info = trans->fs_info;
2595 struct btrfs_root *root = inode->root;
2596 struct btrfs_file_extent_item *extent;
2597 struct extent_buffer *leaf;
2598 struct btrfs_key key;
2600 struct btrfs_ref ref = { 0 };
2603 if (replace_len == 0)
2606 if (extent_info->disk_offset == 0 &&
2607 btrfs_fs_incompat(fs_info, NO_HOLES)) {
2608 btrfs_update_inode_bytes(inode, 0, bytes_to_drop);
2612 key.objectid = btrfs_ino(inode);
2613 key.type = BTRFS_EXTENT_DATA_KEY;
2614 key.offset = extent_info->file_offset;
2615 ret = btrfs_insert_empty_item(trans, root, path, &key,
2616 sizeof(struct btrfs_file_extent_item));
2619 leaf = path->nodes[0];
2620 slot = path->slots[0];
2621 write_extent_buffer(leaf, extent_info->extent_buf,
2622 btrfs_item_ptr_offset(leaf, slot),
2623 sizeof(struct btrfs_file_extent_item));
2624 extent = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
2625 ASSERT(btrfs_file_extent_type(leaf, extent) != BTRFS_FILE_EXTENT_INLINE);
2626 btrfs_set_file_extent_offset(leaf, extent, extent_info->data_offset);
2627 btrfs_set_file_extent_num_bytes(leaf, extent, replace_len);
2628 if (extent_info->is_new_extent)
2629 btrfs_set_file_extent_generation(leaf, extent, trans->transid);
2630 btrfs_mark_buffer_dirty(leaf);
2631 btrfs_release_path(path);
2633 ret = btrfs_inode_set_file_extent_range(inode, extent_info->file_offset,
2638 /* If it's a hole, nothing more needs to be done. */
2639 if (extent_info->disk_offset == 0) {
2640 btrfs_update_inode_bytes(inode, 0, bytes_to_drop);
2644 btrfs_update_inode_bytes(inode, replace_len, bytes_to_drop);
2646 if (extent_info->is_new_extent && extent_info->insertions == 0) {
2647 key.objectid = extent_info->disk_offset;
2648 key.type = BTRFS_EXTENT_ITEM_KEY;
2649 key.offset = extent_info->disk_len;
2650 ret = btrfs_alloc_reserved_file_extent(trans, root,
2652 extent_info->file_offset,
2653 extent_info->qgroup_reserved,
2658 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,
2659 extent_info->disk_offset,
2660 extent_info->disk_len, 0);
2661 ref_offset = extent_info->file_offset - extent_info->data_offset;
2662 btrfs_init_data_ref(&ref, root->root_key.objectid,
2663 btrfs_ino(inode), ref_offset, 0, false);
2664 ret = btrfs_inc_extent_ref(trans, &ref);
2667 extent_info->insertions++;
2673 * The respective range must have been previously locked, as well as the inode.
2674 * The end offset is inclusive (last byte of the range).
2675 * @extent_info is NULL for fallocate's hole punching and non-NULL when replacing
2676 * the file range with an extent.
2677 * When not punching a hole, we don't want to end up in a state where we dropped
2678 * extents without inserting a new one, so we must abort the transaction to avoid
2681 int btrfs_replace_file_extents(struct btrfs_inode *inode,
2682 struct btrfs_path *path, const u64 start,
2684 struct btrfs_replace_extent_info *extent_info,
2685 struct btrfs_trans_handle **trans_out)
2687 struct btrfs_drop_extents_args drop_args = { 0 };
2688 struct btrfs_root *root = inode->root;
2689 struct btrfs_fs_info *fs_info = root->fs_info;
2690 u64 min_size = btrfs_calc_insert_metadata_size(fs_info, 1);
2691 u64 ino_size = round_up(inode->vfs_inode.i_size, fs_info->sectorsize);
2692 struct btrfs_trans_handle *trans = NULL;
2693 struct btrfs_block_rsv *rsv;
2694 unsigned int rsv_count;
2696 u64 len = end - start;
2702 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
2707 rsv->size = btrfs_calc_insert_metadata_size(fs_info, 1);
2711 * 1 - update the inode
2712 * 1 - removing the extents in the range
2713 * 1 - adding the hole extent if no_holes isn't set or if we are
2714 * replacing the range with a new extent
2716 if (!btrfs_fs_incompat(fs_info, NO_HOLES) || extent_info)
2721 trans = btrfs_start_transaction(root, rsv_count);
2722 if (IS_ERR(trans)) {
2723 ret = PTR_ERR(trans);
2728 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
2731 trans->block_rsv = rsv;
2734 drop_args.path = path;
2735 drop_args.end = end + 1;
2736 drop_args.drop_cache = true;
2737 while (cur_offset < end) {
2738 drop_args.start = cur_offset;
2739 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
2740 /* If we are punching a hole decrement the inode's byte count */
2742 btrfs_update_inode_bytes(inode, 0,
2743 drop_args.bytes_found);
2744 if (ret != -ENOSPC) {
2746 * The only time we don't want to abort is if we are
2747 * attempting to clone a partial inline extent, in which
2748 * case we'll get EOPNOTSUPP. However if we aren't
2749 * clone we need to abort no matter what, because if we
2750 * got EOPNOTSUPP via prealloc then we messed up and
2754 (ret != -EOPNOTSUPP ||
2755 (extent_info && extent_info->is_new_extent)))
2756 btrfs_abort_transaction(trans, ret);
2760 trans->block_rsv = &fs_info->trans_block_rsv;
2762 if (!extent_info && cur_offset < drop_args.drop_end &&
2763 cur_offset < ino_size) {
2764 ret = fill_holes(trans, inode, path, cur_offset,
2765 drop_args.drop_end);
2768 * If we failed then we didn't insert our hole
2769 * entries for the area we dropped, so now the
2770 * fs is corrupted, so we must abort the
2773 btrfs_abort_transaction(trans, ret);
2776 } else if (!extent_info && cur_offset < drop_args.drop_end) {
2778 * We are past the i_size here, but since we didn't
2779 * insert holes we need to clear the mapped area so we
2780 * know to not set disk_i_size in this area until a new
2781 * file extent is inserted here.
2783 ret = btrfs_inode_clear_file_extent_range(inode,
2785 drop_args.drop_end - cur_offset);
2788 * We couldn't clear our area, so we could
2789 * presumably adjust up and corrupt the fs, so
2792 btrfs_abort_transaction(trans, ret);
2798 drop_args.drop_end > extent_info->file_offset) {
2799 u64 replace_len = drop_args.drop_end -
2800 extent_info->file_offset;
2802 ret = btrfs_insert_replace_extent(trans, inode, path,
2803 extent_info, replace_len,
2804 drop_args.bytes_found);
2806 btrfs_abort_transaction(trans, ret);
2809 extent_info->data_len -= replace_len;
2810 extent_info->data_offset += replace_len;
2811 extent_info->file_offset += replace_len;
2814 ret = btrfs_update_inode(trans, root, inode);
2818 btrfs_end_transaction(trans);
2819 btrfs_btree_balance_dirty(fs_info);
2821 trans = btrfs_start_transaction(root, rsv_count);
2822 if (IS_ERR(trans)) {
2823 ret = PTR_ERR(trans);
2828 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
2829 rsv, min_size, false);
2830 BUG_ON(ret); /* shouldn't happen */
2831 trans->block_rsv = rsv;
2833 cur_offset = drop_args.drop_end;
2834 len = end - cur_offset;
2835 if (!extent_info && len) {
2836 ret = find_first_non_hole(inode, &cur_offset, &len);
2837 if (unlikely(ret < 0))
2847 * If we were cloning, force the next fsync to be a full one since we
2848 * we replaced (or just dropped in the case of cloning holes when
2849 * NO_HOLES is enabled) file extent items and did not setup new extent
2850 * maps for the replacement extents (or holes).
2852 if (extent_info && !extent_info->is_new_extent)
2853 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
2858 trans->block_rsv = &fs_info->trans_block_rsv;
2860 * If we are using the NO_HOLES feature we might have had already an
2861 * hole that overlaps a part of the region [lockstart, lockend] and
2862 * ends at (or beyond) lockend. Since we have no file extent items to
2863 * represent holes, drop_end can be less than lockend and so we must
2864 * make sure we have an extent map representing the existing hole (the
2865 * call to __btrfs_drop_extents() might have dropped the existing extent
2866 * map representing the existing hole), otherwise the fast fsync path
2867 * will not record the existence of the hole region
2868 * [existing_hole_start, lockend].
2870 if (drop_args.drop_end <= end)
2871 drop_args.drop_end = end + 1;
2873 * Don't insert file hole extent item if it's for a range beyond eof
2874 * (because it's useless) or if it represents a 0 bytes range (when
2875 * cur_offset == drop_end).
2877 if (!extent_info && cur_offset < ino_size &&
2878 cur_offset < drop_args.drop_end) {
2879 ret = fill_holes(trans, inode, path, cur_offset,
2880 drop_args.drop_end);
2882 /* Same comment as above. */
2883 btrfs_abort_transaction(trans, ret);
2886 } else if (!extent_info && cur_offset < drop_args.drop_end) {
2887 /* See the comment in the loop above for the reasoning here. */
2888 ret = btrfs_inode_clear_file_extent_range(inode, cur_offset,
2889 drop_args.drop_end - cur_offset);
2891 btrfs_abort_transaction(trans, ret);
2897 ret = btrfs_insert_replace_extent(trans, inode, path,
2898 extent_info, extent_info->data_len,
2899 drop_args.bytes_found);
2901 btrfs_abort_transaction(trans, ret);
2910 trans->block_rsv = &fs_info->trans_block_rsv;
2912 btrfs_end_transaction(trans);
2916 btrfs_free_block_rsv(fs_info, rsv);
2921 static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2923 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2924 struct btrfs_root *root = BTRFS_I(inode)->root;
2925 struct extent_state *cached_state = NULL;
2926 struct btrfs_path *path;
2927 struct btrfs_trans_handle *trans = NULL;
2932 u64 orig_start = offset;
2936 bool truncated_block = false;
2937 bool updated_inode = false;
2939 ret = btrfs_wait_ordered_range(inode, offset, len);
2943 btrfs_inode_lock(inode, BTRFS_ILOCK_MMAP);
2944 ino_size = round_up(inode->i_size, fs_info->sectorsize);
2945 ret = find_first_non_hole(BTRFS_I(inode), &offset, &len);
2947 goto out_only_mutex;
2949 /* Already in a large hole */
2951 goto out_only_mutex;
2954 lockstart = round_up(offset, btrfs_inode_sectorsize(BTRFS_I(inode)));
2955 lockend = round_down(offset + len,
2956 btrfs_inode_sectorsize(BTRFS_I(inode))) - 1;
2957 same_block = (BTRFS_BYTES_TO_BLKS(fs_info, offset))
2958 == (BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1));
2960 * We needn't truncate any block which is beyond the end of the file
2961 * because we are sure there is no data there.
2964 * Only do this if we are in the same block and we aren't doing the
2967 if (same_block && len < fs_info->sectorsize) {
2968 if (offset < ino_size) {
2969 truncated_block = true;
2970 ret = btrfs_truncate_block(BTRFS_I(inode), offset, len,
2975 goto out_only_mutex;
2978 /* zero back part of the first block */
2979 if (offset < ino_size) {
2980 truncated_block = true;
2981 ret = btrfs_truncate_block(BTRFS_I(inode), offset, 0, 0);
2983 btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
2988 /* Check the aligned pages after the first unaligned page,
2989 * if offset != orig_start, which means the first unaligned page
2990 * including several following pages are already in holes,
2991 * the extra check can be skipped */
2992 if (offset == orig_start) {
2993 /* after truncate page, check hole again */
2994 len = offset + len - lockstart;
2996 ret = find_first_non_hole(BTRFS_I(inode), &offset, &len);
2998 goto out_only_mutex;
3001 goto out_only_mutex;
3006 /* Check the tail unaligned part is in a hole */
3007 tail_start = lockend + 1;
3008 tail_len = offset + len - tail_start;
3010 ret = find_first_non_hole(BTRFS_I(inode), &tail_start, &tail_len);
3011 if (unlikely(ret < 0))
3012 goto out_only_mutex;
3014 /* zero the front end of the last page */
3015 if (tail_start + tail_len < ino_size) {
3016 truncated_block = true;
3017 ret = btrfs_truncate_block(BTRFS_I(inode),
3018 tail_start + tail_len,
3021 goto out_only_mutex;
3026 if (lockend < lockstart) {
3028 goto out_only_mutex;
3031 ret = btrfs_punch_hole_lock_range(inode, lockstart, lockend,
3034 goto out_only_mutex;
3036 path = btrfs_alloc_path();
3042 ret = btrfs_replace_file_extents(BTRFS_I(inode), path, lockstart,
3043 lockend, NULL, &trans);
3044 btrfs_free_path(path);
3048 ASSERT(trans != NULL);
3049 inode_inc_iversion(inode);
3050 inode->i_mtime = inode->i_ctime = current_time(inode);
3051 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
3052 updated_inode = true;
3053 btrfs_end_transaction(trans);
3054 btrfs_btree_balance_dirty(fs_info);
3056 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
3059 if (!updated_inode && truncated_block && !ret) {
3061 * If we only end up zeroing part of a page, we still need to
3062 * update the inode item, so that all the time fields are
3063 * updated as well as the necessary btrfs inode in memory fields
3064 * for detecting, at fsync time, if the inode isn't yet in the
3065 * log tree or it's there but not up to date.
3067 struct timespec64 now = current_time(inode);
3069 inode_inc_iversion(inode);
3070 inode->i_mtime = now;
3071 inode->i_ctime = now;
3072 trans = btrfs_start_transaction(root, 1);
3073 if (IS_ERR(trans)) {
3074 ret = PTR_ERR(trans);
3078 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
3079 ret2 = btrfs_end_transaction(trans);
3084 btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
3088 /* Helper structure to record which range is already reserved */
3089 struct falloc_range {
3090 struct list_head list;
3096 * Helper function to add falloc range
3098 * Caller should have locked the larger range of extent containing
3101 static int add_falloc_range(struct list_head *head, u64 start, u64 len)
3103 struct falloc_range *range = NULL;
3105 if (!list_empty(head)) {
3107 * As fallocate iterates by bytenr order, we only need to check
3110 range = list_last_entry(head, struct falloc_range, list);
3111 if (range->start + range->len == start) {
3117 range = kmalloc(sizeof(*range), GFP_KERNEL);
3120 range->start = start;
3122 list_add_tail(&range->list, head);
3126 static int btrfs_fallocate_update_isize(struct inode *inode,
3130 struct btrfs_trans_handle *trans;
3131 struct btrfs_root *root = BTRFS_I(inode)->root;
3135 if (mode & FALLOC_FL_KEEP_SIZE || end <= i_size_read(inode))
3138 trans = btrfs_start_transaction(root, 1);
3140 return PTR_ERR(trans);
3142 inode->i_ctime = current_time(inode);
3143 i_size_write(inode, end);
3144 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
3145 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
3146 ret2 = btrfs_end_transaction(trans);
3148 return ret ? ret : ret2;
3152 RANGE_BOUNDARY_WRITTEN_EXTENT,
3153 RANGE_BOUNDARY_PREALLOC_EXTENT,
3154 RANGE_BOUNDARY_HOLE,
3157 static int btrfs_zero_range_check_range_boundary(struct btrfs_inode *inode,
3160 const u64 sectorsize = btrfs_inode_sectorsize(inode);
3161 struct extent_map *em;
3164 offset = round_down(offset, sectorsize);
3165 em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize);
3169 if (em->block_start == EXTENT_MAP_HOLE)
3170 ret = RANGE_BOUNDARY_HOLE;
3171 else if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3172 ret = RANGE_BOUNDARY_PREALLOC_EXTENT;
3174 ret = RANGE_BOUNDARY_WRITTEN_EXTENT;
3176 free_extent_map(em);
3180 static int btrfs_zero_range(struct inode *inode,
3185 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3186 struct extent_map *em;
3187 struct extent_changeset *data_reserved = NULL;
3190 const u64 sectorsize = btrfs_inode_sectorsize(BTRFS_I(inode));
3191 u64 alloc_start = round_down(offset, sectorsize);
3192 u64 alloc_end = round_up(offset + len, sectorsize);
3193 u64 bytes_to_reserve = 0;
3194 bool space_reserved = false;
3196 inode_dio_wait(inode);
3198 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, alloc_start,
3199 alloc_end - alloc_start);
3206 * Avoid hole punching and extent allocation for some cases. More cases
3207 * could be considered, but these are unlikely common and we keep things
3208 * as simple as possible for now. Also, intentionally, if the target
3209 * range contains one or more prealloc extents together with regular
3210 * extents and holes, we drop all the existing extents and allocate a
3211 * new prealloc extent, so that we get a larger contiguous disk extent.
3213 if (em->start <= alloc_start &&
3214 test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3215 const u64 em_end = em->start + em->len;
3217 if (em_end >= offset + len) {
3219 * The whole range is already a prealloc extent,
3220 * do nothing except updating the inode's i_size if
3223 free_extent_map(em);
3224 ret = btrfs_fallocate_update_isize(inode, offset + len,
3229 * Part of the range is already a prealloc extent, so operate
3230 * only on the remaining part of the range.
3232 alloc_start = em_end;
3233 ASSERT(IS_ALIGNED(alloc_start, sectorsize));
3234 len = offset + len - alloc_start;
3235 offset = alloc_start;
3236 alloc_hint = em->block_start + em->len;
3238 free_extent_map(em);
3240 if (BTRFS_BYTES_TO_BLKS(fs_info, offset) ==
3241 BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1)) {
3242 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, alloc_start,
3249 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3250 free_extent_map(em);
3251 ret = btrfs_fallocate_update_isize(inode, offset + len,
3255 if (len < sectorsize && em->block_start != EXTENT_MAP_HOLE) {
3256 free_extent_map(em);
3257 ret = btrfs_truncate_block(BTRFS_I(inode), offset, len,
3260 ret = btrfs_fallocate_update_isize(inode,
3265 free_extent_map(em);
3266 alloc_start = round_down(offset, sectorsize);
3267 alloc_end = alloc_start + sectorsize;
3271 alloc_start = round_up(offset, sectorsize);
3272 alloc_end = round_down(offset + len, sectorsize);
3275 * For unaligned ranges, check the pages at the boundaries, they might
3276 * map to an extent, in which case we need to partially zero them, or
3277 * they might map to a hole, in which case we need our allocation range
3280 if (!IS_ALIGNED(offset, sectorsize)) {
3281 ret = btrfs_zero_range_check_range_boundary(BTRFS_I(inode),
3285 if (ret == RANGE_BOUNDARY_HOLE) {
3286 alloc_start = round_down(offset, sectorsize);
3288 } else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) {
3289 ret = btrfs_truncate_block(BTRFS_I(inode), offset, 0, 0);
3297 if (!IS_ALIGNED(offset + len, sectorsize)) {
3298 ret = btrfs_zero_range_check_range_boundary(BTRFS_I(inode),
3302 if (ret == RANGE_BOUNDARY_HOLE) {
3303 alloc_end = round_up(offset + len, sectorsize);
3305 } else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) {
3306 ret = btrfs_truncate_block(BTRFS_I(inode), offset + len,
3316 if (alloc_start < alloc_end) {
3317 struct extent_state *cached_state = NULL;
3318 const u64 lockstart = alloc_start;
3319 const u64 lockend = alloc_end - 1;
3321 bytes_to_reserve = alloc_end - alloc_start;
3322 ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode),
3326 space_reserved = true;
3327 ret = btrfs_punch_hole_lock_range(inode, lockstart, lockend,
3331 ret = btrfs_qgroup_reserve_data(BTRFS_I(inode), &data_reserved,
3332 alloc_start, bytes_to_reserve);
3334 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
3335 lockend, &cached_state);
3338 ret = btrfs_prealloc_file_range(inode, mode, alloc_start,
3339 alloc_end - alloc_start,
3341 offset + len, &alloc_hint);
3342 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
3343 lockend, &cached_state);
3344 /* btrfs_prealloc_file_range releases reserved space on error */
3346 space_reserved = false;
3350 ret = btrfs_fallocate_update_isize(inode, offset + len, mode);
3352 if (ret && space_reserved)
3353 btrfs_free_reserved_data_space(BTRFS_I(inode), data_reserved,
3354 alloc_start, bytes_to_reserve);
3355 extent_changeset_free(data_reserved);
3360 static long btrfs_fallocate(struct file *file, int mode,
3361 loff_t offset, loff_t len)
3363 struct inode *inode = file_inode(file);
3364 struct extent_state *cached_state = NULL;
3365 struct extent_changeset *data_reserved = NULL;
3366 struct falloc_range *range;
3367 struct falloc_range *tmp;
3368 struct list_head reserve_list;
3376 struct extent_map *em;
3377 int blocksize = btrfs_inode_sectorsize(BTRFS_I(inode));
3380 /* Do not allow fallocate in ZONED mode */
3381 if (btrfs_is_zoned(btrfs_sb(inode->i_sb)))
3384 alloc_start = round_down(offset, blocksize);
3385 alloc_end = round_up(offset + len, blocksize);
3386 cur_offset = alloc_start;
3388 /* Make sure we aren't being give some crap mode */
3389 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
3390 FALLOC_FL_ZERO_RANGE))
3393 if (mode & FALLOC_FL_PUNCH_HOLE)
3394 return btrfs_punch_hole(inode, offset, len);
3397 * Only trigger disk allocation, don't trigger qgroup reserve
3399 * For qgroup space, it will be checked later.
3401 if (!(mode & FALLOC_FL_ZERO_RANGE)) {
3402 ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode),
3403 alloc_end - alloc_start);
3408 btrfs_inode_lock(inode, BTRFS_ILOCK_MMAP);
3410 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) {
3411 ret = inode_newsize_ok(inode, offset + len);
3417 * TODO: Move these two operations after we have checked
3418 * accurate reserved space, or fallocate can still fail but
3419 * with page truncated or size expanded.
3421 * But that's a minor problem and won't do much harm BTW.
3423 if (alloc_start > inode->i_size) {
3424 ret = btrfs_cont_expand(BTRFS_I(inode), i_size_read(inode),
3428 } else if (offset + len > inode->i_size) {
3430 * If we are fallocating from the end of the file onward we
3431 * need to zero out the end of the block if i_size lands in the
3432 * middle of a block.
3434 ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
3440 * wait for ordered IO before we have any locks. We'll loop again
3441 * below with the locks held.
3443 ret = btrfs_wait_ordered_range(inode, alloc_start,
3444 alloc_end - alloc_start);
3448 if (mode & FALLOC_FL_ZERO_RANGE) {
3449 ret = btrfs_zero_range(inode, offset, len, mode);
3450 btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
3454 locked_end = alloc_end - 1;
3456 struct btrfs_ordered_extent *ordered;
3458 /* the extent lock is ordered inside the running
3461 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
3462 locked_end, &cached_state);
3463 ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode),
3467 ordered->file_offset + ordered->num_bytes > alloc_start &&
3468 ordered->file_offset < alloc_end) {
3469 btrfs_put_ordered_extent(ordered);
3470 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
3471 alloc_start, locked_end,
3474 * we can't wait on the range with the transaction
3475 * running or with the extent lock held
3477 ret = btrfs_wait_ordered_range(inode, alloc_start,
3478 alloc_end - alloc_start);
3483 btrfs_put_ordered_extent(ordered);
3488 /* First, check if we exceed the qgroup limit */
3489 INIT_LIST_HEAD(&reserve_list);
3490 while (cur_offset < alloc_end) {
3491 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
3492 alloc_end - cur_offset);
3497 last_byte = min(extent_map_end(em), alloc_end);
3498 actual_end = min_t(u64, extent_map_end(em), offset + len);
3499 last_byte = ALIGN(last_byte, blocksize);
3500 if (em->block_start == EXTENT_MAP_HOLE ||
3501 (cur_offset >= inode->i_size &&
3502 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
3503 ret = add_falloc_range(&reserve_list, cur_offset,
3504 last_byte - cur_offset);
3506 free_extent_map(em);
3509 ret = btrfs_qgroup_reserve_data(BTRFS_I(inode),
3510 &data_reserved, cur_offset,
3511 last_byte - cur_offset);
3513 cur_offset = last_byte;
3514 free_extent_map(em);
3519 * Do not need to reserve unwritten extent for this
3520 * range, free reserved data space first, otherwise
3521 * it'll result in false ENOSPC error.
3523 btrfs_free_reserved_data_space(BTRFS_I(inode),
3524 data_reserved, cur_offset,
3525 last_byte - cur_offset);
3527 free_extent_map(em);
3528 cur_offset = last_byte;
3532 * If ret is still 0, means we're OK to fallocate.
3533 * Or just cleanup the list and exit.
3535 list_for_each_entry_safe(range, tmp, &reserve_list, list) {
3537 ret = btrfs_prealloc_file_range(inode, mode,
3539 range->len, i_blocksize(inode),
3540 offset + len, &alloc_hint);
3542 btrfs_free_reserved_data_space(BTRFS_I(inode),
3543 data_reserved, range->start,
3545 list_del(&range->list);
3552 * We didn't need to allocate any more space, but we still extended the
3553 * size of the file so we need to update i_size and the inode item.
3555 ret = btrfs_fallocate_update_isize(inode, actual_end, mode);
3557 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
3560 btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
3561 /* Let go of our reservation. */
3562 if (ret != 0 && !(mode & FALLOC_FL_ZERO_RANGE))
3563 btrfs_free_reserved_data_space(BTRFS_I(inode), data_reserved,
3564 cur_offset, alloc_end - cur_offset);
3565 extent_changeset_free(data_reserved);
3569 static loff_t find_desired_extent(struct btrfs_inode *inode, loff_t offset,
3572 struct btrfs_fs_info *fs_info = inode->root->fs_info;
3573 struct extent_map *em = NULL;
3574 struct extent_state *cached_state = NULL;
3575 loff_t i_size = inode->vfs_inode.i_size;
3582 if (i_size == 0 || offset >= i_size)
3586 * offset can be negative, in this case we start finding DATA/HOLE from
3587 * the very start of the file.
3589 start = max_t(loff_t, 0, offset);
3591 lockstart = round_down(start, fs_info->sectorsize);
3592 lockend = round_up(i_size, fs_info->sectorsize);
3593 if (lockend <= lockstart)
3594 lockend = lockstart + fs_info->sectorsize;
3596 len = lockend - lockstart + 1;
3598 lock_extent_bits(&inode->io_tree, lockstart, lockend, &cached_state);
3600 while (start < i_size) {
3601 em = btrfs_get_extent_fiemap(inode, start, len);
3608 if (whence == SEEK_HOLE &&
3609 (em->block_start == EXTENT_MAP_HOLE ||
3610 test_bit(EXTENT_FLAG_PREALLOC, &em->flags)))
3612 else if (whence == SEEK_DATA &&
3613 (em->block_start != EXTENT_MAP_HOLE &&
3614 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)))
3617 start = em->start + em->len;
3618 free_extent_map(em);
3622 free_extent_map(em);
3623 unlock_extent_cached(&inode->io_tree, lockstart, lockend,
3628 if (whence == SEEK_DATA && start >= i_size)
3631 offset = min_t(loff_t, start, i_size);
3637 static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)
3639 struct inode *inode = file->f_mapping->host;
3643 return generic_file_llseek(file, offset, whence);
3646 btrfs_inode_lock(inode, BTRFS_ILOCK_SHARED);
3647 offset = find_desired_extent(BTRFS_I(inode), offset, whence);
3648 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
3655 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3658 static int btrfs_file_open(struct inode *inode, struct file *filp)
3662 filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
3664 ret = fsverity_file_open(inode, filp);
3667 return generic_file_open(inode, filp);
3670 static int check_direct_read(struct btrfs_fs_info *fs_info,
3671 const struct iov_iter *iter, loff_t offset)
3676 ret = check_direct_IO(fs_info, iter, offset);
3680 if (!iter_is_iovec(iter))
3683 for (seg = 0; seg < iter->nr_segs; seg++)
3684 for (i = seg + 1; i < iter->nr_segs; i++)
3685 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
3690 static ssize_t btrfs_direct_read(struct kiocb *iocb, struct iov_iter *to)
3692 struct inode *inode = file_inode(iocb->ki_filp);
3693 size_t prev_left = 0;
3697 if (fsverity_active(inode))
3700 if (check_direct_read(btrfs_sb(inode->i_sb), to, iocb->ki_pos))
3703 btrfs_inode_lock(inode, BTRFS_ILOCK_SHARED);
3706 * This is similar to what we do for direct IO writes, see the comment
3707 * at btrfs_direct_write(), but we also disable page faults in addition
3708 * to disabling them only at the iov_iter level. This is because when
3709 * reading from a hole or prealloc extent, iomap calls iov_iter_zero(),
3710 * which can still trigger page fault ins despite having set ->nofault
3711 * to true of our 'to' iov_iter.
3713 * The difference to direct IO writes is that we deadlock when trying
3714 * to lock the extent range in the inode's tree during he page reads
3715 * triggered by the fault in (while for writes it is due to waiting for
3716 * our own ordered extent). This is because for direct IO reads,
3717 * btrfs_dio_iomap_begin() returns with the extent range locked, which
3718 * is only unlocked in the endio callback (end_bio_extent_readpage()).
3720 pagefault_disable();
3722 ret = iomap_dio_rw(iocb, to, &btrfs_dio_iomap_ops, &btrfs_dio_ops,
3723 IOMAP_DIO_PARTIAL, read);
3724 to->nofault = false;
3727 /* No increment (+=) because iomap returns a cumulative value. */
3731 if (iov_iter_count(to) > 0 && (ret == -EFAULT || ret > 0)) {
3732 const size_t left = iov_iter_count(to);
3734 if (left == prev_left) {
3736 * We didn't make any progress since the last attempt,
3737 * fallback to a buffered read for the remainder of the
3738 * range. This is just to avoid any possibility of looping
3744 * We made some progress since the last retry or this is
3745 * the first time we are retrying. Fault in as many pages
3746 * as possible and retry.
3748 fault_in_iov_iter_writeable(to, left);
3753 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
3754 return ret < 0 ? ret : read;
3757 static ssize_t btrfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
3761 if (iocb->ki_flags & IOCB_DIRECT) {
3762 ret = btrfs_direct_read(iocb, to);
3763 if (ret < 0 || !iov_iter_count(to) ||
3764 iocb->ki_pos >= i_size_read(file_inode(iocb->ki_filp)))
3768 return filemap_read(iocb, to, ret);
3771 const struct file_operations btrfs_file_operations = {
3772 .llseek = btrfs_file_llseek,
3773 .read_iter = btrfs_file_read_iter,
3774 .splice_read = generic_file_splice_read,
3775 .write_iter = btrfs_file_write_iter,
3776 .splice_write = iter_file_splice_write,
3777 .mmap = btrfs_file_mmap,
3778 .open = btrfs_file_open,
3779 .release = btrfs_release_file,
3780 .fsync = btrfs_sync_file,
3781 .fallocate = btrfs_fallocate,
3782 .unlocked_ioctl = btrfs_ioctl,
3783 #ifdef CONFIG_COMPAT
3784 .compat_ioctl = btrfs_compat_ioctl,
3786 .remap_file_range = btrfs_remap_file_range,
3789 void __cold btrfs_auto_defrag_exit(void)
3791 kmem_cache_destroy(btrfs_inode_defrag_cachep);
3794 int __init btrfs_auto_defrag_init(void)
3796 btrfs_inode_defrag_cachep = kmem_cache_create("btrfs_inode_defrag",
3797 sizeof(struct inode_defrag), 0,
3800 if (!btrfs_inode_defrag_cachep)
3806 int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end)
3811 * So with compression we will find and lock a dirty page and clear the
3812 * first one as dirty, setup an async extent, and immediately return
3813 * with the entire range locked but with nobody actually marked with
3814 * writeback. So we can't just filemap_write_and_wait_range() and
3815 * expect it to work since it will just kick off a thread to do the
3816 * actual work. So we need to call filemap_fdatawrite_range _again_
3817 * since it will wait on the page lock, which won't be unlocked until
3818 * after the pages have been marked as writeback and so we're good to go
3819 * from there. We have to do this otherwise we'll miss the ordered
3820 * extents and that results in badness. Please Josef, do not think you
3821 * know better and pull this out at some point in the future, it is
3822 * right and you are wrong.
3824 ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
3825 if (!ret && test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
3826 &BTRFS_I(inode)->runtime_flags))
3827 ret = filemap_fdatawrite_range(inode->i_mapping, start, end);