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>
21 #include "direct-io.h"
23 #include "transaction.h"
24 #include "btrfs_inode.h"
28 #include "compression.h"
29 #include "delalloc-space.h"
33 #include "accessors.h"
34 #include "extent-tree.h"
35 #include "file-item.h"
39 #include "print-tree.h"
42 * Unlock folio after btrfs_file_write() is done with it.
44 static void btrfs_drop_folio(struct btrfs_fs_info *fs_info, struct folio *folio,
47 u64 block_start = round_down(pos, fs_info->sectorsize);
48 u64 block_len = round_up(pos + copied, fs_info->sectorsize) - block_start;
50 ASSERT(block_len <= U32_MAX);
52 * Folio checked is some magic around finding folios that have been
53 * modified without going through btrfs_dirty_folio(). Clear it here.
54 * There should be no need to mark the pages accessed as
55 * prepare_one_folio() should have marked them accessed in
56 * prepare_one_folio() via find_or_create_page()
58 btrfs_folio_clamp_clear_checked(fs_info, folio, block_start, block_len);
64 * After copy_folio_from_iter_atomic(), update the following things for delalloc:
65 * - Mark newly dirtied folio as DELALLOC in the io tree.
66 * Used to advise which range is to be written back.
67 * - Mark modified folio as Uptodate/Dirty and not needing COW fixup
68 * - Update inode size for past EOF write
70 int btrfs_dirty_folio(struct btrfs_inode *inode, struct folio *folio, loff_t pos,
71 size_t write_bytes, struct extent_state **cached, bool noreserve)
73 struct btrfs_fs_info *fs_info = inode->root->fs_info;
77 u64 end_of_last_block;
78 u64 end_pos = pos + write_bytes;
79 loff_t isize = i_size_read(&inode->vfs_inode);
80 unsigned int extra_bits = 0;
86 extra_bits |= EXTENT_NORESERVE;
88 start_pos = round_down(pos, fs_info->sectorsize);
89 num_bytes = round_up(write_bytes + pos - start_pos,
91 ASSERT(num_bytes <= U32_MAX);
92 ASSERT(folio_pos(folio) <= pos &&
93 folio_pos(folio) + folio_size(folio) >= pos + write_bytes);
95 end_of_last_block = start_pos + num_bytes - 1;
98 * The pages may have already been dirty, clear out old accounting so
99 * we can set things up properly
101 btrfs_clear_extent_bit(&inode->io_tree, start_pos, end_of_last_block,
102 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
105 ret = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
110 btrfs_folio_clamp_set_uptodate(fs_info, folio, start_pos, num_bytes);
111 btrfs_folio_clamp_clear_checked(fs_info, folio, start_pos, num_bytes);
112 btrfs_folio_clamp_set_dirty(fs_info, folio, start_pos, num_bytes);
115 * we've only changed i_size in ram, and we haven't updated
116 * the disk i_size. There is no need to log the inode
120 i_size_write(&inode->vfs_inode, end_pos);
125 * this is very complex, but the basic idea is to drop all extents
126 * in the range start - end. hint_block is filled in with a block number
127 * that would be a good hint to the block allocator for this file.
129 * If an extent intersects the range but is not entirely inside the range
130 * it is either truncated or split. Anything entirely inside the range
131 * is deleted from the tree.
133 * Note: the VFS' inode number of bytes is not updated, it's up to the caller
134 * to deal with that. We set the field 'bytes_found' of the arguments structure
135 * with the number of allocated bytes found in the target range, so that the
136 * caller can update the inode's number of bytes in an atomic way when
137 * replacing extents in a range to avoid races with stat(2).
139 int btrfs_drop_extents(struct btrfs_trans_handle *trans,
140 struct btrfs_root *root, struct btrfs_inode *inode,
141 struct btrfs_drop_extents_args *args)
143 struct btrfs_fs_info *fs_info = root->fs_info;
144 struct extent_buffer *leaf;
145 struct btrfs_file_extent_item *fi;
146 struct btrfs_key key;
147 struct btrfs_key new_key;
148 u64 ino = btrfs_ino(inode);
149 u64 search_start = args->start;
152 u64 extent_offset = 0;
154 u64 last_end = args->start;
160 int modify_tree = -1;
163 struct btrfs_path *path = args->path;
165 args->bytes_found = 0;
166 args->extent_inserted = false;
168 /* Must always have a path if ->replace_extent is true */
169 ASSERT(!(args->replace_extent && !args->path));
172 path = btrfs_alloc_path();
179 if (args->drop_cache)
180 btrfs_drop_extent_map_range(inode, args->start, args->end - 1, false);
182 if (data_race(args->start >= inode->disk_i_size) && !args->replace_extent)
185 update_refs = (btrfs_root_id(root) != BTRFS_TREE_LOG_OBJECTID);
188 ret = btrfs_lookup_file_extent(trans, root, path, ino,
189 search_start, modify_tree);
192 if (ret > 0 && path->slots[0] > 0 && search_start == args->start) {
193 leaf = path->nodes[0];
194 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
195 if (key.objectid == ino &&
196 key.type == BTRFS_EXTENT_DATA_KEY)
201 leaf = path->nodes[0];
202 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
203 if (WARN_ON(del_nr > 0)) {
204 btrfs_print_leaf(leaf);
208 ret = btrfs_next_leaf(root, path);
215 leaf = path->nodes[0];
219 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
221 if (key.objectid > ino)
223 if (WARN_ON_ONCE(key.objectid < ino) ||
224 key.type < BTRFS_EXTENT_DATA_KEY) {
229 if (key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= args->end)
232 fi = btrfs_item_ptr(leaf, path->slots[0],
233 struct btrfs_file_extent_item);
234 extent_type = btrfs_file_extent_type(leaf, fi);
236 if (extent_type == BTRFS_FILE_EXTENT_REG ||
237 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
238 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
239 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
240 extent_offset = btrfs_file_extent_offset(leaf, fi);
241 extent_end = key.offset +
242 btrfs_file_extent_num_bytes(leaf, fi);
243 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
244 extent_end = key.offset +
245 btrfs_file_extent_ram_bytes(leaf, fi);
252 * Don't skip extent items representing 0 byte lengths. They
253 * used to be created (bug) if while punching holes we hit
254 * -ENOSPC condition. So if we find one here, just ensure we
255 * delete it, otherwise we would insert a new file extent item
256 * with the same key (offset) as that 0 bytes length file
257 * extent item in the call to setup_items_for_insert() later
260 if (extent_end == key.offset && extent_end >= search_start) {
261 last_end = extent_end;
262 goto delete_extent_item;
265 if (extent_end <= search_start) {
271 search_start = max(key.offset, args->start);
272 if (recow || !modify_tree) {
274 btrfs_release_path(path);
279 * | - range to drop - |
280 * | -------- extent -------- |
282 if (args->start > key.offset && args->end < extent_end) {
283 if (WARN_ON(del_nr > 0)) {
284 btrfs_print_leaf(leaf);
288 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
293 memcpy(&new_key, &key, sizeof(new_key));
294 new_key.offset = args->start;
295 ret = btrfs_duplicate_item(trans, root, path,
297 if (ret == -EAGAIN) {
298 btrfs_release_path(path);
304 leaf = path->nodes[0];
305 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
306 struct btrfs_file_extent_item);
307 btrfs_set_file_extent_num_bytes(leaf, fi,
308 args->start - key.offset);
310 fi = btrfs_item_ptr(leaf, path->slots[0],
311 struct btrfs_file_extent_item);
313 extent_offset += args->start - key.offset;
314 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
315 btrfs_set_file_extent_num_bytes(leaf, fi,
316 extent_end - args->start);
318 if (update_refs && disk_bytenr > 0) {
319 struct btrfs_ref ref = {
320 .action = BTRFS_ADD_DELAYED_REF,
321 .bytenr = disk_bytenr,
322 .num_bytes = num_bytes,
324 .owning_root = btrfs_root_id(root),
325 .ref_root = btrfs_root_id(root),
327 btrfs_init_data_ref(&ref, new_key.objectid,
328 args->start - extent_offset,
330 ret = btrfs_inc_extent_ref(trans, &ref);
332 btrfs_abort_transaction(trans, ret);
336 key.offset = args->start;
339 * From here on out we will have actually dropped something, so
340 * last_end can be updated.
342 last_end = extent_end;
345 * | ---- range to drop ----- |
346 * | -------- extent -------- |
348 if (args->start <= key.offset && args->end < extent_end) {
349 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
354 memcpy(&new_key, &key, sizeof(new_key));
355 new_key.offset = args->end;
356 btrfs_set_item_key_safe(trans, path, &new_key);
358 extent_offset += args->end - key.offset;
359 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
360 btrfs_set_file_extent_num_bytes(leaf, fi,
361 extent_end - args->end);
362 if (update_refs && disk_bytenr > 0)
363 args->bytes_found += args->end - key.offset;
367 search_start = extent_end;
369 * | ---- range to drop ----- |
370 * | -------- extent -------- |
372 if (args->start > key.offset && args->end >= extent_end) {
373 if (WARN_ON(del_nr > 0)) {
374 btrfs_print_leaf(leaf);
378 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
383 btrfs_set_file_extent_num_bytes(leaf, fi,
384 args->start - key.offset);
385 if (update_refs && disk_bytenr > 0)
386 args->bytes_found += extent_end - args->start;
387 if (args->end == extent_end)
395 * | ---- range to drop ----- |
396 * | ------ extent ------ |
398 if (args->start <= key.offset && args->end >= extent_end) {
401 del_slot = path->slots[0];
404 if (WARN_ON(del_slot + del_nr != path->slots[0])) {
405 btrfs_print_leaf(leaf);
413 extent_type == BTRFS_FILE_EXTENT_INLINE) {
414 args->bytes_found += extent_end - key.offset;
415 extent_end = ALIGN(extent_end,
416 fs_info->sectorsize);
417 } else if (update_refs && disk_bytenr > 0) {
418 struct btrfs_ref ref = {
419 .action = BTRFS_DROP_DELAYED_REF,
420 .bytenr = disk_bytenr,
421 .num_bytes = num_bytes,
423 .owning_root = btrfs_root_id(root),
424 .ref_root = btrfs_root_id(root),
426 btrfs_init_data_ref(&ref, key.objectid,
427 key.offset - extent_offset,
429 ret = btrfs_free_extent(trans, &ref);
431 btrfs_abort_transaction(trans, ret);
434 args->bytes_found += extent_end - key.offset;
437 if (args->end == extent_end)
440 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
445 ret = btrfs_del_items(trans, root, path, del_slot,
448 btrfs_abort_transaction(trans, ret);
455 btrfs_release_path(path);
462 if (!ret && del_nr > 0) {
464 * Set path->slots[0] to first slot, so that after the delete
465 * if items are move off from our leaf to its immediate left or
466 * right neighbor leafs, we end up with a correct and adjusted
467 * path->slots[0] for our insertion (if args->replace_extent).
469 path->slots[0] = del_slot;
470 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
472 btrfs_abort_transaction(trans, ret);
475 leaf = path->nodes[0];
477 * If btrfs_del_items() was called, it might have deleted a leaf, in
478 * which case it unlocked our path, so check path->locks[0] matches a
481 if (!ret && args->replace_extent &&
482 path->locks[0] == BTRFS_WRITE_LOCK &&
483 btrfs_leaf_free_space(leaf) >=
484 sizeof(struct btrfs_item) + args->extent_item_size) {
487 key.type = BTRFS_EXTENT_DATA_KEY;
488 key.offset = args->start;
489 if (!del_nr && path->slots[0] < btrfs_header_nritems(leaf)) {
490 struct btrfs_key slot_key;
492 btrfs_item_key_to_cpu(leaf, &slot_key, path->slots[0]);
493 if (btrfs_comp_cpu_keys(&key, &slot_key) > 0)
496 btrfs_setup_item_for_insert(trans, root, path, &key,
497 args->extent_item_size);
498 args->extent_inserted = true;
502 btrfs_free_path(path);
503 else if (!args->extent_inserted)
504 btrfs_release_path(path);
506 args->drop_end = found ? min(args->end, last_end) : args->end;
511 static bool extent_mergeable(struct extent_buffer *leaf, int slot, u64 objectid,
512 u64 bytenr, u64 orig_offset, u64 *start, u64 *end)
514 struct btrfs_file_extent_item *fi;
515 struct btrfs_key key;
518 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
521 btrfs_item_key_to_cpu(leaf, &key, slot);
522 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
525 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
526 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
527 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
528 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
529 btrfs_file_extent_compression(leaf, fi) ||
530 btrfs_file_extent_encryption(leaf, fi) ||
531 btrfs_file_extent_other_encoding(leaf, fi))
534 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
535 if ((*start && *start != key.offset) || (*end && *end != extent_end))
544 * Mark extent in the range start - end as written.
546 * This changes extent type from 'pre-allocated' to 'regular'. If only
547 * part of extent is marked as written, the extent will be split into
550 int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
551 struct btrfs_inode *inode, u64 start, u64 end)
553 struct btrfs_root *root = inode->root;
554 struct extent_buffer *leaf;
555 BTRFS_PATH_AUTO_FREE(path);
556 struct btrfs_file_extent_item *fi;
557 struct btrfs_ref ref = { 0 };
558 struct btrfs_key key;
559 struct btrfs_key new_key;
571 u64 ino = btrfs_ino(inode);
573 path = btrfs_alloc_path();
580 key.type = BTRFS_EXTENT_DATA_KEY;
583 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
586 if (ret > 0 && path->slots[0] > 0)
589 leaf = path->nodes[0];
590 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
591 if (key.objectid != ino ||
592 key.type != BTRFS_EXTENT_DATA_KEY) {
594 btrfs_abort_transaction(trans, ret);
597 fi = btrfs_item_ptr(leaf, path->slots[0],
598 struct btrfs_file_extent_item);
599 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_PREALLOC) {
601 btrfs_abort_transaction(trans, ret);
604 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
605 if (key.offset > start || extent_end < end) {
607 btrfs_abort_transaction(trans, ret);
611 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
612 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
613 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
614 memcpy(&new_key, &key, sizeof(new_key));
616 if (start == key.offset && end < extent_end) {
619 if (extent_mergeable(leaf, path->slots[0] - 1,
620 ino, bytenr, orig_offset,
621 &other_start, &other_end)) {
622 new_key.offset = end;
623 btrfs_set_item_key_safe(trans, path, &new_key);
624 fi = btrfs_item_ptr(leaf, path->slots[0],
625 struct btrfs_file_extent_item);
626 btrfs_set_file_extent_generation(leaf, fi,
628 btrfs_set_file_extent_num_bytes(leaf, fi,
630 btrfs_set_file_extent_offset(leaf, fi,
632 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
633 struct btrfs_file_extent_item);
634 btrfs_set_file_extent_generation(leaf, fi,
636 btrfs_set_file_extent_num_bytes(leaf, fi,
642 if (start > key.offset && end == extent_end) {
645 if (extent_mergeable(leaf, path->slots[0] + 1,
646 ino, bytenr, orig_offset,
647 &other_start, &other_end)) {
648 fi = btrfs_item_ptr(leaf, path->slots[0],
649 struct btrfs_file_extent_item);
650 btrfs_set_file_extent_num_bytes(leaf, fi,
652 btrfs_set_file_extent_generation(leaf, fi,
655 new_key.offset = start;
656 btrfs_set_item_key_safe(trans, path, &new_key);
658 fi = btrfs_item_ptr(leaf, path->slots[0],
659 struct btrfs_file_extent_item);
660 btrfs_set_file_extent_generation(leaf, fi,
662 btrfs_set_file_extent_num_bytes(leaf, fi,
664 btrfs_set_file_extent_offset(leaf, fi,
665 start - orig_offset);
670 while (start > key.offset || end < extent_end) {
671 if (key.offset == start)
674 new_key.offset = split;
675 ret = btrfs_duplicate_item(trans, root, path, &new_key);
676 if (ret == -EAGAIN) {
677 btrfs_release_path(path);
681 btrfs_abort_transaction(trans, ret);
685 leaf = path->nodes[0];
686 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
687 struct btrfs_file_extent_item);
688 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
689 btrfs_set_file_extent_num_bytes(leaf, fi,
692 fi = btrfs_item_ptr(leaf, path->slots[0],
693 struct btrfs_file_extent_item);
695 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
696 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
697 btrfs_set_file_extent_num_bytes(leaf, fi,
700 ref.action = BTRFS_ADD_DELAYED_REF;
702 ref.num_bytes = num_bytes;
704 ref.owning_root = btrfs_root_id(root);
705 ref.ref_root = btrfs_root_id(root);
706 btrfs_init_data_ref(&ref, ino, orig_offset, 0, false);
707 ret = btrfs_inc_extent_ref(trans, &ref);
709 btrfs_abort_transaction(trans, ret);
713 if (split == start) {
716 if (start != key.offset) {
718 btrfs_abort_transaction(trans, ret);
730 ref.action = BTRFS_DROP_DELAYED_REF;
732 ref.num_bytes = num_bytes;
734 ref.owning_root = btrfs_root_id(root);
735 ref.ref_root = btrfs_root_id(root);
736 btrfs_init_data_ref(&ref, ino, orig_offset, 0, false);
737 if (extent_mergeable(leaf, path->slots[0] + 1,
738 ino, bytenr, orig_offset,
739 &other_start, &other_end)) {
741 btrfs_release_path(path);
744 extent_end = other_end;
745 del_slot = path->slots[0] + 1;
747 ret = btrfs_free_extent(trans, &ref);
749 btrfs_abort_transaction(trans, ret);
755 if (extent_mergeable(leaf, path->slots[0] - 1,
756 ino, bytenr, orig_offset,
757 &other_start, &other_end)) {
759 btrfs_release_path(path);
762 key.offset = other_start;
763 del_slot = path->slots[0];
765 ret = btrfs_free_extent(trans, &ref);
767 btrfs_abort_transaction(trans, ret);
772 fi = btrfs_item_ptr(leaf, path->slots[0],
773 struct btrfs_file_extent_item);
774 btrfs_set_file_extent_type(leaf, fi,
775 BTRFS_FILE_EXTENT_REG);
776 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
778 fi = btrfs_item_ptr(leaf, del_slot - 1,
779 struct btrfs_file_extent_item);
780 btrfs_set_file_extent_type(leaf, fi,
781 BTRFS_FILE_EXTENT_REG);
782 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
783 btrfs_set_file_extent_num_bytes(leaf, fi,
784 extent_end - key.offset);
786 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
788 btrfs_abort_transaction(trans, ret);
797 * On error return an unlocked folio and the error value
798 * On success return a locked folio and 0
800 static int prepare_uptodate_folio(struct inode *inode, struct folio *folio, u64 pos,
803 u64 clamp_start = max_t(u64, pos, folio_pos(folio));
804 u64 clamp_end = min_t(u64, pos + len, folio_pos(folio) + folio_size(folio));
805 const u32 blocksize = inode_to_fs_info(inode)->sectorsize;
808 if (folio_test_uptodate(folio))
811 if (IS_ALIGNED(clamp_start, blocksize) &&
812 IS_ALIGNED(clamp_end, blocksize))
815 ret = btrfs_read_folio(NULL, folio);
819 if (!folio_test_uptodate(folio)) {
825 * Since btrfs_read_folio() will unlock the folio before it returns,
826 * there is a window where btrfs_release_folio() can be called to
827 * release the page. Here we check both inode mapping and page
828 * private to make sure the page was not released.
830 * The private flag check is essential for subpage as we need to store
831 * extra bitmap using folio private.
833 if (folio->mapping != inode->i_mapping || !folio_test_private(folio)) {
840 static gfp_t get_prepare_gfp_flags(struct inode *inode, bool nowait)
844 gfp = btrfs_alloc_write_mask(inode->i_mapping);
846 gfp &= ~__GFP_DIRECT_RECLAIM;
854 * Get folio into the page cache and lock it.
856 static noinline int prepare_one_folio(struct inode *inode, struct folio **folio_ret,
857 loff_t pos, size_t write_bytes,
860 unsigned long index = pos >> PAGE_SHIFT;
861 gfp_t mask = get_prepare_gfp_flags(inode, nowait);
862 fgf_t fgp_flags = (nowait ? FGP_WRITEBEGIN | FGP_NOWAIT : FGP_WRITEBEGIN) |
863 fgf_set_order(write_bytes);
868 folio = __filemap_get_folio(inode->i_mapping, index, fgp_flags, mask);
870 return PTR_ERR(folio);
872 ret = set_folio_extent_mapped(folio);
878 ret = prepare_uptodate_folio(inode, folio, pos, write_bytes);
880 /* The folio is already unlocked. */
882 if (!nowait && ret == -EAGAIN) {
893 * Locks the extent and properly waits for data=ordered extents to finish
894 * before allowing the folios to be modified if need.
897 * 1 - the extent is locked
898 * 0 - the extent is not locked, and everything is OK
899 * -EAGAIN - need to prepare the folios again
902 lock_and_cleanup_extent_if_need(struct btrfs_inode *inode, struct folio *folio,
903 loff_t pos, size_t write_bytes,
904 u64 *lockstart, u64 *lockend, bool nowait,
905 struct extent_state **cached_state)
907 struct btrfs_fs_info *fs_info = inode->root->fs_info;
912 start_pos = round_down(pos, fs_info->sectorsize);
913 last_pos = round_up(pos + write_bytes, fs_info->sectorsize) - 1;
915 if (start_pos < inode->vfs_inode.i_size) {
916 struct btrfs_ordered_extent *ordered;
919 if (!btrfs_try_lock_extent(&inode->io_tree, start_pos,
920 last_pos, cached_state)) {
926 btrfs_lock_extent(&inode->io_tree, start_pos, last_pos,
930 ordered = btrfs_lookup_ordered_range(inode, start_pos,
931 last_pos - start_pos + 1);
933 ordered->file_offset + ordered->num_bytes > start_pos &&
934 ordered->file_offset <= last_pos) {
935 btrfs_unlock_extent(&inode->io_tree, start_pos, last_pos,
939 btrfs_start_ordered_extent(ordered);
940 btrfs_put_ordered_extent(ordered);
944 btrfs_put_ordered_extent(ordered);
946 *lockstart = start_pos;
952 * We should be called after prepare_one_folio() which should have locked
953 * all pages in the range.
955 WARN_ON(!folio_test_locked(folio));
961 * Check if we can do nocow write into the range [@pos, @pos + @write_bytes)
964 * @write_bytes: The length to write, will be updated to the nocow writeable
967 * This function will flush ordered extents in the range to ensure proper
971 * > 0 If we can nocow, and updates @write_bytes.
972 * 0 If we can't do a nocow write.
973 * -EAGAIN If we can't do a nocow write because snapshoting of the inode's
974 * root is in progress.
975 * < 0 If an error happened.
977 * NOTE: Callers need to call btrfs_check_nocow_unlock() if we return > 0.
979 int btrfs_check_nocow_lock(struct btrfs_inode *inode, loff_t pos,
980 size_t *write_bytes, bool nowait)
982 struct btrfs_fs_info *fs_info = inode->root->fs_info;
983 struct btrfs_root *root = inode->root;
984 struct extent_state *cached_state = NULL;
985 u64 lockstart, lockend;
989 if (!(inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)))
992 if (!btrfs_drew_try_write_lock(&root->snapshot_lock))
995 lockstart = round_down(pos, fs_info->sectorsize);
996 lockend = round_up(pos + *write_bytes,
997 fs_info->sectorsize) - 1;
998 num_bytes = lockend - lockstart + 1;
1001 if (!btrfs_try_lock_ordered_range(inode, lockstart, lockend,
1003 btrfs_drew_write_unlock(&root->snapshot_lock);
1007 btrfs_lock_and_flush_ordered_range(inode, lockstart, lockend,
1010 ret = can_nocow_extent(inode, lockstart, &num_bytes, NULL, nowait);
1012 btrfs_drew_write_unlock(&root->snapshot_lock);
1014 *write_bytes = min_t(size_t, *write_bytes ,
1015 num_bytes - pos + lockstart);
1016 btrfs_unlock_extent(&inode->io_tree, lockstart, lockend, &cached_state);
1021 void btrfs_check_nocow_unlock(struct btrfs_inode *inode)
1023 btrfs_drew_write_unlock(&inode->root->snapshot_lock);
1026 int btrfs_write_check(struct kiocb *iocb, size_t count)
1028 struct file *file = iocb->ki_filp;
1029 struct inode *inode = file_inode(file);
1030 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
1031 loff_t pos = iocb->ki_pos;
1036 * Quickly bail out on NOWAIT writes if we don't have the nodatacow or
1037 * prealloc flags, as without those flags we always have to COW. We will
1038 * later check if we can really COW into the target range (using
1039 * can_nocow_extent() at btrfs_get_blocks_direct_write()).
1041 if ((iocb->ki_flags & IOCB_NOWAIT) &&
1042 !(BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)))
1045 ret = file_remove_privs(file);
1050 * We reserve space for updating the inode when we reserve space for the
1051 * extent we are going to write, so we will enospc out there. We don't
1052 * need to start yet another transaction to update the inode as we will
1053 * update the inode when we finish writing whatever data we write.
1055 if (!IS_NOCMTIME(inode)) {
1056 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
1057 inode_inc_iversion(inode);
1060 oldsize = i_size_read(inode);
1061 if (pos > oldsize) {
1062 /* Expand hole size to cover write data, preventing empty gap */
1063 loff_t end_pos = round_up(pos + count, fs_info->sectorsize);
1065 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, end_pos);
1073 static void release_space(struct btrfs_inode *inode, struct extent_changeset *data_reserved,
1074 u64 start, u64 len, bool only_release_metadata)
1079 if (only_release_metadata) {
1080 btrfs_check_nocow_unlock(inode);
1081 btrfs_delalloc_release_metadata(inode, len, true);
1083 const struct btrfs_fs_info *fs_info = inode->root->fs_info;
1085 btrfs_delalloc_release_space(inode, data_reserved,
1086 round_down(start, fs_info->sectorsize),
1092 * Reserve data and metadata space for this buffered write range.
1094 * Return >0 for the number of bytes reserved, which is always block aligned.
1095 * Return <0 for error.
1097 static ssize_t reserve_space(struct btrfs_inode *inode,
1098 struct extent_changeset **data_reserved,
1099 u64 start, size_t *len, bool nowait,
1100 bool *only_release_metadata)
1102 const struct btrfs_fs_info *fs_info = inode->root->fs_info;
1103 const unsigned int block_offset = (start & (fs_info->sectorsize - 1));
1104 size_t reserve_bytes;
1107 ret = btrfs_check_data_free_space(inode, data_reserved, start, *len, nowait);
1111 if (nowait && (ret == -ENOSPC || ret == -EAGAIN))
1115 * If we don't have to COW at the offset, reserve metadata only.
1116 * write_bytes may get smaller than requested here.
1118 can_nocow = btrfs_check_nocow_lock(inode, start, len, nowait);
1125 *only_release_metadata = true;
1128 reserve_bytes = round_up(*len + block_offset, fs_info->sectorsize);
1129 WARN_ON(reserve_bytes == 0);
1130 ret = btrfs_delalloc_reserve_metadata(inode, reserve_bytes,
1131 reserve_bytes, nowait);
1133 if (!*only_release_metadata)
1134 btrfs_free_reserved_data_space(inode, *data_reserved,
1137 btrfs_check_nocow_unlock(inode);
1139 if (nowait && ret == -ENOSPC)
1143 return reserve_bytes;
1146 /* Shrink the reserved data and metadata space from @reserved_len to @new_len. */
1147 static void shrink_reserved_space(struct btrfs_inode *inode,
1148 struct extent_changeset *data_reserved,
1149 u64 reserved_start, u64 reserved_len,
1150 u64 new_len, bool only_release_metadata)
1152 const u64 diff = reserved_len - new_len;
1154 ASSERT(new_len <= reserved_len);
1155 btrfs_delalloc_shrink_extents(inode, reserved_len, new_len);
1156 if (only_release_metadata)
1157 btrfs_delalloc_release_metadata(inode, diff, true);
1159 btrfs_delalloc_release_space(inode, data_reserved,
1160 reserved_start + new_len, diff, true);
1163 /* Calculate the maximum amount of bytes we can write into one folio. */
1164 static size_t calc_write_bytes(const struct btrfs_inode *inode,
1165 const struct iov_iter *iter, u64 start)
1167 const size_t max_folio_size = mapping_max_folio_size(inode->vfs_inode.i_mapping);
1169 return min(max_folio_size - (start & (max_folio_size - 1)),
1170 iov_iter_count(iter));
1174 * Do the heavy-lifting work to copy one range into one folio of the page cache.
1176 * Return > 0 in case we copied all bytes or just some of them.
1177 * Return 0 if no bytes were copied, in which case the caller should retry.
1178 * Return <0 on error.
1180 static int copy_one_range(struct btrfs_inode *inode, struct iov_iter *iter,
1181 struct extent_changeset **data_reserved, u64 start,
1184 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1185 struct extent_state *cached_state = NULL;
1186 size_t write_bytes = calc_write_bytes(inode, iter, start);
1188 const u64 reserved_start = round_down(start, fs_info->sectorsize);
1190 struct folio *folio = NULL;
1194 bool only_release_metadata = false;
1195 const unsigned int bdp_flags = (nowait ? BDP_ASYNC : 0);
1199 * Fault all pages before locking them in prepare_one_folio() to avoid
1202 if (unlikely(fault_in_iov_iter_readable(iter, write_bytes)))
1204 extent_changeset_release(*data_reserved);
1205 ret = reserve_space(inode, data_reserved, start, &write_bytes, nowait,
1206 &only_release_metadata);
1210 /* Write range must be inside the reserved range. */
1211 ASSERT(reserved_start <= start);
1212 ASSERT(start + write_bytes <= reserved_start + reserved_len);
1215 ret = balance_dirty_pages_ratelimited_flags(inode->vfs_inode.i_mapping,
1218 btrfs_delalloc_release_extents(inode, reserved_len);
1219 release_space(inode, *data_reserved, reserved_start, reserved_len,
1220 only_release_metadata);
1224 ret = prepare_one_folio(&inode->vfs_inode, &folio, start, write_bytes, false);
1226 btrfs_delalloc_release_extents(inode, reserved_len);
1227 release_space(inode, *data_reserved, reserved_start, reserved_len,
1228 only_release_metadata);
1233 * The reserved range goes beyond the current folio, shrink the reserved
1234 * space to the folio boundary.
1236 if (reserved_start + reserved_len > folio_pos(folio) + folio_size(folio)) {
1237 const u64 last_block = folio_pos(folio) + folio_size(folio);
1239 shrink_reserved_space(inode, *data_reserved, reserved_start,
1240 reserved_len, last_block - reserved_start,
1241 only_release_metadata);
1242 write_bytes = last_block - start;
1243 reserved_len = last_block - reserved_start;
1246 extents_locked = lock_and_cleanup_extent_if_need(inode, folio, start,
1247 write_bytes, &lockstart,
1250 if (extents_locked < 0) {
1251 if (!nowait && extents_locked == -EAGAIN)
1254 btrfs_delalloc_release_extents(inode, reserved_len);
1255 release_space(inode, *data_reserved, reserved_start, reserved_len,
1256 only_release_metadata);
1257 ret = extents_locked;
1261 copied = copy_folio_from_iter_atomic(folio, offset_in_folio(folio, start),
1263 flush_dcache_folio(folio);
1265 if (unlikely(copied < write_bytes)) {
1269 * The original write range doesn't need an uptodate folio as
1270 * the range is block aligned. But now a short copy happened.
1271 * We cannot handle it without an uptodate folio.
1273 * So just revert the range and we will retry.
1275 if (!folio_test_uptodate(folio)) {
1276 iov_iter_revert(iter, copied);
1280 /* No copied bytes, unlock, release reserved space and exit. */
1283 btrfs_unlock_extent(&inode->io_tree, lockstart, lockend,
1286 btrfs_free_extent_state(cached_state);
1287 btrfs_delalloc_release_extents(inode, reserved_len);
1288 release_space(inode, *data_reserved, reserved_start, reserved_len,
1289 only_release_metadata);
1290 btrfs_drop_folio(fs_info, folio, start, copied);
1294 /* Release the reserved space beyond the last block. */
1295 last_block = round_up(start + copied, fs_info->sectorsize);
1297 shrink_reserved_space(inode, *data_reserved, reserved_start,
1298 reserved_len, last_block - reserved_start,
1299 only_release_metadata);
1300 reserved_len = last_block - reserved_start;
1303 ret = btrfs_dirty_folio(inode, folio, start, copied, &cached_state,
1304 only_release_metadata);
1306 * If we have not locked the extent range, because the range's start
1307 * offset is >= i_size, we might still have a non-NULL cached extent
1308 * state, acquired while marking the extent range as delalloc through
1309 * btrfs_dirty_page(). Therefore free any possible cached extent state
1310 * to avoid a memory leak.
1313 btrfs_unlock_extent(&inode->io_tree, lockstart, lockend, &cached_state);
1315 btrfs_free_extent_state(cached_state);
1317 btrfs_delalloc_release_extents(inode, reserved_len);
1319 btrfs_drop_folio(fs_info, folio, start, copied);
1320 release_space(inode, *data_reserved, reserved_start, reserved_len,
1321 only_release_metadata);
1324 if (only_release_metadata)
1325 btrfs_check_nocow_unlock(inode);
1327 btrfs_drop_folio(fs_info, folio, start, copied);
1331 ssize_t btrfs_buffered_write(struct kiocb *iocb, struct iov_iter *iter)
1333 struct file *file = iocb->ki_filp;
1335 struct inode *inode = file_inode(file);
1336 struct extent_changeset *data_reserved = NULL;
1337 size_t num_written = 0;
1340 unsigned int ilock_flags = 0;
1341 const bool nowait = (iocb->ki_flags & IOCB_NOWAIT);
1344 ilock_flags |= BTRFS_ILOCK_TRY;
1346 ret = btrfs_inode_lock(BTRFS_I(inode), ilock_flags);
1351 * We can only trust the isize with inode lock held, or it can race with
1352 * other buffered writes and cause incorrect call of
1353 * pagecache_isize_extended() to overwrite existing data.
1355 old_isize = i_size_read(inode);
1357 ret = generic_write_checks(iocb, iter);
1361 ret = btrfs_write_check(iocb, ret);
1366 while (iov_iter_count(iter) > 0) {
1367 ret = copy_one_range(BTRFS_I(inode), iter, &data_reserved, pos, nowait);
1375 extent_changeset_free(data_reserved);
1376 if (num_written > 0) {
1377 pagecache_isize_extended(inode, old_isize, iocb->ki_pos);
1378 iocb->ki_pos += num_written;
1381 btrfs_inode_unlock(BTRFS_I(inode), ilock_flags);
1382 return num_written ? num_written : ret;
1385 static ssize_t btrfs_encoded_write(struct kiocb *iocb, struct iov_iter *from,
1386 const struct btrfs_ioctl_encoded_io_args *encoded)
1388 struct file *file = iocb->ki_filp;
1389 struct inode *inode = file_inode(file);
1393 btrfs_inode_lock(BTRFS_I(inode), 0);
1394 count = encoded->len;
1395 ret = generic_write_checks_count(iocb, &count);
1396 if (ret == 0 && count != encoded->len) {
1398 * The write got truncated by generic_write_checks_count(). We
1399 * can't do a partial encoded write.
1403 if (ret || encoded->len == 0)
1406 ret = btrfs_write_check(iocb, encoded->len);
1410 ret = btrfs_do_encoded_write(iocb, from, encoded);
1412 btrfs_inode_unlock(BTRFS_I(inode), 0);
1416 ssize_t btrfs_do_write_iter(struct kiocb *iocb, struct iov_iter *from,
1417 const struct btrfs_ioctl_encoded_io_args *encoded)
1419 struct file *file = iocb->ki_filp;
1420 struct btrfs_inode *inode = BTRFS_I(file_inode(file));
1421 ssize_t num_written, num_sync;
1424 * If the fs flips readonly due to some impossible error, although we
1425 * have opened a file as writable, we have to stop this write operation
1426 * to ensure consistency.
1428 if (BTRFS_FS_ERROR(inode->root->fs_info))
1431 if (encoded && (iocb->ki_flags & IOCB_NOWAIT))
1435 num_written = btrfs_encoded_write(iocb, from, encoded);
1436 num_sync = encoded->len;
1437 } else if (iocb->ki_flags & IOCB_DIRECT) {
1438 num_written = btrfs_direct_write(iocb, from);
1439 num_sync = num_written;
1441 num_written = btrfs_buffered_write(iocb, from);
1442 num_sync = num_written;
1445 btrfs_set_inode_last_sub_trans(inode);
1448 num_sync = generic_write_sync(iocb, num_sync);
1450 num_written = num_sync;
1456 static ssize_t btrfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1458 return btrfs_do_write_iter(iocb, from, NULL);
1461 int btrfs_release_file(struct inode *inode, struct file *filp)
1463 struct btrfs_file_private *private = filp->private_data;
1466 kfree(private->filldir_buf);
1467 btrfs_free_extent_state(private->llseek_cached_state);
1469 filp->private_data = NULL;
1473 * Set by setattr when we are about to truncate a file from a non-zero
1474 * size to a zero size. This tries to flush down new bytes that may
1475 * have been written if the application were using truncate to replace
1478 if (test_and_clear_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
1479 &BTRFS_I(inode)->runtime_flags))
1480 filemap_flush(inode->i_mapping);
1484 static int start_ordered_ops(struct btrfs_inode *inode, loff_t start, loff_t end)
1487 struct blk_plug plug;
1490 * This is only called in fsync, which would do synchronous writes, so
1491 * a plug can merge adjacent IOs as much as possible. Esp. in case of
1492 * multiple disks using raid profile, a large IO can be split to
1493 * several segments of stripe length (currently 64K).
1495 blk_start_plug(&plug);
1496 ret = btrfs_fdatawrite_range(inode, start, end);
1497 blk_finish_plug(&plug);
1502 static inline bool skip_inode_logging(const struct btrfs_log_ctx *ctx)
1504 struct btrfs_inode *inode = ctx->inode;
1505 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1507 if (btrfs_inode_in_log(inode, btrfs_get_fs_generation(fs_info)) &&
1508 list_empty(&ctx->ordered_extents))
1512 * If we are doing a fast fsync we can not bail out if the inode's
1513 * last_trans is <= then the last committed transaction, because we only
1514 * update the last_trans of the inode during ordered extent completion,
1515 * and for a fast fsync we don't wait for that, we only wait for the
1516 * writeback to complete.
1518 if (inode->last_trans <= btrfs_get_last_trans_committed(fs_info) &&
1519 (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) ||
1520 list_empty(&ctx->ordered_extents)))
1527 * fsync call for both files and directories. This logs the inode into
1528 * the tree log instead of forcing full commits whenever possible.
1530 * It needs to call filemap_fdatawait so that all ordered extent updates are
1531 * in the metadata btree are up to date for copying to the log.
1533 * It drops the inode mutex before doing the tree log commit. This is an
1534 * important optimization for directories because holding the mutex prevents
1535 * new operations on the dir while we write to disk.
1537 int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
1539 struct dentry *dentry = file_dentry(file);
1540 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
1541 struct btrfs_root *root = inode->root;
1542 struct btrfs_fs_info *fs_info = root->fs_info;
1543 struct btrfs_trans_handle *trans;
1544 struct btrfs_log_ctx ctx;
1548 bool skip_ilock = false;
1550 if (current->journal_info == BTRFS_TRANS_DIO_WRITE_STUB) {
1552 current->journal_info = NULL;
1553 btrfs_assert_inode_locked(inode);
1556 trace_btrfs_sync_file(file, datasync);
1558 btrfs_init_log_ctx(&ctx, inode);
1561 * Always set the range to a full range, otherwise we can get into
1562 * several problems, from missing file extent items to represent holes
1563 * when not using the NO_HOLES feature, to log tree corruption due to
1564 * races between hole detection during logging and completion of ordered
1565 * extents outside the range, to missing checksums due to ordered extents
1566 * for which we flushed only a subset of their pages.
1570 len = (u64)LLONG_MAX + 1;
1573 * We write the dirty pages in the range and wait until they complete
1574 * out of the ->i_mutex. If so, we can flush the dirty pages by
1575 * multi-task, and make the performance up. See
1576 * btrfs_wait_ordered_range for an explanation of the ASYNC check.
1578 ret = start_ordered_ops(inode, start, end);
1583 down_write(&inode->i_mmap_lock);
1585 btrfs_inode_lock(inode, BTRFS_ILOCK_MMAP);
1587 atomic_inc(&root->log_batch);
1590 * Before we acquired the inode's lock and the mmap lock, someone may
1591 * have dirtied more pages in the target range. We need to make sure
1592 * that writeback for any such pages does not start while we are logging
1593 * the inode, because if it does, any of the following might happen when
1594 * we are not doing a full inode sync:
1596 * 1) We log an extent after its writeback finishes but before its
1597 * checksums are added to the csum tree, leading to -EIO errors
1598 * when attempting to read the extent after a log replay.
1600 * 2) We can end up logging an extent before its writeback finishes.
1601 * Therefore after the log replay we will have a file extent item
1602 * pointing to an unwritten extent (and no data checksums as well).
1604 * So trigger writeback for any eventual new dirty pages and then we
1605 * wait for all ordered extents to complete below.
1607 ret = start_ordered_ops(inode, start, end);
1610 up_write(&inode->i_mmap_lock);
1612 btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
1617 * Always check for the full sync flag while holding the inode's lock,
1618 * to avoid races with other tasks. The flag must be either set all the
1619 * time during logging or always off all the time while logging.
1620 * We check the flag here after starting delalloc above, because when
1621 * running delalloc the full sync flag may be set if we need to drop
1622 * extra extent map ranges due to temporary memory allocation failures.
1624 full_sync = test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
1627 * We have to do this here to avoid the priority inversion of waiting on
1628 * IO of a lower priority task while holding a transaction open.
1630 * For a full fsync we wait for the ordered extents to complete while
1631 * for a fast fsync we wait just for writeback to complete, and then
1632 * attach the ordered extents to the transaction so that a transaction
1633 * commit waits for their completion, to avoid data loss if we fsync,
1634 * the current transaction commits before the ordered extents complete
1635 * and a power failure happens right after that.
1637 * For zoned filesystem, if a write IO uses a ZONE_APPEND command, the
1638 * logical address recorded in the ordered extent may change. We need
1639 * to wait for the IO to stabilize the logical address.
1641 if (full_sync || btrfs_is_zoned(fs_info)) {
1642 ret = btrfs_wait_ordered_range(inode, start, len);
1643 clear_bit(BTRFS_INODE_COW_WRITE_ERROR, &inode->runtime_flags);
1646 * Get our ordered extents as soon as possible to avoid doing
1647 * checksum lookups in the csum tree, and use instead the
1648 * checksums attached to the ordered extents.
1650 btrfs_get_ordered_extents_for_logging(inode, &ctx.ordered_extents);
1651 ret = filemap_fdatawait_range(inode->vfs_inode.i_mapping, start, end);
1653 goto out_release_extents;
1656 * Check and clear the BTRFS_INODE_COW_WRITE_ERROR now after
1657 * starting and waiting for writeback, because for buffered IO
1658 * it may have been set during the end IO callback
1659 * (end_bbio_data_write() -> btrfs_finish_ordered_extent()) in
1660 * case an error happened and we need to wait for ordered
1661 * extents to complete so that any extent maps that point to
1662 * unwritten locations are dropped and we don't log them.
1664 if (test_and_clear_bit(BTRFS_INODE_COW_WRITE_ERROR, &inode->runtime_flags))
1665 ret = btrfs_wait_ordered_range(inode, start, len);
1669 goto out_release_extents;
1671 atomic_inc(&root->log_batch);
1673 if (skip_inode_logging(&ctx)) {
1675 * We've had everything committed since the last time we were
1676 * modified so clear this flag in case it was set for whatever
1677 * reason, it's no longer relevant.
1679 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
1681 * An ordered extent might have started before and completed
1682 * already with io errors, in which case the inode was not
1683 * updated and we end up here. So check the inode's mapping
1684 * for any errors that might have happened since we last
1685 * checked called fsync.
1687 ret = filemap_check_wb_err(inode->vfs_inode.i_mapping, file->f_wb_err);
1688 goto out_release_extents;
1691 btrfs_init_log_ctx_scratch_eb(&ctx);
1694 * We use start here because we will need to wait on the IO to complete
1695 * in btrfs_sync_log, which could require joining a transaction (for
1696 * example checking cross references in the nocow path). If we use join
1697 * here we could get into a situation where we're waiting on IO to
1698 * happen that is blocked on a transaction trying to commit. With start
1699 * we inc the extwriter counter, so we wait for all extwriters to exit
1700 * before we start blocking joiners. This comment is to keep somebody
1701 * from thinking they are super smart and changing this to
1702 * btrfs_join_transaction *cough*Josef*cough*.
1704 trans = btrfs_start_transaction(root, 0);
1705 if (IS_ERR(trans)) {
1706 ret = PTR_ERR(trans);
1707 goto out_release_extents;
1709 trans->in_fsync = true;
1711 ret = btrfs_log_dentry_safe(trans, dentry, &ctx);
1713 * Scratch eb no longer needed, release before syncing log or commit
1714 * transaction, to avoid holding unnecessary memory during such long
1717 if (ctx.scratch_eb) {
1718 free_extent_buffer(ctx.scratch_eb);
1719 ctx.scratch_eb = NULL;
1721 btrfs_release_log_ctx_extents(&ctx);
1723 /* Fallthrough and commit/free transaction. */
1724 ret = BTRFS_LOG_FORCE_COMMIT;
1727 /* we've logged all the items and now have a consistent
1728 * version of the file in the log. It is possible that
1729 * someone will come in and modify the file, but that's
1730 * fine because the log is consistent on disk, and we
1731 * have references to all of the file's extents
1733 * It is possible that someone will come in and log the
1734 * file again, but that will end up using the synchronization
1735 * inside btrfs_sync_log to keep things safe.
1738 up_write(&inode->i_mmap_lock);
1740 btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
1742 if (ret == BTRFS_NO_LOG_SYNC) {
1743 ret = btrfs_end_transaction(trans);
1747 /* We successfully logged the inode, attempt to sync the log. */
1749 ret = btrfs_sync_log(trans, root, &ctx);
1751 ret = btrfs_end_transaction(trans);
1757 * At this point we need to commit the transaction because we had
1758 * btrfs_need_log_full_commit() or some other error.
1760 * If we didn't do a full sync we have to stop the trans handle, wait on
1761 * the ordered extents, start it again and commit the transaction. If
1762 * we attempt to wait on the ordered extents here we could deadlock with
1763 * something like fallocate() that is holding the extent lock trying to
1764 * start a transaction while some other thread is trying to commit the
1765 * transaction while we (fsync) are currently holding the transaction
1769 ret = btrfs_end_transaction(trans);
1772 ret = btrfs_wait_ordered_range(inode, start, len);
1777 * This is safe to use here because we're only interested in
1778 * making sure the transaction that had the ordered extents is
1779 * committed. We aren't waiting on anything past this point,
1780 * we're purely getting the transaction and committing it.
1782 trans = btrfs_attach_transaction_barrier(root);
1783 if (IS_ERR(trans)) {
1784 ret = PTR_ERR(trans);
1787 * We committed the transaction and there's no currently
1788 * running transaction, this means everything we care
1789 * about made it to disk and we are done.
1797 ret = btrfs_commit_transaction(trans);
1799 free_extent_buffer(ctx.scratch_eb);
1800 ASSERT(list_empty(&ctx.list));
1801 ASSERT(list_empty(&ctx.conflict_inodes));
1802 err = file_check_and_advance_wb_err(file);
1805 return ret > 0 ? -EIO : ret;
1807 out_release_extents:
1808 btrfs_release_log_ctx_extents(&ctx);
1810 up_write(&inode->i_mmap_lock);
1812 btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
1817 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1818 * called from a page fault handler when a page is first dirtied. Hence we must
1819 * be careful to check for EOF conditions here. We set the page up correctly
1820 * for a written page which means we get ENOSPC checking when writing into
1821 * holes and correct delalloc and unwritten extent mapping on filesystems that
1822 * support these features.
1824 * We are not allowed to take the i_mutex here so we have to play games to
1825 * protect against truncate races as the page could now be beyond EOF. Because
1826 * truncate_setsize() writes the inode size before removing pages, once we have
1827 * the page lock we can determine safely if the page is beyond EOF. If it is not
1828 * beyond EOF, then the page is guaranteed safe against truncation until we
1831 static vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
1833 struct page *page = vmf->page;
1834 struct folio *folio = page_folio(page);
1835 struct inode *inode = file_inode(vmf->vma->vm_file);
1836 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
1837 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1838 struct btrfs_ordered_extent *ordered;
1839 struct extent_state *cached_state = NULL;
1840 struct extent_changeset *data_reserved = NULL;
1841 unsigned long zero_start;
1843 size_t fsize = folio_size(folio);
1850 reserved_space = fsize;
1852 sb_start_pagefault(inode->i_sb);
1853 page_start = folio_pos(folio);
1854 page_end = page_start + folio_size(folio) - 1;
1858 * Reserving delalloc space after obtaining the page lock can lead to
1859 * deadlock. For example, if a dirty page is locked by this function
1860 * and the call to btrfs_delalloc_reserve_space() ends up triggering
1861 * dirty page write out, then the btrfs_writepages() function could
1862 * end up waiting indefinitely to get a lock on the page currently
1863 * being processed by btrfs_page_mkwrite() function.
1865 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
1866 page_start, reserved_space);
1870 ret = file_update_time(vmf->vma->vm_file);
1874 down_read(&BTRFS_I(inode)->i_mmap_lock);
1876 size = i_size_read(inode);
1878 if ((folio->mapping != inode->i_mapping) ||
1879 (page_start >= size)) {
1880 /* Page got truncated out from underneath us. */
1883 folio_wait_writeback(folio);
1885 btrfs_lock_extent(io_tree, page_start, page_end, &cached_state);
1886 ret = set_folio_extent_mapped(folio);
1888 btrfs_unlock_extent(io_tree, page_start, page_end, &cached_state);
1893 * We can't set the delalloc bits if there are pending ordered
1894 * extents. Drop our locks and wait for them to finish.
1896 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, fsize);
1898 btrfs_unlock_extent(io_tree, page_start, page_end, &cached_state);
1899 folio_unlock(folio);
1900 up_read(&BTRFS_I(inode)->i_mmap_lock);
1901 btrfs_start_ordered_extent(ordered);
1902 btrfs_put_ordered_extent(ordered);
1906 if (folio_contains(folio, (size - 1) >> PAGE_SHIFT)) {
1907 reserved_space = round_up(size - page_start, fs_info->sectorsize);
1908 if (reserved_space < fsize) {
1909 end = page_start + reserved_space - 1;
1910 btrfs_delalloc_release_space(BTRFS_I(inode),
1911 data_reserved, end + 1,
1912 fsize - reserved_space, true);
1917 * page_mkwrite gets called when the page is firstly dirtied after it's
1918 * faulted in, but write(2) could also dirty a page and set delalloc
1919 * bits, thus in this case for space account reason, we still need to
1920 * clear any delalloc bits within this page range since we have to
1921 * reserve data&meta space before lock_page() (see above comments).
1923 btrfs_clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
1924 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
1925 EXTENT_DEFRAG, &cached_state);
1927 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
1930 btrfs_unlock_extent(io_tree, page_start, page_end, &cached_state);
1934 /* Page is wholly or partially inside EOF. */
1935 if (page_start + folio_size(folio) > size)
1936 zero_start = offset_in_folio(folio, size);
1940 if (zero_start != fsize)
1941 folio_zero_range(folio, zero_start, folio_size(folio) - zero_start);
1943 btrfs_folio_clear_checked(fs_info, folio, page_start, fsize);
1944 btrfs_folio_set_dirty(fs_info, folio, page_start, end + 1 - page_start);
1945 btrfs_folio_set_uptodate(fs_info, folio, page_start, end + 1 - page_start);
1947 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
1949 btrfs_unlock_extent(io_tree, page_start, page_end, &cached_state);
1950 up_read(&BTRFS_I(inode)->i_mmap_lock);
1952 btrfs_delalloc_release_extents(BTRFS_I(inode), fsize);
1953 sb_end_pagefault(inode->i_sb);
1954 extent_changeset_free(data_reserved);
1955 return VM_FAULT_LOCKED;
1958 folio_unlock(folio);
1959 up_read(&BTRFS_I(inode)->i_mmap_lock);
1961 btrfs_delalloc_release_extents(BTRFS_I(inode), fsize);
1962 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
1963 reserved_space, true);
1964 extent_changeset_free(data_reserved);
1966 sb_end_pagefault(inode->i_sb);
1969 return vmf_error(ret);
1971 /* Make the VM retry the fault. */
1972 return VM_FAULT_NOPAGE;
1975 static const struct vm_operations_struct btrfs_file_vm_ops = {
1976 .fault = filemap_fault,
1977 .map_pages = filemap_map_pages,
1978 .page_mkwrite = btrfs_page_mkwrite,
1981 static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1983 struct address_space *mapping = filp->f_mapping;
1985 if (!mapping->a_ops->read_folio)
1988 file_accessed(filp);
1989 vma->vm_ops = &btrfs_file_vm_ops;
1994 static bool hole_mergeable(struct btrfs_inode *inode, struct extent_buffer *leaf,
1995 int slot, u64 start, u64 end)
1997 struct btrfs_file_extent_item *fi;
1998 struct btrfs_key key;
2000 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
2003 btrfs_item_key_to_cpu(leaf, &key, slot);
2004 if (key.objectid != btrfs_ino(inode) ||
2005 key.type != BTRFS_EXTENT_DATA_KEY)
2008 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
2010 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2013 if (btrfs_file_extent_disk_bytenr(leaf, fi))
2016 if (key.offset == end)
2018 if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
2023 static int fill_holes(struct btrfs_trans_handle *trans,
2024 struct btrfs_inode *inode,
2025 struct btrfs_path *path, u64 offset, u64 end)
2027 struct btrfs_fs_info *fs_info = trans->fs_info;
2028 struct btrfs_root *root = inode->root;
2029 struct extent_buffer *leaf;
2030 struct btrfs_file_extent_item *fi;
2031 struct extent_map *hole_em;
2032 struct btrfs_key key;
2035 if (btrfs_fs_incompat(fs_info, NO_HOLES))
2038 key.objectid = btrfs_ino(inode);
2039 key.type = BTRFS_EXTENT_DATA_KEY;
2040 key.offset = offset;
2042 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2045 * We should have dropped this offset, so if we find it then
2046 * something has gone horribly wrong.
2053 leaf = path->nodes[0];
2054 if (hole_mergeable(inode, leaf, path->slots[0] - 1, offset, end)) {
2058 fi = btrfs_item_ptr(leaf, path->slots[0],
2059 struct btrfs_file_extent_item);
2060 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
2062 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2063 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2064 btrfs_set_file_extent_offset(leaf, fi, 0);
2065 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2069 if (hole_mergeable(inode, leaf, path->slots[0], offset, end)) {
2072 key.offset = offset;
2073 btrfs_set_item_key_safe(trans, path, &key);
2074 fi = btrfs_item_ptr(leaf, path->slots[0],
2075 struct btrfs_file_extent_item);
2076 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
2078 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2079 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2080 btrfs_set_file_extent_offset(leaf, fi, 0);
2081 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2084 btrfs_release_path(path);
2086 ret = btrfs_insert_hole_extent(trans, root, btrfs_ino(inode), offset,
2092 btrfs_release_path(path);
2094 hole_em = btrfs_alloc_extent_map();
2096 btrfs_drop_extent_map_range(inode, offset, end - 1, false);
2097 btrfs_set_inode_full_sync(inode);
2099 hole_em->start = offset;
2100 hole_em->len = end - offset;
2101 hole_em->ram_bytes = hole_em->len;
2103 hole_em->disk_bytenr = EXTENT_MAP_HOLE;
2104 hole_em->disk_num_bytes = 0;
2105 hole_em->generation = trans->transid;
2107 ret = btrfs_replace_extent_map_range(inode, hole_em, true);
2108 btrfs_free_extent_map(hole_em);
2110 btrfs_set_inode_full_sync(inode);
2117 * Find a hole extent on given inode and change start/len to the end of hole
2118 * extent.(hole/vacuum extent whose em->start <= start &&
2119 * em->start + em->len > start)
2120 * When a hole extent is found, return 1 and modify start/len.
2122 static int find_first_non_hole(struct btrfs_inode *inode, u64 *start, u64 *len)
2124 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2125 struct extent_map *em;
2128 em = btrfs_get_extent(inode, NULL,
2129 round_down(*start, fs_info->sectorsize),
2130 round_up(*len, fs_info->sectorsize));
2134 /* Hole or vacuum extent(only exists in no-hole mode) */
2135 if (em->disk_bytenr == EXTENT_MAP_HOLE) {
2137 *len = em->start + em->len > *start + *len ?
2138 0 : *start + *len - em->start - em->len;
2139 *start = em->start + em->len;
2141 btrfs_free_extent_map(em);
2146 * Check if there is no folio in the range.
2148 * We cannot utilize filemap_range_has_page() in a filemap with large folios
2149 * as we can hit the following false positive:
2153 * |//|//|//|//| | | | | | | | |//|//|
2157 * That large folio A and B cover the start and end indexes.
2158 * In that case filemap_range_has_page() will always return true, but the above
2159 * case is fine for btrfs_punch_hole_lock_range() usage.
2161 * So here we only ensure that no other folios is in the range, excluding the
2162 * head/tail large folio.
2164 static bool check_range_has_page(struct inode *inode, u64 start, u64 end)
2166 struct folio_batch fbatch;
2169 * For subpage case, if the range is not at page boundary, we could
2170 * have pages at the leading/tailing part of the range.
2171 * This could lead to dead loop since filemap_range_has_page()
2172 * will always return true.
2173 * So here we need to do extra page alignment for
2174 * filemap_range_has_page().
2176 * And do not decrease page_lockend right now, as it can be 0.
2178 const u64 page_lockstart = round_up(start, PAGE_SIZE);
2179 const u64 page_lockend = round_down(end + 1, PAGE_SIZE);
2180 const pgoff_t start_index = page_lockstart >> PAGE_SHIFT;
2181 const pgoff_t end_index = (page_lockend - 1) >> PAGE_SHIFT;
2182 pgoff_t tmp = start_index;
2185 /* The same page or adjacent pages. */
2186 if (page_lockend <= page_lockstart)
2189 folio_batch_init(&fbatch);
2190 found_folios = filemap_get_folios(inode->i_mapping, &tmp, end_index, &fbatch);
2191 for (int i = 0; i < found_folios; i++) {
2192 struct folio *folio = fbatch.folios[i];
2194 /* A large folio begins before the start. Not a target. */
2195 if (folio->index < start_index)
2197 /* A large folio extends beyond the end. Not a target. */
2198 if (folio->index + folio_nr_pages(folio) > end_index)
2200 /* A folio doesn't cover the head/tail index. Found a target. */
2204 folio_batch_release(&fbatch);
2208 static void btrfs_punch_hole_lock_range(struct inode *inode,
2209 const u64 lockstart, const u64 lockend,
2210 struct extent_state **cached_state)
2213 truncate_pagecache_range(inode, lockstart, lockend);
2215 btrfs_lock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2218 * We can't have ordered extents in the range, nor dirty/writeback
2219 * pages, because we have locked the inode's VFS lock in exclusive
2220 * mode, we have locked the inode's i_mmap_lock in exclusive mode,
2221 * we have flushed all delalloc in the range and we have waited
2222 * for any ordered extents in the range to complete.
2223 * We can race with anyone reading pages from this range, so after
2224 * locking the range check if we have pages in the range, and if
2225 * we do, unlock the range and retry.
2227 if (!check_range_has_page(inode, lockstart, lockend))
2230 btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2234 btrfs_assert_inode_range_clean(BTRFS_I(inode), lockstart, lockend);
2237 static int btrfs_insert_replace_extent(struct btrfs_trans_handle *trans,
2238 struct btrfs_inode *inode,
2239 struct btrfs_path *path,
2240 struct btrfs_replace_extent_info *extent_info,
2241 const u64 replace_len,
2242 const u64 bytes_to_drop)
2244 struct btrfs_fs_info *fs_info = trans->fs_info;
2245 struct btrfs_root *root = inode->root;
2246 struct btrfs_file_extent_item *extent;
2247 struct extent_buffer *leaf;
2248 struct btrfs_key key;
2252 if (replace_len == 0)
2255 if (extent_info->disk_offset == 0 &&
2256 btrfs_fs_incompat(fs_info, NO_HOLES)) {
2257 btrfs_update_inode_bytes(inode, 0, bytes_to_drop);
2261 key.objectid = btrfs_ino(inode);
2262 key.type = BTRFS_EXTENT_DATA_KEY;
2263 key.offset = extent_info->file_offset;
2264 ret = btrfs_insert_empty_item(trans, root, path, &key,
2265 sizeof(struct btrfs_file_extent_item));
2268 leaf = path->nodes[0];
2269 slot = path->slots[0];
2270 write_extent_buffer(leaf, extent_info->extent_buf,
2271 btrfs_item_ptr_offset(leaf, slot),
2272 sizeof(struct btrfs_file_extent_item));
2273 extent = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
2274 ASSERT(btrfs_file_extent_type(leaf, extent) != BTRFS_FILE_EXTENT_INLINE);
2275 btrfs_set_file_extent_offset(leaf, extent, extent_info->data_offset);
2276 btrfs_set_file_extent_num_bytes(leaf, extent, replace_len);
2277 if (extent_info->is_new_extent)
2278 btrfs_set_file_extent_generation(leaf, extent, trans->transid);
2279 btrfs_release_path(path);
2281 ret = btrfs_inode_set_file_extent_range(inode, extent_info->file_offset,
2286 /* If it's a hole, nothing more needs to be done. */
2287 if (extent_info->disk_offset == 0) {
2288 btrfs_update_inode_bytes(inode, 0, bytes_to_drop);
2292 btrfs_update_inode_bytes(inode, replace_len, bytes_to_drop);
2294 if (extent_info->is_new_extent && extent_info->insertions == 0) {
2295 key.objectid = extent_info->disk_offset;
2296 key.type = BTRFS_EXTENT_ITEM_KEY;
2297 key.offset = extent_info->disk_len;
2298 ret = btrfs_alloc_reserved_file_extent(trans, root,
2300 extent_info->file_offset,
2301 extent_info->qgroup_reserved,
2304 struct btrfs_ref ref = {
2305 .action = BTRFS_ADD_DELAYED_REF,
2306 .bytenr = extent_info->disk_offset,
2307 .num_bytes = extent_info->disk_len,
2308 .owning_root = btrfs_root_id(root),
2309 .ref_root = btrfs_root_id(root),
2313 ref_offset = extent_info->file_offset - extent_info->data_offset;
2314 btrfs_init_data_ref(&ref, btrfs_ino(inode), ref_offset, 0, false);
2315 ret = btrfs_inc_extent_ref(trans, &ref);
2318 extent_info->insertions++;
2324 * The respective range must have been previously locked, as well as the inode.
2325 * The end offset is inclusive (last byte of the range).
2326 * @extent_info is NULL for fallocate's hole punching and non-NULL when replacing
2327 * the file range with an extent.
2328 * When not punching a hole, we don't want to end up in a state where we dropped
2329 * extents without inserting a new one, so we must abort the transaction to avoid
2332 int btrfs_replace_file_extents(struct btrfs_inode *inode,
2333 struct btrfs_path *path, const u64 start,
2335 struct btrfs_replace_extent_info *extent_info,
2336 struct btrfs_trans_handle **trans_out)
2338 struct btrfs_drop_extents_args drop_args = { 0 };
2339 struct btrfs_root *root = inode->root;
2340 struct btrfs_fs_info *fs_info = root->fs_info;
2341 u64 min_size = btrfs_calc_insert_metadata_size(fs_info, 1);
2342 u64 ino_size = round_up(inode->vfs_inode.i_size, fs_info->sectorsize);
2343 struct btrfs_trans_handle *trans = NULL;
2344 struct btrfs_block_rsv *rsv;
2345 unsigned int rsv_count;
2347 u64 len = end - start;
2353 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
2358 rsv->size = btrfs_calc_insert_metadata_size(fs_info, 1);
2359 rsv->failfast = true;
2362 * 1 - update the inode
2363 * 1 - removing the extents in the range
2364 * 1 - adding the hole extent if no_holes isn't set or if we are
2365 * replacing the range with a new extent
2367 if (!btrfs_fs_incompat(fs_info, NO_HOLES) || extent_info)
2372 trans = btrfs_start_transaction(root, rsv_count);
2373 if (IS_ERR(trans)) {
2374 ret = PTR_ERR(trans);
2379 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
2383 trans->block_rsv = rsv;
2386 drop_args.path = path;
2387 drop_args.end = end + 1;
2388 drop_args.drop_cache = true;
2389 while (cur_offset < end) {
2390 drop_args.start = cur_offset;
2391 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
2392 /* If we are punching a hole decrement the inode's byte count */
2394 btrfs_update_inode_bytes(inode, 0,
2395 drop_args.bytes_found);
2396 if (ret != -ENOSPC) {
2398 * The only time we don't want to abort is if we are
2399 * attempting to clone a partial inline extent, in which
2400 * case we'll get EOPNOTSUPP. However if we aren't
2401 * clone we need to abort no matter what, because if we
2402 * got EOPNOTSUPP via prealloc then we messed up and
2406 (ret != -EOPNOTSUPP ||
2407 (extent_info && extent_info->is_new_extent)))
2408 btrfs_abort_transaction(trans, ret);
2412 trans->block_rsv = &fs_info->trans_block_rsv;
2414 if (!extent_info && cur_offset < drop_args.drop_end &&
2415 cur_offset < ino_size) {
2416 ret = fill_holes(trans, inode, path, cur_offset,
2417 drop_args.drop_end);
2420 * If we failed then we didn't insert our hole
2421 * entries for the area we dropped, so now the
2422 * fs is corrupted, so we must abort the
2425 btrfs_abort_transaction(trans, ret);
2428 } else if (!extent_info && cur_offset < drop_args.drop_end) {
2430 * We are past the i_size here, but since we didn't
2431 * insert holes we need to clear the mapped area so we
2432 * know to not set disk_i_size in this area until a new
2433 * file extent is inserted here.
2435 ret = btrfs_inode_clear_file_extent_range(inode,
2437 drop_args.drop_end - cur_offset);
2440 * We couldn't clear our area, so we could
2441 * presumably adjust up and corrupt the fs, so
2444 btrfs_abort_transaction(trans, ret);
2450 drop_args.drop_end > extent_info->file_offset) {
2451 u64 replace_len = drop_args.drop_end -
2452 extent_info->file_offset;
2454 ret = btrfs_insert_replace_extent(trans, inode, path,
2455 extent_info, replace_len,
2456 drop_args.bytes_found);
2458 btrfs_abort_transaction(trans, ret);
2461 extent_info->data_len -= replace_len;
2462 extent_info->data_offset += replace_len;
2463 extent_info->file_offset += replace_len;
2467 * We are releasing our handle on the transaction, balance the
2468 * dirty pages of the btree inode and flush delayed items, and
2469 * then get a new transaction handle, which may now point to a
2470 * new transaction in case someone else may have committed the
2471 * transaction we used to replace/drop file extent items. So
2472 * bump the inode's iversion and update mtime and ctime except
2473 * if we are called from a dedupe context. This is because a
2474 * power failure/crash may happen after the transaction is
2475 * committed and before we finish replacing/dropping all the
2476 * file extent items we need.
2478 inode_inc_iversion(&inode->vfs_inode);
2480 if (!extent_info || extent_info->update_times)
2481 inode_set_mtime_to_ts(&inode->vfs_inode,
2482 inode_set_ctime_current(&inode->vfs_inode));
2484 ret = btrfs_update_inode(trans, inode);
2488 btrfs_end_transaction(trans);
2489 btrfs_btree_balance_dirty(fs_info);
2491 trans = btrfs_start_transaction(root, rsv_count);
2492 if (IS_ERR(trans)) {
2493 ret = PTR_ERR(trans);
2498 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
2499 rsv, min_size, false);
2502 trans->block_rsv = rsv;
2504 cur_offset = drop_args.drop_end;
2505 len = end - cur_offset;
2506 if (!extent_info && len) {
2507 ret = find_first_non_hole(inode, &cur_offset, &len);
2508 if (unlikely(ret < 0))
2518 * If we were cloning, force the next fsync to be a full one since we
2519 * we replaced (or just dropped in the case of cloning holes when
2520 * NO_HOLES is enabled) file extent items and did not setup new extent
2521 * maps for the replacement extents (or holes).
2523 if (extent_info && !extent_info->is_new_extent)
2524 btrfs_set_inode_full_sync(inode);
2529 trans->block_rsv = &fs_info->trans_block_rsv;
2531 * If we are using the NO_HOLES feature we might have had already an
2532 * hole that overlaps a part of the region [lockstart, lockend] and
2533 * ends at (or beyond) lockend. Since we have no file extent items to
2534 * represent holes, drop_end can be less than lockend and so we must
2535 * make sure we have an extent map representing the existing hole (the
2536 * call to __btrfs_drop_extents() might have dropped the existing extent
2537 * map representing the existing hole), otherwise the fast fsync path
2538 * will not record the existence of the hole region
2539 * [existing_hole_start, lockend].
2541 if (drop_args.drop_end <= end)
2542 drop_args.drop_end = end + 1;
2544 * Don't insert file hole extent item if it's for a range beyond eof
2545 * (because it's useless) or if it represents a 0 bytes range (when
2546 * cur_offset == drop_end).
2548 if (!extent_info && cur_offset < ino_size &&
2549 cur_offset < drop_args.drop_end) {
2550 ret = fill_holes(trans, inode, path, cur_offset,
2551 drop_args.drop_end);
2553 /* Same comment as above. */
2554 btrfs_abort_transaction(trans, ret);
2557 } else if (!extent_info && cur_offset < drop_args.drop_end) {
2558 /* See the comment in the loop above for the reasoning here. */
2559 ret = btrfs_inode_clear_file_extent_range(inode, cur_offset,
2560 drop_args.drop_end - cur_offset);
2562 btrfs_abort_transaction(trans, ret);
2568 ret = btrfs_insert_replace_extent(trans, inode, path,
2569 extent_info, extent_info->data_len,
2570 drop_args.bytes_found);
2572 btrfs_abort_transaction(trans, ret);
2581 trans->block_rsv = &fs_info->trans_block_rsv;
2583 btrfs_end_transaction(trans);
2587 btrfs_free_block_rsv(fs_info, rsv);
2592 static int btrfs_punch_hole(struct file *file, loff_t offset, loff_t len)
2594 struct inode *inode = file_inode(file);
2595 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
2596 struct btrfs_root *root = BTRFS_I(inode)->root;
2597 struct extent_state *cached_state = NULL;
2598 struct btrfs_path *path;
2599 struct btrfs_trans_handle *trans = NULL;
2604 const u64 orig_start = offset;
2605 const u64 orig_end = offset + len - 1;
2609 bool truncated_block = false;
2610 bool updated_inode = false;
2612 btrfs_inode_lock(BTRFS_I(inode), BTRFS_ILOCK_MMAP);
2614 ret = btrfs_wait_ordered_range(BTRFS_I(inode), offset, len);
2616 goto out_only_mutex;
2618 ino_size = round_up(inode->i_size, fs_info->sectorsize);
2619 ret = find_first_non_hole(BTRFS_I(inode), &offset, &len);
2621 goto out_only_mutex;
2623 /* Already in a large hole */
2625 goto out_only_mutex;
2628 ret = file_modified(file);
2630 goto out_only_mutex;
2632 lockstart = round_up(offset, fs_info->sectorsize);
2633 lockend = round_down(offset + len, fs_info->sectorsize) - 1;
2634 same_block = (BTRFS_BYTES_TO_BLKS(fs_info, offset))
2635 == (BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1));
2637 * Only do this if we are in the same block and we aren't doing the
2640 if (same_block && len < fs_info->sectorsize) {
2641 if (offset < ino_size) {
2642 truncated_block = true;
2643 ret = btrfs_truncate_block(BTRFS_I(inode), offset + len - 1,
2644 orig_start, orig_end);
2648 goto out_only_mutex;
2651 /* zero back part of the first block */
2652 if (offset < ino_size) {
2653 truncated_block = true;
2654 ret = btrfs_truncate_block(BTRFS_I(inode), offset, orig_start, orig_end);
2656 btrfs_inode_unlock(BTRFS_I(inode), BTRFS_ILOCK_MMAP);
2661 /* Check the aligned pages after the first unaligned page,
2662 * if offset != orig_start, which means the first unaligned page
2663 * including several following pages are already in holes,
2664 * the extra check can be skipped */
2665 if (offset == orig_start) {
2666 /* after truncate page, check hole again */
2667 len = offset + len - lockstart;
2669 ret = find_first_non_hole(BTRFS_I(inode), &offset, &len);
2671 goto out_only_mutex;
2674 goto out_only_mutex;
2679 /* Check the tail unaligned part is in a hole */
2680 tail_start = lockend + 1;
2681 tail_len = offset + len - tail_start;
2683 ret = find_first_non_hole(BTRFS_I(inode), &tail_start, &tail_len);
2684 if (unlikely(ret < 0))
2685 goto out_only_mutex;
2687 /* zero the front end of the last page */
2688 if (tail_start + tail_len < ino_size) {
2689 truncated_block = true;
2690 ret = btrfs_truncate_block(BTRFS_I(inode),
2691 tail_start + tail_len - 1,
2692 orig_start, orig_end);
2694 goto out_only_mutex;
2699 if (lockend < lockstart) {
2701 goto out_only_mutex;
2704 btrfs_punch_hole_lock_range(inode, lockstart, lockend, &cached_state);
2706 path = btrfs_alloc_path();
2712 ret = btrfs_replace_file_extents(BTRFS_I(inode), path, lockstart,
2713 lockend, NULL, &trans);
2714 btrfs_free_path(path);
2718 ASSERT(trans != NULL);
2719 inode_inc_iversion(inode);
2720 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2721 ret = btrfs_update_inode(trans, BTRFS_I(inode));
2722 updated_inode = true;
2723 btrfs_end_transaction(trans);
2724 btrfs_btree_balance_dirty(fs_info);
2726 btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2729 if (!updated_inode && truncated_block && !ret) {
2731 * If we only end up zeroing part of a page, we still need to
2732 * update the inode item, so that all the time fields are
2733 * updated as well as the necessary btrfs inode in memory fields
2734 * for detecting, at fsync time, if the inode isn't yet in the
2735 * log tree or it's there but not up to date.
2737 struct timespec64 now = inode_set_ctime_current(inode);
2739 inode_inc_iversion(inode);
2740 inode_set_mtime_to_ts(inode, now);
2741 trans = btrfs_start_transaction(root, 1);
2742 if (IS_ERR(trans)) {
2743 ret = PTR_ERR(trans);
2747 ret = btrfs_update_inode(trans, BTRFS_I(inode));
2748 ret2 = btrfs_end_transaction(trans);
2753 btrfs_inode_unlock(BTRFS_I(inode), BTRFS_ILOCK_MMAP);
2757 /* Helper structure to record which range is already reserved */
2758 struct falloc_range {
2759 struct list_head list;
2765 * Helper function to add falloc range
2767 * Caller should have locked the larger range of extent containing
2770 static int add_falloc_range(struct list_head *head, u64 start, u64 len)
2772 struct falloc_range *range = NULL;
2774 if (!list_empty(head)) {
2776 * As fallocate iterates by bytenr order, we only need to check
2779 range = list_last_entry(head, struct falloc_range, list);
2780 if (range->start + range->len == start) {
2786 range = kmalloc(sizeof(*range), GFP_KERNEL);
2789 range->start = start;
2791 list_add_tail(&range->list, head);
2795 static int btrfs_fallocate_update_isize(struct inode *inode,
2799 struct btrfs_trans_handle *trans;
2800 struct btrfs_root *root = BTRFS_I(inode)->root;
2804 if (mode & FALLOC_FL_KEEP_SIZE || end <= i_size_read(inode))
2807 trans = btrfs_start_transaction(root, 1);
2809 return PTR_ERR(trans);
2811 inode_set_ctime_current(inode);
2812 i_size_write(inode, end);
2813 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
2814 ret = btrfs_update_inode(trans, BTRFS_I(inode));
2815 ret2 = btrfs_end_transaction(trans);
2817 return ret ? ret : ret2;
2821 RANGE_BOUNDARY_WRITTEN_EXTENT,
2822 RANGE_BOUNDARY_PREALLOC_EXTENT,
2823 RANGE_BOUNDARY_HOLE,
2826 static int btrfs_zero_range_check_range_boundary(struct btrfs_inode *inode,
2829 const u64 sectorsize = inode->root->fs_info->sectorsize;
2830 struct extent_map *em;
2833 offset = round_down(offset, sectorsize);
2834 em = btrfs_get_extent(inode, NULL, offset, sectorsize);
2838 if (em->disk_bytenr == EXTENT_MAP_HOLE)
2839 ret = RANGE_BOUNDARY_HOLE;
2840 else if (em->flags & EXTENT_FLAG_PREALLOC)
2841 ret = RANGE_BOUNDARY_PREALLOC_EXTENT;
2843 ret = RANGE_BOUNDARY_WRITTEN_EXTENT;
2845 btrfs_free_extent_map(em);
2849 static int btrfs_zero_range(struct inode *inode,
2854 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2855 struct extent_map *em;
2856 struct extent_changeset *data_reserved = NULL;
2859 const u64 sectorsize = fs_info->sectorsize;
2860 const u64 orig_start = offset;
2861 const u64 orig_end = offset + len - 1;
2862 u64 alloc_start = round_down(offset, sectorsize);
2863 u64 alloc_end = round_up(offset + len, sectorsize);
2864 u64 bytes_to_reserve = 0;
2865 bool space_reserved = false;
2867 em = btrfs_get_extent(BTRFS_I(inode), NULL, alloc_start,
2868 alloc_end - alloc_start);
2875 * Avoid hole punching and extent allocation for some cases. More cases
2876 * could be considered, but these are unlikely common and we keep things
2877 * as simple as possible for now. Also, intentionally, if the target
2878 * range contains one or more prealloc extents together with regular
2879 * extents and holes, we drop all the existing extents and allocate a
2880 * new prealloc extent, so that we get a larger contiguous disk extent.
2882 if (em->start <= alloc_start && (em->flags & EXTENT_FLAG_PREALLOC)) {
2883 const u64 em_end = em->start + em->len;
2885 if (em_end >= offset + len) {
2887 * The whole range is already a prealloc extent,
2888 * do nothing except updating the inode's i_size if
2891 btrfs_free_extent_map(em);
2892 ret = btrfs_fallocate_update_isize(inode, offset + len,
2897 * Part of the range is already a prealloc extent, so operate
2898 * only on the remaining part of the range.
2900 alloc_start = em_end;
2901 ASSERT(IS_ALIGNED(alloc_start, sectorsize));
2902 len = offset + len - alloc_start;
2903 offset = alloc_start;
2904 alloc_hint = btrfs_extent_map_block_start(em) + em->len;
2906 btrfs_free_extent_map(em);
2908 if (BTRFS_BYTES_TO_BLKS(fs_info, offset) ==
2909 BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1)) {
2910 em = btrfs_get_extent(BTRFS_I(inode), NULL, alloc_start, sectorsize);
2916 if (em->flags & EXTENT_FLAG_PREALLOC) {
2917 btrfs_free_extent_map(em);
2918 ret = btrfs_fallocate_update_isize(inode, offset + len,
2922 if (len < sectorsize && em->disk_bytenr != EXTENT_MAP_HOLE) {
2923 btrfs_free_extent_map(em);
2924 ret = btrfs_truncate_block(BTRFS_I(inode), offset + len - 1,
2925 orig_start, orig_end);
2927 ret = btrfs_fallocate_update_isize(inode,
2932 btrfs_free_extent_map(em);
2933 alloc_start = round_down(offset, sectorsize);
2934 alloc_end = alloc_start + sectorsize;
2938 alloc_start = round_up(offset, sectorsize);
2939 alloc_end = round_down(offset + len, sectorsize);
2942 * For unaligned ranges, check the pages at the boundaries, they might
2943 * map to an extent, in which case we need to partially zero them, or
2944 * they might map to a hole, in which case we need our allocation range
2947 if (!IS_ALIGNED(offset, sectorsize)) {
2948 ret = btrfs_zero_range_check_range_boundary(BTRFS_I(inode),
2952 if (ret == RANGE_BOUNDARY_HOLE) {
2953 alloc_start = round_down(offset, sectorsize);
2955 } else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) {
2956 ret = btrfs_truncate_block(BTRFS_I(inode), offset,
2957 orig_start, orig_end);
2965 if (!IS_ALIGNED(offset + len, sectorsize)) {
2966 ret = btrfs_zero_range_check_range_boundary(BTRFS_I(inode),
2970 if (ret == RANGE_BOUNDARY_HOLE) {
2971 alloc_end = round_up(offset + len, sectorsize);
2973 } else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) {
2974 ret = btrfs_truncate_block(BTRFS_I(inode), offset + len - 1,
2975 orig_start, orig_end);
2984 if (alloc_start < alloc_end) {
2985 struct extent_state *cached_state = NULL;
2986 const u64 lockstart = alloc_start;
2987 const u64 lockend = alloc_end - 1;
2989 bytes_to_reserve = alloc_end - alloc_start;
2990 ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode),
2994 space_reserved = true;
2995 btrfs_punch_hole_lock_range(inode, lockstart, lockend,
2997 ret = btrfs_qgroup_reserve_data(BTRFS_I(inode), &data_reserved,
2998 alloc_start, bytes_to_reserve);
3000 btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, lockstart,
3001 lockend, &cached_state);
3004 ret = btrfs_prealloc_file_range(inode, mode, alloc_start,
3005 alloc_end - alloc_start,
3006 fs_info->sectorsize,
3007 offset + len, &alloc_hint);
3008 btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend,
3010 /* btrfs_prealloc_file_range releases reserved space on error */
3012 space_reserved = false;
3016 ret = btrfs_fallocate_update_isize(inode, offset + len, mode);
3018 if (ret && space_reserved)
3019 btrfs_free_reserved_data_space(BTRFS_I(inode), data_reserved,
3020 alloc_start, bytes_to_reserve);
3021 extent_changeset_free(data_reserved);
3026 static long btrfs_fallocate(struct file *file, int mode,
3027 loff_t offset, loff_t len)
3029 struct inode *inode = file_inode(file);
3030 struct extent_state *cached_state = NULL;
3031 struct extent_changeset *data_reserved = NULL;
3032 struct falloc_range *range;
3033 struct falloc_range *tmp;
3034 LIST_HEAD(reserve_list);
3042 u64 data_space_needed = 0;
3043 u64 data_space_reserved = 0;
3044 u64 qgroup_reserved = 0;
3045 struct extent_map *em;
3046 int blocksize = BTRFS_I(inode)->root->fs_info->sectorsize;
3049 /* Do not allow fallocate in ZONED mode */
3050 if (btrfs_is_zoned(inode_to_fs_info(inode)))
3053 alloc_start = round_down(offset, blocksize);
3054 alloc_end = round_up(offset + len, blocksize);
3055 cur_offset = alloc_start;
3057 /* Make sure we aren't being give some crap mode */
3058 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
3059 FALLOC_FL_ZERO_RANGE))
3062 if (mode & FALLOC_FL_PUNCH_HOLE)
3063 return btrfs_punch_hole(file, offset, len);
3065 btrfs_inode_lock(BTRFS_I(inode), BTRFS_ILOCK_MMAP);
3067 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) {
3068 ret = inode_newsize_ok(inode, offset + len);
3073 ret = file_modified(file);
3078 * TODO: Move these two operations after we have checked
3079 * accurate reserved space, or fallocate can still fail but
3080 * with page truncated or size expanded.
3082 * But that's a minor problem and won't do much harm BTW.
3084 if (alloc_start > inode->i_size) {
3085 ret = btrfs_cont_expand(BTRFS_I(inode), i_size_read(inode),
3089 } else if (offset + len > inode->i_size) {
3091 * If we are fallocating from the end of the file onward we
3092 * need to zero out the end of the block if i_size lands in the
3093 * middle of a block.
3095 ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size,
3096 inode->i_size, (u64)-1);
3102 * We have locked the inode at the VFS level (in exclusive mode) and we
3103 * have locked the i_mmap_lock lock (in exclusive mode). Now before
3104 * locking the file range, flush all dealloc in the range and wait for
3105 * all ordered extents in the range to complete. After this we can lock
3106 * the file range and, due to the previous locking we did, we know there
3107 * can't be more delalloc or ordered extents in the range.
3109 ret = btrfs_wait_ordered_range(BTRFS_I(inode), alloc_start,
3110 alloc_end - alloc_start);
3114 if (mode & FALLOC_FL_ZERO_RANGE) {
3115 ret = btrfs_zero_range(inode, offset, len, mode);
3116 btrfs_inode_unlock(BTRFS_I(inode), BTRFS_ILOCK_MMAP);
3120 locked_end = alloc_end - 1;
3121 btrfs_lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
3124 btrfs_assert_inode_range_clean(BTRFS_I(inode), alloc_start, locked_end);
3126 /* First, check if we exceed the qgroup limit */
3127 while (cur_offset < alloc_end) {
3128 em = btrfs_get_extent(BTRFS_I(inode), NULL, cur_offset,
3129 alloc_end - cur_offset);
3134 last_byte = min(btrfs_extent_map_end(em), alloc_end);
3135 actual_end = min_t(u64, btrfs_extent_map_end(em), offset + len);
3136 last_byte = ALIGN(last_byte, blocksize);
3137 if (em->disk_bytenr == EXTENT_MAP_HOLE ||
3138 (cur_offset >= inode->i_size &&
3139 !(em->flags & EXTENT_FLAG_PREALLOC))) {
3140 const u64 range_len = last_byte - cur_offset;
3142 ret = add_falloc_range(&reserve_list, cur_offset, range_len);
3144 btrfs_free_extent_map(em);
3147 ret = btrfs_qgroup_reserve_data(BTRFS_I(inode),
3148 &data_reserved, cur_offset, range_len);
3150 btrfs_free_extent_map(em);
3153 qgroup_reserved += range_len;
3154 data_space_needed += range_len;
3156 btrfs_free_extent_map(em);
3157 cur_offset = last_byte;
3160 if (!ret && data_space_needed > 0) {
3162 * We are safe to reserve space here as we can't have delalloc
3163 * in the range, see above.
3165 ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode),
3168 data_space_reserved = data_space_needed;
3172 * If ret is still 0, means we're OK to fallocate.
3173 * Or just cleanup the list and exit.
3175 list_for_each_entry_safe(range, tmp, &reserve_list, list) {
3177 ret = btrfs_prealloc_file_range(inode, mode,
3179 range->len, blocksize,
3180 offset + len, &alloc_hint);
3182 * btrfs_prealloc_file_range() releases space even
3183 * if it returns an error.
3185 data_space_reserved -= range->len;
3186 qgroup_reserved -= range->len;
3187 } else if (data_space_reserved > 0) {
3188 btrfs_free_reserved_data_space(BTRFS_I(inode),
3189 data_reserved, range->start,
3191 data_space_reserved -= range->len;
3192 qgroup_reserved -= range->len;
3193 } else if (qgroup_reserved > 0) {
3194 btrfs_qgroup_free_data(BTRFS_I(inode), data_reserved,
3195 range->start, range->len, NULL);
3196 qgroup_reserved -= range->len;
3198 list_del(&range->list);
3205 * We didn't need to allocate any more space, but we still extended the
3206 * size of the file so we need to update i_size and the inode item.
3208 ret = btrfs_fallocate_update_isize(inode, actual_end, mode);
3210 btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
3213 btrfs_inode_unlock(BTRFS_I(inode), BTRFS_ILOCK_MMAP);
3214 extent_changeset_free(data_reserved);
3219 * Helper for btrfs_find_delalloc_in_range(). Find a subrange in a given range
3220 * that has unflushed and/or flushing delalloc. There might be other adjacent
3221 * subranges after the one it found, so btrfs_find_delalloc_in_range() keeps
3222 * looping while it gets adjacent subranges, and merging them together.
3224 static bool find_delalloc_subrange(struct btrfs_inode *inode, u64 start, u64 end,
3225 struct extent_state **cached_state,
3226 bool *search_io_tree,
3227 u64 *delalloc_start_ret, u64 *delalloc_end_ret)
3229 u64 len = end + 1 - start;
3230 u64 delalloc_len = 0;
3231 struct btrfs_ordered_extent *oe;
3236 * Search the io tree first for EXTENT_DELALLOC. If we find any, it
3237 * means we have delalloc (dirty pages) for which writeback has not
3240 if (*search_io_tree) {
3241 spin_lock(&inode->lock);
3242 if (inode->delalloc_bytes > 0) {
3243 spin_unlock(&inode->lock);
3244 *delalloc_start_ret = start;
3245 delalloc_len = btrfs_count_range_bits(&inode->io_tree,
3246 delalloc_start_ret, end,
3247 len, EXTENT_DELALLOC, 1,
3250 spin_unlock(&inode->lock);
3254 if (delalloc_len > 0) {
3256 * If delalloc was found then *delalloc_start_ret has a sector size
3257 * aligned value (rounded down).
3259 *delalloc_end_ret = *delalloc_start_ret + delalloc_len - 1;
3261 if (*delalloc_start_ret == start) {
3262 /* Delalloc for the whole range, nothing more to do. */
3263 if (*delalloc_end_ret == end)
3265 /* Else trim our search range for ordered extents. */
3266 start = *delalloc_end_ret + 1;
3267 len = end + 1 - start;
3270 /* No delalloc, future calls don't need to search again. */
3271 *search_io_tree = false;
3275 * Now also check if there's any ordered extent in the range.
3276 * We do this because:
3278 * 1) When delalloc is flushed, the file range is locked, we clear the
3279 * EXTENT_DELALLOC bit from the io tree and create an extent map and
3280 * an ordered extent for the write. So we might just have been called
3281 * after delalloc is flushed and before the ordered extent completes
3282 * and inserts the new file extent item in the subvolume's btree;
3284 * 2) We may have an ordered extent created by flushing delalloc for a
3285 * subrange that starts before the subrange we found marked with
3286 * EXTENT_DELALLOC in the io tree.
3288 * We could also use the extent map tree to find such delalloc that is
3289 * being flushed, but using the ordered extents tree is more efficient
3290 * because it's usually much smaller as ordered extents are removed from
3291 * the tree once they complete. With the extent maps, we mau have them
3292 * in the extent map tree for a very long time, and they were either
3293 * created by previous writes or loaded by read operations.
3295 oe = btrfs_lookup_first_ordered_range(inode, start, len);
3297 return (delalloc_len > 0);
3299 /* The ordered extent may span beyond our search range. */
3300 oe_start = max(oe->file_offset, start);
3301 oe_end = min(oe->file_offset + oe->num_bytes - 1, end);
3303 btrfs_put_ordered_extent(oe);
3305 /* Don't have unflushed delalloc, return the ordered extent range. */
3306 if (delalloc_len == 0) {
3307 *delalloc_start_ret = oe_start;
3308 *delalloc_end_ret = oe_end;
3313 * We have both unflushed delalloc (io_tree) and an ordered extent.
3314 * If the ranges are adjacent returned a combined range, otherwise
3315 * return the leftmost range.
3317 if (oe_start < *delalloc_start_ret) {
3318 if (oe_end < *delalloc_start_ret)
3319 *delalloc_end_ret = oe_end;
3320 *delalloc_start_ret = oe_start;
3321 } else if (*delalloc_end_ret + 1 == oe_start) {
3322 *delalloc_end_ret = oe_end;
3329 * Check if there's delalloc in a given range.
3331 * @inode: The inode.
3332 * @start: The start offset of the range. It does not need to be
3333 * sector size aligned.
3334 * @end: The end offset (inclusive value) of the search range.
3335 * It does not need to be sector size aligned.
3336 * @cached_state: Extent state record used for speeding up delalloc
3337 * searches in the inode's io_tree. Can be NULL.
3338 * @delalloc_start_ret: Output argument, set to the start offset of the
3339 * subrange found with delalloc (may not be sector size
3341 * @delalloc_end_ret: Output argument, set to he end offset (inclusive value)
3342 * of the subrange found with delalloc.
3344 * Returns true if a subrange with delalloc is found within the given range, and
3345 * if so it sets @delalloc_start_ret and @delalloc_end_ret with the start and
3346 * end offsets of the subrange.
3348 bool btrfs_find_delalloc_in_range(struct btrfs_inode *inode, u64 start, u64 end,
3349 struct extent_state **cached_state,
3350 u64 *delalloc_start_ret, u64 *delalloc_end_ret)
3352 u64 cur_offset = round_down(start, inode->root->fs_info->sectorsize);
3353 u64 prev_delalloc_end = 0;
3354 bool search_io_tree = true;
3357 while (cur_offset <= end) {
3362 delalloc = find_delalloc_subrange(inode, cur_offset, end,
3363 cached_state, &search_io_tree,
3369 if (prev_delalloc_end == 0) {
3370 /* First subrange found. */
3371 *delalloc_start_ret = max(delalloc_start, start);
3372 *delalloc_end_ret = delalloc_end;
3374 } else if (delalloc_start == prev_delalloc_end + 1) {
3375 /* Subrange adjacent to the previous one, merge them. */
3376 *delalloc_end_ret = delalloc_end;
3378 /* Subrange not adjacent to the previous one, exit. */
3382 prev_delalloc_end = delalloc_end;
3383 cur_offset = delalloc_end + 1;
3391 * Check if there's a hole or delalloc range in a range representing a hole (or
3392 * prealloc extent) found in the inode's subvolume btree.
3394 * @inode: The inode.
3395 * @whence: Seek mode (SEEK_DATA or SEEK_HOLE).
3396 * @start: Start offset of the hole region. It does not need to be sector
3398 * @end: End offset (inclusive value) of the hole region. It does not
3399 * need to be sector size aligned.
3400 * @start_ret: Return parameter, used to set the start of the subrange in the
3401 * hole that matches the search criteria (seek mode), if such
3402 * subrange is found (return value of the function is true).
3403 * The value returned here may not be sector size aligned.
3405 * Returns true if a subrange matching the given seek mode is found, and if one
3406 * is found, it updates @start_ret with the start of the subrange.
3408 static bool find_desired_extent_in_hole(struct btrfs_inode *inode, int whence,
3409 struct extent_state **cached_state,
3410 u64 start, u64 end, u64 *start_ret)
3416 delalloc = btrfs_find_delalloc_in_range(inode, start, end, cached_state,
3417 &delalloc_start, &delalloc_end);
3418 if (delalloc && whence == SEEK_DATA) {
3419 *start_ret = delalloc_start;
3423 if (delalloc && whence == SEEK_HOLE) {
3425 * We found delalloc but it starts after out start offset. So we
3426 * have a hole between our start offset and the delalloc start.
3428 if (start < delalloc_start) {
3433 * Delalloc range starts at our start offset.
3434 * If the delalloc range's length is smaller than our range,
3435 * then it means we have a hole that starts where the delalloc
3438 if (delalloc_end < end) {
3439 *start_ret = delalloc_end + 1;
3443 /* There's delalloc for the whole range. */
3447 if (!delalloc && whence == SEEK_HOLE) {
3453 * No delalloc in the range and we are seeking for data. The caller has
3454 * to iterate to the next extent item in the subvolume btree.
3459 static loff_t find_desired_extent(struct file *file, loff_t offset, int whence)
3461 struct btrfs_inode *inode = BTRFS_I(file->f_mapping->host);
3462 struct btrfs_file_private *private;
3463 struct btrfs_fs_info *fs_info = inode->root->fs_info;
3464 struct extent_state *cached_state = NULL;
3465 struct extent_state **delalloc_cached_state;
3466 const loff_t i_size = i_size_read(&inode->vfs_inode);
3467 const u64 ino = btrfs_ino(inode);
3468 struct btrfs_root *root = inode->root;
3469 struct btrfs_path *path;
3470 struct btrfs_key key;
3471 u64 last_extent_end;
3478 if (i_size == 0 || offset >= i_size)
3482 * Quick path. If the inode has no prealloc extents and its number of
3483 * bytes used matches its i_size, then it can not have holes.
3485 if (whence == SEEK_HOLE &&
3486 !(inode->flags & BTRFS_INODE_PREALLOC) &&
3487 inode_get_bytes(&inode->vfs_inode) == i_size)
3490 spin_lock(&inode->lock);
3491 private = file->private_data;
3492 spin_unlock(&inode->lock);
3494 if (private && private->owner_task != current) {
3496 * Not allocated by us, don't use it as its cached state is used
3497 * by the task that allocated it and we don't want neither to
3498 * mess with it nor get incorrect results because it reflects an
3499 * invalid state for the current task.
3502 } else if (!private) {
3503 private = kzalloc(sizeof(*private), GFP_KERNEL);
3505 * No worries if memory allocation failed.
3506 * The private structure is used only for speeding up multiple
3507 * lseek SEEK_HOLE/DATA calls to a file when there's delalloc,
3508 * so everything will still be correct.
3513 private->owner_task = current;
3515 spin_lock(&inode->lock);
3516 if (file->private_data)
3519 file->private_data = private;
3520 spin_unlock(&inode->lock);
3530 delalloc_cached_state = &private->llseek_cached_state;
3532 delalloc_cached_state = NULL;
3535 * offset can be negative, in this case we start finding DATA/HOLE from
3536 * the very start of the file.
3538 start = max_t(loff_t, 0, offset);
3540 lockstart = round_down(start, fs_info->sectorsize);
3541 lockend = round_up(i_size, fs_info->sectorsize);
3542 if (lockend <= lockstart)
3543 lockend = lockstart + fs_info->sectorsize;
3546 path = btrfs_alloc_path();
3549 path->reada = READA_FORWARD;
3552 key.type = BTRFS_EXTENT_DATA_KEY;
3555 last_extent_end = lockstart;
3557 btrfs_lock_extent(&inode->io_tree, lockstart, lockend, &cached_state);
3559 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3562 } else if (ret > 0 && path->slots[0] > 0) {
3563 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
3564 if (key.objectid == ino && key.type == BTRFS_EXTENT_DATA_KEY)
3568 while (start < i_size) {
3569 struct extent_buffer *leaf = path->nodes[0];
3570 struct btrfs_file_extent_item *extent;
3574 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3575 ret = btrfs_next_leaf(root, path);
3581 leaf = path->nodes[0];
3584 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3585 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
3588 extent_end = btrfs_file_extent_end(path);
3591 * In the first iteration we may have a slot that points to an
3592 * extent that ends before our start offset, so skip it.
3594 if (extent_end <= start) {
3599 /* We have an implicit hole, NO_HOLES feature is likely set. */
3600 if (last_extent_end < key.offset) {
3601 u64 search_start = last_extent_end;
3605 * First iteration, @start matches @offset and it's
3608 if (start == offset)
3609 search_start = offset;
3611 found = find_desired_extent_in_hole(inode, whence,
3612 delalloc_cached_state,
3617 start = found_start;
3621 * Didn't find data or a hole (due to delalloc) in the
3622 * implicit hole range, so need to analyze the extent.
3626 extent = btrfs_item_ptr(leaf, path->slots[0],
3627 struct btrfs_file_extent_item);
3628 type = btrfs_file_extent_type(leaf, extent);
3631 * Can't access the extent's disk_bytenr field if this is an
3632 * inline extent, since at that offset, it's where the extent
3635 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
3636 (type == BTRFS_FILE_EXTENT_REG &&
3637 btrfs_file_extent_disk_bytenr(leaf, extent) == 0)) {
3639 * Explicit hole or prealloc extent, search for delalloc.
3640 * A prealloc extent is treated like a hole.
3642 u64 search_start = key.offset;
3646 * First iteration, @start matches @offset and it's
3649 if (start == offset)
3650 search_start = offset;
3652 found = find_desired_extent_in_hole(inode, whence,
3653 delalloc_cached_state,
3658 start = found_start;
3662 * Didn't find data or a hole (due to delalloc) in the
3663 * implicit hole range, so need to analyze the next
3668 * Found a regular or inline extent.
3669 * If we are seeking for data, adjust the start offset
3670 * and stop, we're done.
3672 if (whence == SEEK_DATA) {
3673 start = max_t(u64, key.offset, offset);
3678 * Else, we are seeking for a hole, check the next file
3684 last_extent_end = extent_end;
3686 if (fatal_signal_pending(current)) {
3693 /* We have an implicit hole from the last extent found up to i_size. */
3694 if (!found && start < i_size) {
3695 found = find_desired_extent_in_hole(inode, whence,
3696 delalloc_cached_state, start,
3697 i_size - 1, &start);
3703 btrfs_unlock_extent(&inode->io_tree, lockstart, lockend, &cached_state);
3704 btrfs_free_path(path);
3709 if (whence == SEEK_DATA && start >= i_size)
3712 return min_t(loff_t, start, i_size);
3715 static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)
3717 struct inode *inode = file->f_mapping->host;
3721 return generic_file_llseek(file, offset, whence);
3724 btrfs_inode_lock(BTRFS_I(inode), BTRFS_ILOCK_SHARED);
3725 offset = find_desired_extent(file, offset, whence);
3726 btrfs_inode_unlock(BTRFS_I(inode), BTRFS_ILOCK_SHARED);
3733 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3736 static int btrfs_file_open(struct inode *inode, struct file *filp)
3740 filp->f_mode |= FMODE_NOWAIT | FMODE_CAN_ODIRECT;
3742 ret = fsverity_file_open(inode, filp);
3745 return generic_file_open(inode, filp);
3748 static ssize_t btrfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
3752 if (iocb->ki_flags & IOCB_DIRECT) {
3753 ret = btrfs_direct_read(iocb, to);
3754 if (ret < 0 || !iov_iter_count(to) ||
3755 iocb->ki_pos >= i_size_read(file_inode(iocb->ki_filp)))
3759 return filemap_read(iocb, to, ret);
3762 const struct file_operations btrfs_file_operations = {
3763 .llseek = btrfs_file_llseek,
3764 .read_iter = btrfs_file_read_iter,
3765 .splice_read = filemap_splice_read,
3766 .write_iter = btrfs_file_write_iter,
3767 .splice_write = iter_file_splice_write,
3768 .mmap = btrfs_file_mmap,
3769 .open = btrfs_file_open,
3770 .release = btrfs_release_file,
3771 .get_unmapped_area = thp_get_unmapped_area,
3772 .fsync = btrfs_sync_file,
3773 .fallocate = btrfs_fallocate,
3774 .unlocked_ioctl = btrfs_ioctl,
3775 #ifdef CONFIG_COMPAT
3776 .compat_ioctl = btrfs_compat_ioctl,
3778 .remap_file_range = btrfs_remap_file_range,
3779 .uring_cmd = btrfs_uring_cmd,
3780 .fop_flags = FOP_BUFFER_RASYNC | FOP_BUFFER_WASYNC,
3783 int btrfs_fdatawrite_range(struct btrfs_inode *inode, loff_t start, loff_t end)
3785 struct address_space *mapping = inode->vfs_inode.i_mapping;
3789 * So with compression we will find and lock a dirty page and clear the
3790 * first one as dirty, setup an async extent, and immediately return
3791 * with the entire range locked but with nobody actually marked with
3792 * writeback. So we can't just filemap_write_and_wait_range() and
3793 * expect it to work since it will just kick off a thread to do the
3794 * actual work. So we need to call filemap_fdatawrite_range _again_
3795 * since it will wait on the page lock, which won't be unlocked until
3796 * after the pages have been marked as writeback and so we're good to go
3797 * from there. We have to do this otherwise we'll miss the ordered
3798 * extents and that results in badness. Please Josef, do not think you
3799 * know better and pull this out at some point in the future, it is
3800 * right and you are wrong.
3802 ret = filemap_fdatawrite_range(mapping, start, end);
3803 if (!ret && test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags))
3804 ret = filemap_fdatawrite_range(mapping, start, end);