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>
21 #include "transaction.h"
22 #include "btrfs_inode.h"
23 #include "print-tree.h"
28 #include "compression.h"
29 #include "delalloc-space.h"
32 static struct kmem_cache *btrfs_inode_defrag_cachep;
34 * when auto defrag is enabled we
35 * queue up these defrag structs to remember which
36 * inodes need defragging passes
39 struct rb_node rb_node;
43 * transid where the defrag was added, we search for
44 * extents newer than this
51 /* last offset we were able to defrag */
54 /* if we've wrapped around back to zero once already */
58 static int __compare_inode_defrag(struct inode_defrag *defrag1,
59 struct inode_defrag *defrag2)
61 if (defrag1->root > defrag2->root)
63 else if (defrag1->root < defrag2->root)
65 else if (defrag1->ino > defrag2->ino)
67 else if (defrag1->ino < defrag2->ino)
73 /* pop a record for an inode into the defrag tree. The lock
74 * must be held already
76 * If you're inserting a record for an older transid than an
77 * existing record, the transid already in the tree is lowered
79 * If an existing record is found the defrag item you
82 static int __btrfs_add_inode_defrag(struct btrfs_inode *inode,
83 struct inode_defrag *defrag)
85 struct btrfs_fs_info *fs_info = inode->root->fs_info;
86 struct inode_defrag *entry;
88 struct rb_node *parent = NULL;
91 p = &fs_info->defrag_inodes.rb_node;
94 entry = rb_entry(parent, struct inode_defrag, rb_node);
96 ret = __compare_inode_defrag(defrag, entry);
100 p = &parent->rb_right;
102 /* if we're reinserting an entry for
103 * an old defrag run, make sure to
104 * lower the transid of our existing record
106 if (defrag->transid < entry->transid)
107 entry->transid = defrag->transid;
108 if (defrag->last_offset > entry->last_offset)
109 entry->last_offset = defrag->last_offset;
113 set_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags);
114 rb_link_node(&defrag->rb_node, parent, p);
115 rb_insert_color(&defrag->rb_node, &fs_info->defrag_inodes);
119 static inline int __need_auto_defrag(struct btrfs_fs_info *fs_info)
121 if (!btrfs_test_opt(fs_info, AUTO_DEFRAG))
124 if (btrfs_fs_closing(fs_info))
131 * insert a defrag record for this inode if auto defrag is
134 int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
135 struct btrfs_inode *inode)
137 struct btrfs_root *root = inode->root;
138 struct btrfs_fs_info *fs_info = root->fs_info;
139 struct inode_defrag *defrag;
143 if (!__need_auto_defrag(fs_info))
146 if (test_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags))
150 transid = trans->transid;
152 transid = inode->root->last_trans;
154 defrag = kmem_cache_zalloc(btrfs_inode_defrag_cachep, GFP_NOFS);
158 defrag->ino = btrfs_ino(inode);
159 defrag->transid = transid;
160 defrag->root = root->root_key.objectid;
162 spin_lock(&fs_info->defrag_inodes_lock);
163 if (!test_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags)) {
165 * If we set IN_DEFRAG flag and evict the inode from memory,
166 * and then re-read this inode, this new inode doesn't have
167 * IN_DEFRAG flag. At the case, we may find the existed defrag.
169 ret = __btrfs_add_inode_defrag(inode, defrag);
171 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
173 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
175 spin_unlock(&fs_info->defrag_inodes_lock);
180 * Requeue the defrag object. If there is a defrag object that points to
181 * the same inode in the tree, we will merge them together (by
182 * __btrfs_add_inode_defrag()) and free the one that we want to requeue.
184 static void btrfs_requeue_inode_defrag(struct btrfs_inode *inode,
185 struct inode_defrag *defrag)
187 struct btrfs_fs_info *fs_info = inode->root->fs_info;
190 if (!__need_auto_defrag(fs_info))
194 * Here we don't check the IN_DEFRAG flag, because we need merge
197 spin_lock(&fs_info->defrag_inodes_lock);
198 ret = __btrfs_add_inode_defrag(inode, defrag);
199 spin_unlock(&fs_info->defrag_inodes_lock);
204 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
208 * pick the defragable inode that we want, if it doesn't exist, we will get
211 static struct inode_defrag *
212 btrfs_pick_defrag_inode(struct btrfs_fs_info *fs_info, u64 root, u64 ino)
214 struct inode_defrag *entry = NULL;
215 struct inode_defrag tmp;
217 struct rb_node *parent = NULL;
223 spin_lock(&fs_info->defrag_inodes_lock);
224 p = fs_info->defrag_inodes.rb_node;
227 entry = rb_entry(parent, struct inode_defrag, rb_node);
229 ret = __compare_inode_defrag(&tmp, entry);
233 p = parent->rb_right;
238 if (parent && __compare_inode_defrag(&tmp, entry) > 0) {
239 parent = rb_next(parent);
241 entry = rb_entry(parent, struct inode_defrag, rb_node);
247 rb_erase(parent, &fs_info->defrag_inodes);
248 spin_unlock(&fs_info->defrag_inodes_lock);
252 void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info)
254 struct inode_defrag *defrag;
255 struct rb_node *node;
257 spin_lock(&fs_info->defrag_inodes_lock);
258 node = rb_first(&fs_info->defrag_inodes);
260 rb_erase(node, &fs_info->defrag_inodes);
261 defrag = rb_entry(node, struct inode_defrag, rb_node);
262 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
264 cond_resched_lock(&fs_info->defrag_inodes_lock);
266 node = rb_first(&fs_info->defrag_inodes);
268 spin_unlock(&fs_info->defrag_inodes_lock);
271 #define BTRFS_DEFRAG_BATCH 1024
273 static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
274 struct inode_defrag *defrag)
276 struct btrfs_root *inode_root;
278 struct btrfs_ioctl_defrag_range_args range;
283 inode_root = btrfs_get_fs_root(fs_info, defrag->root, true);
284 if (IS_ERR(inode_root)) {
285 ret = PTR_ERR(inode_root);
289 inode = btrfs_iget(fs_info->sb, defrag->ino, inode_root);
290 btrfs_put_root(inode_root);
292 ret = PTR_ERR(inode);
296 /* do a chunk of defrag */
297 clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
298 memset(&range, 0, sizeof(range));
300 range.start = defrag->last_offset;
302 sb_start_write(fs_info->sb);
303 num_defrag = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
305 sb_end_write(fs_info->sb);
307 * if we filled the whole defrag batch, there
308 * must be more work to do. Queue this defrag
311 if (num_defrag == BTRFS_DEFRAG_BATCH) {
312 defrag->last_offset = range.start;
313 btrfs_requeue_inode_defrag(BTRFS_I(inode), defrag);
314 } else if (defrag->last_offset && !defrag->cycled) {
316 * we didn't fill our defrag batch, but
317 * we didn't start at zero. Make sure we loop
318 * around to the start of the file.
320 defrag->last_offset = 0;
322 btrfs_requeue_inode_defrag(BTRFS_I(inode), defrag);
324 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
330 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
335 * run through the list of inodes in the FS that need
338 int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info)
340 struct inode_defrag *defrag;
342 u64 root_objectid = 0;
344 atomic_inc(&fs_info->defrag_running);
346 /* Pause the auto defragger. */
347 if (test_bit(BTRFS_FS_STATE_REMOUNTING,
351 if (!__need_auto_defrag(fs_info))
354 /* find an inode to defrag */
355 defrag = btrfs_pick_defrag_inode(fs_info, root_objectid,
358 if (root_objectid || first_ino) {
367 first_ino = defrag->ino + 1;
368 root_objectid = defrag->root;
370 __btrfs_run_defrag_inode(fs_info, defrag);
372 atomic_dec(&fs_info->defrag_running);
375 * during unmount, we use the transaction_wait queue to
376 * wait for the defragger to stop
378 wake_up(&fs_info->transaction_wait);
382 /* simple helper to fault in pages and copy. This should go away
383 * and be replaced with calls into generic code.
385 static noinline int btrfs_copy_from_user(loff_t pos, size_t write_bytes,
386 struct page **prepared_pages,
390 size_t total_copied = 0;
392 int offset = offset_in_page(pos);
394 while (write_bytes > 0) {
395 size_t count = min_t(size_t,
396 PAGE_SIZE - offset, write_bytes);
397 struct page *page = prepared_pages[pg];
399 * Copy data from userspace to the current page
401 copied = iov_iter_copy_from_user_atomic(page, i, offset, count);
403 /* Flush processor's dcache for this page */
404 flush_dcache_page(page);
407 * if we get a partial write, we can end up with
408 * partially up to date pages. These add
409 * a lot of complexity, so make sure they don't
410 * happen by forcing this copy to be retried.
412 * The rest of the btrfs_file_write code will fall
413 * back to page at a time copies after we return 0.
415 if (!PageUptodate(page) && copied < count)
418 iov_iter_advance(i, copied);
419 write_bytes -= copied;
420 total_copied += copied;
422 /* Return to btrfs_file_write_iter to fault page */
423 if (unlikely(copied == 0))
426 if (copied < PAGE_SIZE - offset) {
437 * unlocks pages after btrfs_file_write is done with them
439 static void btrfs_drop_pages(struct page **pages, size_t num_pages)
442 for (i = 0; i < num_pages; i++) {
443 /* page checked is some magic around finding pages that
444 * have been modified without going through btrfs_set_page_dirty
445 * clear it here. There should be no need to mark the pages
446 * accessed as prepare_pages should have marked them accessed
447 * in prepare_pages via find_or_create_page()
449 ClearPageChecked(pages[i]);
450 unlock_page(pages[i]);
456 * after copy_from_user, pages need to be dirtied and we need to make
457 * sure holes are created between the current EOF and the start of
458 * any next extents (if required).
460 * this also makes the decision about creating an inline extent vs
461 * doing real data extents, marking pages dirty and delalloc as required.
463 int btrfs_dirty_pages(struct btrfs_inode *inode, struct page **pages,
464 size_t num_pages, loff_t pos, size_t write_bytes,
465 struct extent_state **cached)
467 struct btrfs_fs_info *fs_info = inode->root->fs_info;
472 u64 end_of_last_block;
473 u64 end_pos = pos + write_bytes;
474 loff_t isize = i_size_read(&inode->vfs_inode);
475 unsigned int extra_bits = 0;
477 start_pos = pos & ~((u64) fs_info->sectorsize - 1);
478 num_bytes = round_up(write_bytes + pos - start_pos,
479 fs_info->sectorsize);
481 end_of_last_block = start_pos + num_bytes - 1;
484 * The pages may have already been dirty, clear out old accounting so
485 * we can set things up properly
487 clear_extent_bit(&inode->io_tree, start_pos, end_of_last_block,
488 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
491 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
496 for (i = 0; i < num_pages; i++) {
497 struct page *p = pages[i];
504 * we've only changed i_size in ram, and we haven't updated
505 * the disk i_size. There is no need to log the inode
509 i_size_write(&inode->vfs_inode, end_pos);
514 * this drops all the extents in the cache that intersect the range
515 * [start, end]. Existing extents are split as required.
517 void btrfs_drop_extent_cache(struct btrfs_inode *inode, u64 start, u64 end,
520 struct extent_map *em;
521 struct extent_map *split = NULL;
522 struct extent_map *split2 = NULL;
523 struct extent_map_tree *em_tree = &inode->extent_tree;
524 u64 len = end - start + 1;
532 WARN_ON(end < start);
533 if (end == (u64)-1) {
542 split = alloc_extent_map();
544 split2 = alloc_extent_map();
545 if (!split || !split2)
548 write_lock(&em_tree->lock);
549 em = lookup_extent_mapping(em_tree, start, len);
551 write_unlock(&em_tree->lock);
555 gen = em->generation;
556 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
557 if (testend && em->start + em->len >= start + len) {
559 write_unlock(&em_tree->lock);
562 start = em->start + em->len;
564 len = start + len - (em->start + em->len);
566 write_unlock(&em_tree->lock);
569 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
570 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
571 clear_bit(EXTENT_FLAG_LOGGING, &flags);
572 modified = !list_empty(&em->list);
576 if (em->start < start) {
577 split->start = em->start;
578 split->len = start - em->start;
580 if (em->block_start < EXTENT_MAP_LAST_BYTE) {
581 split->orig_start = em->orig_start;
582 split->block_start = em->block_start;
585 split->block_len = em->block_len;
587 split->block_len = split->len;
588 split->orig_block_len = max(split->block_len,
590 split->ram_bytes = em->ram_bytes;
592 split->orig_start = split->start;
593 split->block_len = 0;
594 split->block_start = em->block_start;
595 split->orig_block_len = 0;
596 split->ram_bytes = split->len;
599 split->generation = gen;
600 split->flags = flags;
601 split->compress_type = em->compress_type;
602 replace_extent_mapping(em_tree, em, split, modified);
603 free_extent_map(split);
607 if (testend && em->start + em->len > start + len) {
608 u64 diff = start + len - em->start;
610 split->start = start + len;
611 split->len = em->start + em->len - (start + len);
612 split->flags = flags;
613 split->compress_type = em->compress_type;
614 split->generation = gen;
616 if (em->block_start < EXTENT_MAP_LAST_BYTE) {
617 split->orig_block_len = max(em->block_len,
620 split->ram_bytes = em->ram_bytes;
622 split->block_len = em->block_len;
623 split->block_start = em->block_start;
624 split->orig_start = em->orig_start;
626 split->block_len = split->len;
627 split->block_start = em->block_start
629 split->orig_start = em->orig_start;
632 split->ram_bytes = split->len;
633 split->orig_start = split->start;
634 split->block_len = 0;
635 split->block_start = em->block_start;
636 split->orig_block_len = 0;
639 if (extent_map_in_tree(em)) {
640 replace_extent_mapping(em_tree, em, split,
643 ret = add_extent_mapping(em_tree, split,
645 ASSERT(ret == 0); /* Logic error */
647 free_extent_map(split);
651 if (extent_map_in_tree(em))
652 remove_extent_mapping(em_tree, em);
653 write_unlock(&em_tree->lock);
657 /* once for the tree*/
661 free_extent_map(split);
663 free_extent_map(split2);
667 * this is very complex, but the basic idea is to drop all extents
668 * in the range start - end. hint_block is filled in with a block number
669 * that would be a good hint to the block allocator for this file.
671 * If an extent intersects the range but is not entirely inside the range
672 * it is either truncated or split. Anything entirely inside the range
673 * is deleted from the tree.
675 int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
676 struct btrfs_root *root, struct btrfs_inode *inode,
677 struct btrfs_path *path, u64 start, u64 end,
678 u64 *drop_end, int drop_cache,
680 u32 extent_item_size,
683 struct btrfs_fs_info *fs_info = root->fs_info;
684 struct extent_buffer *leaf;
685 struct btrfs_file_extent_item *fi;
686 struct btrfs_ref ref = { 0 };
687 struct btrfs_key key;
688 struct btrfs_key new_key;
689 struct inode *vfs_inode = &inode->vfs_inode;
690 u64 ino = btrfs_ino(inode);
691 u64 search_start = start;
694 u64 extent_offset = 0;
696 u64 last_end = start;
702 int modify_tree = -1;
705 int leafs_visited = 0;
708 btrfs_drop_extent_cache(inode, start, end - 1, 0);
710 if (start >= inode->disk_i_size && !replace_extent)
713 update_refs = (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
714 root == fs_info->tree_root);
717 ret = btrfs_lookup_file_extent(trans, root, path, ino,
718 search_start, modify_tree);
721 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
722 leaf = path->nodes[0];
723 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
724 if (key.objectid == ino &&
725 key.type == BTRFS_EXTENT_DATA_KEY)
731 leaf = path->nodes[0];
732 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
734 ret = btrfs_next_leaf(root, path);
742 leaf = path->nodes[0];
746 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
748 if (key.objectid > ino)
750 if (WARN_ON_ONCE(key.objectid < ino) ||
751 key.type < BTRFS_EXTENT_DATA_KEY) {
756 if (key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
759 fi = btrfs_item_ptr(leaf, path->slots[0],
760 struct btrfs_file_extent_item);
761 extent_type = btrfs_file_extent_type(leaf, fi);
763 if (extent_type == BTRFS_FILE_EXTENT_REG ||
764 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
765 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
766 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
767 extent_offset = btrfs_file_extent_offset(leaf, fi);
768 extent_end = key.offset +
769 btrfs_file_extent_num_bytes(leaf, fi);
770 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
771 extent_end = key.offset +
772 btrfs_file_extent_ram_bytes(leaf, fi);
779 * Don't skip extent items representing 0 byte lengths. They
780 * used to be created (bug) if while punching holes we hit
781 * -ENOSPC condition. So if we find one here, just ensure we
782 * delete it, otherwise we would insert a new file extent item
783 * with the same key (offset) as that 0 bytes length file
784 * extent item in the call to setup_items_for_insert() later
787 if (extent_end == key.offset && extent_end >= search_start) {
788 last_end = extent_end;
789 goto delete_extent_item;
792 if (extent_end <= search_start) {
798 search_start = max(key.offset, start);
799 if (recow || !modify_tree) {
801 btrfs_release_path(path);
806 * | - range to drop - |
807 * | -------- extent -------- |
809 if (start > key.offset && end < extent_end) {
811 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
816 memcpy(&new_key, &key, sizeof(new_key));
817 new_key.offset = start;
818 ret = btrfs_duplicate_item(trans, root, path,
820 if (ret == -EAGAIN) {
821 btrfs_release_path(path);
827 leaf = path->nodes[0];
828 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
829 struct btrfs_file_extent_item);
830 btrfs_set_file_extent_num_bytes(leaf, fi,
833 fi = btrfs_item_ptr(leaf, path->slots[0],
834 struct btrfs_file_extent_item);
836 extent_offset += start - key.offset;
837 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
838 btrfs_set_file_extent_num_bytes(leaf, fi,
840 btrfs_mark_buffer_dirty(leaf);
842 if (update_refs && disk_bytenr > 0) {
843 btrfs_init_generic_ref(&ref,
844 BTRFS_ADD_DELAYED_REF,
845 disk_bytenr, num_bytes, 0);
846 btrfs_init_data_ref(&ref,
847 root->root_key.objectid,
849 start - extent_offset);
850 ret = btrfs_inc_extent_ref(trans, &ref);
851 BUG_ON(ret); /* -ENOMEM */
856 * From here on out we will have actually dropped something, so
857 * last_end can be updated.
859 last_end = extent_end;
862 * | ---- range to drop ----- |
863 * | -------- extent -------- |
865 if (start <= key.offset && end < extent_end) {
866 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
871 memcpy(&new_key, &key, sizeof(new_key));
872 new_key.offset = end;
873 btrfs_set_item_key_safe(fs_info, path, &new_key);
875 extent_offset += end - key.offset;
876 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
877 btrfs_set_file_extent_num_bytes(leaf, fi,
879 btrfs_mark_buffer_dirty(leaf);
880 if (update_refs && disk_bytenr > 0)
881 inode_sub_bytes(vfs_inode, end - key.offset);
885 search_start = extent_end;
887 * | ---- range to drop ----- |
888 * | -------- extent -------- |
890 if (start > key.offset && end >= extent_end) {
892 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
897 btrfs_set_file_extent_num_bytes(leaf, fi,
899 btrfs_mark_buffer_dirty(leaf);
900 if (update_refs && disk_bytenr > 0)
901 inode_sub_bytes(vfs_inode, extent_end - start);
902 if (end == extent_end)
910 * | ---- range to drop ----- |
911 * | ------ extent ------ |
913 if (start <= key.offset && end >= extent_end) {
916 del_slot = path->slots[0];
919 BUG_ON(del_slot + del_nr != path->slots[0]);
924 extent_type == BTRFS_FILE_EXTENT_INLINE) {
925 inode_sub_bytes(vfs_inode,
926 extent_end - key.offset);
927 extent_end = ALIGN(extent_end,
928 fs_info->sectorsize);
929 } else if (update_refs && disk_bytenr > 0) {
930 btrfs_init_generic_ref(&ref,
931 BTRFS_DROP_DELAYED_REF,
932 disk_bytenr, num_bytes, 0);
933 btrfs_init_data_ref(&ref,
934 root->root_key.objectid,
936 key.offset - extent_offset);
937 ret = btrfs_free_extent(trans, &ref);
938 BUG_ON(ret); /* -ENOMEM */
939 inode_sub_bytes(vfs_inode,
940 extent_end - key.offset);
943 if (end == extent_end)
946 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
951 ret = btrfs_del_items(trans, root, path, del_slot,
954 btrfs_abort_transaction(trans, ret);
961 btrfs_release_path(path);
968 if (!ret && del_nr > 0) {
970 * Set path->slots[0] to first slot, so that after the delete
971 * if items are move off from our leaf to its immediate left or
972 * right neighbor leafs, we end up with a correct and adjusted
973 * path->slots[0] for our insertion (if replace_extent != 0).
975 path->slots[0] = del_slot;
976 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
978 btrfs_abort_transaction(trans, ret);
981 leaf = path->nodes[0];
983 * If btrfs_del_items() was called, it might have deleted a leaf, in
984 * which case it unlocked our path, so check path->locks[0] matches a
987 if (!ret && replace_extent && leafs_visited == 1 &&
988 (path->locks[0] == BTRFS_WRITE_LOCK_BLOCKING ||
989 path->locks[0] == BTRFS_WRITE_LOCK) &&
990 btrfs_leaf_free_space(leaf) >=
991 sizeof(struct btrfs_item) + extent_item_size) {
994 key.type = BTRFS_EXTENT_DATA_KEY;
996 if (!del_nr && path->slots[0] < btrfs_header_nritems(leaf)) {
997 struct btrfs_key slot_key;
999 btrfs_item_key_to_cpu(leaf, &slot_key, path->slots[0]);
1000 if (btrfs_comp_cpu_keys(&key, &slot_key) > 0)
1003 setup_items_for_insert(root, path, &key, &extent_item_size, 1);
1007 if (!replace_extent || !(*key_inserted))
1008 btrfs_release_path(path);
1010 *drop_end = found ? min(end, last_end) : end;
1014 int btrfs_drop_extents(struct btrfs_trans_handle *trans,
1015 struct btrfs_root *root, struct inode *inode, u64 start,
1016 u64 end, int drop_cache)
1018 struct btrfs_path *path;
1021 path = btrfs_alloc_path();
1024 ret = __btrfs_drop_extents(trans, root, BTRFS_I(inode), path, start,
1025 end, NULL, drop_cache, 0, 0, NULL);
1026 btrfs_free_path(path);
1030 static int extent_mergeable(struct extent_buffer *leaf, int slot,
1031 u64 objectid, u64 bytenr, u64 orig_offset,
1032 u64 *start, u64 *end)
1034 struct btrfs_file_extent_item *fi;
1035 struct btrfs_key key;
1038 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
1041 btrfs_item_key_to_cpu(leaf, &key, slot);
1042 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
1045 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
1046 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
1047 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
1048 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
1049 btrfs_file_extent_compression(leaf, fi) ||
1050 btrfs_file_extent_encryption(leaf, fi) ||
1051 btrfs_file_extent_other_encoding(leaf, fi))
1054 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
1055 if ((*start && *start != key.offset) || (*end && *end != extent_end))
1058 *start = key.offset;
1064 * Mark extent in the range start - end as written.
1066 * This changes extent type from 'pre-allocated' to 'regular'. If only
1067 * part of extent is marked as written, the extent will be split into
1070 int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
1071 struct btrfs_inode *inode, u64 start, u64 end)
1073 struct btrfs_fs_info *fs_info = trans->fs_info;
1074 struct btrfs_root *root = inode->root;
1075 struct extent_buffer *leaf;
1076 struct btrfs_path *path;
1077 struct btrfs_file_extent_item *fi;
1078 struct btrfs_ref ref = { 0 };
1079 struct btrfs_key key;
1080 struct btrfs_key new_key;
1092 u64 ino = btrfs_ino(inode);
1094 path = btrfs_alloc_path();
1101 key.type = BTRFS_EXTENT_DATA_KEY;
1104 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1107 if (ret > 0 && path->slots[0] > 0)
1110 leaf = path->nodes[0];
1111 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1112 if (key.objectid != ino ||
1113 key.type != BTRFS_EXTENT_DATA_KEY) {
1115 btrfs_abort_transaction(trans, ret);
1118 fi = btrfs_item_ptr(leaf, path->slots[0],
1119 struct btrfs_file_extent_item);
1120 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_PREALLOC) {
1122 btrfs_abort_transaction(trans, ret);
1125 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
1126 if (key.offset > start || extent_end < end) {
1128 btrfs_abort_transaction(trans, ret);
1132 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1133 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1134 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
1135 memcpy(&new_key, &key, sizeof(new_key));
1137 if (start == key.offset && end < extent_end) {
1140 if (extent_mergeable(leaf, path->slots[0] - 1,
1141 ino, bytenr, orig_offset,
1142 &other_start, &other_end)) {
1143 new_key.offset = end;
1144 btrfs_set_item_key_safe(fs_info, path, &new_key);
1145 fi = btrfs_item_ptr(leaf, path->slots[0],
1146 struct btrfs_file_extent_item);
1147 btrfs_set_file_extent_generation(leaf, fi,
1149 btrfs_set_file_extent_num_bytes(leaf, fi,
1151 btrfs_set_file_extent_offset(leaf, fi,
1153 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
1154 struct btrfs_file_extent_item);
1155 btrfs_set_file_extent_generation(leaf, fi,
1157 btrfs_set_file_extent_num_bytes(leaf, fi,
1159 btrfs_mark_buffer_dirty(leaf);
1164 if (start > key.offset && end == extent_end) {
1167 if (extent_mergeable(leaf, path->slots[0] + 1,
1168 ino, bytenr, orig_offset,
1169 &other_start, &other_end)) {
1170 fi = btrfs_item_ptr(leaf, path->slots[0],
1171 struct btrfs_file_extent_item);
1172 btrfs_set_file_extent_num_bytes(leaf, fi,
1173 start - key.offset);
1174 btrfs_set_file_extent_generation(leaf, fi,
1177 new_key.offset = start;
1178 btrfs_set_item_key_safe(fs_info, path, &new_key);
1180 fi = btrfs_item_ptr(leaf, path->slots[0],
1181 struct btrfs_file_extent_item);
1182 btrfs_set_file_extent_generation(leaf, fi,
1184 btrfs_set_file_extent_num_bytes(leaf, fi,
1186 btrfs_set_file_extent_offset(leaf, fi,
1187 start - orig_offset);
1188 btrfs_mark_buffer_dirty(leaf);
1193 while (start > key.offset || end < extent_end) {
1194 if (key.offset == start)
1197 new_key.offset = split;
1198 ret = btrfs_duplicate_item(trans, root, path, &new_key);
1199 if (ret == -EAGAIN) {
1200 btrfs_release_path(path);
1204 btrfs_abort_transaction(trans, ret);
1208 leaf = path->nodes[0];
1209 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
1210 struct btrfs_file_extent_item);
1211 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1212 btrfs_set_file_extent_num_bytes(leaf, fi,
1213 split - key.offset);
1215 fi = btrfs_item_ptr(leaf, path->slots[0],
1216 struct btrfs_file_extent_item);
1218 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1219 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
1220 btrfs_set_file_extent_num_bytes(leaf, fi,
1221 extent_end - split);
1222 btrfs_mark_buffer_dirty(leaf);
1224 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, bytenr,
1226 btrfs_init_data_ref(&ref, root->root_key.objectid, ino,
1228 ret = btrfs_inc_extent_ref(trans, &ref);
1230 btrfs_abort_transaction(trans, ret);
1234 if (split == start) {
1237 if (start != key.offset) {
1239 btrfs_abort_transaction(trans, ret);
1250 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
1252 btrfs_init_data_ref(&ref, root->root_key.objectid, ino, orig_offset);
1253 if (extent_mergeable(leaf, path->slots[0] + 1,
1254 ino, bytenr, orig_offset,
1255 &other_start, &other_end)) {
1257 btrfs_release_path(path);
1260 extent_end = other_end;
1261 del_slot = path->slots[0] + 1;
1263 ret = btrfs_free_extent(trans, &ref);
1265 btrfs_abort_transaction(trans, ret);
1271 if (extent_mergeable(leaf, path->slots[0] - 1,
1272 ino, bytenr, orig_offset,
1273 &other_start, &other_end)) {
1275 btrfs_release_path(path);
1278 key.offset = other_start;
1279 del_slot = path->slots[0];
1281 ret = btrfs_free_extent(trans, &ref);
1283 btrfs_abort_transaction(trans, ret);
1288 fi = btrfs_item_ptr(leaf, path->slots[0],
1289 struct btrfs_file_extent_item);
1290 btrfs_set_file_extent_type(leaf, fi,
1291 BTRFS_FILE_EXTENT_REG);
1292 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1293 btrfs_mark_buffer_dirty(leaf);
1295 fi = btrfs_item_ptr(leaf, del_slot - 1,
1296 struct btrfs_file_extent_item);
1297 btrfs_set_file_extent_type(leaf, fi,
1298 BTRFS_FILE_EXTENT_REG);
1299 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1300 btrfs_set_file_extent_num_bytes(leaf, fi,
1301 extent_end - key.offset);
1302 btrfs_mark_buffer_dirty(leaf);
1304 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
1306 btrfs_abort_transaction(trans, ret);
1311 btrfs_free_path(path);
1316 * on error we return an unlocked page and the error value
1317 * on success we return a locked page and 0
1319 static int prepare_uptodate_page(struct inode *inode,
1320 struct page *page, u64 pos,
1321 bool force_uptodate)
1325 if (((pos & (PAGE_SIZE - 1)) || force_uptodate) &&
1326 !PageUptodate(page)) {
1327 ret = btrfs_readpage(NULL, page);
1331 if (!PageUptodate(page)) {
1335 if (page->mapping != inode->i_mapping) {
1344 * this just gets pages into the page cache and locks them down.
1346 static noinline int prepare_pages(struct inode *inode, struct page **pages,
1347 size_t num_pages, loff_t pos,
1348 size_t write_bytes, bool force_uptodate)
1351 unsigned long index = pos >> PAGE_SHIFT;
1352 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1356 for (i = 0; i < num_pages; i++) {
1358 pages[i] = find_or_create_page(inode->i_mapping, index + i,
1359 mask | __GFP_WRITE);
1367 err = prepare_uptodate_page(inode, pages[i], pos,
1369 if (!err && i == num_pages - 1)
1370 err = prepare_uptodate_page(inode, pages[i],
1371 pos + write_bytes, false);
1374 if (err == -EAGAIN) {
1381 wait_on_page_writeback(pages[i]);
1386 while (faili >= 0) {
1387 unlock_page(pages[faili]);
1388 put_page(pages[faili]);
1396 * This function locks the extent and properly waits for data=ordered extents
1397 * to finish before allowing the pages to be modified if need.
1400 * 1 - the extent is locked
1401 * 0 - the extent is not locked, and everything is OK
1402 * -EAGAIN - need re-prepare the pages
1403 * the other < 0 number - Something wrong happens
1406 lock_and_cleanup_extent_if_need(struct btrfs_inode *inode, struct page **pages,
1407 size_t num_pages, loff_t pos,
1409 u64 *lockstart, u64 *lockend,
1410 struct extent_state **cached_state)
1412 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1418 start_pos = round_down(pos, fs_info->sectorsize);
1419 last_pos = round_up(pos + write_bytes, fs_info->sectorsize) - 1;
1421 if (start_pos < inode->vfs_inode.i_size) {
1422 struct btrfs_ordered_extent *ordered;
1424 lock_extent_bits(&inode->io_tree, start_pos, last_pos,
1426 ordered = btrfs_lookup_ordered_range(inode, start_pos,
1427 last_pos - start_pos + 1);
1429 ordered->file_offset + ordered->num_bytes > start_pos &&
1430 ordered->file_offset <= last_pos) {
1431 unlock_extent_cached(&inode->io_tree, start_pos,
1432 last_pos, cached_state);
1433 for (i = 0; i < num_pages; i++) {
1434 unlock_page(pages[i]);
1437 btrfs_start_ordered_extent(ordered, 1);
1438 btrfs_put_ordered_extent(ordered);
1442 btrfs_put_ordered_extent(ordered);
1444 *lockstart = start_pos;
1445 *lockend = last_pos;
1450 * It's possible the pages are dirty right now, but we don't want
1451 * to clean them yet because copy_from_user may catch a page fault
1452 * and we might have to fall back to one page at a time. If that
1453 * happens, we'll unlock these pages and we'd have a window where
1454 * reclaim could sneak in and drop the once-dirty page on the floor
1455 * without writing it.
1457 * We have the pages locked and the extent range locked, so there's
1458 * no way someone can start IO on any dirty pages in this range.
1460 * We'll call btrfs_dirty_pages() later on, and that will flip around
1461 * delalloc bits and dirty the pages as required.
1463 for (i = 0; i < num_pages; i++) {
1464 set_page_extent_mapped(pages[i]);
1465 WARN_ON(!PageLocked(pages[i]));
1471 static int check_can_nocow(struct btrfs_inode *inode, loff_t pos,
1472 size_t *write_bytes, bool nowait)
1474 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1475 struct btrfs_root *root = inode->root;
1476 u64 lockstart, lockend;
1480 if (!(inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)))
1483 if (!nowait && !btrfs_drew_try_write_lock(&root->snapshot_lock))
1486 lockstart = round_down(pos, fs_info->sectorsize);
1487 lockend = round_up(pos + *write_bytes,
1488 fs_info->sectorsize) - 1;
1489 num_bytes = lockend - lockstart + 1;
1492 struct btrfs_ordered_extent *ordered;
1494 if (!try_lock_extent(&inode->io_tree, lockstart, lockend))
1497 ordered = btrfs_lookup_ordered_range(inode, lockstart,
1500 btrfs_put_ordered_extent(ordered);
1505 btrfs_lock_and_flush_ordered_range(inode, lockstart,
1509 ret = can_nocow_extent(&inode->vfs_inode, lockstart, &num_bytes,
1510 NULL, NULL, NULL, false);
1514 btrfs_drew_write_unlock(&root->snapshot_lock);
1516 *write_bytes = min_t(size_t, *write_bytes ,
1517 num_bytes - pos + lockstart);
1520 unlock_extent(&inode->io_tree, lockstart, lockend);
1525 static int check_nocow_nolock(struct btrfs_inode *inode, loff_t pos,
1526 size_t *write_bytes)
1528 return check_can_nocow(inode, pos, write_bytes, true);
1532 * Check if we can do nocow write into the range [@pos, @pos + @write_bytes)
1535 * @write_bytes: The length to write, will be updated to the nocow writeable
1538 * This function will flush ordered extents in the range to ensure proper
1542 * >0 and update @write_bytes if we can do nocow write
1543 * 0 if we can't do nocow write
1544 * -EAGAIN if we can't get the needed lock or there are ordered extents
1545 * for * (nowait == true) case
1546 * <0 if other error happened
1548 * NOTE: Callers need to release the lock by btrfs_check_nocow_unlock().
1550 int btrfs_check_nocow_lock(struct btrfs_inode *inode, loff_t pos,
1551 size_t *write_bytes)
1553 return check_can_nocow(inode, pos, write_bytes, false);
1556 void btrfs_check_nocow_unlock(struct btrfs_inode *inode)
1558 btrfs_drew_write_unlock(&inode->root->snapshot_lock);
1561 static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb,
1564 struct file *file = iocb->ki_filp;
1565 loff_t pos = iocb->ki_pos;
1566 struct inode *inode = file_inode(file);
1567 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1568 struct page **pages = NULL;
1569 struct extent_changeset *data_reserved = NULL;
1570 u64 release_bytes = 0;
1573 size_t num_written = 0;
1576 bool only_release_metadata = false;
1577 bool force_page_uptodate = false;
1579 nrptrs = min(DIV_ROUND_UP(iov_iter_count(i), PAGE_SIZE),
1580 PAGE_SIZE / (sizeof(struct page *)));
1581 nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
1582 nrptrs = max(nrptrs, 8);
1583 pages = kmalloc_array(nrptrs, sizeof(struct page *), GFP_KERNEL);
1587 while (iov_iter_count(i) > 0) {
1588 struct extent_state *cached_state = NULL;
1589 size_t offset = offset_in_page(pos);
1590 size_t sector_offset;
1591 size_t write_bytes = min(iov_iter_count(i),
1592 nrptrs * (size_t)PAGE_SIZE -
1594 size_t num_pages = DIV_ROUND_UP(write_bytes + offset,
1596 size_t reserve_bytes;
1599 size_t dirty_sectors;
1603 WARN_ON(num_pages > nrptrs);
1606 * Fault pages before locking them in prepare_pages
1607 * to avoid recursive lock
1609 if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
1614 only_release_metadata = false;
1615 sector_offset = pos & (fs_info->sectorsize - 1);
1616 reserve_bytes = round_up(write_bytes + sector_offset,
1617 fs_info->sectorsize);
1619 extent_changeset_release(data_reserved);
1620 ret = btrfs_check_data_free_space(BTRFS_I(inode),
1621 &data_reserved, pos,
1624 if (btrfs_check_nocow_lock(BTRFS_I(inode), pos,
1625 &write_bytes) > 0) {
1627 * For nodata cow case, no need to reserve
1630 only_release_metadata = true;
1632 * our prealloc extent may be smaller than
1633 * write_bytes, so scale down.
1635 num_pages = DIV_ROUND_UP(write_bytes + offset,
1637 reserve_bytes = round_up(write_bytes +
1639 fs_info->sectorsize);
1645 WARN_ON(reserve_bytes == 0);
1646 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode),
1649 if (!only_release_metadata)
1650 btrfs_free_reserved_data_space(BTRFS_I(inode),
1654 btrfs_check_nocow_unlock(BTRFS_I(inode));
1658 release_bytes = reserve_bytes;
1661 * This is going to setup the pages array with the number of
1662 * pages we want, so we don't really need to worry about the
1663 * contents of pages from loop to loop
1665 ret = prepare_pages(inode, pages, num_pages,
1667 force_page_uptodate);
1669 btrfs_delalloc_release_extents(BTRFS_I(inode),
1674 extents_locked = lock_and_cleanup_extent_if_need(
1675 BTRFS_I(inode), pages,
1676 num_pages, pos, write_bytes, &lockstart,
1677 &lockend, &cached_state);
1678 if (extents_locked < 0) {
1679 if (extents_locked == -EAGAIN)
1681 btrfs_delalloc_release_extents(BTRFS_I(inode),
1683 ret = extents_locked;
1687 copied = btrfs_copy_from_user(pos, write_bytes, pages, i);
1689 num_sectors = BTRFS_BYTES_TO_BLKS(fs_info, reserve_bytes);
1690 dirty_sectors = round_up(copied + sector_offset,
1691 fs_info->sectorsize);
1692 dirty_sectors = BTRFS_BYTES_TO_BLKS(fs_info, dirty_sectors);
1695 * if we have trouble faulting in the pages, fall
1696 * back to one page at a time
1698 if (copied < write_bytes)
1702 force_page_uptodate = true;
1706 force_page_uptodate = false;
1707 dirty_pages = DIV_ROUND_UP(copied + offset,
1711 if (num_sectors > dirty_sectors) {
1712 /* release everything except the sectors we dirtied */
1713 release_bytes -= dirty_sectors <<
1714 fs_info->sb->s_blocksize_bits;
1715 if (only_release_metadata) {
1716 btrfs_delalloc_release_metadata(BTRFS_I(inode),
1717 release_bytes, true);
1721 __pos = round_down(pos,
1722 fs_info->sectorsize) +
1723 (dirty_pages << PAGE_SHIFT);
1724 btrfs_delalloc_release_space(BTRFS_I(inode),
1725 data_reserved, __pos,
1726 release_bytes, true);
1730 release_bytes = round_up(copied + sector_offset,
1731 fs_info->sectorsize);
1734 ret = btrfs_dirty_pages(BTRFS_I(inode), pages,
1735 dirty_pages, pos, copied,
1739 * If we have not locked the extent range, because the range's
1740 * start offset is >= i_size, we might still have a non-NULL
1741 * cached extent state, acquired while marking the extent range
1742 * as delalloc through btrfs_dirty_pages(). Therefore free any
1743 * possible cached extent state to avoid a memory leak.
1746 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1747 lockstart, lockend, &cached_state);
1749 free_extent_state(cached_state);
1751 btrfs_delalloc_release_extents(BTRFS_I(inode), reserve_bytes);
1753 btrfs_drop_pages(pages, num_pages);
1758 if (only_release_metadata)
1759 btrfs_check_nocow_unlock(BTRFS_I(inode));
1761 if (only_release_metadata && copied > 0) {
1762 lockstart = round_down(pos,
1763 fs_info->sectorsize);
1764 lockend = round_up(pos + copied,
1765 fs_info->sectorsize) - 1;
1767 set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
1768 lockend, EXTENT_NORESERVE, NULL,
1772 btrfs_drop_pages(pages, num_pages);
1776 balance_dirty_pages_ratelimited(inode->i_mapping);
1779 num_written += copied;
1784 if (release_bytes) {
1785 if (only_release_metadata) {
1786 btrfs_check_nocow_unlock(BTRFS_I(inode));
1787 btrfs_delalloc_release_metadata(BTRFS_I(inode),
1788 release_bytes, true);
1790 btrfs_delalloc_release_space(BTRFS_I(inode),
1792 round_down(pos, fs_info->sectorsize),
1793 release_bytes, true);
1797 extent_changeset_free(data_reserved);
1798 return num_written ? num_written : ret;
1801 static ssize_t __btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from)
1803 struct file *file = iocb->ki_filp;
1804 struct inode *inode = file_inode(file);
1807 ssize_t written_buffered;
1811 written = btrfs_direct_IO(iocb, from);
1813 if (written < 0 || !iov_iter_count(from))
1817 written_buffered = btrfs_buffered_write(iocb, from);
1818 if (written_buffered < 0) {
1819 err = written_buffered;
1823 * Ensure all data is persisted. We want the next direct IO read to be
1824 * able to read what was just written.
1826 endbyte = pos + written_buffered - 1;
1827 err = btrfs_fdatawrite_range(inode, pos, endbyte);
1830 err = filemap_fdatawait_range(inode->i_mapping, pos, endbyte);
1833 written += written_buffered;
1834 iocb->ki_pos = pos + written_buffered;
1835 invalidate_mapping_pages(file->f_mapping, pos >> PAGE_SHIFT,
1836 endbyte >> PAGE_SHIFT);
1838 return written ? written : err;
1841 static void update_time_for_write(struct inode *inode)
1843 struct timespec64 now;
1845 if (IS_NOCMTIME(inode))
1848 now = current_time(inode);
1849 if (!timespec64_equal(&inode->i_mtime, &now))
1850 inode->i_mtime = now;
1852 if (!timespec64_equal(&inode->i_ctime, &now))
1853 inode->i_ctime = now;
1855 if (IS_I_VERSION(inode))
1856 inode_inc_iversion(inode);
1859 static ssize_t btrfs_file_write_iter(struct kiocb *iocb,
1860 struct iov_iter *from)
1862 struct file *file = iocb->ki_filp;
1863 struct inode *inode = file_inode(file);
1864 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1865 struct btrfs_root *root = BTRFS_I(inode)->root;
1868 ssize_t num_written = 0;
1869 const bool sync = iocb->ki_flags & IOCB_DSYNC;
1876 if (!(iocb->ki_flags & IOCB_DIRECT) &&
1877 (iocb->ki_flags & IOCB_NOWAIT))
1880 if (iocb->ki_flags & IOCB_NOWAIT) {
1881 if (!inode_trylock(inode))
1887 err = generic_write_checks(iocb, from);
1889 inode_unlock(inode);
1894 count = iov_iter_count(from);
1895 if (iocb->ki_flags & IOCB_NOWAIT) {
1896 size_t nocow_bytes = count;
1899 * We will allocate space in case nodatacow is not set,
1902 if (check_nocow_nolock(BTRFS_I(inode), pos, &nocow_bytes)
1904 inode_unlock(inode);
1908 * There are holes in the range or parts of the range that must
1909 * be COWed (shared extents, RO block groups, etc), so just bail
1912 if (nocow_bytes < count) {
1913 inode_unlock(inode);
1918 current->backing_dev_info = inode_to_bdi(inode);
1919 err = file_remove_privs(file);
1921 inode_unlock(inode);
1926 * If BTRFS flips readonly due to some impossible error
1927 * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
1928 * although we have opened a file as writable, we have
1929 * to stop this write operation to ensure FS consistency.
1931 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
1932 inode_unlock(inode);
1938 * We reserve space for updating the inode when we reserve space for the
1939 * extent we are going to write, so we will enospc out there. We don't
1940 * need to start yet another transaction to update the inode as we will
1941 * update the inode when we finish writing whatever data we write.
1943 update_time_for_write(inode);
1945 start_pos = round_down(pos, fs_info->sectorsize);
1946 oldsize = i_size_read(inode);
1947 if (start_pos > oldsize) {
1948 /* Expand hole size to cover write data, preventing empty gap */
1949 end_pos = round_up(pos + count,
1950 fs_info->sectorsize);
1951 err = btrfs_cont_expand(inode, oldsize, end_pos);
1953 inode_unlock(inode);
1956 if (start_pos > round_up(oldsize, fs_info->sectorsize))
1961 atomic_inc(&BTRFS_I(inode)->sync_writers);
1963 if (iocb->ki_flags & IOCB_DIRECT) {
1965 * 1. We must always clear IOCB_DSYNC in order to not deadlock
1966 * in iomap, as it calls generic_write_sync() in this case.
1967 * 2. If we are async, we can call iomap_dio_complete() either
1970 * 2.1. A worker thread from the last bio completed. In this
1971 * case we need to mark the btrfs_dio_data that it is
1972 * async in order to call generic_write_sync() properly.
1973 * This is handled by setting BTRFS_DIO_SYNC_STUB in the
1974 * current->journal_info.
1975 * 2.2 The submitter context, because all IO completed
1976 * before we exited iomap_dio_rw(). In this case we can
1977 * just re-set the IOCB_DSYNC on the iocb and we'll do
1978 * the sync below. If our ->end_io() gets called and
1979 * current->journal_info is set, then we know we're in
1980 * our current context and we will clear
1981 * current->journal_info to indicate that we need to
1985 ASSERT(current->journal_info == NULL);
1986 iocb->ki_flags &= ~IOCB_DSYNC;
1987 current->journal_info = BTRFS_DIO_SYNC_STUB;
1989 num_written = __btrfs_direct_write(iocb, from);
1992 * As stated above, we cleared journal_info, so we need to do
1993 * the sync ourselves.
1995 if (sync && current->journal_info == NULL)
1996 iocb->ki_flags |= IOCB_DSYNC;
1997 current->journal_info = NULL;
1999 num_written = btrfs_buffered_write(iocb, from);
2000 if (num_written > 0)
2001 iocb->ki_pos = pos + num_written;
2003 pagecache_isize_extended(inode, oldsize,
2004 i_size_read(inode));
2007 inode_unlock(inode);
2010 * We also have to set last_sub_trans to the current log transid,
2011 * otherwise subsequent syncs to a file that's been synced in this
2012 * transaction will appear to have already occurred.
2014 spin_lock(&BTRFS_I(inode)->lock);
2015 BTRFS_I(inode)->last_sub_trans = root->log_transid;
2016 spin_unlock(&BTRFS_I(inode)->lock);
2017 if (num_written > 0)
2018 num_written = generic_write_sync(iocb, num_written);
2021 atomic_dec(&BTRFS_I(inode)->sync_writers);
2023 current->backing_dev_info = NULL;
2024 return num_written ? num_written : err;
2027 int btrfs_release_file(struct inode *inode, struct file *filp)
2029 struct btrfs_file_private *private = filp->private_data;
2031 if (private && private->filldir_buf)
2032 kfree(private->filldir_buf);
2034 filp->private_data = NULL;
2037 * Set by setattr when we are about to truncate a file from a non-zero
2038 * size to a zero size. This tries to flush down new bytes that may
2039 * have been written if the application were using truncate to replace
2042 if (test_and_clear_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
2043 &BTRFS_I(inode)->runtime_flags))
2044 filemap_flush(inode->i_mapping);
2048 static int start_ordered_ops(struct inode *inode, loff_t start, loff_t end)
2051 struct blk_plug plug;
2054 * This is only called in fsync, which would do synchronous writes, so
2055 * a plug can merge adjacent IOs as much as possible. Esp. in case of
2056 * multiple disks using raid profile, a large IO can be split to
2057 * several segments of stripe length (currently 64K).
2059 blk_start_plug(&plug);
2060 atomic_inc(&BTRFS_I(inode)->sync_writers);
2061 ret = btrfs_fdatawrite_range(inode, start, end);
2062 atomic_dec(&BTRFS_I(inode)->sync_writers);
2063 blk_finish_plug(&plug);
2069 * fsync call for both files and directories. This logs the inode into
2070 * the tree log instead of forcing full commits whenever possible.
2072 * It needs to call filemap_fdatawait so that all ordered extent updates are
2073 * in the metadata btree are up to date for copying to the log.
2075 * It drops the inode mutex before doing the tree log commit. This is an
2076 * important optimization for directories because holding the mutex prevents
2077 * new operations on the dir while we write to disk.
2079 int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
2081 struct dentry *dentry = file_dentry(file);
2082 struct inode *inode = d_inode(dentry);
2083 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2084 struct btrfs_root *root = BTRFS_I(inode)->root;
2085 struct btrfs_trans_handle *trans;
2086 struct btrfs_log_ctx ctx;
2091 trace_btrfs_sync_file(file, datasync);
2093 btrfs_init_log_ctx(&ctx, inode);
2096 * Always set the range to a full range, otherwise we can get into
2097 * several problems, from missing file extent items to represent holes
2098 * when not using the NO_HOLES feature, to log tree corruption due to
2099 * races between hole detection during logging and completion of ordered
2100 * extents outside the range, to missing checksums due to ordered extents
2101 * for which we flushed only a subset of their pages.
2105 len = (u64)LLONG_MAX + 1;
2108 * We write the dirty pages in the range and wait until they complete
2109 * out of the ->i_mutex. If so, we can flush the dirty pages by
2110 * multi-task, and make the performance up. See
2111 * btrfs_wait_ordered_range for an explanation of the ASYNC check.
2113 ret = start_ordered_ops(inode, start, end);
2120 * We take the dio_sem here because the tree log stuff can race with
2121 * lockless dio writes and get an extent map logged for an extent we
2122 * never waited on. We need it this high up for lockdep reasons.
2124 down_write(&BTRFS_I(inode)->dio_sem);
2126 atomic_inc(&root->log_batch);
2129 * Always check for the full sync flag while holding the inode's lock,
2130 * to avoid races with other tasks. The flag must be either set all the
2131 * time during logging or always off all the time while logging.
2133 full_sync = test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2134 &BTRFS_I(inode)->runtime_flags);
2137 * Before we acquired the inode's lock, someone may have dirtied more
2138 * pages in the target range. We need to make sure that writeback for
2139 * any such pages does not start while we are logging the inode, because
2140 * if it does, any of the following might happen when we are not doing a
2143 * 1) We log an extent after its writeback finishes but before its
2144 * checksums are added to the csum tree, leading to -EIO errors
2145 * when attempting to read the extent after a log replay.
2147 * 2) We can end up logging an extent before its writeback finishes.
2148 * Therefore after the log replay we will have a file extent item
2149 * pointing to an unwritten extent (and no data checksums as well).
2151 * So trigger writeback for any eventual new dirty pages and then we
2152 * wait for all ordered extents to complete below.
2154 ret = start_ordered_ops(inode, start, end);
2156 up_write(&BTRFS_I(inode)->dio_sem);
2157 inode_unlock(inode);
2162 * We have to do this here to avoid the priority inversion of waiting on
2163 * IO of a lower priority task while holding a transaction open.
2165 * For a full fsync we wait for the ordered extents to complete while
2166 * for a fast fsync we wait just for writeback to complete, and then
2167 * attach the ordered extents to the transaction so that a transaction
2168 * commit waits for their completion, to avoid data loss if we fsync,
2169 * the current transaction commits before the ordered extents complete
2170 * and a power failure happens right after that.
2173 ret = btrfs_wait_ordered_range(inode, start, len);
2176 * Get our ordered extents as soon as possible to avoid doing
2177 * checksum lookups in the csum tree, and use instead the
2178 * checksums attached to the ordered extents.
2180 btrfs_get_ordered_extents_for_logging(BTRFS_I(inode),
2181 &ctx.ordered_extents);
2182 ret = filemap_fdatawait_range(inode->i_mapping, start, end);
2186 goto out_release_extents;
2188 atomic_inc(&root->log_batch);
2191 * If we are doing a fast fsync we can not bail out if the inode's
2192 * last_trans is <= then the last committed transaction, because we only
2193 * update the last_trans of the inode during ordered extent completion,
2194 * and for a fast fsync we don't wait for that, we only wait for the
2195 * writeback to complete.
2198 if (btrfs_inode_in_log(BTRFS_I(inode), fs_info->generation) ||
2199 (BTRFS_I(inode)->last_trans <= fs_info->last_trans_committed &&
2200 (full_sync || list_empty(&ctx.ordered_extents)))) {
2202 * We've had everything committed since the last time we were
2203 * modified so clear this flag in case it was set for whatever
2204 * reason, it's no longer relevant.
2206 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2207 &BTRFS_I(inode)->runtime_flags);
2209 * An ordered extent might have started before and completed
2210 * already with io errors, in which case the inode was not
2211 * updated and we end up here. So check the inode's mapping
2212 * for any errors that might have happened since we last
2213 * checked called fsync.
2215 ret = filemap_check_wb_err(inode->i_mapping, file->f_wb_err);
2216 goto out_release_extents;
2220 * We use start here because we will need to wait on the IO to complete
2221 * in btrfs_sync_log, which could require joining a transaction (for
2222 * example checking cross references in the nocow path). If we use join
2223 * here we could get into a situation where we're waiting on IO to
2224 * happen that is blocked on a transaction trying to commit. With start
2225 * we inc the extwriter counter, so we wait for all extwriters to exit
2226 * before we start blocking joiners. This comment is to keep somebody
2227 * from thinking they are super smart and changing this to
2228 * btrfs_join_transaction *cough*Josef*cough*.
2230 trans = btrfs_start_transaction(root, 0);
2231 if (IS_ERR(trans)) {
2232 ret = PTR_ERR(trans);
2233 goto out_release_extents;
2236 ret = btrfs_log_dentry_safe(trans, dentry, &ctx);
2237 btrfs_release_log_ctx_extents(&ctx);
2239 /* Fallthrough and commit/free transaction. */
2243 /* we've logged all the items and now have a consistent
2244 * version of the file in the log. It is possible that
2245 * someone will come in and modify the file, but that's
2246 * fine because the log is consistent on disk, and we
2247 * have references to all of the file's extents
2249 * It is possible that someone will come in and log the
2250 * file again, but that will end up using the synchronization
2251 * inside btrfs_sync_log to keep things safe.
2253 up_write(&BTRFS_I(inode)->dio_sem);
2254 inode_unlock(inode);
2256 if (ret != BTRFS_NO_LOG_SYNC) {
2258 ret = btrfs_sync_log(trans, root, &ctx);
2260 ret = btrfs_end_transaction(trans);
2265 ret = btrfs_wait_ordered_range(inode, start, len);
2267 btrfs_end_transaction(trans);
2271 ret = btrfs_commit_transaction(trans);
2273 ret = btrfs_end_transaction(trans);
2276 ASSERT(list_empty(&ctx.list));
2277 err = file_check_and_advance_wb_err(file);
2280 return ret > 0 ? -EIO : ret;
2282 out_release_extents:
2283 btrfs_release_log_ctx_extents(&ctx);
2284 up_write(&BTRFS_I(inode)->dio_sem);
2285 inode_unlock(inode);
2289 static const struct vm_operations_struct btrfs_file_vm_ops = {
2290 .fault = filemap_fault,
2291 .map_pages = filemap_map_pages,
2292 .page_mkwrite = btrfs_page_mkwrite,
2295 static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
2297 struct address_space *mapping = filp->f_mapping;
2299 if (!mapping->a_ops->readpage)
2302 file_accessed(filp);
2303 vma->vm_ops = &btrfs_file_vm_ops;
2308 static int hole_mergeable(struct btrfs_inode *inode, struct extent_buffer *leaf,
2309 int slot, u64 start, u64 end)
2311 struct btrfs_file_extent_item *fi;
2312 struct btrfs_key key;
2314 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
2317 btrfs_item_key_to_cpu(leaf, &key, slot);
2318 if (key.objectid != btrfs_ino(inode) ||
2319 key.type != BTRFS_EXTENT_DATA_KEY)
2322 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
2324 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2327 if (btrfs_file_extent_disk_bytenr(leaf, fi))
2330 if (key.offset == end)
2332 if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
2337 static int fill_holes(struct btrfs_trans_handle *trans,
2338 struct btrfs_inode *inode,
2339 struct btrfs_path *path, u64 offset, u64 end)
2341 struct btrfs_fs_info *fs_info = trans->fs_info;
2342 struct btrfs_root *root = inode->root;
2343 struct extent_buffer *leaf;
2344 struct btrfs_file_extent_item *fi;
2345 struct extent_map *hole_em;
2346 struct extent_map_tree *em_tree = &inode->extent_tree;
2347 struct btrfs_key key;
2350 if (btrfs_fs_incompat(fs_info, NO_HOLES))
2353 key.objectid = btrfs_ino(inode);
2354 key.type = BTRFS_EXTENT_DATA_KEY;
2355 key.offset = offset;
2357 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2360 * We should have dropped this offset, so if we find it then
2361 * something has gone horribly wrong.
2368 leaf = path->nodes[0];
2369 if (hole_mergeable(inode, leaf, path->slots[0] - 1, offset, end)) {
2373 fi = btrfs_item_ptr(leaf, path->slots[0],
2374 struct btrfs_file_extent_item);
2375 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
2377 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2378 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2379 btrfs_set_file_extent_offset(leaf, fi, 0);
2380 btrfs_mark_buffer_dirty(leaf);
2384 if (hole_mergeable(inode, leaf, path->slots[0], offset, end)) {
2387 key.offset = offset;
2388 btrfs_set_item_key_safe(fs_info, path, &key);
2389 fi = btrfs_item_ptr(leaf, path->slots[0],
2390 struct btrfs_file_extent_item);
2391 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
2393 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2394 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2395 btrfs_set_file_extent_offset(leaf, fi, 0);
2396 btrfs_mark_buffer_dirty(leaf);
2399 btrfs_release_path(path);
2401 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
2402 offset, 0, 0, end - offset, 0, end - offset, 0, 0, 0);
2407 btrfs_release_path(path);
2409 hole_em = alloc_extent_map();
2411 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
2412 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
2414 hole_em->start = offset;
2415 hole_em->len = end - offset;
2416 hole_em->ram_bytes = hole_em->len;
2417 hole_em->orig_start = offset;
2419 hole_em->block_start = EXTENT_MAP_HOLE;
2420 hole_em->block_len = 0;
2421 hole_em->orig_block_len = 0;
2422 hole_em->compress_type = BTRFS_COMPRESS_NONE;
2423 hole_em->generation = trans->transid;
2426 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
2427 write_lock(&em_tree->lock);
2428 ret = add_extent_mapping(em_tree, hole_em, 1);
2429 write_unlock(&em_tree->lock);
2430 } while (ret == -EEXIST);
2431 free_extent_map(hole_em);
2433 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2434 &inode->runtime_flags);
2441 * Find a hole extent on given inode and change start/len to the end of hole
2442 * extent.(hole/vacuum extent whose em->start <= start &&
2443 * em->start + em->len > start)
2444 * When a hole extent is found, return 1 and modify start/len.
2446 static int find_first_non_hole(struct inode *inode, u64 *start, u64 *len)
2448 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2449 struct extent_map *em;
2452 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0,
2453 round_down(*start, fs_info->sectorsize),
2454 round_up(*len, fs_info->sectorsize));
2458 /* Hole or vacuum extent(only exists in no-hole mode) */
2459 if (em->block_start == EXTENT_MAP_HOLE) {
2461 *len = em->start + em->len > *start + *len ?
2462 0 : *start + *len - em->start - em->len;
2463 *start = em->start + em->len;
2465 free_extent_map(em);
2469 static int btrfs_punch_hole_lock_range(struct inode *inode,
2470 const u64 lockstart,
2472 struct extent_state **cached_state)
2475 struct btrfs_ordered_extent *ordered;
2478 truncate_pagecache_range(inode, lockstart, lockend);
2480 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2482 ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode),
2486 * We need to make sure we have no ordered extents in this range
2487 * and nobody raced in and read a page in this range, if we did
2488 * we need to try again.
2491 (ordered->file_offset + ordered->num_bytes <= lockstart ||
2492 ordered->file_offset > lockend)) &&
2493 !filemap_range_has_page(inode->i_mapping,
2494 lockstart, lockend)) {
2496 btrfs_put_ordered_extent(ordered);
2500 btrfs_put_ordered_extent(ordered);
2501 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
2502 lockend, cached_state);
2503 ret = btrfs_wait_ordered_range(inode, lockstart,
2504 lockend - lockstart + 1);
2511 static int btrfs_insert_replace_extent(struct btrfs_trans_handle *trans,
2512 struct inode *inode,
2513 struct btrfs_path *path,
2514 struct btrfs_replace_extent_info *extent_info,
2515 const u64 replace_len)
2517 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2518 struct btrfs_root *root = BTRFS_I(inode)->root;
2519 struct btrfs_file_extent_item *extent;
2520 struct extent_buffer *leaf;
2521 struct btrfs_key key;
2523 struct btrfs_ref ref = { 0 };
2526 if (replace_len == 0)
2529 if (extent_info->disk_offset == 0 &&
2530 btrfs_fs_incompat(fs_info, NO_HOLES))
2533 key.objectid = btrfs_ino(BTRFS_I(inode));
2534 key.type = BTRFS_EXTENT_DATA_KEY;
2535 key.offset = extent_info->file_offset;
2536 ret = btrfs_insert_empty_item(trans, root, path, &key,
2537 sizeof(struct btrfs_file_extent_item));
2540 leaf = path->nodes[0];
2541 slot = path->slots[0];
2542 write_extent_buffer(leaf, extent_info->extent_buf,
2543 btrfs_item_ptr_offset(leaf, slot),
2544 sizeof(struct btrfs_file_extent_item));
2545 extent = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
2546 ASSERT(btrfs_file_extent_type(leaf, extent) != BTRFS_FILE_EXTENT_INLINE);
2547 btrfs_set_file_extent_offset(leaf, extent, extent_info->data_offset);
2548 btrfs_set_file_extent_num_bytes(leaf, extent, replace_len);
2549 if (extent_info->is_new_extent)
2550 btrfs_set_file_extent_generation(leaf, extent, trans->transid);
2551 btrfs_mark_buffer_dirty(leaf);
2552 btrfs_release_path(path);
2554 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
2555 extent_info->file_offset, replace_len);
2559 /* If it's a hole, nothing more needs to be done. */
2560 if (extent_info->disk_offset == 0)
2563 inode_add_bytes(inode, replace_len);
2565 if (extent_info->is_new_extent && extent_info->insertions == 0) {
2566 key.objectid = extent_info->disk_offset;
2567 key.type = BTRFS_EXTENT_ITEM_KEY;
2568 key.offset = extent_info->disk_len;
2569 ret = btrfs_alloc_reserved_file_extent(trans, root,
2570 btrfs_ino(BTRFS_I(inode)),
2571 extent_info->file_offset,
2572 extent_info->qgroup_reserved,
2577 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,
2578 extent_info->disk_offset,
2579 extent_info->disk_len, 0);
2580 ref_offset = extent_info->file_offset - extent_info->data_offset;
2581 btrfs_init_data_ref(&ref, root->root_key.objectid,
2582 btrfs_ino(BTRFS_I(inode)), ref_offset);
2583 ret = btrfs_inc_extent_ref(trans, &ref);
2586 extent_info->insertions++;
2592 * The respective range must have been previously locked, as well as the inode.
2593 * The end offset is inclusive (last byte of the range).
2594 * @extent_info is NULL for fallocate's hole punching and non-NULL when replacing
2595 * the file range with an extent.
2596 * When not punching a hole, we don't want to end up in a state where we dropped
2597 * extents without inserting a new one, so we must abort the transaction to avoid
2600 int btrfs_replace_file_extents(struct inode *inode, struct btrfs_path *path,
2601 const u64 start, const u64 end,
2602 struct btrfs_replace_extent_info *extent_info,
2603 struct btrfs_trans_handle **trans_out)
2605 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2606 u64 min_size = btrfs_calc_insert_metadata_size(fs_info, 1);
2607 u64 ino_size = round_up(inode->i_size, fs_info->sectorsize);
2608 struct btrfs_root *root = BTRFS_I(inode)->root;
2609 struct btrfs_trans_handle *trans = NULL;
2610 struct btrfs_block_rsv *rsv;
2611 unsigned int rsv_count;
2614 u64 len = end - start;
2620 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
2625 rsv->size = btrfs_calc_insert_metadata_size(fs_info, 1);
2629 * 1 - update the inode
2630 * 1 - removing the extents in the range
2631 * 1 - adding the hole extent if no_holes isn't set or if we are
2632 * replacing the range with a new extent
2634 if (!btrfs_fs_incompat(fs_info, NO_HOLES) || extent_info)
2639 trans = btrfs_start_transaction(root, rsv_count);
2640 if (IS_ERR(trans)) {
2641 ret = PTR_ERR(trans);
2646 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
2649 trans->block_rsv = rsv;
2652 while (cur_offset < end) {
2653 ret = __btrfs_drop_extents(trans, root, BTRFS_I(inode), path,
2654 cur_offset, end + 1, &drop_end,
2656 if (ret != -ENOSPC) {
2658 * When cloning we want to avoid transaction aborts when
2659 * nothing was done and we are attempting to clone parts
2660 * of inline extents, in such cases -EOPNOTSUPP is
2661 * returned by __btrfs_drop_extents() without having
2662 * changed anything in the file.
2664 if (extent_info && !extent_info->is_new_extent &&
2665 ret && ret != -EOPNOTSUPP)
2666 btrfs_abort_transaction(trans, ret);
2670 trans->block_rsv = &fs_info->trans_block_rsv;
2672 if (!extent_info && cur_offset < drop_end &&
2673 cur_offset < ino_size) {
2674 ret = fill_holes(trans, BTRFS_I(inode), path,
2675 cur_offset, drop_end);
2678 * If we failed then we didn't insert our hole
2679 * entries for the area we dropped, so now the
2680 * fs is corrupted, so we must abort the
2683 btrfs_abort_transaction(trans, ret);
2686 } else if (!extent_info && cur_offset < drop_end) {
2688 * We are past the i_size here, but since we didn't
2689 * insert holes we need to clear the mapped area so we
2690 * know to not set disk_i_size in this area until a new
2691 * file extent is inserted here.
2693 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
2694 cur_offset, drop_end - cur_offset);
2697 * We couldn't clear our area, so we could
2698 * presumably adjust up and corrupt the fs, so
2701 btrfs_abort_transaction(trans, ret);
2706 if (extent_info && drop_end > extent_info->file_offset) {
2707 u64 replace_len = drop_end - extent_info->file_offset;
2709 ret = btrfs_insert_replace_extent(trans, inode, path,
2710 extent_info, replace_len);
2712 btrfs_abort_transaction(trans, ret);
2715 extent_info->data_len -= replace_len;
2716 extent_info->data_offset += replace_len;
2717 extent_info->file_offset += replace_len;
2720 cur_offset = drop_end;
2722 ret = btrfs_update_inode(trans, root, inode);
2726 btrfs_end_transaction(trans);
2727 btrfs_btree_balance_dirty(fs_info);
2729 trans = btrfs_start_transaction(root, rsv_count);
2730 if (IS_ERR(trans)) {
2731 ret = PTR_ERR(trans);
2736 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
2737 rsv, min_size, false);
2738 BUG_ON(ret); /* shouldn't happen */
2739 trans->block_rsv = rsv;
2742 ret = find_first_non_hole(inode, &cur_offset, &len);
2743 if (unlikely(ret < 0))
2753 * If we were cloning, force the next fsync to be a full one since we
2754 * we replaced (or just dropped in the case of cloning holes when
2755 * NO_HOLES is enabled) extents and extent maps.
2756 * This is for the sake of simplicity, and cloning into files larger
2757 * than 16Mb would force the full fsync any way (when
2758 * try_release_extent_mapping() is invoked during page cache truncation.
2760 if (extent_info && !extent_info->is_new_extent)
2761 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2762 &BTRFS_I(inode)->runtime_flags);
2767 trans->block_rsv = &fs_info->trans_block_rsv;
2769 * If we are using the NO_HOLES feature we might have had already an
2770 * hole that overlaps a part of the region [lockstart, lockend] and
2771 * ends at (or beyond) lockend. Since we have no file extent items to
2772 * represent holes, drop_end can be less than lockend and so we must
2773 * make sure we have an extent map representing the existing hole (the
2774 * call to __btrfs_drop_extents() might have dropped the existing extent
2775 * map representing the existing hole), otherwise the fast fsync path
2776 * will not record the existence of the hole region
2777 * [existing_hole_start, lockend].
2779 if (drop_end <= end)
2782 * Don't insert file hole extent item if it's for a range beyond eof
2783 * (because it's useless) or if it represents a 0 bytes range (when
2784 * cur_offset == drop_end).
2786 if (!extent_info && cur_offset < ino_size && cur_offset < drop_end) {
2787 ret = fill_holes(trans, BTRFS_I(inode), path,
2788 cur_offset, drop_end);
2790 /* Same comment as above. */
2791 btrfs_abort_transaction(trans, ret);
2794 } else if (!extent_info && cur_offset < drop_end) {
2795 /* See the comment in the loop above for the reasoning here. */
2796 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
2797 cur_offset, drop_end - cur_offset);
2799 btrfs_abort_transaction(trans, ret);
2805 ret = btrfs_insert_replace_extent(trans, inode, path, extent_info,
2806 extent_info->data_len);
2808 btrfs_abort_transaction(trans, ret);
2817 trans->block_rsv = &fs_info->trans_block_rsv;
2819 btrfs_end_transaction(trans);
2823 btrfs_free_block_rsv(fs_info, rsv);
2828 static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2830 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2831 struct btrfs_root *root = BTRFS_I(inode)->root;
2832 struct extent_state *cached_state = NULL;
2833 struct btrfs_path *path;
2834 struct btrfs_trans_handle *trans = NULL;
2839 u64 orig_start = offset;
2843 bool truncated_block = false;
2844 bool updated_inode = false;
2846 ret = btrfs_wait_ordered_range(inode, offset, len);
2851 ino_size = round_up(inode->i_size, fs_info->sectorsize);
2852 ret = find_first_non_hole(inode, &offset, &len);
2854 goto out_only_mutex;
2856 /* Already in a large hole */
2858 goto out_only_mutex;
2861 lockstart = round_up(offset, btrfs_inode_sectorsize(BTRFS_I(inode)));
2862 lockend = round_down(offset + len,
2863 btrfs_inode_sectorsize(BTRFS_I(inode))) - 1;
2864 same_block = (BTRFS_BYTES_TO_BLKS(fs_info, offset))
2865 == (BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1));
2867 * We needn't truncate any block which is beyond the end of the file
2868 * because we are sure there is no data there.
2871 * Only do this if we are in the same block and we aren't doing the
2874 if (same_block && len < fs_info->sectorsize) {
2875 if (offset < ino_size) {
2876 truncated_block = true;
2877 ret = btrfs_truncate_block(inode, offset, len, 0);
2881 goto out_only_mutex;
2884 /* zero back part of the first block */
2885 if (offset < ino_size) {
2886 truncated_block = true;
2887 ret = btrfs_truncate_block(inode, offset, 0, 0);
2889 inode_unlock(inode);
2894 /* Check the aligned pages after the first unaligned page,
2895 * if offset != orig_start, which means the first unaligned page
2896 * including several following pages are already in holes,
2897 * the extra check can be skipped */
2898 if (offset == orig_start) {
2899 /* after truncate page, check hole again */
2900 len = offset + len - lockstart;
2902 ret = find_first_non_hole(inode, &offset, &len);
2904 goto out_only_mutex;
2907 goto out_only_mutex;
2912 /* Check the tail unaligned part is in a hole */
2913 tail_start = lockend + 1;
2914 tail_len = offset + len - tail_start;
2916 ret = find_first_non_hole(inode, &tail_start, &tail_len);
2917 if (unlikely(ret < 0))
2918 goto out_only_mutex;
2920 /* zero the front end of the last page */
2921 if (tail_start + tail_len < ino_size) {
2922 truncated_block = true;
2923 ret = btrfs_truncate_block(inode,
2924 tail_start + tail_len,
2927 goto out_only_mutex;
2932 if (lockend < lockstart) {
2934 goto out_only_mutex;
2937 ret = btrfs_punch_hole_lock_range(inode, lockstart, lockend,
2940 goto out_only_mutex;
2942 path = btrfs_alloc_path();
2948 ret = btrfs_replace_file_extents(inode, path, lockstart, lockend, NULL,
2950 btrfs_free_path(path);
2954 ASSERT(trans != NULL);
2955 inode_inc_iversion(inode);
2956 inode->i_mtime = inode->i_ctime = current_time(inode);
2957 ret = btrfs_update_inode(trans, root, inode);
2958 updated_inode = true;
2959 btrfs_end_transaction(trans);
2960 btrfs_btree_balance_dirty(fs_info);
2962 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2965 if (!updated_inode && truncated_block && !ret) {
2967 * If we only end up zeroing part of a page, we still need to
2968 * update the inode item, so that all the time fields are
2969 * updated as well as the necessary btrfs inode in memory fields
2970 * for detecting, at fsync time, if the inode isn't yet in the
2971 * log tree or it's there but not up to date.
2973 struct timespec64 now = current_time(inode);
2975 inode_inc_iversion(inode);
2976 inode->i_mtime = now;
2977 inode->i_ctime = now;
2978 trans = btrfs_start_transaction(root, 1);
2979 if (IS_ERR(trans)) {
2980 ret = PTR_ERR(trans);
2984 ret = btrfs_update_inode(trans, root, inode);
2985 ret2 = btrfs_end_transaction(trans);
2990 inode_unlock(inode);
2994 /* Helper structure to record which range is already reserved */
2995 struct falloc_range {
2996 struct list_head list;
3002 * Helper function to add falloc range
3004 * Caller should have locked the larger range of extent containing
3007 static int add_falloc_range(struct list_head *head, u64 start, u64 len)
3009 struct falloc_range *prev = NULL;
3010 struct falloc_range *range = NULL;
3012 if (list_empty(head))
3016 * As fallocate iterate by bytenr order, we only need to check
3019 prev = list_entry(head->prev, struct falloc_range, list);
3020 if (prev->start + prev->len == start) {
3025 range = kmalloc(sizeof(*range), GFP_KERNEL);
3028 range->start = start;
3030 list_add_tail(&range->list, head);
3034 static int btrfs_fallocate_update_isize(struct inode *inode,
3038 struct btrfs_trans_handle *trans;
3039 struct btrfs_root *root = BTRFS_I(inode)->root;
3043 if (mode & FALLOC_FL_KEEP_SIZE || end <= i_size_read(inode))
3046 trans = btrfs_start_transaction(root, 1);
3048 return PTR_ERR(trans);
3050 inode->i_ctime = current_time(inode);
3051 i_size_write(inode, end);
3052 btrfs_inode_safe_disk_i_size_write(inode, 0);
3053 ret = btrfs_update_inode(trans, root, inode);
3054 ret2 = btrfs_end_transaction(trans);
3056 return ret ? ret : ret2;
3060 RANGE_BOUNDARY_WRITTEN_EXTENT,
3061 RANGE_BOUNDARY_PREALLOC_EXTENT,
3062 RANGE_BOUNDARY_HOLE,
3065 static int btrfs_zero_range_check_range_boundary(struct btrfs_inode *inode,
3068 const u64 sectorsize = btrfs_inode_sectorsize(inode);
3069 struct extent_map *em;
3072 offset = round_down(offset, sectorsize);
3073 em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize);
3077 if (em->block_start == EXTENT_MAP_HOLE)
3078 ret = RANGE_BOUNDARY_HOLE;
3079 else if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3080 ret = RANGE_BOUNDARY_PREALLOC_EXTENT;
3082 ret = RANGE_BOUNDARY_WRITTEN_EXTENT;
3084 free_extent_map(em);
3088 static int btrfs_zero_range(struct inode *inode,
3093 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3094 struct extent_map *em;
3095 struct extent_changeset *data_reserved = NULL;
3098 const u64 sectorsize = btrfs_inode_sectorsize(BTRFS_I(inode));
3099 u64 alloc_start = round_down(offset, sectorsize);
3100 u64 alloc_end = round_up(offset + len, sectorsize);
3101 u64 bytes_to_reserve = 0;
3102 bool space_reserved = false;
3104 inode_dio_wait(inode);
3106 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, alloc_start,
3107 alloc_end - alloc_start);
3114 * Avoid hole punching and extent allocation for some cases. More cases
3115 * could be considered, but these are unlikely common and we keep things
3116 * as simple as possible for now. Also, intentionally, if the target
3117 * range contains one or more prealloc extents together with regular
3118 * extents and holes, we drop all the existing extents and allocate a
3119 * new prealloc extent, so that we get a larger contiguous disk extent.
3121 if (em->start <= alloc_start &&
3122 test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3123 const u64 em_end = em->start + em->len;
3125 if (em_end >= offset + len) {
3127 * The whole range is already a prealloc extent,
3128 * do nothing except updating the inode's i_size if
3131 free_extent_map(em);
3132 ret = btrfs_fallocate_update_isize(inode, offset + len,
3137 * Part of the range is already a prealloc extent, so operate
3138 * only on the remaining part of the range.
3140 alloc_start = em_end;
3141 ASSERT(IS_ALIGNED(alloc_start, sectorsize));
3142 len = offset + len - alloc_start;
3143 offset = alloc_start;
3144 alloc_hint = em->block_start + em->len;
3146 free_extent_map(em);
3148 if (BTRFS_BYTES_TO_BLKS(fs_info, offset) ==
3149 BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1)) {
3150 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, alloc_start,
3157 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3158 free_extent_map(em);
3159 ret = btrfs_fallocate_update_isize(inode, offset + len,
3163 if (len < sectorsize && em->block_start != EXTENT_MAP_HOLE) {
3164 free_extent_map(em);
3165 ret = btrfs_truncate_block(inode, offset, len, 0);
3167 ret = btrfs_fallocate_update_isize(inode,
3172 free_extent_map(em);
3173 alloc_start = round_down(offset, sectorsize);
3174 alloc_end = alloc_start + sectorsize;
3178 alloc_start = round_up(offset, sectorsize);
3179 alloc_end = round_down(offset + len, sectorsize);
3182 * For unaligned ranges, check the pages at the boundaries, they might
3183 * map to an extent, in which case we need to partially zero them, or
3184 * they might map to a hole, in which case we need our allocation range
3187 if (!IS_ALIGNED(offset, sectorsize)) {
3188 ret = btrfs_zero_range_check_range_boundary(BTRFS_I(inode),
3192 if (ret == RANGE_BOUNDARY_HOLE) {
3193 alloc_start = round_down(offset, sectorsize);
3195 } else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) {
3196 ret = btrfs_truncate_block(inode, offset, 0, 0);
3204 if (!IS_ALIGNED(offset + len, sectorsize)) {
3205 ret = btrfs_zero_range_check_range_boundary(BTRFS_I(inode),
3209 if (ret == RANGE_BOUNDARY_HOLE) {
3210 alloc_end = round_up(offset + len, sectorsize);
3212 } else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) {
3213 ret = btrfs_truncate_block(inode, offset + len, 0, 1);
3222 if (alloc_start < alloc_end) {
3223 struct extent_state *cached_state = NULL;
3224 const u64 lockstart = alloc_start;
3225 const u64 lockend = alloc_end - 1;
3227 bytes_to_reserve = alloc_end - alloc_start;
3228 ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode),
3232 space_reserved = true;
3233 ret = btrfs_punch_hole_lock_range(inode, lockstart, lockend,
3237 ret = btrfs_qgroup_reserve_data(BTRFS_I(inode), &data_reserved,
3238 alloc_start, bytes_to_reserve);
3241 ret = btrfs_prealloc_file_range(inode, mode, alloc_start,
3242 alloc_end - alloc_start,
3244 offset + len, &alloc_hint);
3245 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
3246 lockend, &cached_state);
3247 /* btrfs_prealloc_file_range releases reserved space on error */
3249 space_reserved = false;
3253 ret = btrfs_fallocate_update_isize(inode, offset + len, mode);
3255 if (ret && space_reserved)
3256 btrfs_free_reserved_data_space(BTRFS_I(inode), data_reserved,
3257 alloc_start, bytes_to_reserve);
3258 extent_changeset_free(data_reserved);
3263 static long btrfs_fallocate(struct file *file, int mode,
3264 loff_t offset, loff_t len)
3266 struct inode *inode = file_inode(file);
3267 struct extent_state *cached_state = NULL;
3268 struct extent_changeset *data_reserved = NULL;
3269 struct falloc_range *range;
3270 struct falloc_range *tmp;
3271 struct list_head reserve_list;
3279 struct extent_map *em;
3280 int blocksize = btrfs_inode_sectorsize(BTRFS_I(inode));
3283 alloc_start = round_down(offset, blocksize);
3284 alloc_end = round_up(offset + len, blocksize);
3285 cur_offset = alloc_start;
3287 /* Make sure we aren't being give some crap mode */
3288 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
3289 FALLOC_FL_ZERO_RANGE))
3292 if (mode & FALLOC_FL_PUNCH_HOLE)
3293 return btrfs_punch_hole(inode, offset, len);
3296 * Only trigger disk allocation, don't trigger qgroup reserve
3298 * For qgroup space, it will be checked later.
3300 if (!(mode & FALLOC_FL_ZERO_RANGE)) {
3301 ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode),
3302 alloc_end - alloc_start);
3309 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) {
3310 ret = inode_newsize_ok(inode, offset + len);
3316 * TODO: Move these two operations after we have checked
3317 * accurate reserved space, or fallocate can still fail but
3318 * with page truncated or size expanded.
3320 * But that's a minor problem and won't do much harm BTW.
3322 if (alloc_start > inode->i_size) {
3323 ret = btrfs_cont_expand(inode, i_size_read(inode),
3327 } else if (offset + len > inode->i_size) {
3329 * If we are fallocating from the end of the file onward we
3330 * need to zero out the end of the block if i_size lands in the
3331 * middle of a block.
3333 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
3339 * wait for ordered IO before we have any locks. We'll loop again
3340 * below with the locks held.
3342 ret = btrfs_wait_ordered_range(inode, alloc_start,
3343 alloc_end - alloc_start);
3347 if (mode & FALLOC_FL_ZERO_RANGE) {
3348 ret = btrfs_zero_range(inode, offset, len, mode);
3349 inode_unlock(inode);
3353 locked_end = alloc_end - 1;
3355 struct btrfs_ordered_extent *ordered;
3357 /* the extent lock is ordered inside the running
3360 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
3361 locked_end, &cached_state);
3362 ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode),
3366 ordered->file_offset + ordered->num_bytes > alloc_start &&
3367 ordered->file_offset < alloc_end) {
3368 btrfs_put_ordered_extent(ordered);
3369 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
3370 alloc_start, locked_end,
3373 * we can't wait on the range with the transaction
3374 * running or with the extent lock held
3376 ret = btrfs_wait_ordered_range(inode, alloc_start,
3377 alloc_end - alloc_start);
3382 btrfs_put_ordered_extent(ordered);
3387 /* First, check if we exceed the qgroup limit */
3388 INIT_LIST_HEAD(&reserve_list);
3389 while (cur_offset < alloc_end) {
3390 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
3391 alloc_end - cur_offset);
3396 last_byte = min(extent_map_end(em), alloc_end);
3397 actual_end = min_t(u64, extent_map_end(em), offset + len);
3398 last_byte = ALIGN(last_byte, blocksize);
3399 if (em->block_start == EXTENT_MAP_HOLE ||
3400 (cur_offset >= inode->i_size &&
3401 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
3402 ret = add_falloc_range(&reserve_list, cur_offset,
3403 last_byte - cur_offset);
3405 free_extent_map(em);
3408 ret = btrfs_qgroup_reserve_data(BTRFS_I(inode),
3409 &data_reserved, cur_offset,
3410 last_byte - cur_offset);
3412 cur_offset = last_byte;
3413 free_extent_map(em);
3418 * Do not need to reserve unwritten extent for this
3419 * range, free reserved data space first, otherwise
3420 * it'll result in false ENOSPC error.
3422 btrfs_free_reserved_data_space(BTRFS_I(inode),
3423 data_reserved, cur_offset,
3424 last_byte - cur_offset);
3426 free_extent_map(em);
3427 cur_offset = last_byte;
3431 * If ret is still 0, means we're OK to fallocate.
3432 * Or just cleanup the list and exit.
3434 list_for_each_entry_safe(range, tmp, &reserve_list, list) {
3436 ret = btrfs_prealloc_file_range(inode, mode,
3438 range->len, i_blocksize(inode),
3439 offset + len, &alloc_hint);
3441 btrfs_free_reserved_data_space(BTRFS_I(inode),
3442 data_reserved, range->start,
3444 list_del(&range->list);
3451 * We didn't need to allocate any more space, but we still extended the
3452 * size of the file so we need to update i_size and the inode item.
3454 ret = btrfs_fallocate_update_isize(inode, actual_end, mode);
3456 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
3459 inode_unlock(inode);
3460 /* Let go of our reservation. */
3461 if (ret != 0 && !(mode & FALLOC_FL_ZERO_RANGE))
3462 btrfs_free_reserved_data_space(BTRFS_I(inode), data_reserved,
3463 cur_offset, alloc_end - cur_offset);
3464 extent_changeset_free(data_reserved);
3468 static loff_t find_desired_extent(struct inode *inode, loff_t offset,
3471 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3472 struct extent_map *em = NULL;
3473 struct extent_state *cached_state = NULL;
3474 loff_t i_size = inode->i_size;
3481 if (i_size == 0 || offset >= i_size)
3485 * offset can be negative, in this case we start finding DATA/HOLE from
3486 * the very start of the file.
3488 start = max_t(loff_t, 0, offset);
3490 lockstart = round_down(start, fs_info->sectorsize);
3491 lockend = round_up(i_size, fs_info->sectorsize);
3492 if (lockend <= lockstart)
3493 lockend = lockstart + fs_info->sectorsize;
3495 len = lockend - lockstart + 1;
3497 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
3500 while (start < i_size) {
3501 em = btrfs_get_extent_fiemap(BTRFS_I(inode), start, len);
3508 if (whence == SEEK_HOLE &&
3509 (em->block_start == EXTENT_MAP_HOLE ||
3510 test_bit(EXTENT_FLAG_PREALLOC, &em->flags)))
3512 else if (whence == SEEK_DATA &&
3513 (em->block_start != EXTENT_MAP_HOLE &&
3514 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)))
3517 start = em->start + em->len;
3518 free_extent_map(em);
3522 free_extent_map(em);
3523 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
3528 if (whence == SEEK_DATA && start >= i_size)
3531 offset = min_t(loff_t, start, i_size);
3537 static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)
3539 struct inode *inode = file->f_mapping->host;
3543 return generic_file_llseek(file, offset, whence);
3546 inode_lock_shared(inode);
3547 offset = find_desired_extent(inode, offset, whence);
3548 inode_unlock_shared(inode);
3555 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3558 static int btrfs_file_open(struct inode *inode, struct file *filp)
3560 filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
3561 return generic_file_open(inode, filp);
3564 static ssize_t btrfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
3568 if (iocb->ki_flags & IOCB_DIRECT) {
3569 struct inode *inode = file_inode(iocb->ki_filp);
3571 inode_lock_shared(inode);
3572 ret = btrfs_direct_IO(iocb, to);
3573 inode_unlock_shared(inode);
3574 if (ret < 0 || !iov_iter_count(to) ||
3575 iocb->ki_pos >= i_size_read(file_inode(iocb->ki_filp)))
3579 return generic_file_buffered_read(iocb, to, ret);
3582 const struct file_operations btrfs_file_operations = {
3583 .llseek = btrfs_file_llseek,
3584 .read_iter = btrfs_file_read_iter,
3585 .splice_read = generic_file_splice_read,
3586 .write_iter = btrfs_file_write_iter,
3587 .splice_write = iter_file_splice_write,
3588 .mmap = btrfs_file_mmap,
3589 .open = btrfs_file_open,
3590 .release = btrfs_release_file,
3591 .fsync = btrfs_sync_file,
3592 .fallocate = btrfs_fallocate,
3593 .unlocked_ioctl = btrfs_ioctl,
3594 #ifdef CONFIG_COMPAT
3595 .compat_ioctl = btrfs_compat_ioctl,
3597 .remap_file_range = btrfs_remap_file_range,
3600 void __cold btrfs_auto_defrag_exit(void)
3602 kmem_cache_destroy(btrfs_inode_defrag_cachep);
3605 int __init btrfs_auto_defrag_init(void)
3607 btrfs_inode_defrag_cachep = kmem_cache_create("btrfs_inode_defrag",
3608 sizeof(struct inode_defrag), 0,
3611 if (!btrfs_inode_defrag_cachep)
3617 int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end)
3622 * So with compression we will find and lock a dirty page and clear the
3623 * first one as dirty, setup an async extent, and immediately return
3624 * with the entire range locked but with nobody actually marked with
3625 * writeback. So we can't just filemap_write_and_wait_range() and
3626 * expect it to work since it will just kick off a thread to do the
3627 * actual work. So we need to call filemap_fdatawrite_range _again_
3628 * since it will wait on the page lock, which won't be unlocked until
3629 * after the pages have been marked as writeback and so we're good to go
3630 * from there. We have to do this otherwise we'll miss the ordered
3631 * extents and that results in badness. Please Josef, do not think you
3632 * know better and pull this out at some point in the future, it is
3633 * right and you are wrong.
3635 ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
3636 if (!ret && test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
3637 &BTRFS_I(inode)->runtime_flags))
3638 ret = filemap_fdatawrite_range(inode->i_mapping, start, end);