1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <crypto/hash.h>
7 #include <linux/kernel.h>
9 #include <linux/blk-cgroup.h>
10 #include <linux/file.h>
12 #include <linux/pagemap.h>
13 #include <linux/highmem.h>
14 #include <linux/time.h>
15 #include <linux/init.h>
16 #include <linux/string.h>
17 #include <linux/backing-dev.h>
18 #include <linux/writeback.h>
19 #include <linux/compat.h>
20 #include <linux/xattr.h>
21 #include <linux/posix_acl.h>
22 #include <linux/falloc.h>
23 #include <linux/slab.h>
24 #include <linux/ratelimit.h>
25 #include <linux/btrfs.h>
26 #include <linux/blkdev.h>
27 #include <linux/posix_acl_xattr.h>
28 #include <linux/uio.h>
29 #include <linux/magic.h>
30 #include <linux/iversion.h>
31 #include <linux/swap.h>
32 #include <linux/migrate.h>
33 #include <linux/sched/mm.h>
34 #include <linux/iomap.h>
35 #include <asm/unaligned.h>
36 #include <linux/fsverity.h>
40 #include "transaction.h"
41 #include "btrfs_inode.h"
42 #include "ordered-data.h"
46 #include "compression.h"
50 #include "delalloc-space.h"
51 #include "block-group.h"
52 #include "space-info.h"
55 #include "inode-item.h"
57 #include "accessors.h"
58 #include "extent-tree.h"
59 #include "root-tree.h"
62 #include "file-item.h"
63 #include "uuid-tree.h"
67 #include "relocation.h"
72 #include "raid-stripe-tree.h"
74 struct btrfs_iget_args {
76 struct btrfs_root *root;
79 struct btrfs_dio_data {
81 struct extent_changeset *data_reserved;
82 struct btrfs_ordered_extent *ordered;
83 bool data_space_reserved;
87 struct btrfs_dio_private {
92 /* This must be last */
93 struct btrfs_bio bbio;
96 static struct bio_set btrfs_dio_bioset;
98 struct btrfs_rename_ctx {
99 /* Output field. Stores the index number of the old directory entry. */
104 * Used by data_reloc_print_warning_inode() to pass needed info for filename
105 * resolution and output of error message.
107 struct data_reloc_warn {
108 struct btrfs_path path;
109 struct btrfs_fs_info *fs_info;
110 u64 extent_item_size;
116 * For the file_extent_tree, we want to hold the inode lock when we lookup and
117 * update the disk_i_size, but lockdep will complain because our io_tree we hold
118 * the tree lock and get the inode lock when setting delalloc. These two things
119 * are unrelated, so make a class for the file_extent_tree so we don't get the
120 * two locking patterns mixed up.
122 static struct lock_class_key file_extent_tree_class;
124 static const struct inode_operations btrfs_dir_inode_operations;
125 static const struct inode_operations btrfs_symlink_inode_operations;
126 static const struct inode_operations btrfs_special_inode_operations;
127 static const struct inode_operations btrfs_file_inode_operations;
128 static const struct address_space_operations btrfs_aops;
129 static const struct file_operations btrfs_dir_file_operations;
131 static struct kmem_cache *btrfs_inode_cachep;
133 static int btrfs_setsize(struct inode *inode, struct iattr *attr);
134 static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback);
136 static noinline int run_delalloc_cow(struct btrfs_inode *inode,
137 struct page *locked_page, u64 start,
138 u64 end, struct writeback_control *wbc,
140 static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
141 u64 len, u64 orig_start, u64 block_start,
142 u64 block_len, u64 orig_block_len,
143 u64 ram_bytes, int compress_type,
146 static int data_reloc_print_warning_inode(u64 inum, u64 offset, u64 num_bytes,
147 u64 root, void *warn_ctx)
149 struct data_reloc_warn *warn = warn_ctx;
150 struct btrfs_fs_info *fs_info = warn->fs_info;
151 struct extent_buffer *eb;
152 struct btrfs_inode_item *inode_item;
153 struct inode_fs_paths *ipath = NULL;
154 struct btrfs_root *local_root;
155 struct btrfs_key key;
156 unsigned int nofs_flag;
160 local_root = btrfs_get_fs_root(fs_info, root, true);
161 if (IS_ERR(local_root)) {
162 ret = PTR_ERR(local_root);
166 /* This makes the path point to (inum INODE_ITEM ioff). */
168 key.type = BTRFS_INODE_ITEM_KEY;
171 ret = btrfs_search_slot(NULL, local_root, &key, &warn->path, 0, 0);
173 btrfs_put_root(local_root);
174 btrfs_release_path(&warn->path);
178 eb = warn->path.nodes[0];
179 inode_item = btrfs_item_ptr(eb, warn->path.slots[0], struct btrfs_inode_item);
180 nlink = btrfs_inode_nlink(eb, inode_item);
181 btrfs_release_path(&warn->path);
183 nofs_flag = memalloc_nofs_save();
184 ipath = init_ipath(4096, local_root, &warn->path);
185 memalloc_nofs_restore(nofs_flag);
187 btrfs_put_root(local_root);
188 ret = PTR_ERR(ipath);
191 * -ENOMEM, not a critical error, just output an generic error
195 "checksum error at logical %llu mirror %u root %llu, inode %llu offset %llu",
196 warn->logical, warn->mirror_num, root, inum, offset);
199 ret = paths_from_inode(inum, ipath);
204 * We deliberately ignore the bit ipath might have been too small to
205 * hold all of the paths here
207 for (int i = 0; i < ipath->fspath->elem_cnt; i++) {
209 "checksum error at logical %llu mirror %u root %llu inode %llu offset %llu length %u links %u (path: %s)",
210 warn->logical, warn->mirror_num, root, inum, offset,
211 fs_info->sectorsize, nlink,
212 (char *)(unsigned long)ipath->fspath->val[i]);
215 btrfs_put_root(local_root);
221 "checksum error at logical %llu mirror %u root %llu inode %llu offset %llu, path resolving failed with ret=%d",
222 warn->logical, warn->mirror_num, root, inum, offset, ret);
229 * Do extra user-friendly error output (e.g. lookup all the affected files).
231 * Return true if we succeeded doing the backref lookup.
232 * Return false if such lookup failed, and has to fallback to the old error message.
234 static void print_data_reloc_error(const struct btrfs_inode *inode, u64 file_off,
235 const u8 *csum, const u8 *csum_expected,
238 struct btrfs_fs_info *fs_info = inode->root->fs_info;
239 struct btrfs_path path = { 0 };
240 struct btrfs_key found_key = { 0 };
241 struct extent_buffer *eb;
242 struct btrfs_extent_item *ei;
243 const u32 csum_size = fs_info->csum_size;
249 mutex_lock(&fs_info->reloc_mutex);
250 logical = btrfs_get_reloc_bg_bytenr(fs_info);
251 mutex_unlock(&fs_info->reloc_mutex);
253 if (logical == U64_MAX) {
254 btrfs_warn_rl(fs_info, "has data reloc tree but no running relocation");
255 btrfs_warn_rl(fs_info,
256 "csum failed root %lld ino %llu off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d",
257 btrfs_root_id(inode->root), btrfs_ino(inode), file_off,
258 CSUM_FMT_VALUE(csum_size, csum),
259 CSUM_FMT_VALUE(csum_size, csum_expected),
265 btrfs_warn_rl(fs_info,
266 "csum failed root %lld ino %llu off %llu logical %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d",
267 btrfs_root_id(inode->root),
268 btrfs_ino(inode), file_off, logical,
269 CSUM_FMT_VALUE(csum_size, csum),
270 CSUM_FMT_VALUE(csum_size, csum_expected),
273 ret = extent_from_logical(fs_info, logical, &path, &found_key, &flags);
275 btrfs_err_rl(fs_info, "failed to lookup extent item for logical %llu: %d",
280 ei = btrfs_item_ptr(eb, path.slots[0], struct btrfs_extent_item);
281 item_size = btrfs_item_size(eb, path.slots[0]);
282 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
283 unsigned long ptr = 0;
288 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
289 item_size, &ref_root,
292 btrfs_warn_rl(fs_info,
293 "failed to resolve tree backref for logical %llu: %d",
300 btrfs_warn_rl(fs_info,
301 "csum error at logical %llu mirror %u: metadata %s (level %d) in tree %llu",
303 (ref_level ? "node" : "leaf"),
304 ref_level, ref_root);
306 btrfs_release_path(&path);
308 struct btrfs_backref_walk_ctx ctx = { 0 };
309 struct data_reloc_warn reloc_warn = { 0 };
311 btrfs_release_path(&path);
313 ctx.bytenr = found_key.objectid;
314 ctx.extent_item_pos = logical - found_key.objectid;
315 ctx.fs_info = fs_info;
317 reloc_warn.logical = logical;
318 reloc_warn.extent_item_size = found_key.offset;
319 reloc_warn.mirror_num = mirror_num;
320 reloc_warn.fs_info = fs_info;
322 iterate_extent_inodes(&ctx, true,
323 data_reloc_print_warning_inode, &reloc_warn);
327 static void __cold btrfs_print_data_csum_error(struct btrfs_inode *inode,
328 u64 logical_start, u8 *csum, u8 *csum_expected, int mirror_num)
330 struct btrfs_root *root = inode->root;
331 const u32 csum_size = root->fs_info->csum_size;
333 /* For data reloc tree, it's better to do a backref lookup instead. */
334 if (btrfs_root_id(root) == BTRFS_DATA_RELOC_TREE_OBJECTID)
335 return print_data_reloc_error(inode, logical_start, csum,
336 csum_expected, mirror_num);
338 /* Output without objectid, which is more meaningful */
339 if (btrfs_root_id(root) >= BTRFS_LAST_FREE_OBJECTID) {
340 btrfs_warn_rl(root->fs_info,
341 "csum failed root %lld ino %lld off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d",
342 btrfs_root_id(root), btrfs_ino(inode),
344 CSUM_FMT_VALUE(csum_size, csum),
345 CSUM_FMT_VALUE(csum_size, csum_expected),
348 btrfs_warn_rl(root->fs_info,
349 "csum failed root %llu ino %llu off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d",
350 btrfs_root_id(root), btrfs_ino(inode),
352 CSUM_FMT_VALUE(csum_size, csum),
353 CSUM_FMT_VALUE(csum_size, csum_expected),
359 * Lock inode i_rwsem based on arguments passed.
361 * ilock_flags can have the following bit set:
363 * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode
364 * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt
366 * BTRFS_ILOCK_MMAP - acquire a write lock on the i_mmap_lock
368 int btrfs_inode_lock(struct btrfs_inode *inode, unsigned int ilock_flags)
370 if (ilock_flags & BTRFS_ILOCK_SHARED) {
371 if (ilock_flags & BTRFS_ILOCK_TRY) {
372 if (!inode_trylock_shared(&inode->vfs_inode))
377 inode_lock_shared(&inode->vfs_inode);
379 if (ilock_flags & BTRFS_ILOCK_TRY) {
380 if (!inode_trylock(&inode->vfs_inode))
385 inode_lock(&inode->vfs_inode);
387 if (ilock_flags & BTRFS_ILOCK_MMAP)
388 down_write(&inode->i_mmap_lock);
393 * Unock inode i_rwsem.
395 * ilock_flags should contain the same bits set as passed to btrfs_inode_lock()
396 * to decide whether the lock acquired is shared or exclusive.
398 void btrfs_inode_unlock(struct btrfs_inode *inode, unsigned int ilock_flags)
400 if (ilock_flags & BTRFS_ILOCK_MMAP)
401 up_write(&inode->i_mmap_lock);
402 if (ilock_flags & BTRFS_ILOCK_SHARED)
403 inode_unlock_shared(&inode->vfs_inode);
405 inode_unlock(&inode->vfs_inode);
409 * Cleanup all submitted ordered extents in specified range to handle errors
410 * from the btrfs_run_delalloc_range() callback.
412 * NOTE: caller must ensure that when an error happens, it can not call
413 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
414 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
415 * to be released, which we want to happen only when finishing the ordered
416 * extent (btrfs_finish_ordered_io()).
418 static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
419 struct page *locked_page,
420 u64 offset, u64 bytes)
422 unsigned long index = offset >> PAGE_SHIFT;
423 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
424 u64 page_start = 0, page_end = 0;
428 page_start = page_offset(locked_page);
429 page_end = page_start + PAGE_SIZE - 1;
432 while (index <= end_index) {
434 * For locked page, we will call btrfs_mark_ordered_io_finished
435 * through btrfs_mark_ordered_io_finished() on it
436 * in run_delalloc_range() for the error handling, which will
437 * clear page Ordered and run the ordered extent accounting.
439 * Here we can't just clear the Ordered bit, or
440 * btrfs_mark_ordered_io_finished() would skip the accounting
441 * for the page range, and the ordered extent will never finish.
443 if (locked_page && index == (page_start >> PAGE_SHIFT)) {
447 page = find_get_page(inode->vfs_inode.i_mapping, index);
453 * Here we just clear all Ordered bits for every page in the
454 * range, then btrfs_mark_ordered_io_finished() will handle
455 * the ordered extent accounting for the range.
457 btrfs_folio_clamp_clear_ordered(inode->root->fs_info,
458 page_folio(page), offset, bytes);
463 /* The locked page covers the full range, nothing needs to be done */
464 if (bytes + offset <= page_start + PAGE_SIZE)
467 * In case this page belongs to the delalloc range being
468 * instantiated then skip it, since the first page of a range is
469 * going to be properly cleaned up by the caller of
472 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
473 bytes = offset + bytes - page_offset(locked_page) - PAGE_SIZE;
474 offset = page_offset(locked_page) + PAGE_SIZE;
478 return btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes, false);
481 static int btrfs_dirty_inode(struct btrfs_inode *inode);
483 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
484 struct btrfs_new_inode_args *args)
488 if (args->default_acl) {
489 err = __btrfs_set_acl(trans, args->inode, args->default_acl,
495 err = __btrfs_set_acl(trans, args->inode, args->acl, ACL_TYPE_ACCESS);
499 if (!args->default_acl && !args->acl)
500 cache_no_acl(args->inode);
501 return btrfs_xattr_security_init(trans, args->inode, args->dir,
502 &args->dentry->d_name);
506 * this does all the hard work for inserting an inline extent into
507 * the btree. The caller should have done a btrfs_drop_extents so that
508 * no overlapping inline items exist in the btree
510 static int insert_inline_extent(struct btrfs_trans_handle *trans,
511 struct btrfs_path *path,
512 struct btrfs_inode *inode, bool extent_inserted,
513 size_t size, size_t compressed_size,
515 struct folio *compressed_folio,
518 struct btrfs_root *root = inode->root;
519 struct extent_buffer *leaf;
520 struct page *page = NULL;
521 const u32 sectorsize = trans->fs_info->sectorsize;
524 struct btrfs_file_extent_item *ei;
526 size_t cur_size = size;
530 * The decompressed size must still be no larger than a sector. Under
531 * heavy race, we can have size == 0 passed in, but that shouldn't be a
532 * big deal and we can continue the insertion.
534 ASSERT(size <= sectorsize);
537 * The compressed size also needs to be no larger than a sector.
538 * That's also why we only need one page as the parameter.
540 if (compressed_folio)
541 ASSERT(compressed_size <= sectorsize);
543 ASSERT(compressed_size == 0);
545 if (compressed_size && compressed_folio)
546 cur_size = compressed_size;
548 if (!extent_inserted) {
549 struct btrfs_key key;
552 key.objectid = btrfs_ino(inode);
554 key.type = BTRFS_EXTENT_DATA_KEY;
556 datasize = btrfs_file_extent_calc_inline_size(cur_size);
557 ret = btrfs_insert_empty_item(trans, root, path, &key,
562 leaf = path->nodes[0];
563 ei = btrfs_item_ptr(leaf, path->slots[0],
564 struct btrfs_file_extent_item);
565 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
566 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
567 btrfs_set_file_extent_encryption(leaf, ei, 0);
568 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
569 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
570 ptr = btrfs_file_extent_inline_start(ei);
572 if (compress_type != BTRFS_COMPRESS_NONE) {
573 kaddr = kmap_local_folio(compressed_folio, 0);
574 write_extent_buffer(leaf, kaddr, ptr, compressed_size);
577 btrfs_set_file_extent_compression(leaf, ei,
580 page = find_get_page(inode->vfs_inode.i_mapping, 0);
581 btrfs_set_file_extent_compression(leaf, ei, 0);
582 kaddr = kmap_local_page(page);
583 write_extent_buffer(leaf, kaddr, ptr, size);
587 btrfs_mark_buffer_dirty(trans, leaf);
588 btrfs_release_path(path);
591 * We align size to sectorsize for inline extents just for simplicity
594 ret = btrfs_inode_set_file_extent_range(inode, 0,
595 ALIGN(size, root->fs_info->sectorsize));
600 * We're an inline extent, so nobody can extend the file past i_size
601 * without locking a page we already have locked.
603 * We must do any i_size and inode updates before we unlock the pages.
604 * Otherwise we could end up racing with unlink.
606 i_size = i_size_read(&inode->vfs_inode);
607 if (update_i_size && size > i_size) {
608 i_size_write(&inode->vfs_inode, size);
611 inode->disk_i_size = i_size;
617 static bool can_cow_file_range_inline(struct btrfs_inode *inode,
618 u64 offset, u64 size,
619 size_t compressed_size)
621 struct btrfs_fs_info *fs_info = inode->root->fs_info;
622 u64 data_len = (compressed_size ?: size);
624 /* Inline extents must start at offset 0. */
629 * Due to the page size limit, for subpage we can only trigger the
630 * writeback for the dirty sectors of page, that means data writeback
631 * is doing more writeback than what we want.
633 * This is especially unexpected for some call sites like fallocate,
634 * where we only increase i_size after everything is done.
635 * This means we can trigger inline extent even if we didn't want to.
636 * So here we skip inline extent creation completely.
638 if (fs_info->sectorsize != PAGE_SIZE)
641 /* Inline extents are limited to sectorsize. */
642 if (size > fs_info->sectorsize)
645 /* We cannot exceed the maximum inline data size. */
646 if (data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
649 /* We cannot exceed the user specified max_inline size. */
650 if (data_len > fs_info->max_inline)
653 /* Inline extents must be the entirety of the file. */
654 if (size < i_size_read(&inode->vfs_inode))
661 * conditionally insert an inline extent into the file. This
662 * does the checks required to make sure the data is small enough
663 * to fit as an inline extent.
665 * If being used directly, you must have already checked we're allowed to cow
666 * the range by getting true from can_cow_file_range_inline().
668 static noinline int __cow_file_range_inline(struct btrfs_inode *inode, u64 offset,
669 u64 size, size_t compressed_size,
671 struct folio *compressed_folio,
674 struct btrfs_drop_extents_args drop_args = { 0 };
675 struct btrfs_root *root = inode->root;
676 struct btrfs_fs_info *fs_info = root->fs_info;
677 struct btrfs_trans_handle *trans;
678 u64 data_len = (compressed_size ?: size);
680 struct btrfs_path *path;
682 path = btrfs_alloc_path();
686 trans = btrfs_join_transaction(root);
688 btrfs_free_path(path);
689 return PTR_ERR(trans);
691 trans->block_rsv = &inode->block_rsv;
693 drop_args.path = path;
695 drop_args.end = fs_info->sectorsize;
696 drop_args.drop_cache = true;
697 drop_args.replace_extent = true;
698 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(data_len);
699 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
701 btrfs_abort_transaction(trans, ret);
705 ret = insert_inline_extent(trans, path, inode, drop_args.extent_inserted,
706 size, compressed_size, compress_type,
707 compressed_folio, update_i_size);
708 if (ret && ret != -ENOSPC) {
709 btrfs_abort_transaction(trans, ret);
711 } else if (ret == -ENOSPC) {
716 btrfs_update_inode_bytes(inode, size, drop_args.bytes_found);
717 ret = btrfs_update_inode(trans, inode);
718 if (ret && ret != -ENOSPC) {
719 btrfs_abort_transaction(trans, ret);
721 } else if (ret == -ENOSPC) {
726 btrfs_set_inode_full_sync(inode);
729 * Don't forget to free the reserved space, as for inlined extent
730 * it won't count as data extent, free them directly here.
731 * And at reserve time, it's always aligned to page size, so
732 * just free one page here.
734 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE, NULL);
735 btrfs_free_path(path);
736 btrfs_end_transaction(trans);
740 static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 offset,
742 size_t compressed_size,
744 struct folio *compressed_folio,
747 struct extent_state *cached = NULL;
748 unsigned long clear_flags = EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
749 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING | EXTENT_LOCKED;
750 u64 size = min_t(u64, i_size_read(&inode->vfs_inode), end + 1);
753 if (!can_cow_file_range_inline(inode, offset, size, compressed_size))
756 lock_extent(&inode->io_tree, offset, end, &cached);
757 ret = __cow_file_range_inline(inode, offset, size, compressed_size,
758 compress_type, compressed_folio,
761 unlock_extent(&inode->io_tree, offset, end, &cached);
765 extent_clear_unlock_delalloc(inode, offset, end, NULL, &cached,
767 PAGE_UNLOCK | PAGE_START_WRITEBACK |
772 struct async_extent {
776 struct folio **folios;
777 unsigned long nr_folios;
779 struct list_head list;
783 struct btrfs_inode *inode;
784 struct page *locked_page;
787 blk_opf_t write_flags;
788 struct list_head extents;
789 struct cgroup_subsys_state *blkcg_css;
790 struct btrfs_work work;
791 struct async_cow *async_cow;
796 struct async_chunk chunks[];
799 static noinline int add_async_extent(struct async_chunk *cow,
800 u64 start, u64 ram_size,
802 struct folio **folios,
803 unsigned long nr_folios,
806 struct async_extent *async_extent;
808 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
811 async_extent->start = start;
812 async_extent->ram_size = ram_size;
813 async_extent->compressed_size = compressed_size;
814 async_extent->folios = folios;
815 async_extent->nr_folios = nr_folios;
816 async_extent->compress_type = compress_type;
817 list_add_tail(&async_extent->list, &cow->extents);
822 * Check if the inode needs to be submitted to compression, based on mount
823 * options, defragmentation, properties or heuristics.
825 static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
828 struct btrfs_fs_info *fs_info = inode->root->fs_info;
830 if (!btrfs_inode_can_compress(inode)) {
831 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
832 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
837 * Special check for subpage.
839 * We lock the full page then run each delalloc range in the page, thus
840 * for the following case, we will hit some subpage specific corner case:
843 * | |///////| |///////|
846 * In above case, both range A and range B will try to unlock the full
847 * page [0, 64K), causing the one finished later will have page
848 * unlocked already, triggering various page lock requirement BUG_ON()s.
850 * So here we add an artificial limit that subpage compression can only
851 * if the range is fully page aligned.
853 * In theory we only need to ensure the first page is fully covered, but
854 * the tailing partial page will be locked until the full compression
855 * finishes, delaying the write of other range.
857 * TODO: Make btrfs_run_delalloc_range() to lock all delalloc range
858 * first to prevent any submitted async extent to unlock the full page.
859 * By this, we can ensure for subpage case that only the last async_cow
860 * will unlock the full page.
862 if (fs_info->sectorsize < PAGE_SIZE) {
863 if (!PAGE_ALIGNED(start) ||
864 !PAGE_ALIGNED(end + 1))
869 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
872 if (inode->defrag_compress)
874 /* bad compression ratios */
875 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
877 if (btrfs_test_opt(fs_info, COMPRESS) ||
878 inode->flags & BTRFS_INODE_COMPRESS ||
879 inode->prop_compress)
880 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
884 static inline void inode_should_defrag(struct btrfs_inode *inode,
885 u64 start, u64 end, u64 num_bytes, u32 small_write)
887 /* If this is a small write inside eof, kick off a defrag */
888 if (num_bytes < small_write &&
889 (start > 0 || end + 1 < inode->disk_i_size))
890 btrfs_add_inode_defrag(NULL, inode, small_write);
894 * Work queue call back to started compression on a file and pages.
896 * This is done inside an ordered work queue, and the compression is spread
897 * across many cpus. The actual IO submission is step two, and the ordered work
898 * queue takes care of making sure that happens in the same order things were
899 * put onto the queue by writepages and friends.
901 * If this code finds it can't get good compression, it puts an entry onto the
902 * work queue to write the uncompressed bytes. This makes sure that both
903 * compressed inodes and uncompressed inodes are written in the same order that
904 * the flusher thread sent them down.
906 static void compress_file_range(struct btrfs_work *work)
908 struct async_chunk *async_chunk =
909 container_of(work, struct async_chunk, work);
910 struct btrfs_inode *inode = async_chunk->inode;
911 struct btrfs_fs_info *fs_info = inode->root->fs_info;
912 struct address_space *mapping = inode->vfs_inode.i_mapping;
913 u64 blocksize = fs_info->sectorsize;
914 u64 start = async_chunk->start;
915 u64 end = async_chunk->end;
919 struct folio **folios;
920 unsigned long nr_folios;
921 unsigned long total_compressed = 0;
922 unsigned long total_in = 0;
925 int compress_type = fs_info->compress_type;
927 inode_should_defrag(inode, start, end, end - start + 1, SZ_16K);
930 * We need to call clear_page_dirty_for_io on each page in the range.
931 * Otherwise applications with the file mmap'd can wander in and change
932 * the page contents while we are compressing them.
934 extent_range_clear_dirty_for_io(&inode->vfs_inode, start, end);
937 * We need to save i_size before now because it could change in between
938 * us evaluating the size and assigning it. This is because we lock and
939 * unlock the page in truncate and fallocate, and then modify the i_size
942 * The barriers are to emulate READ_ONCE, remove that once i_size_read
946 i_size = i_size_read(&inode->vfs_inode);
948 actual_end = min_t(u64, i_size, end + 1);
951 nr_folios = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
952 nr_folios = min_t(unsigned long, nr_folios, BTRFS_MAX_COMPRESSED_PAGES);
955 * we don't want to send crud past the end of i_size through
956 * compression, that's just a waste of CPU time. So, if the
957 * end of the file is before the start of our current
958 * requested range of bytes, we bail out to the uncompressed
959 * cleanup code that can deal with all of this.
961 * It isn't really the fastest way to fix things, but this is a
962 * very uncommon corner.
964 if (actual_end <= start)
965 goto cleanup_and_bail_uncompressed;
967 total_compressed = actual_end - start;
970 * Skip compression for a small file range(<=blocksize) that
971 * isn't an inline extent, since it doesn't save disk space at all.
973 if (total_compressed <= blocksize &&
974 (start > 0 || end + 1 < inode->disk_i_size))
975 goto cleanup_and_bail_uncompressed;
978 * For subpage case, we require full page alignment for the sector
980 * Thus we must also check against @actual_end, not just @end.
982 if (blocksize < PAGE_SIZE) {
983 if (!PAGE_ALIGNED(start) ||
984 !PAGE_ALIGNED(round_up(actual_end, blocksize)))
985 goto cleanup_and_bail_uncompressed;
988 total_compressed = min_t(unsigned long, total_compressed,
989 BTRFS_MAX_UNCOMPRESSED);
994 * We do compression for mount -o compress and when the inode has not
995 * been flagged as NOCOMPRESS. This flag can change at any time if we
996 * discover bad compression ratios.
998 if (!inode_need_compress(inode, start, end))
999 goto cleanup_and_bail_uncompressed;
1001 folios = kcalloc(nr_folios, sizeof(struct folio *), GFP_NOFS);
1004 * Memory allocation failure is not a fatal error, we can fall
1005 * back to uncompressed code.
1007 goto cleanup_and_bail_uncompressed;
1010 if (inode->defrag_compress)
1011 compress_type = inode->defrag_compress;
1012 else if (inode->prop_compress)
1013 compress_type = inode->prop_compress;
1015 /* Compression level is applied here. */
1016 ret = btrfs_compress_folios(compress_type | (fs_info->compress_level << 4),
1017 mapping, start, folios, &nr_folios, &total_in,
1020 goto mark_incompressible;
1023 * Zero the tail end of the last page, as we might be sending it down
1026 poff = offset_in_page(total_compressed);
1028 folio_zero_range(folios[nr_folios - 1], poff, PAGE_SIZE - poff);
1031 * Try to create an inline extent.
1033 * If we didn't compress the entire range, try to create an uncompressed
1034 * inline extent, else a compressed one.
1036 * Check cow_file_range() for why we don't even try to create inline
1037 * extent for the subpage case.
1039 if (total_in < actual_end)
1040 ret = cow_file_range_inline(inode, start, end, 0,
1041 BTRFS_COMPRESS_NONE, NULL, false);
1043 ret = cow_file_range_inline(inode, start, end, total_compressed,
1044 compress_type, folios[0], false);
1047 mapping_set_error(mapping, -EIO);
1052 * We aren't doing an inline extent. Round the compressed size up to a
1053 * block size boundary so the allocator does sane things.
1055 total_compressed = ALIGN(total_compressed, blocksize);
1058 * One last check to make sure the compression is really a win, compare
1059 * the page count read with the blocks on disk, compression must free at
1062 total_in = round_up(total_in, fs_info->sectorsize);
1063 if (total_compressed + blocksize > total_in)
1064 goto mark_incompressible;
1067 * The async work queues will take care of doing actual allocation on
1068 * disk for these compressed pages, and will submit the bios.
1070 ret = add_async_extent(async_chunk, start, total_in, total_compressed, folios,
1071 nr_folios, compress_type);
1073 if (start + total_in < end) {
1080 mark_incompressible:
1081 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && !inode->prop_compress)
1082 inode->flags |= BTRFS_INODE_NOCOMPRESS;
1083 cleanup_and_bail_uncompressed:
1084 ret = add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
1085 BTRFS_COMPRESS_NONE);
1089 for (i = 0; i < nr_folios; i++) {
1090 WARN_ON(folios[i]->mapping);
1091 btrfs_free_compr_folio(folios[i]);
1097 static void free_async_extent_pages(struct async_extent *async_extent)
1101 if (!async_extent->folios)
1104 for (i = 0; i < async_extent->nr_folios; i++) {
1105 WARN_ON(async_extent->folios[i]->mapping);
1106 btrfs_free_compr_folio(async_extent->folios[i]);
1108 kfree(async_extent->folios);
1109 async_extent->nr_folios = 0;
1110 async_extent->folios = NULL;
1113 static void submit_uncompressed_range(struct btrfs_inode *inode,
1114 struct async_extent *async_extent,
1115 struct page *locked_page)
1117 u64 start = async_extent->start;
1118 u64 end = async_extent->start + async_extent->ram_size - 1;
1120 struct writeback_control wbc = {
1121 .sync_mode = WB_SYNC_ALL,
1122 .range_start = start,
1124 .no_cgroup_owner = 1,
1127 wbc_attach_fdatawrite_inode(&wbc, &inode->vfs_inode);
1128 ret = run_delalloc_cow(inode, locked_page, start, end, &wbc, false);
1129 wbc_detach_inode(&wbc);
1131 btrfs_cleanup_ordered_extents(inode, locked_page, start, end - start + 1);
1133 const u64 page_start = page_offset(locked_page);
1135 set_page_writeback(locked_page);
1136 end_page_writeback(locked_page);
1137 btrfs_mark_ordered_io_finished(inode, locked_page,
1138 page_start, PAGE_SIZE,
1140 mapping_set_error(locked_page->mapping, ret);
1141 unlock_page(locked_page);
1146 static void submit_one_async_extent(struct async_chunk *async_chunk,
1147 struct async_extent *async_extent,
1150 struct btrfs_inode *inode = async_chunk->inode;
1151 struct extent_io_tree *io_tree = &inode->io_tree;
1152 struct btrfs_root *root = inode->root;
1153 struct btrfs_fs_info *fs_info = root->fs_info;
1154 struct btrfs_ordered_extent *ordered;
1155 struct btrfs_key ins;
1156 struct page *locked_page = NULL;
1157 struct extent_state *cached = NULL;
1158 struct extent_map *em;
1160 u64 start = async_extent->start;
1161 u64 end = async_extent->start + async_extent->ram_size - 1;
1163 if (async_chunk->blkcg_css)
1164 kthread_associate_blkcg(async_chunk->blkcg_css);
1167 * If async_chunk->locked_page is in the async_extent range, we need to
1170 if (async_chunk->locked_page) {
1171 u64 locked_page_start = page_offset(async_chunk->locked_page);
1172 u64 locked_page_end = locked_page_start + PAGE_SIZE - 1;
1174 if (!(start >= locked_page_end || end <= locked_page_start))
1175 locked_page = async_chunk->locked_page;
1178 if (async_extent->compress_type == BTRFS_COMPRESS_NONE) {
1179 submit_uncompressed_range(inode, async_extent, locked_page);
1183 ret = btrfs_reserve_extent(root, async_extent->ram_size,
1184 async_extent->compressed_size,
1185 async_extent->compressed_size,
1186 0, *alloc_hint, &ins, 1, 1);
1189 * We can't reserve contiguous space for the compressed size.
1190 * Unlikely, but it's possible that we could have enough
1191 * non-contiguous space for the uncompressed size instead. So
1192 * fall back to uncompressed.
1194 submit_uncompressed_range(inode, async_extent, locked_page);
1198 lock_extent(io_tree, start, end, &cached);
1200 /* Here we're doing allocation and writeback of the compressed pages */
1201 em = create_io_em(inode, start,
1202 async_extent->ram_size, /* len */
1203 start, /* orig_start */
1204 ins.objectid, /* block_start */
1205 ins.offset, /* block_len */
1206 ins.offset, /* orig_block_len */
1207 async_extent->ram_size, /* ram_bytes */
1208 async_extent->compress_type,
1209 BTRFS_ORDERED_COMPRESSED);
1212 goto out_free_reserve;
1214 free_extent_map(em);
1216 ordered = btrfs_alloc_ordered_extent(inode, start, /* file_offset */
1217 async_extent->ram_size, /* num_bytes */
1218 async_extent->ram_size, /* ram_bytes */
1219 ins.objectid, /* disk_bytenr */
1220 ins.offset, /* disk_num_bytes */
1222 1 << BTRFS_ORDERED_COMPRESSED,
1223 async_extent->compress_type);
1224 if (IS_ERR(ordered)) {
1225 btrfs_drop_extent_map_range(inode, start, end, false);
1226 ret = PTR_ERR(ordered);
1227 goto out_free_reserve;
1229 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1231 /* Clear dirty, set writeback and unlock the pages. */
1232 extent_clear_unlock_delalloc(inode, start, end,
1233 NULL, &cached, EXTENT_LOCKED | EXTENT_DELALLOC,
1234 PAGE_UNLOCK | PAGE_START_WRITEBACK);
1235 btrfs_submit_compressed_write(ordered,
1236 async_extent->folios, /* compressed_folios */
1237 async_extent->nr_folios,
1238 async_chunk->write_flags, true);
1239 *alloc_hint = ins.objectid + ins.offset;
1241 if (async_chunk->blkcg_css)
1242 kthread_associate_blkcg(NULL);
1243 kfree(async_extent);
1247 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1248 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
1249 mapping_set_error(inode->vfs_inode.i_mapping, -EIO);
1250 extent_clear_unlock_delalloc(inode, start, end,
1252 EXTENT_LOCKED | EXTENT_DELALLOC |
1253 EXTENT_DELALLOC_NEW |
1254 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
1255 PAGE_UNLOCK | PAGE_START_WRITEBACK |
1256 PAGE_END_WRITEBACK);
1257 free_async_extent_pages(async_extent);
1258 if (async_chunk->blkcg_css)
1259 kthread_associate_blkcg(NULL);
1260 btrfs_debug(fs_info,
1261 "async extent submission failed root=%lld inode=%llu start=%llu len=%llu ret=%d",
1262 btrfs_root_id(root), btrfs_ino(inode), start,
1263 async_extent->ram_size, ret);
1264 kfree(async_extent);
1267 static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
1270 struct extent_map_tree *em_tree = &inode->extent_tree;
1271 struct extent_map *em;
1274 read_lock(&em_tree->lock);
1275 em = search_extent_mapping(em_tree, start, num_bytes);
1278 * if block start isn't an actual block number then find the
1279 * first block in this inode and use that as a hint. If that
1280 * block is also bogus then just don't worry about it.
1282 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1283 free_extent_map(em);
1284 em = search_extent_mapping(em_tree, 0, 0);
1285 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
1286 alloc_hint = em->block_start;
1288 free_extent_map(em);
1290 alloc_hint = em->block_start;
1291 free_extent_map(em);
1294 read_unlock(&em_tree->lock);
1300 * when extent_io.c finds a delayed allocation range in the file,
1301 * the call backs end up in this code. The basic idea is to
1302 * allocate extents on disk for the range, and create ordered data structs
1303 * in ram to track those extents.
1305 * locked_page is the page that writepage had locked already. We use
1306 * it to make sure we don't do extra locks or unlocks.
1308 * When this function fails, it unlocks all pages except @locked_page.
1310 * When this function successfully creates an inline extent, it returns 1 and
1311 * unlocks all pages including locked_page and starts I/O on them.
1312 * (In reality inline extents are limited to a single page, so locked_page is
1313 * the only page handled anyway).
1315 * When this function succeed and creates a normal extent, the page locking
1316 * status depends on the passed in flags:
1318 * - If @keep_locked is set, all pages are kept locked.
1319 * - Else all pages except for @locked_page are unlocked.
1321 * When a failure happens in the second or later iteration of the
1322 * while-loop, the ordered extents created in previous iterations are kept
1323 * intact. So, the caller must clean them up by calling
1324 * btrfs_cleanup_ordered_extents(). See btrfs_run_delalloc_range() for
1327 static noinline int cow_file_range(struct btrfs_inode *inode,
1328 struct page *locked_page, u64 start, u64 end,
1330 bool keep_locked, bool no_inline)
1332 struct btrfs_root *root = inode->root;
1333 struct btrfs_fs_info *fs_info = root->fs_info;
1334 struct extent_state *cached = NULL;
1336 u64 orig_start = start;
1338 unsigned long ram_size;
1339 u64 cur_alloc_size = 0;
1341 u64 blocksize = fs_info->sectorsize;
1342 struct btrfs_key ins;
1343 struct extent_map *em;
1344 unsigned clear_bits;
1345 unsigned long page_ops;
1346 bool extent_reserved = false;
1349 if (btrfs_is_free_space_inode(inode)) {
1354 num_bytes = ALIGN(end - start + 1, blocksize);
1355 num_bytes = max(blocksize, num_bytes);
1356 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
1358 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
1361 /* lets try to make an inline extent */
1362 ret = cow_file_range_inline(inode, start, end, 0,
1363 BTRFS_COMPRESS_NONE, NULL, false);
1366 * We succeeded, return 1 so the caller knows we're done
1367 * with this page and already handled the IO.
1369 * If there was an error then cow_file_range_inline() has
1370 * already done the cleanup.
1378 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1381 * Relocation relies on the relocated extents to have exactly the same
1382 * size as the original extents. Normally writeback for relocation data
1383 * extents follows a NOCOW path because relocation preallocates the
1384 * extents. However, due to an operation such as scrub turning a block
1385 * group to RO mode, it may fallback to COW mode, so we must make sure
1386 * an extent allocated during COW has exactly the requested size and can
1387 * not be split into smaller extents, otherwise relocation breaks and
1388 * fails during the stage where it updates the bytenr of file extent
1391 if (btrfs_is_data_reloc_root(root))
1392 min_alloc_size = num_bytes;
1394 min_alloc_size = fs_info->sectorsize;
1396 while (num_bytes > 0) {
1397 struct btrfs_ordered_extent *ordered;
1399 cur_alloc_size = num_bytes;
1400 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
1401 min_alloc_size, 0, alloc_hint,
1403 if (ret == -EAGAIN) {
1405 * btrfs_reserve_extent only returns -EAGAIN for zoned
1406 * file systems, which is an indication that there are
1407 * no active zones to allocate from at the moment.
1409 * If this is the first loop iteration, wait for at
1410 * least one zone to finish before retrying the
1411 * allocation. Otherwise ask the caller to write out
1412 * the already allocated blocks before coming back to
1413 * us, or return -ENOSPC if it can't handle retries.
1415 ASSERT(btrfs_is_zoned(fs_info));
1416 if (start == orig_start) {
1417 wait_on_bit_io(&inode->root->fs_info->flags,
1418 BTRFS_FS_NEED_ZONE_FINISH,
1419 TASK_UNINTERRUPTIBLE);
1423 *done_offset = start - 1;
1430 cur_alloc_size = ins.offset;
1431 extent_reserved = true;
1433 ram_size = ins.offset;
1435 lock_extent(&inode->io_tree, start, start + ram_size - 1,
1438 em = create_io_em(inode, start, ins.offset, /* len */
1439 start, /* orig_start */
1440 ins.objectid, /* block_start */
1441 ins.offset, /* block_len */
1442 ins.offset, /* orig_block_len */
1443 ram_size, /* ram_bytes */
1444 BTRFS_COMPRESS_NONE, /* compress_type */
1445 BTRFS_ORDERED_REGULAR /* type */);
1447 unlock_extent(&inode->io_tree, start,
1448 start + ram_size - 1, &cached);
1452 free_extent_map(em);
1454 ordered = btrfs_alloc_ordered_extent(inode, start, ram_size,
1455 ram_size, ins.objectid, cur_alloc_size,
1456 0, 1 << BTRFS_ORDERED_REGULAR,
1457 BTRFS_COMPRESS_NONE);
1458 if (IS_ERR(ordered)) {
1459 unlock_extent(&inode->io_tree, start,
1460 start + ram_size - 1, &cached);
1461 ret = PTR_ERR(ordered);
1462 goto out_drop_extent_cache;
1465 if (btrfs_is_data_reloc_root(root)) {
1466 ret = btrfs_reloc_clone_csums(ordered);
1469 * Only drop cache here, and process as normal.
1471 * We must not allow extent_clear_unlock_delalloc()
1472 * at out_unlock label to free meta of this ordered
1473 * extent, as its meta should be freed by
1474 * btrfs_finish_ordered_io().
1476 * So we must continue until @start is increased to
1477 * skip current ordered extent.
1480 btrfs_drop_extent_map_range(inode, start,
1481 start + ram_size - 1,
1484 btrfs_put_ordered_extent(ordered);
1486 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1489 * We're not doing compressed IO, don't unlock the first page
1490 * (which the caller expects to stay locked), don't clear any
1491 * dirty bits and don't set any writeback bits
1493 * Do set the Ordered (Private2) bit so we know this page was
1494 * properly setup for writepage.
1496 page_ops = (keep_locked ? 0 : PAGE_UNLOCK);
1497 page_ops |= PAGE_SET_ORDERED;
1499 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
1500 locked_page, &cached,
1501 EXTENT_LOCKED | EXTENT_DELALLOC,
1503 if (num_bytes < cur_alloc_size)
1506 num_bytes -= cur_alloc_size;
1507 alloc_hint = ins.objectid + ins.offset;
1508 start += cur_alloc_size;
1509 extent_reserved = false;
1512 * btrfs_reloc_clone_csums() error, since start is increased
1513 * extent_clear_unlock_delalloc() at out_unlock label won't
1514 * free metadata of current ordered extent, we're OK to exit.
1524 out_drop_extent_cache:
1525 btrfs_drop_extent_map_range(inode, start, start + ram_size - 1, false);
1527 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1528 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
1531 * Now, we have three regions to clean up:
1533 * |-------(1)----|---(2)---|-------------(3)----------|
1534 * `- orig_start `- start `- start + cur_alloc_size `- end
1536 * We process each region below.
1539 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1540 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
1541 page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK;
1544 * For the range (1). We have already instantiated the ordered extents
1545 * for this region. They are cleaned up by
1546 * btrfs_cleanup_ordered_extents() in e.g,
1547 * btrfs_run_delalloc_range(). EXTENT_LOCKED | EXTENT_DELALLOC are
1548 * already cleared in the above loop. And, EXTENT_DELALLOC_NEW |
1549 * EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV are handled by the cleanup
1552 * However, in case of @keep_locked, we still need to unlock the pages
1553 * (except @locked_page) to ensure all the pages are unlocked.
1555 if (keep_locked && orig_start < start) {
1557 mapping_set_error(inode->vfs_inode.i_mapping, ret);
1558 extent_clear_unlock_delalloc(inode, orig_start, start - 1,
1559 locked_page, NULL, 0, page_ops);
1563 * At this point we're unlocked, we want to make sure we're only
1564 * clearing these flags under the extent lock, so lock the rest of the
1565 * range and clear everything up.
1567 lock_extent(&inode->io_tree, start, end, NULL);
1570 * For the range (2). If we reserved an extent for our delalloc range
1571 * (or a subrange) and failed to create the respective ordered extent,
1572 * then it means that when we reserved the extent we decremented the
1573 * extent's size from the data space_info's bytes_may_use counter and
1574 * incremented the space_info's bytes_reserved counter by the same
1575 * amount. We must make sure extent_clear_unlock_delalloc() does not try
1576 * to decrement again the data space_info's bytes_may_use counter,
1577 * therefore we do not pass it the flag EXTENT_CLEAR_DATA_RESV.
1579 if (extent_reserved) {
1580 extent_clear_unlock_delalloc(inode, start,
1581 start + cur_alloc_size - 1,
1582 locked_page, &cached,
1585 start += cur_alloc_size;
1589 * For the range (3). We never touched the region. In addition to the
1590 * clear_bits above, we add EXTENT_CLEAR_DATA_RESV to release the data
1591 * space_info's bytes_may_use counter, reserved in
1592 * btrfs_check_data_free_space().
1595 clear_bits |= EXTENT_CLEAR_DATA_RESV;
1596 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1597 &cached, clear_bits, page_ops);
1603 * Phase two of compressed writeback. This is the ordered portion of the code,
1604 * which only gets called in the order the work was queued. We walk all the
1605 * async extents created by compress_file_range and send them down to the disk.
1607 * If called with @do_free == true then it'll try to finish the work and free
1608 * the work struct eventually.
1610 static noinline void submit_compressed_extents(struct btrfs_work *work, bool do_free)
1612 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1614 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
1615 struct async_extent *async_extent;
1616 unsigned long nr_pages;
1620 struct async_chunk *async_chunk;
1621 struct async_cow *async_cow;
1623 async_chunk = container_of(work, struct async_chunk, work);
1624 btrfs_add_delayed_iput(async_chunk->inode);
1625 if (async_chunk->blkcg_css)
1626 css_put(async_chunk->blkcg_css);
1628 async_cow = async_chunk->async_cow;
1629 if (atomic_dec_and_test(&async_cow->num_chunks))
1634 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
1637 while (!list_empty(&async_chunk->extents)) {
1638 async_extent = list_entry(async_chunk->extents.next,
1639 struct async_extent, list);
1640 list_del(&async_extent->list);
1641 submit_one_async_extent(async_chunk, async_extent, &alloc_hint);
1644 /* atomic_sub_return implies a barrier */
1645 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1647 cond_wake_up_nomb(&fs_info->async_submit_wait);
1650 static bool run_delalloc_compressed(struct btrfs_inode *inode,
1651 struct page *locked_page, u64 start,
1652 u64 end, struct writeback_control *wbc)
1654 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1655 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
1656 struct async_cow *ctx;
1657 struct async_chunk *async_chunk;
1658 unsigned long nr_pages;
1659 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1662 const blk_opf_t write_flags = wbc_to_write_flags(wbc);
1664 nofs_flag = memalloc_nofs_save();
1665 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1666 memalloc_nofs_restore(nofs_flag);
1670 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1672 async_chunk = ctx->chunks;
1673 atomic_set(&ctx->num_chunks, num_chunks);
1675 for (i = 0; i < num_chunks; i++) {
1676 u64 cur_end = min(end, start + SZ_512K - 1);
1679 * igrab is called higher up in the call chain, take only the
1680 * lightweight reference for the callback lifetime
1682 ihold(&inode->vfs_inode);
1683 async_chunk[i].async_cow = ctx;
1684 async_chunk[i].inode = inode;
1685 async_chunk[i].start = start;
1686 async_chunk[i].end = cur_end;
1687 async_chunk[i].write_flags = write_flags;
1688 INIT_LIST_HEAD(&async_chunk[i].extents);
1691 * The locked_page comes all the way from writepage and its
1692 * the original page we were actually given. As we spread
1693 * this large delalloc region across multiple async_chunk
1694 * structs, only the first struct needs a pointer to locked_page
1696 * This way we don't need racey decisions about who is supposed
1701 * Depending on the compressibility, the pages might or
1702 * might not go through async. We want all of them to
1703 * be accounted against wbc once. Let's do it here
1704 * before the paths diverge. wbc accounting is used
1705 * only for foreign writeback detection and doesn't
1706 * need full accuracy. Just account the whole thing
1707 * against the first page.
1709 wbc_account_cgroup_owner(wbc, locked_page,
1711 async_chunk[i].locked_page = locked_page;
1714 async_chunk[i].locked_page = NULL;
1717 if (blkcg_css != blkcg_root_css) {
1719 async_chunk[i].blkcg_css = blkcg_css;
1720 async_chunk[i].write_flags |= REQ_BTRFS_CGROUP_PUNT;
1722 async_chunk[i].blkcg_css = NULL;
1725 btrfs_init_work(&async_chunk[i].work, compress_file_range,
1726 submit_compressed_extents);
1728 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
1729 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
1731 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
1733 start = cur_end + 1;
1739 * Run the delalloc range from start to end, and write back any dirty pages
1740 * covered by the range.
1742 static noinline int run_delalloc_cow(struct btrfs_inode *inode,
1743 struct page *locked_page, u64 start,
1744 u64 end, struct writeback_control *wbc,
1747 u64 done_offset = end;
1750 while (start <= end) {
1751 ret = cow_file_range(inode, locked_page, start, end, &done_offset,
1755 extent_write_locked_range(&inode->vfs_inode, locked_page, start,
1756 done_offset, wbc, pages_dirty);
1757 start = done_offset + 1;
1763 static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
1764 const u64 start, const u64 end)
1766 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1767 const bool is_reloc_ino = btrfs_is_data_reloc_root(inode->root);
1768 const u64 range_bytes = end + 1 - start;
1769 struct extent_io_tree *io_tree = &inode->io_tree;
1770 struct extent_state *cached_state = NULL;
1771 u64 range_start = start;
1776 * If EXTENT_NORESERVE is set it means that when the buffered write was
1777 * made we had not enough available data space and therefore we did not
1778 * reserve data space for it, since we though we could do NOCOW for the
1779 * respective file range (either there is prealloc extent or the inode
1780 * has the NOCOW bit set).
1782 * However when we need to fallback to COW mode (because for example the
1783 * block group for the corresponding extent was turned to RO mode by a
1784 * scrub or relocation) we need to do the following:
1786 * 1) We increment the bytes_may_use counter of the data space info.
1787 * If COW succeeds, it allocates a new data extent and after doing
1788 * that it decrements the space info's bytes_may_use counter and
1789 * increments its bytes_reserved counter by the same amount (we do
1790 * this at btrfs_add_reserved_bytes()). So we need to increment the
1791 * bytes_may_use counter to compensate (when space is reserved at
1792 * buffered write time, the bytes_may_use counter is incremented);
1794 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1795 * that if the COW path fails for any reason, it decrements (through
1796 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1797 * data space info, which we incremented in the step above.
1799 * If we need to fallback to cow and the inode corresponds to a free
1800 * space cache inode or an inode of the data relocation tree, we must
1801 * also increment bytes_may_use of the data space_info for the same
1802 * reason. Space caches and relocated data extents always get a prealloc
1803 * extent for them, however scrub or balance may have set the block
1804 * group that contains that extent to RO mode and therefore force COW
1805 * when starting writeback.
1807 lock_extent(io_tree, start, end, &cached_state);
1808 count = count_range_bits(io_tree, &range_start, end, range_bytes,
1809 EXTENT_NORESERVE, 0, NULL);
1810 if (count > 0 || is_space_ino || is_reloc_ino) {
1812 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1813 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1815 if (is_space_ino || is_reloc_ino)
1816 bytes = range_bytes;
1818 spin_lock(&sinfo->lock);
1819 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
1820 spin_unlock(&sinfo->lock);
1823 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1826 unlock_extent(io_tree, start, end, &cached_state);
1829 * Don't try to create inline extents, as a mix of inline extent that
1830 * is written out and unlocked directly and a normal NOCOW extent
1833 ret = cow_file_range(inode, locked_page, start, end, NULL, false, true);
1838 struct can_nocow_file_extent_args {
1841 /* Start file offset of the range we want to NOCOW. */
1843 /* End file offset (inclusive) of the range we want to NOCOW. */
1845 bool writeback_path;
1848 * Free the path passed to can_nocow_file_extent() once it's not needed
1853 /* Output fields. Only set when can_nocow_file_extent() returns 1. */
1858 /* Number of bytes that can be written to in NOCOW mode. */
1863 * Check if we can NOCOW the file extent that the path points to.
1864 * This function may return with the path released, so the caller should check
1865 * if path->nodes[0] is NULL or not if it needs to use the path afterwards.
1867 * Returns: < 0 on error
1868 * 0 if we can not NOCOW
1871 static int can_nocow_file_extent(struct btrfs_path *path,
1872 struct btrfs_key *key,
1873 struct btrfs_inode *inode,
1874 struct can_nocow_file_extent_args *args)
1876 const bool is_freespace_inode = btrfs_is_free_space_inode(inode);
1877 struct extent_buffer *leaf = path->nodes[0];
1878 struct btrfs_root *root = inode->root;
1879 struct btrfs_file_extent_item *fi;
1880 struct btrfs_root *csum_root;
1885 bool nowait = path->nowait;
1887 fi = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
1888 extent_type = btrfs_file_extent_type(leaf, fi);
1890 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1893 /* Can't access these fields unless we know it's not an inline extent. */
1894 args->disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1895 args->disk_num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1896 args->extent_offset = btrfs_file_extent_offset(leaf, fi);
1898 if (!(inode->flags & BTRFS_INODE_NODATACOW) &&
1899 extent_type == BTRFS_FILE_EXTENT_REG)
1903 * If the extent was created before the generation where the last snapshot
1904 * for its subvolume was created, then this implies the extent is shared,
1905 * hence we must COW.
1907 if (!args->strict &&
1908 btrfs_file_extent_generation(leaf, fi) <=
1909 btrfs_root_last_snapshot(&root->root_item))
1912 /* An explicit hole, must COW. */
1913 if (args->disk_bytenr == 0)
1916 /* Compressed/encrypted/encoded extents must be COWed. */
1917 if (btrfs_file_extent_compression(leaf, fi) ||
1918 btrfs_file_extent_encryption(leaf, fi) ||
1919 btrfs_file_extent_other_encoding(leaf, fi))
1922 extent_end = btrfs_file_extent_end(path);
1925 * The following checks can be expensive, as they need to take other
1926 * locks and do btree or rbtree searches, so release the path to avoid
1927 * blocking other tasks for too long.
1929 btrfs_release_path(path);
1931 ret = btrfs_cross_ref_exist(root, btrfs_ino(inode),
1932 key->offset - args->extent_offset,
1933 args->disk_bytenr, args->strict, path);
1934 WARN_ON_ONCE(ret > 0 && is_freespace_inode);
1938 if (args->free_path) {
1940 * We don't need the path anymore, plus through the
1941 * btrfs_lookup_csums_list() call below we will end up allocating
1942 * another path. So free the path to avoid unnecessary extra
1945 btrfs_free_path(path);
1949 /* If there are pending snapshots for this root, we must COW. */
1950 if (args->writeback_path && !is_freespace_inode &&
1951 atomic_read(&root->snapshot_force_cow))
1954 args->disk_bytenr += args->extent_offset;
1955 args->disk_bytenr += args->start - key->offset;
1956 args->num_bytes = min(args->end + 1, extent_end) - args->start;
1959 * Force COW if csums exist in the range. This ensures that csums for a
1960 * given extent are either valid or do not exist.
1963 csum_root = btrfs_csum_root(root->fs_info, args->disk_bytenr);
1964 ret = btrfs_lookup_csums_list(csum_root, args->disk_bytenr,
1965 args->disk_bytenr + args->num_bytes - 1,
1967 WARN_ON_ONCE(ret > 0 && is_freespace_inode);
1973 if (args->free_path && path)
1974 btrfs_free_path(path);
1976 return ret < 0 ? ret : can_nocow;
1980 * when nowcow writeback call back. This checks for snapshots or COW copies
1981 * of the extents that exist in the file, and COWs the file as required.
1983 * If no cow copies or snapshots exist, we write directly to the existing
1986 static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
1987 struct page *locked_page,
1988 const u64 start, const u64 end)
1990 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1991 struct btrfs_root *root = inode->root;
1992 struct btrfs_path *path;
1993 u64 cow_start = (u64)-1;
1994 u64 cur_offset = start;
1996 bool check_prev = true;
1997 u64 ino = btrfs_ino(inode);
1998 struct can_nocow_file_extent_args nocow_args = { 0 };
2001 * Normally on a zoned device we're only doing COW writes, but in case
2002 * of relocation on a zoned filesystem serializes I/O so that we're only
2003 * writing sequentially and can end up here as well.
2005 ASSERT(!btrfs_is_zoned(fs_info) || btrfs_is_data_reloc_root(root));
2007 path = btrfs_alloc_path();
2013 nocow_args.end = end;
2014 nocow_args.writeback_path = true;
2016 while (cur_offset <= end) {
2017 struct btrfs_block_group *nocow_bg = NULL;
2018 struct btrfs_ordered_extent *ordered;
2019 struct btrfs_key found_key;
2020 struct btrfs_file_extent_item *fi;
2021 struct extent_buffer *leaf;
2022 struct extent_state *cached_state = NULL;
2029 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
2035 * If there is no extent for our range when doing the initial
2036 * search, then go back to the previous slot as it will be the
2037 * one containing the search offset
2039 if (ret > 0 && path->slots[0] > 0 && check_prev) {
2040 leaf = path->nodes[0];
2041 btrfs_item_key_to_cpu(leaf, &found_key,
2042 path->slots[0] - 1);
2043 if (found_key.objectid == ino &&
2044 found_key.type == BTRFS_EXTENT_DATA_KEY)
2049 /* Go to next leaf if we have exhausted the current one */
2050 leaf = path->nodes[0];
2051 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2052 ret = btrfs_next_leaf(root, path);
2057 leaf = path->nodes[0];
2060 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2062 /* Didn't find anything for our INO */
2063 if (found_key.objectid > ino)
2066 * Keep searching until we find an EXTENT_ITEM or there are no
2067 * more extents for this inode
2069 if (WARN_ON_ONCE(found_key.objectid < ino) ||
2070 found_key.type < BTRFS_EXTENT_DATA_KEY) {
2075 /* Found key is not EXTENT_DATA_KEY or starts after req range */
2076 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
2077 found_key.offset > end)
2081 * If the found extent starts after requested offset, then
2082 * adjust extent_end to be right before this extent begins
2084 if (found_key.offset > cur_offset) {
2085 extent_end = found_key.offset;
2091 * Found extent which begins before our range and potentially
2094 fi = btrfs_item_ptr(leaf, path->slots[0],
2095 struct btrfs_file_extent_item);
2096 extent_type = btrfs_file_extent_type(leaf, fi);
2097 /* If this is triggered then we have a memory corruption. */
2098 ASSERT(extent_type < BTRFS_NR_FILE_EXTENT_TYPES);
2099 if (WARN_ON(extent_type >= BTRFS_NR_FILE_EXTENT_TYPES)) {
2103 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
2104 extent_end = btrfs_file_extent_end(path);
2107 * If the extent we got ends before our current offset, skip to
2110 if (extent_end <= cur_offset) {
2115 nocow_args.start = cur_offset;
2116 ret = can_nocow_file_extent(path, &found_key, inode, &nocow_args);
2123 nocow_bg = btrfs_inc_nocow_writers(fs_info, nocow_args.disk_bytenr);
2127 * If we can't perform NOCOW writeback for the range,
2128 * then record the beginning of the range that needs to
2129 * be COWed. It will be written out before the next
2130 * NOCOW range if we find one, or when exiting this
2133 if (cow_start == (u64)-1)
2134 cow_start = cur_offset;
2135 cur_offset = extent_end;
2136 if (cur_offset > end)
2138 if (!path->nodes[0])
2145 * COW range from cow_start to found_key.offset - 1. As the key
2146 * will contain the beginning of the first extent that can be
2147 * NOCOW, following one which needs to be COW'ed
2149 if (cow_start != (u64)-1) {
2150 ret = fallback_to_cow(inode, locked_page,
2151 cow_start, found_key.offset - 1);
2152 cow_start = (u64)-1;
2154 btrfs_dec_nocow_writers(nocow_bg);
2159 nocow_end = cur_offset + nocow_args.num_bytes - 1;
2160 lock_extent(&inode->io_tree, cur_offset, nocow_end, &cached_state);
2162 is_prealloc = extent_type == BTRFS_FILE_EXTENT_PREALLOC;
2164 u64 orig_start = found_key.offset - nocow_args.extent_offset;
2165 struct extent_map *em;
2167 em = create_io_em(inode, cur_offset, nocow_args.num_bytes,
2169 nocow_args.disk_bytenr, /* block_start */
2170 nocow_args.num_bytes, /* block_len */
2171 nocow_args.disk_num_bytes, /* orig_block_len */
2172 ram_bytes, BTRFS_COMPRESS_NONE,
2173 BTRFS_ORDERED_PREALLOC);
2175 unlock_extent(&inode->io_tree, cur_offset,
2176 nocow_end, &cached_state);
2177 btrfs_dec_nocow_writers(nocow_bg);
2181 free_extent_map(em);
2184 ordered = btrfs_alloc_ordered_extent(inode, cur_offset,
2185 nocow_args.num_bytes, nocow_args.num_bytes,
2186 nocow_args.disk_bytenr, nocow_args.num_bytes, 0,
2188 ? (1 << BTRFS_ORDERED_PREALLOC)
2189 : (1 << BTRFS_ORDERED_NOCOW),
2190 BTRFS_COMPRESS_NONE);
2191 btrfs_dec_nocow_writers(nocow_bg);
2192 if (IS_ERR(ordered)) {
2194 btrfs_drop_extent_map_range(inode, cur_offset,
2197 unlock_extent(&inode->io_tree, cur_offset,
2198 nocow_end, &cached_state);
2199 ret = PTR_ERR(ordered);
2203 if (btrfs_is_data_reloc_root(root))
2205 * Error handled later, as we must prevent
2206 * extent_clear_unlock_delalloc() in error handler
2207 * from freeing metadata of created ordered extent.
2209 ret = btrfs_reloc_clone_csums(ordered);
2210 btrfs_put_ordered_extent(ordered);
2212 extent_clear_unlock_delalloc(inode, cur_offset, nocow_end,
2213 locked_page, &cached_state,
2214 EXTENT_LOCKED | EXTENT_DELALLOC |
2215 EXTENT_CLEAR_DATA_RESV,
2216 PAGE_UNLOCK | PAGE_SET_ORDERED);
2218 cur_offset = extent_end;
2221 * btrfs_reloc_clone_csums() error, now we're OK to call error
2222 * handler, as metadata for created ordered extent will only
2223 * be freed by btrfs_finish_ordered_io().
2228 btrfs_release_path(path);
2230 if (cur_offset <= end && cow_start == (u64)-1)
2231 cow_start = cur_offset;
2233 if (cow_start != (u64)-1) {
2235 ret = fallback_to_cow(inode, locked_page, cow_start, end);
2236 cow_start = (u64)-1;
2241 btrfs_free_path(path);
2246 * If an error happened while a COW region is outstanding, cur_offset
2247 * needs to be reset to cow_start to ensure the COW region is unlocked
2250 if (cow_start != (u64)-1)
2251 cur_offset = cow_start;
2254 * We need to lock the extent here because we're clearing DELALLOC and
2255 * we're not locked at this point.
2257 if (cur_offset < end) {
2258 struct extent_state *cached = NULL;
2260 lock_extent(&inode->io_tree, cur_offset, end, &cached);
2261 extent_clear_unlock_delalloc(inode, cur_offset, end,
2262 locked_page, &cached,
2263 EXTENT_LOCKED | EXTENT_DELALLOC |
2265 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
2266 PAGE_START_WRITEBACK |
2267 PAGE_END_WRITEBACK);
2269 btrfs_free_path(path);
2273 static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end)
2275 if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) {
2276 if (inode->defrag_bytes &&
2277 test_range_bit_exists(&inode->io_tree, start, end, EXTENT_DEFRAG))
2285 * Function to process delayed allocation (create CoW) for ranges which are
2286 * being touched for the first time.
2288 int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
2289 u64 start, u64 end, struct writeback_control *wbc)
2291 const bool zoned = btrfs_is_zoned(inode->root->fs_info);
2295 * The range must cover part of the @locked_page, or a return of 1
2296 * can confuse the caller.
2298 ASSERT(!(end <= page_offset(locked_page) ||
2299 start >= page_offset(locked_page) + PAGE_SIZE));
2301 if (should_nocow(inode, start, end)) {
2302 ret = run_delalloc_nocow(inode, locked_page, start, end);
2306 if (btrfs_inode_can_compress(inode) &&
2307 inode_need_compress(inode, start, end) &&
2308 run_delalloc_compressed(inode, locked_page, start, end, wbc))
2312 ret = run_delalloc_cow(inode, locked_page, start, end, wbc,
2315 ret = cow_file_range(inode, locked_page, start, end, NULL,
2320 btrfs_cleanup_ordered_extents(inode, locked_page, start,
2325 void btrfs_split_delalloc_extent(struct btrfs_inode *inode,
2326 struct extent_state *orig, u64 split)
2328 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2331 lockdep_assert_held(&inode->io_tree.lock);
2333 /* not delalloc, ignore it */
2334 if (!(orig->state & EXTENT_DELALLOC))
2337 size = orig->end - orig->start + 1;
2338 if (size > fs_info->max_extent_size) {
2343 * See the explanation in btrfs_merge_delalloc_extent, the same
2344 * applies here, just in reverse.
2346 new_size = orig->end - split + 1;
2347 num_extents = count_max_extents(fs_info, new_size);
2348 new_size = split - orig->start;
2349 num_extents += count_max_extents(fs_info, new_size);
2350 if (count_max_extents(fs_info, size) >= num_extents)
2354 spin_lock(&inode->lock);
2355 btrfs_mod_outstanding_extents(inode, 1);
2356 spin_unlock(&inode->lock);
2360 * Handle merged delayed allocation extents so we can keep track of new extents
2361 * that are just merged onto old extents, such as when we are doing sequential
2362 * writes, so we can properly account for the metadata space we'll need.
2364 void btrfs_merge_delalloc_extent(struct btrfs_inode *inode, struct extent_state *new,
2365 struct extent_state *other)
2367 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2368 u64 new_size, old_size;
2371 lockdep_assert_held(&inode->io_tree.lock);
2373 /* not delalloc, ignore it */
2374 if (!(other->state & EXTENT_DELALLOC))
2377 if (new->start > other->start)
2378 new_size = new->end - other->start + 1;
2380 new_size = other->end - new->start + 1;
2382 /* we're not bigger than the max, unreserve the space and go */
2383 if (new_size <= fs_info->max_extent_size) {
2384 spin_lock(&inode->lock);
2385 btrfs_mod_outstanding_extents(inode, -1);
2386 spin_unlock(&inode->lock);
2391 * We have to add up either side to figure out how many extents were
2392 * accounted for before we merged into one big extent. If the number of
2393 * extents we accounted for is <= the amount we need for the new range
2394 * then we can return, otherwise drop. Think of it like this
2398 * So we've grown the extent by a MAX_SIZE extent, this would mean we
2399 * need 2 outstanding extents, on one side we have 1 and the other side
2400 * we have 1 so they are == and we can return. But in this case
2402 * [MAX_SIZE+4k][MAX_SIZE+4k]
2404 * Each range on their own accounts for 2 extents, but merged together
2405 * they are only 3 extents worth of accounting, so we need to drop in
2408 old_size = other->end - other->start + 1;
2409 num_extents = count_max_extents(fs_info, old_size);
2410 old_size = new->end - new->start + 1;
2411 num_extents += count_max_extents(fs_info, old_size);
2412 if (count_max_extents(fs_info, new_size) >= num_extents)
2415 spin_lock(&inode->lock);
2416 btrfs_mod_outstanding_extents(inode, -1);
2417 spin_unlock(&inode->lock);
2420 static void btrfs_add_delalloc_inode(struct btrfs_inode *inode)
2422 struct btrfs_root *root = inode->root;
2423 struct btrfs_fs_info *fs_info = root->fs_info;
2425 spin_lock(&root->delalloc_lock);
2426 ASSERT(list_empty(&inode->delalloc_inodes));
2427 list_add_tail(&inode->delalloc_inodes, &root->delalloc_inodes);
2428 root->nr_delalloc_inodes++;
2429 if (root->nr_delalloc_inodes == 1) {
2430 spin_lock(&fs_info->delalloc_root_lock);
2431 ASSERT(list_empty(&root->delalloc_root));
2432 list_add_tail(&root->delalloc_root, &fs_info->delalloc_roots);
2433 spin_unlock(&fs_info->delalloc_root_lock);
2435 spin_unlock(&root->delalloc_lock);
2438 void btrfs_del_delalloc_inode(struct btrfs_inode *inode)
2440 struct btrfs_root *root = inode->root;
2441 struct btrfs_fs_info *fs_info = root->fs_info;
2443 lockdep_assert_held(&root->delalloc_lock);
2446 * We may be called after the inode was already deleted from the list,
2447 * namely in the transaction abort path btrfs_destroy_delalloc_inodes(),
2448 * and then later through btrfs_clear_delalloc_extent() while the inode
2449 * still has ->delalloc_bytes > 0.
2451 if (!list_empty(&inode->delalloc_inodes)) {
2452 list_del_init(&inode->delalloc_inodes);
2453 root->nr_delalloc_inodes--;
2454 if (!root->nr_delalloc_inodes) {
2455 ASSERT(list_empty(&root->delalloc_inodes));
2456 spin_lock(&fs_info->delalloc_root_lock);
2457 ASSERT(!list_empty(&root->delalloc_root));
2458 list_del_init(&root->delalloc_root);
2459 spin_unlock(&fs_info->delalloc_root_lock);
2465 * Properly track delayed allocation bytes in the inode and to maintain the
2466 * list of inodes that have pending delalloc work to be done.
2468 void btrfs_set_delalloc_extent(struct btrfs_inode *inode, struct extent_state *state,
2471 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2473 lockdep_assert_held(&inode->io_tree.lock);
2475 if ((bits & EXTENT_DEFRAG) && !(bits & EXTENT_DELALLOC))
2478 * set_bit and clear bit hooks normally require _irqsave/restore
2479 * but in this case, we are only testing for the DELALLOC
2480 * bit, which is only set or cleared with irqs on
2482 if (!(state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
2483 u64 len = state->end + 1 - state->start;
2484 u64 prev_delalloc_bytes;
2485 u32 num_extents = count_max_extents(fs_info, len);
2487 spin_lock(&inode->lock);
2488 btrfs_mod_outstanding_extents(inode, num_extents);
2489 spin_unlock(&inode->lock);
2491 /* For sanity tests */
2492 if (btrfs_is_testing(fs_info))
2495 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2496 fs_info->delalloc_batch);
2497 spin_lock(&inode->lock);
2498 prev_delalloc_bytes = inode->delalloc_bytes;
2499 inode->delalloc_bytes += len;
2500 if (bits & EXTENT_DEFRAG)
2501 inode->defrag_bytes += len;
2502 spin_unlock(&inode->lock);
2505 * We don't need to be under the protection of the inode's lock,
2506 * because we are called while holding the inode's io_tree lock
2507 * and are therefore protected against concurrent calls of this
2508 * function and btrfs_clear_delalloc_extent().
2510 if (!btrfs_is_free_space_inode(inode) && prev_delalloc_bytes == 0)
2511 btrfs_add_delalloc_inode(inode);
2514 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2515 (bits & EXTENT_DELALLOC_NEW)) {
2516 spin_lock(&inode->lock);
2517 inode->new_delalloc_bytes += state->end + 1 - state->start;
2518 spin_unlock(&inode->lock);
2523 * Once a range is no longer delalloc this function ensures that proper
2524 * accounting happens.
2526 void btrfs_clear_delalloc_extent(struct btrfs_inode *inode,
2527 struct extent_state *state, u32 bits)
2529 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2530 u64 len = state->end + 1 - state->start;
2531 u32 num_extents = count_max_extents(fs_info, len);
2533 lockdep_assert_held(&inode->io_tree.lock);
2535 if ((state->state & EXTENT_DEFRAG) && (bits & EXTENT_DEFRAG)) {
2536 spin_lock(&inode->lock);
2537 inode->defrag_bytes -= len;
2538 spin_unlock(&inode->lock);
2542 * set_bit and clear bit hooks normally require _irqsave/restore
2543 * but in this case, we are only testing for the DELALLOC
2544 * bit, which is only set or cleared with irqs on
2546 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
2547 struct btrfs_root *root = inode->root;
2548 u64 new_delalloc_bytes;
2550 spin_lock(&inode->lock);
2551 btrfs_mod_outstanding_extents(inode, -num_extents);
2552 spin_unlock(&inode->lock);
2555 * We don't reserve metadata space for space cache inodes so we
2556 * don't need to call delalloc_release_metadata if there is an
2559 if (bits & EXTENT_CLEAR_META_RESV &&
2560 root != fs_info->tree_root)
2561 btrfs_delalloc_release_metadata(inode, len, true);
2563 /* For sanity tests. */
2564 if (btrfs_is_testing(fs_info))
2567 if (!btrfs_is_data_reloc_root(root) &&
2568 !btrfs_is_free_space_inode(inode) &&
2569 !(state->state & EXTENT_NORESERVE) &&
2570 (bits & EXTENT_CLEAR_DATA_RESV))
2571 btrfs_free_reserved_data_space_noquota(fs_info, len);
2573 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2574 fs_info->delalloc_batch);
2575 spin_lock(&inode->lock);
2576 inode->delalloc_bytes -= len;
2577 new_delalloc_bytes = inode->delalloc_bytes;
2578 spin_unlock(&inode->lock);
2581 * We don't need to be under the protection of the inode's lock,
2582 * because we are called while holding the inode's io_tree lock
2583 * and are therefore protected against concurrent calls of this
2584 * function and btrfs_set_delalloc_extent().
2586 if (!btrfs_is_free_space_inode(inode) && new_delalloc_bytes == 0) {
2587 spin_lock(&root->delalloc_lock);
2588 btrfs_del_delalloc_inode(inode);
2589 spin_unlock(&root->delalloc_lock);
2593 if ((state->state & EXTENT_DELALLOC_NEW) &&
2594 (bits & EXTENT_DELALLOC_NEW)) {
2595 spin_lock(&inode->lock);
2596 ASSERT(inode->new_delalloc_bytes >= len);
2597 inode->new_delalloc_bytes -= len;
2598 if (bits & EXTENT_ADD_INODE_BYTES)
2599 inode_add_bytes(&inode->vfs_inode, len);
2600 spin_unlock(&inode->lock);
2604 static int btrfs_extract_ordered_extent(struct btrfs_bio *bbio,
2605 struct btrfs_ordered_extent *ordered)
2607 u64 start = (u64)bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT;
2608 u64 len = bbio->bio.bi_iter.bi_size;
2609 struct btrfs_ordered_extent *new;
2612 /* Must always be called for the beginning of an ordered extent. */
2613 if (WARN_ON_ONCE(start != ordered->disk_bytenr))
2616 /* No need to split if the ordered extent covers the entire bio. */
2617 if (ordered->disk_num_bytes == len) {
2618 refcount_inc(&ordered->refs);
2619 bbio->ordered = ordered;
2624 * Don't split the extent_map for NOCOW extents, as we're writing into
2625 * a pre-existing one.
2627 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
2628 ret = split_extent_map(bbio->inode, bbio->file_offset,
2629 ordered->num_bytes, len,
2630 ordered->disk_bytenr);
2635 new = btrfs_split_ordered_extent(ordered, len);
2637 return PTR_ERR(new);
2638 bbio->ordered = new;
2643 * given a list of ordered sums record them in the inode. This happens
2644 * at IO completion time based on sums calculated at bio submission time.
2646 static int add_pending_csums(struct btrfs_trans_handle *trans,
2647 struct list_head *list)
2649 struct btrfs_ordered_sum *sum;
2650 struct btrfs_root *csum_root = NULL;
2653 list_for_each_entry(sum, list, list) {
2654 trans->adding_csums = true;
2656 csum_root = btrfs_csum_root(trans->fs_info,
2658 ret = btrfs_csum_file_blocks(trans, csum_root, sum);
2659 trans->adding_csums = false;
2666 static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2669 struct extent_state **cached_state)
2671 u64 search_start = start;
2672 const u64 end = start + len - 1;
2674 while (search_start < end) {
2675 const u64 search_len = end - search_start + 1;
2676 struct extent_map *em;
2680 em = btrfs_get_extent(inode, NULL, search_start, search_len);
2684 if (em->block_start != EXTENT_MAP_HOLE)
2688 if (em->start < search_start)
2689 em_len -= search_start - em->start;
2690 if (em_len > search_len)
2691 em_len = search_len;
2693 ret = set_extent_bit(&inode->io_tree, search_start,
2694 search_start + em_len - 1,
2695 EXTENT_DELALLOC_NEW, cached_state);
2697 search_start = extent_map_end(em);
2698 free_extent_map(em);
2705 int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
2706 unsigned int extra_bits,
2707 struct extent_state **cached_state)
2709 WARN_ON(PAGE_ALIGNED(end));
2711 if (start >= i_size_read(&inode->vfs_inode) &&
2712 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2714 * There can't be any extents following eof in this case so just
2715 * set the delalloc new bit for the range directly.
2717 extra_bits |= EXTENT_DELALLOC_NEW;
2721 ret = btrfs_find_new_delalloc_bytes(inode, start,
2728 return set_extent_bit(&inode->io_tree, start, end,
2729 EXTENT_DELALLOC | extra_bits, cached_state);
2732 /* see btrfs_writepage_start_hook for details on why this is required */
2733 struct btrfs_writepage_fixup {
2735 struct btrfs_inode *inode;
2736 struct btrfs_work work;
2739 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
2741 struct btrfs_writepage_fixup *fixup =
2742 container_of(work, struct btrfs_writepage_fixup, work);
2743 struct btrfs_ordered_extent *ordered;
2744 struct extent_state *cached_state = NULL;
2745 struct extent_changeset *data_reserved = NULL;
2746 struct page *page = fixup->page;
2747 struct btrfs_inode *inode = fixup->inode;
2748 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2749 u64 page_start = page_offset(page);
2750 u64 page_end = page_offset(page) + PAGE_SIZE - 1;
2752 bool free_delalloc_space = true;
2755 * This is similar to page_mkwrite, we need to reserve the space before
2756 * we take the page lock.
2758 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2764 * Before we queued this fixup, we took a reference on the page.
2765 * page->mapping may go NULL, but it shouldn't be moved to a different
2768 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2770 * Unfortunately this is a little tricky, either
2772 * 1) We got here and our page had already been dealt with and
2773 * we reserved our space, thus ret == 0, so we need to just
2774 * drop our space reservation and bail. This can happen the
2775 * first time we come into the fixup worker, or could happen
2776 * while waiting for the ordered extent.
2777 * 2) Our page was already dealt with, but we happened to get an
2778 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2779 * this case we obviously don't have anything to release, but
2780 * because the page was already dealt with we don't want to
2781 * mark the page with an error, so make sure we're resetting
2782 * ret to 0. This is why we have this check _before_ the ret
2783 * check, because we do not want to have a surprise ENOSPC
2784 * when the page was already properly dealt with.
2787 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2788 btrfs_delalloc_release_space(inode, data_reserved,
2789 page_start, PAGE_SIZE,
2797 * We can't mess with the page state unless it is locked, so now that
2798 * it is locked bail if we failed to make our space reservation.
2803 lock_extent(&inode->io_tree, page_start, page_end, &cached_state);
2805 /* already ordered? We're done */
2806 if (PageOrdered(page))
2809 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
2811 unlock_extent(&inode->io_tree, page_start, page_end,
2814 btrfs_start_ordered_extent(ordered);
2815 btrfs_put_ordered_extent(ordered);
2819 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2825 * Everything went as planned, we're now the owner of a dirty page with
2826 * delayed allocation bits set and space reserved for our COW
2829 * The page was dirty when we started, nothing should have cleaned it.
2831 BUG_ON(!PageDirty(page));
2832 free_delalloc_space = false;
2834 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2835 if (free_delalloc_space)
2836 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2838 unlock_extent(&inode->io_tree, page_start, page_end, &cached_state);
2842 * We hit ENOSPC or other errors. Update the mapping and page
2843 * to reflect the errors and clean the page.
2845 mapping_set_error(page->mapping, ret);
2846 btrfs_mark_ordered_io_finished(inode, page, page_start,
2848 clear_page_dirty_for_io(page);
2850 btrfs_folio_clear_checked(fs_info, page_folio(page), page_start, PAGE_SIZE);
2854 extent_changeset_free(data_reserved);
2856 * As a precaution, do a delayed iput in case it would be the last iput
2857 * that could need flushing space. Recursing back to fixup worker would
2860 btrfs_add_delayed_iput(inode);
2864 * There are a few paths in the higher layers of the kernel that directly
2865 * set the page dirty bit without asking the filesystem if it is a
2866 * good idea. This causes problems because we want to make sure COW
2867 * properly happens and the data=ordered rules are followed.
2869 * In our case any range that doesn't have the ORDERED bit set
2870 * hasn't been properly setup for IO. We kick off an async process
2871 * to fix it up. The async helper will wait for ordered extents, set
2872 * the delalloc bit and make it safe to write the page.
2874 int btrfs_writepage_cow_fixup(struct page *page)
2876 struct inode *inode = page->mapping->host;
2877 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
2878 struct btrfs_writepage_fixup *fixup;
2880 /* This page has ordered extent covering it already */
2881 if (PageOrdered(page))
2885 * PageChecked is set below when we create a fixup worker for this page,
2886 * don't try to create another one if we're already PageChecked()
2888 * The extent_io writepage code will redirty the page if we send back
2891 if (PageChecked(page))
2894 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2899 * We are already holding a reference to this inode from
2900 * write_cache_pages. We need to hold it because the space reservation
2901 * takes place outside of the page lock, and we can't trust
2902 * page->mapping outside of the page lock.
2905 btrfs_folio_set_checked(fs_info, page_folio(page), page_offset(page), PAGE_SIZE);
2907 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL);
2909 fixup->inode = BTRFS_I(inode);
2910 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
2915 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2916 struct btrfs_inode *inode, u64 file_pos,
2917 struct btrfs_file_extent_item *stack_fi,
2918 const bool update_inode_bytes,
2919 u64 qgroup_reserved)
2921 struct btrfs_root *root = inode->root;
2922 const u64 sectorsize = root->fs_info->sectorsize;
2923 struct btrfs_path *path;
2924 struct extent_buffer *leaf;
2925 struct btrfs_key ins;
2926 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2927 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2928 u64 offset = btrfs_stack_file_extent_offset(stack_fi);
2929 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2930 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
2931 struct btrfs_drop_extents_args drop_args = { 0 };
2934 path = btrfs_alloc_path();
2939 * we may be replacing one extent in the tree with another.
2940 * The new extent is pinned in the extent map, and we don't want
2941 * to drop it from the cache until it is completely in the btree.
2943 * So, tell btrfs_drop_extents to leave this extent in the cache.
2944 * the caller is expected to unpin it and allow it to be merged
2947 drop_args.path = path;
2948 drop_args.start = file_pos;
2949 drop_args.end = file_pos + num_bytes;
2950 drop_args.replace_extent = true;
2951 drop_args.extent_item_size = sizeof(*stack_fi);
2952 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
2956 if (!drop_args.extent_inserted) {
2957 ins.objectid = btrfs_ino(inode);
2958 ins.offset = file_pos;
2959 ins.type = BTRFS_EXTENT_DATA_KEY;
2961 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2966 leaf = path->nodes[0];
2967 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2968 write_extent_buffer(leaf, stack_fi,
2969 btrfs_item_ptr_offset(leaf, path->slots[0]),
2970 sizeof(struct btrfs_file_extent_item));
2972 btrfs_mark_buffer_dirty(trans, leaf);
2973 btrfs_release_path(path);
2976 * If we dropped an inline extent here, we know the range where it is
2977 * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the
2978 * number of bytes only for that range containing the inline extent.
2979 * The remaining of the range will be processed when clearning the
2980 * EXTENT_DELALLOC_BIT bit through the ordered extent completion.
2982 if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) {
2983 u64 inline_size = round_down(drop_args.bytes_found, sectorsize);
2985 inline_size = drop_args.bytes_found - inline_size;
2986 btrfs_update_inode_bytes(inode, sectorsize, inline_size);
2987 drop_args.bytes_found -= inline_size;
2988 num_bytes -= sectorsize;
2991 if (update_inode_bytes)
2992 btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
2994 ins.objectid = disk_bytenr;
2995 ins.offset = disk_num_bytes;
2996 ins.type = BTRFS_EXTENT_ITEM_KEY;
2998 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
3002 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
3004 qgroup_reserved, &ins);
3006 btrfs_free_path(path);
3011 static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
3014 struct btrfs_block_group *cache;
3016 cache = btrfs_lookup_block_group(fs_info, start);
3019 spin_lock(&cache->lock);
3020 cache->delalloc_bytes -= len;
3021 spin_unlock(&cache->lock);
3023 btrfs_put_block_group(cache);
3026 static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
3027 struct btrfs_ordered_extent *oe)
3029 struct btrfs_file_extent_item stack_fi;
3030 bool update_inode_bytes;
3031 u64 num_bytes = oe->num_bytes;
3032 u64 ram_bytes = oe->ram_bytes;
3034 memset(&stack_fi, 0, sizeof(stack_fi));
3035 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
3036 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
3037 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
3038 oe->disk_num_bytes);
3039 btrfs_set_stack_file_extent_offset(&stack_fi, oe->offset);
3040 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags)) {
3041 num_bytes = oe->truncated_len;
3042 ram_bytes = num_bytes;
3044 btrfs_set_stack_file_extent_num_bytes(&stack_fi, num_bytes);
3045 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, ram_bytes);
3046 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
3047 /* Encryption and other encoding is reserved and all 0 */
3050 * For delalloc, when completing an ordered extent we update the inode's
3051 * bytes when clearing the range in the inode's io tree, so pass false
3052 * as the argument 'update_inode_bytes' to insert_reserved_file_extent(),
3053 * except if the ordered extent was truncated.
3055 update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) ||
3056 test_bit(BTRFS_ORDERED_ENCODED, &oe->flags) ||
3057 test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags);
3059 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
3060 oe->file_offset, &stack_fi,
3061 update_inode_bytes, oe->qgroup_rsv);
3065 * As ordered data IO finishes, this gets called so we can finish
3066 * an ordered extent if the range of bytes in the file it covers are
3069 int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent)
3071 struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
3072 struct btrfs_root *root = inode->root;
3073 struct btrfs_fs_info *fs_info = root->fs_info;
3074 struct btrfs_trans_handle *trans = NULL;
3075 struct extent_io_tree *io_tree = &inode->io_tree;
3076 struct extent_state *cached_state = NULL;
3078 int compress_type = 0;
3080 u64 logical_len = ordered_extent->num_bytes;
3081 bool freespace_inode;
3082 bool truncated = false;
3083 bool clear_reserved_extent = true;
3084 unsigned int clear_bits = EXTENT_DEFRAG;
3086 start = ordered_extent->file_offset;
3087 end = start + ordered_extent->num_bytes - 1;
3089 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3090 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
3091 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags) &&
3092 !test_bit(BTRFS_ORDERED_ENCODED, &ordered_extent->flags))
3093 clear_bits |= EXTENT_DELALLOC_NEW;
3095 freespace_inode = btrfs_is_free_space_inode(inode);
3096 if (!freespace_inode)
3097 btrfs_lockdep_acquire(fs_info, btrfs_ordered_extent);
3099 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
3104 if (btrfs_is_zoned(fs_info))
3105 btrfs_zone_finish_endio(fs_info, ordered_extent->disk_bytenr,
3106 ordered_extent->disk_num_bytes);
3108 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
3110 logical_len = ordered_extent->truncated_len;
3111 /* Truncated the entire extent, don't bother adding */
3116 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
3117 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
3119 btrfs_inode_safe_disk_i_size_write(inode, 0);
3120 if (freespace_inode)
3121 trans = btrfs_join_transaction_spacecache(root);
3123 trans = btrfs_join_transaction(root);
3124 if (IS_ERR(trans)) {
3125 ret = PTR_ERR(trans);
3129 trans->block_rsv = &inode->block_rsv;
3130 ret = btrfs_update_inode_fallback(trans, inode);
3131 if (ret) /* -ENOMEM or corruption */
3132 btrfs_abort_transaction(trans, ret);
3136 clear_bits |= EXTENT_LOCKED;
3137 lock_extent(io_tree, start, end, &cached_state);
3139 if (freespace_inode)
3140 trans = btrfs_join_transaction_spacecache(root);
3142 trans = btrfs_join_transaction(root);
3143 if (IS_ERR(trans)) {
3144 ret = PTR_ERR(trans);
3149 trans->block_rsv = &inode->block_rsv;
3151 ret = btrfs_insert_raid_extent(trans, ordered_extent);
3155 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
3156 compress_type = ordered_extent->compress_type;
3157 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3158 BUG_ON(compress_type);
3159 ret = btrfs_mark_extent_written(trans, inode,
3160 ordered_extent->file_offset,
3161 ordered_extent->file_offset +
3163 btrfs_zoned_release_data_reloc_bg(fs_info, ordered_extent->disk_bytenr,
3164 ordered_extent->disk_num_bytes);
3166 BUG_ON(root == fs_info->tree_root);
3167 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
3169 clear_reserved_extent = false;
3170 btrfs_release_delalloc_bytes(fs_info,
3171 ordered_extent->disk_bytenr,
3172 ordered_extent->disk_num_bytes);
3176 btrfs_abort_transaction(trans, ret);
3180 ret = unpin_extent_cache(inode, ordered_extent->file_offset,
3181 ordered_extent->num_bytes, trans->transid);
3183 btrfs_abort_transaction(trans, ret);
3187 ret = add_pending_csums(trans, &ordered_extent->list);
3189 btrfs_abort_transaction(trans, ret);
3194 * If this is a new delalloc range, clear its new delalloc flag to
3195 * update the inode's number of bytes. This needs to be done first
3196 * before updating the inode item.
3198 if ((clear_bits & EXTENT_DELALLOC_NEW) &&
3199 !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))
3200 clear_extent_bit(&inode->io_tree, start, end,
3201 EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES,
3204 btrfs_inode_safe_disk_i_size_write(inode, 0);
3205 ret = btrfs_update_inode_fallback(trans, inode);
3206 if (ret) { /* -ENOMEM or corruption */
3207 btrfs_abort_transaction(trans, ret);
3211 clear_extent_bit(&inode->io_tree, start, end, clear_bits,
3215 btrfs_end_transaction(trans);
3217 if (ret || truncated) {
3218 u64 unwritten_start = start;
3221 * If we failed to finish this ordered extent for any reason we
3222 * need to make sure BTRFS_ORDERED_IOERR is set on the ordered
3223 * extent, and mark the inode with the error if it wasn't
3224 * already set. Any error during writeback would have already
3225 * set the mapping error, so we need to set it if we're the ones
3226 * marking this ordered extent as failed.
3229 btrfs_mark_ordered_extent_error(ordered_extent);
3232 unwritten_start += logical_len;
3233 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
3236 * Drop extent maps for the part of the extent we didn't write.
3238 * We have an exception here for the free_space_inode, this is
3239 * because when we do btrfs_get_extent() on the free space inode
3240 * we will search the commit root. If this is a new block group
3241 * we won't find anything, and we will trip over the assert in
3242 * writepage where we do ASSERT(em->block_start !=
3245 * Theoretically we could also skip this for any NOCOW extent as
3246 * we don't mess with the extent map tree in the NOCOW case, but
3247 * for now simply skip this if we are the free space inode.
3249 if (!btrfs_is_free_space_inode(inode))
3250 btrfs_drop_extent_map_range(inode, unwritten_start,
3254 * If the ordered extent had an IOERR or something else went
3255 * wrong we need to return the space for this ordered extent
3256 * back to the allocator. We only free the extent in the
3257 * truncated case if we didn't write out the extent at all.
3259 * If we made it past insert_reserved_file_extent before we
3260 * errored out then we don't need to do this as the accounting
3261 * has already been done.
3263 if ((ret || !logical_len) &&
3264 clear_reserved_extent &&
3265 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3266 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3268 * Discard the range before returning it back to the
3271 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
3272 btrfs_discard_extent(fs_info,
3273 ordered_extent->disk_bytenr,
3274 ordered_extent->disk_num_bytes,
3276 btrfs_free_reserved_extent(fs_info,
3277 ordered_extent->disk_bytenr,
3278 ordered_extent->disk_num_bytes, 1);
3280 * Actually free the qgroup rsv which was released when
3281 * the ordered extent was created.
3283 btrfs_qgroup_free_refroot(fs_info, btrfs_root_id(inode->root),
3284 ordered_extent->qgroup_rsv,
3285 BTRFS_QGROUP_RSV_DATA);
3290 * This needs to be done to make sure anybody waiting knows we are done
3291 * updating everything for this ordered extent.
3293 btrfs_remove_ordered_extent(inode, ordered_extent);
3296 btrfs_put_ordered_extent(ordered_extent);
3297 /* once for the tree */
3298 btrfs_put_ordered_extent(ordered_extent);
3303 int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered)
3305 if (btrfs_is_zoned(inode_to_fs_info(ordered->inode)) &&
3306 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
3307 list_empty(&ordered->bioc_list))
3308 btrfs_finish_ordered_zoned(ordered);
3309 return btrfs_finish_one_ordered(ordered);
3313 * Verify the checksum for a single sector without any extra action that depend
3314 * on the type of I/O.
3316 int btrfs_check_sector_csum(struct btrfs_fs_info *fs_info, struct page *page,
3317 u32 pgoff, u8 *csum, const u8 * const csum_expected)
3319 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
3322 ASSERT(pgoff + fs_info->sectorsize <= PAGE_SIZE);
3324 shash->tfm = fs_info->csum_shash;
3326 kaddr = kmap_local_page(page) + pgoff;
3327 crypto_shash_digest(shash, kaddr, fs_info->sectorsize, csum);
3328 kunmap_local(kaddr);
3330 if (memcmp(csum, csum_expected, fs_info->csum_size))
3336 * Verify the checksum of a single data sector.
3338 * @bbio: btrfs_io_bio which contains the csum
3339 * @dev: device the sector is on
3340 * @bio_offset: offset to the beginning of the bio (in bytes)
3341 * @bv: bio_vec to check
3343 * Check if the checksum on a data block is valid. When a checksum mismatch is
3344 * detected, report the error and fill the corrupted range with zero.
3346 * Return %true if the sector is ok or had no checksum to start with, else %false.
3348 bool btrfs_data_csum_ok(struct btrfs_bio *bbio, struct btrfs_device *dev,
3349 u32 bio_offset, struct bio_vec *bv)
3351 struct btrfs_inode *inode = bbio->inode;
3352 struct btrfs_fs_info *fs_info = inode->root->fs_info;
3353 u64 file_offset = bbio->file_offset + bio_offset;
3354 u64 end = file_offset + bv->bv_len - 1;
3356 u8 csum[BTRFS_CSUM_SIZE];
3358 ASSERT(bv->bv_len == fs_info->sectorsize);
3363 if (btrfs_is_data_reloc_root(inode->root) &&
3364 test_range_bit(&inode->io_tree, file_offset, end, EXTENT_NODATASUM,
3366 /* Skip the range without csum for data reloc inode */
3367 clear_extent_bits(&inode->io_tree, file_offset, end,
3372 csum_expected = bbio->csum + (bio_offset >> fs_info->sectorsize_bits) *
3374 if (btrfs_check_sector_csum(fs_info, bv->bv_page, bv->bv_offset, csum,
3380 btrfs_print_data_csum_error(inode, file_offset, csum, csum_expected,
3383 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS);
3389 * Perform a delayed iput on @inode.
3391 * @inode: The inode we want to perform iput on
3393 * This function uses the generic vfs_inode::i_count to track whether we should
3394 * just decrement it (in case it's > 1) or if this is the last iput then link
3395 * the inode to the delayed iput machinery. Delayed iputs are processed at
3396 * transaction commit time/superblock commit/cleaner kthread.
3398 void btrfs_add_delayed_iput(struct btrfs_inode *inode)
3400 struct btrfs_fs_info *fs_info = inode->root->fs_info;
3401 unsigned long flags;
3403 if (atomic_add_unless(&inode->vfs_inode.i_count, -1, 1))
3406 atomic_inc(&fs_info->nr_delayed_iputs);
3408 * Need to be irq safe here because we can be called from either an irq
3409 * context (see bio.c and btrfs_put_ordered_extent()) or a non-irq
3412 spin_lock_irqsave(&fs_info->delayed_iput_lock, flags);
3413 ASSERT(list_empty(&inode->delayed_iput));
3414 list_add_tail(&inode->delayed_iput, &fs_info->delayed_iputs);
3415 spin_unlock_irqrestore(&fs_info->delayed_iput_lock, flags);
3416 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3417 wake_up_process(fs_info->cleaner_kthread);
3420 static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3421 struct btrfs_inode *inode)
3423 list_del_init(&inode->delayed_iput);
3424 spin_unlock_irq(&fs_info->delayed_iput_lock);
3425 iput(&inode->vfs_inode);
3426 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3427 wake_up(&fs_info->delayed_iputs_wait);
3428 spin_lock_irq(&fs_info->delayed_iput_lock);
3431 static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3432 struct btrfs_inode *inode)
3434 if (!list_empty(&inode->delayed_iput)) {
3435 spin_lock_irq(&fs_info->delayed_iput_lock);
3436 if (!list_empty(&inode->delayed_iput))
3437 run_delayed_iput_locked(fs_info, inode);
3438 spin_unlock_irq(&fs_info->delayed_iput_lock);
3442 void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
3445 * btrfs_put_ordered_extent() can run in irq context (see bio.c), which
3446 * calls btrfs_add_delayed_iput() and that needs to lock
3447 * fs_info->delayed_iput_lock. So we need to disable irqs here to
3448 * prevent a deadlock.
3450 spin_lock_irq(&fs_info->delayed_iput_lock);
3451 while (!list_empty(&fs_info->delayed_iputs)) {
3452 struct btrfs_inode *inode;
3454 inode = list_first_entry(&fs_info->delayed_iputs,
3455 struct btrfs_inode, delayed_iput);
3456 run_delayed_iput_locked(fs_info, inode);
3457 if (need_resched()) {
3458 spin_unlock_irq(&fs_info->delayed_iput_lock);
3460 spin_lock_irq(&fs_info->delayed_iput_lock);
3463 spin_unlock_irq(&fs_info->delayed_iput_lock);
3467 * Wait for flushing all delayed iputs
3469 * @fs_info: the filesystem
3471 * This will wait on any delayed iputs that are currently running with KILLABLE
3472 * set. Once they are all done running we will return, unless we are killed in
3473 * which case we return EINTR. This helps in user operations like fallocate etc
3474 * that might get blocked on the iputs.
3476 * Return EINTR if we were killed, 0 if nothing's pending
3478 int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3480 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3481 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3488 * This creates an orphan entry for the given inode in case something goes wrong
3489 * in the middle of an unlink.
3491 int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3492 struct btrfs_inode *inode)
3496 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3497 if (ret && ret != -EEXIST) {
3498 btrfs_abort_transaction(trans, ret);
3506 * We have done the delete so we can go ahead and remove the orphan item for
3507 * this particular inode.
3509 static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3510 struct btrfs_inode *inode)
3512 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
3516 * this cleans up any orphans that may be left on the list from the last use
3519 int btrfs_orphan_cleanup(struct btrfs_root *root)
3521 struct btrfs_fs_info *fs_info = root->fs_info;
3522 struct btrfs_path *path;
3523 struct extent_buffer *leaf;
3524 struct btrfs_key key, found_key;
3525 struct btrfs_trans_handle *trans;
3526 struct inode *inode;
3527 u64 last_objectid = 0;
3528 int ret = 0, nr_unlink = 0;
3530 if (test_and_set_bit(BTRFS_ROOT_ORPHAN_CLEANUP, &root->state))
3533 path = btrfs_alloc_path();
3538 path->reada = READA_BACK;
3540 key.objectid = BTRFS_ORPHAN_OBJECTID;
3541 key.type = BTRFS_ORPHAN_ITEM_KEY;
3542 key.offset = (u64)-1;
3545 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3550 * if ret == 0 means we found what we were searching for, which
3551 * is weird, but possible, so only screw with path if we didn't
3552 * find the key and see if we have stuff that matches
3556 if (path->slots[0] == 0)
3561 /* pull out the item */
3562 leaf = path->nodes[0];
3563 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3565 /* make sure the item matches what we want */
3566 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3568 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
3571 /* release the path since we're done with it */
3572 btrfs_release_path(path);
3575 * this is where we are basically btrfs_lookup, without the
3576 * crossing root thing. we store the inode number in the
3577 * offset of the orphan item.
3580 if (found_key.offset == last_objectid) {
3582 * We found the same inode as before. This means we were
3583 * not able to remove its items via eviction triggered
3584 * by an iput(). A transaction abort may have happened,
3585 * due to -ENOSPC for example, so try to grab the error
3586 * that lead to a transaction abort, if any.
3589 "Error removing orphan entry, stopping orphan cleanup");
3590 ret = BTRFS_FS_ERROR(fs_info) ?: -EINVAL;
3594 last_objectid = found_key.offset;
3596 found_key.objectid = found_key.offset;
3597 found_key.type = BTRFS_INODE_ITEM_KEY;
3598 found_key.offset = 0;
3599 inode = btrfs_iget(fs_info->sb, last_objectid, root);
3600 if (IS_ERR(inode)) {
3601 ret = PTR_ERR(inode);
3607 if (!inode && root == fs_info->tree_root) {
3608 struct btrfs_root *dead_root;
3609 int is_dead_root = 0;
3612 * This is an orphan in the tree root. Currently these
3613 * could come from 2 sources:
3614 * a) a root (snapshot/subvolume) deletion in progress
3615 * b) a free space cache inode
3616 * We need to distinguish those two, as the orphan item
3617 * for a root must not get deleted before the deletion
3618 * of the snapshot/subvolume's tree completes.
3620 * btrfs_find_orphan_roots() ran before us, which has
3621 * found all deleted roots and loaded them into
3622 * fs_info->fs_roots_radix. So here we can find if an
3623 * orphan item corresponds to a deleted root by looking
3624 * up the root from that radix tree.
3627 spin_lock(&fs_info->fs_roots_radix_lock);
3628 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3629 (unsigned long)found_key.objectid);
3630 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3632 spin_unlock(&fs_info->fs_roots_radix_lock);
3635 /* prevent this orphan from being found again */
3636 key.offset = found_key.objectid - 1;
3643 * If we have an inode with links, there are a couple of
3646 * 1. We were halfway through creating fsverity metadata for the
3647 * file. In that case, the orphan item represents incomplete
3648 * fsverity metadata which must be cleaned up with
3649 * btrfs_drop_verity_items and deleting the orphan item.
3651 * 2. Old kernels (before v3.12) used to create an
3652 * orphan item for truncate indicating that there were possibly
3653 * extent items past i_size that needed to be deleted. In v3.12,
3654 * truncate was changed to update i_size in sync with the extent
3655 * items, but the (useless) orphan item was still created. Since
3656 * v4.18, we don't create the orphan item for truncate at all.
3658 * So, this item could mean that we need to do a truncate, but
3659 * only if this filesystem was last used on a pre-v3.12 kernel
3660 * and was not cleanly unmounted. The odds of that are quite
3661 * slim, and it's a pain to do the truncate now, so just delete
3664 * It's also possible that this orphan item was supposed to be
3665 * deleted but wasn't. The inode number may have been reused,
3666 * but either way, we can delete the orphan item.
3668 if (!inode || inode->i_nlink) {
3670 ret = btrfs_drop_verity_items(BTRFS_I(inode));
3676 trans = btrfs_start_transaction(root, 1);
3677 if (IS_ERR(trans)) {
3678 ret = PTR_ERR(trans);
3681 btrfs_debug(fs_info, "auto deleting %Lu",
3682 found_key.objectid);
3683 ret = btrfs_del_orphan_item(trans, root,
3684 found_key.objectid);
3685 btrfs_end_transaction(trans);
3693 /* this will do delete_inode and everything for us */
3696 /* release the path since we're done with it */
3697 btrfs_release_path(path);
3699 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
3700 trans = btrfs_join_transaction(root);
3702 btrfs_end_transaction(trans);
3706 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
3710 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
3711 btrfs_free_path(path);
3716 * very simple check to peek ahead in the leaf looking for xattrs. If we
3717 * don't find any xattrs, we know there can't be any acls.
3719 * slot is the slot the inode is in, objectid is the objectid of the inode
3721 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3722 int slot, u64 objectid,
3723 int *first_xattr_slot)
3725 u32 nritems = btrfs_header_nritems(leaf);
3726 struct btrfs_key found_key;
3727 static u64 xattr_access = 0;
3728 static u64 xattr_default = 0;
3731 if (!xattr_access) {
3732 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3733 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3734 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3735 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
3739 *first_xattr_slot = -1;
3740 while (slot < nritems) {
3741 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3743 /* we found a different objectid, there must not be acls */
3744 if (found_key.objectid != objectid)
3747 /* we found an xattr, assume we've got an acl */
3748 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3749 if (*first_xattr_slot == -1)
3750 *first_xattr_slot = slot;
3751 if (found_key.offset == xattr_access ||
3752 found_key.offset == xattr_default)
3757 * we found a key greater than an xattr key, there can't
3758 * be any acls later on
3760 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3767 * it goes inode, inode backrefs, xattrs, extents,
3768 * so if there are a ton of hard links to an inode there can
3769 * be a lot of backrefs. Don't waste time searching too hard,
3770 * this is just an optimization
3775 /* we hit the end of the leaf before we found an xattr or
3776 * something larger than an xattr. We have to assume the inode
3779 if (*first_xattr_slot == -1)
3780 *first_xattr_slot = slot;
3785 * read an inode from the btree into the in-memory inode
3787 static int btrfs_read_locked_inode(struct inode *inode,
3788 struct btrfs_path *in_path)
3790 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
3791 struct btrfs_path *path = in_path;
3792 struct extent_buffer *leaf;
3793 struct btrfs_inode_item *inode_item;
3794 struct btrfs_root *root = BTRFS_I(inode)->root;
3795 struct btrfs_key location;
3800 bool filled = false;
3801 int first_xattr_slot;
3803 ret = btrfs_fill_inode(inode, &rdev);
3808 path = btrfs_alloc_path();
3813 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
3815 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
3817 if (path != in_path)
3818 btrfs_free_path(path);
3822 leaf = path->nodes[0];
3827 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3828 struct btrfs_inode_item);
3829 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
3830 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
3831 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3832 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
3833 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
3834 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3835 round_up(i_size_read(inode), fs_info->sectorsize));
3837 inode_set_atime(inode, btrfs_timespec_sec(leaf, &inode_item->atime),
3838 btrfs_timespec_nsec(leaf, &inode_item->atime));
3840 inode_set_mtime(inode, btrfs_timespec_sec(leaf, &inode_item->mtime),
3841 btrfs_timespec_nsec(leaf, &inode_item->mtime));
3843 inode_set_ctime(inode, btrfs_timespec_sec(leaf, &inode_item->ctime),
3844 btrfs_timespec_nsec(leaf, &inode_item->ctime));
3846 BTRFS_I(inode)->i_otime_sec = btrfs_timespec_sec(leaf, &inode_item->otime);
3847 BTRFS_I(inode)->i_otime_nsec = btrfs_timespec_nsec(leaf, &inode_item->otime);
3849 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
3850 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
3851 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3853 inode_set_iversion_queried(inode,
3854 btrfs_inode_sequence(leaf, inode_item));
3855 inode->i_generation = BTRFS_I(inode)->generation;
3857 rdev = btrfs_inode_rdev(leaf, inode_item);
3859 BTRFS_I(inode)->index_cnt = (u64)-1;
3860 btrfs_inode_split_flags(btrfs_inode_flags(leaf, inode_item),
3861 &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags);
3865 * If we were modified in the current generation and evicted from memory
3866 * and then re-read we need to do a full sync since we don't have any
3867 * idea about which extents were modified before we were evicted from
3870 * This is required for both inode re-read from disk and delayed inode
3871 * in the delayed_nodes xarray.
3873 if (BTRFS_I(inode)->last_trans == btrfs_get_fs_generation(fs_info))
3874 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3875 &BTRFS_I(inode)->runtime_flags);
3878 * We don't persist the id of the transaction where an unlink operation
3879 * against the inode was last made. So here we assume the inode might
3880 * have been evicted, and therefore the exact value of last_unlink_trans
3881 * lost, and set it to last_trans to avoid metadata inconsistencies
3882 * between the inode and its parent if the inode is fsync'ed and the log
3883 * replayed. For example, in the scenario:
3886 * ln mydir/foo mydir/bar
3889 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3890 * xfs_io -c fsync mydir/foo
3892 * mount fs, triggers fsync log replay
3894 * We must make sure that when we fsync our inode foo we also log its
3895 * parent inode, otherwise after log replay the parent still has the
3896 * dentry with the "bar" name but our inode foo has a link count of 1
3897 * and doesn't have an inode ref with the name "bar" anymore.
3899 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3900 * but it guarantees correctness at the expense of occasional full
3901 * transaction commits on fsync if our inode is a directory, or if our
3902 * inode is not a directory, logging its parent unnecessarily.
3904 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3907 * Same logic as for last_unlink_trans. We don't persist the generation
3908 * of the last transaction where this inode was used for a reflink
3909 * operation, so after eviction and reloading the inode we must be
3910 * pessimistic and assume the last transaction that modified the inode.
3912 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3915 if (inode->i_nlink != 1 ||
3916 path->slots[0] >= btrfs_header_nritems(leaf))
3919 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3920 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
3923 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3924 if (location.type == BTRFS_INODE_REF_KEY) {
3925 struct btrfs_inode_ref *ref;
3927 ref = (struct btrfs_inode_ref *)ptr;
3928 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3929 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3930 struct btrfs_inode_extref *extref;
3932 extref = (struct btrfs_inode_extref *)ptr;
3933 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3938 * try to precache a NULL acl entry for files that don't have
3939 * any xattrs or acls
3941 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3942 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
3943 if (first_xattr_slot != -1) {
3944 path->slots[0] = first_xattr_slot;
3945 ret = btrfs_load_inode_props(inode, path);
3948 "error loading props for ino %llu (root %llu): %d",
3949 btrfs_ino(BTRFS_I(inode)),
3950 btrfs_root_id(root), ret);
3952 if (path != in_path)
3953 btrfs_free_path(path);
3956 cache_no_acl(inode);
3958 switch (inode->i_mode & S_IFMT) {
3960 inode->i_mapping->a_ops = &btrfs_aops;
3961 inode->i_fop = &btrfs_file_operations;
3962 inode->i_op = &btrfs_file_inode_operations;
3965 inode->i_fop = &btrfs_dir_file_operations;
3966 inode->i_op = &btrfs_dir_inode_operations;
3969 inode->i_op = &btrfs_symlink_inode_operations;
3970 inode_nohighmem(inode);
3971 inode->i_mapping->a_ops = &btrfs_aops;
3974 inode->i_op = &btrfs_special_inode_operations;
3975 init_special_inode(inode, inode->i_mode, rdev);
3979 btrfs_sync_inode_flags_to_i_flags(inode);
3984 * given a leaf and an inode, copy the inode fields into the leaf
3986 static void fill_inode_item(struct btrfs_trans_handle *trans,
3987 struct extent_buffer *leaf,
3988 struct btrfs_inode_item *item,
3989 struct inode *inode)
3991 struct btrfs_map_token token;
3994 btrfs_init_map_token(&token, leaf);
3996 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3997 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3998 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3999 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
4000 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
4002 btrfs_set_token_timespec_sec(&token, &item->atime,
4003 inode_get_atime_sec(inode));
4004 btrfs_set_token_timespec_nsec(&token, &item->atime,
4005 inode_get_atime_nsec(inode));
4007 btrfs_set_token_timespec_sec(&token, &item->mtime,
4008 inode_get_mtime_sec(inode));
4009 btrfs_set_token_timespec_nsec(&token, &item->mtime,
4010 inode_get_mtime_nsec(inode));
4012 btrfs_set_token_timespec_sec(&token, &item->ctime,
4013 inode_get_ctime_sec(inode));
4014 btrfs_set_token_timespec_nsec(&token, &item->ctime,
4015 inode_get_ctime_nsec(inode));
4017 btrfs_set_token_timespec_sec(&token, &item->otime, BTRFS_I(inode)->i_otime_sec);
4018 btrfs_set_token_timespec_nsec(&token, &item->otime, BTRFS_I(inode)->i_otime_nsec);
4020 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
4021 btrfs_set_token_inode_generation(&token, item,
4022 BTRFS_I(inode)->generation);
4023 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
4024 btrfs_set_token_inode_transid(&token, item, trans->transid);
4025 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
4026 flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
4027 BTRFS_I(inode)->ro_flags);
4028 btrfs_set_token_inode_flags(&token, item, flags);
4029 btrfs_set_token_inode_block_group(&token, item, 0);
4033 * copy everything in the in-memory inode into the btree.
4035 static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
4036 struct btrfs_inode *inode)
4038 struct btrfs_inode_item *inode_item;
4039 struct btrfs_path *path;
4040 struct extent_buffer *leaf;
4043 path = btrfs_alloc_path();
4047 ret = btrfs_lookup_inode(trans, inode->root, path, &inode->location, 1);
4054 leaf = path->nodes[0];
4055 inode_item = btrfs_item_ptr(leaf, path->slots[0],
4056 struct btrfs_inode_item);
4058 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
4059 btrfs_mark_buffer_dirty(trans, leaf);
4060 btrfs_set_inode_last_trans(trans, inode);
4063 btrfs_free_path(path);
4068 * copy everything in the in-memory inode into the btree.
4070 int btrfs_update_inode(struct btrfs_trans_handle *trans,
4071 struct btrfs_inode *inode)
4073 struct btrfs_root *root = inode->root;
4074 struct btrfs_fs_info *fs_info = root->fs_info;
4078 * If the inode is a free space inode, we can deadlock during commit
4079 * if we put it into the delayed code.
4081 * The data relocation inode should also be directly updated
4084 if (!btrfs_is_free_space_inode(inode)
4085 && !btrfs_is_data_reloc_root(root)
4086 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
4087 btrfs_update_root_times(trans, root);
4089 ret = btrfs_delayed_update_inode(trans, inode);
4091 btrfs_set_inode_last_trans(trans, inode);
4095 return btrfs_update_inode_item(trans, inode);
4098 int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4099 struct btrfs_inode *inode)
4103 ret = btrfs_update_inode(trans, inode);
4105 return btrfs_update_inode_item(trans, inode);
4110 * unlink helper that gets used here in inode.c and in the tree logging
4111 * recovery code. It remove a link in a directory with a given name, and
4112 * also drops the back refs in the inode to the directory
4114 static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4115 struct btrfs_inode *dir,
4116 struct btrfs_inode *inode,
4117 const struct fscrypt_str *name,
4118 struct btrfs_rename_ctx *rename_ctx)
4120 struct btrfs_root *root = dir->root;
4121 struct btrfs_fs_info *fs_info = root->fs_info;
4122 struct btrfs_path *path;
4124 struct btrfs_dir_item *di;
4126 u64 ino = btrfs_ino(inode);
4127 u64 dir_ino = btrfs_ino(dir);
4129 path = btrfs_alloc_path();
4135 di = btrfs_lookup_dir_item(trans, root, path, dir_ino, name, -1);
4136 if (IS_ERR_OR_NULL(di)) {
4137 ret = di ? PTR_ERR(di) : -ENOENT;
4140 ret = btrfs_delete_one_dir_name(trans, root, path, di);
4143 btrfs_release_path(path);
4146 * If we don't have dir index, we have to get it by looking up
4147 * the inode ref, since we get the inode ref, remove it directly,
4148 * it is unnecessary to do delayed deletion.
4150 * But if we have dir index, needn't search inode ref to get it.
4151 * Since the inode ref is close to the inode item, it is better
4152 * that we delay to delete it, and just do this deletion when
4153 * we update the inode item.
4155 if (inode->dir_index) {
4156 ret = btrfs_delayed_delete_inode_ref(inode);
4158 index = inode->dir_index;
4163 ret = btrfs_del_inode_ref(trans, root, name, ino, dir_ino, &index);
4166 "failed to delete reference to %.*s, inode %llu parent %llu",
4167 name->len, name->name, ino, dir_ino);
4168 btrfs_abort_transaction(trans, ret);
4173 rename_ctx->index = index;
4175 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
4177 btrfs_abort_transaction(trans, ret);
4182 * If we are in a rename context, we don't need to update anything in the
4183 * log. That will be done later during the rename by btrfs_log_new_name().
4184 * Besides that, doing it here would only cause extra unnecessary btree
4185 * operations on the log tree, increasing latency for applications.
4188 btrfs_del_inode_ref_in_log(trans, root, name, inode, dir_ino);
4189 btrfs_del_dir_entries_in_log(trans, root, name, dir, index);
4193 * If we have a pending delayed iput we could end up with the final iput
4194 * being run in btrfs-cleaner context. If we have enough of these built
4195 * up we can end up burning a lot of time in btrfs-cleaner without any
4196 * way to throttle the unlinks. Since we're currently holding a ref on
4197 * the inode we can run the delayed iput here without any issues as the
4198 * final iput won't be done until after we drop the ref we're currently
4201 btrfs_run_delayed_iput(fs_info, inode);
4203 btrfs_free_path(path);
4207 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name->len * 2);
4208 inode_inc_iversion(&inode->vfs_inode);
4209 inode_inc_iversion(&dir->vfs_inode);
4210 inode_set_mtime_to_ts(&dir->vfs_inode, inode_set_ctime_current(&dir->vfs_inode));
4211 ret = btrfs_update_inode(trans, dir);
4216 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4217 struct btrfs_inode *dir, struct btrfs_inode *inode,
4218 const struct fscrypt_str *name)
4222 ret = __btrfs_unlink_inode(trans, dir, inode, name, NULL);
4224 drop_nlink(&inode->vfs_inode);
4225 ret = btrfs_update_inode(trans, inode);
4231 * helper to start transaction for unlink and rmdir.
4233 * unlink and rmdir are special in btrfs, they do not always free space, so
4234 * if we cannot make our reservations the normal way try and see if there is
4235 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4236 * allow the unlink to occur.
4238 static struct btrfs_trans_handle *__unlink_start_trans(struct btrfs_inode *dir)
4240 struct btrfs_root *root = dir->root;
4242 return btrfs_start_transaction_fallback_global_rsv(root,
4243 BTRFS_UNLINK_METADATA_UNITS);
4246 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4248 struct btrfs_trans_handle *trans;
4249 struct inode *inode = d_inode(dentry);
4251 struct fscrypt_name fname;
4253 ret = fscrypt_setup_filename(dir, &dentry->d_name, 1, &fname);
4257 /* This needs to handle no-key deletions later on */
4259 trans = __unlink_start_trans(BTRFS_I(dir));
4260 if (IS_ERR(trans)) {
4261 ret = PTR_ERR(trans);
4265 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4268 ret = btrfs_unlink_inode(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4273 if (inode->i_nlink == 0) {
4274 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
4280 btrfs_end_transaction(trans);
4281 btrfs_btree_balance_dirty(BTRFS_I(dir)->root->fs_info);
4283 fscrypt_free_filename(&fname);
4287 static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4288 struct btrfs_inode *dir, struct dentry *dentry)
4290 struct btrfs_root *root = dir->root;
4291 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
4292 struct btrfs_path *path;
4293 struct extent_buffer *leaf;
4294 struct btrfs_dir_item *di;
4295 struct btrfs_key key;
4299 u64 dir_ino = btrfs_ino(dir);
4300 struct fscrypt_name fname;
4302 ret = fscrypt_setup_filename(&dir->vfs_inode, &dentry->d_name, 1, &fname);
4306 /* This needs to handle no-key deletions later on */
4308 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
4309 objectid = btrfs_root_id(inode->root);
4310 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4311 objectid = inode->location.objectid;
4314 fscrypt_free_filename(&fname);
4318 path = btrfs_alloc_path();
4324 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
4325 &fname.disk_name, -1);
4326 if (IS_ERR_OR_NULL(di)) {
4327 ret = di ? PTR_ERR(di) : -ENOENT;
4331 leaf = path->nodes[0];
4332 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4333 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4334 ret = btrfs_delete_one_dir_name(trans, root, path, di);
4336 btrfs_abort_transaction(trans, ret);
4339 btrfs_release_path(path);
4342 * This is a placeholder inode for a subvolume we didn't have a
4343 * reference to at the time of the snapshot creation. In the meantime
4344 * we could have renamed the real subvol link into our snapshot, so
4345 * depending on btrfs_del_root_ref to return -ENOENT here is incorrect.
4346 * Instead simply lookup the dir_index_item for this entry so we can
4347 * remove it. Otherwise we know we have a ref to the root and we can
4348 * call btrfs_del_root_ref, and it _shouldn't_ fail.
4350 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4351 di = btrfs_search_dir_index_item(root, path, dir_ino, &fname.disk_name);
4352 if (IS_ERR_OR_NULL(di)) {
4357 btrfs_abort_transaction(trans, ret);
4361 leaf = path->nodes[0];
4362 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4364 btrfs_release_path(path);
4366 ret = btrfs_del_root_ref(trans, objectid,
4367 btrfs_root_id(root), dir_ino,
4368 &index, &fname.disk_name);
4370 btrfs_abort_transaction(trans, ret);
4375 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
4377 btrfs_abort_transaction(trans, ret);
4381 btrfs_i_size_write(dir, dir->vfs_inode.i_size - fname.disk_name.len * 2);
4382 inode_inc_iversion(&dir->vfs_inode);
4383 inode_set_mtime_to_ts(&dir->vfs_inode, inode_set_ctime_current(&dir->vfs_inode));
4384 ret = btrfs_update_inode_fallback(trans, dir);
4386 btrfs_abort_transaction(trans, ret);
4388 btrfs_free_path(path);
4389 fscrypt_free_filename(&fname);
4394 * Helper to check if the subvolume references other subvolumes or if it's
4397 static noinline int may_destroy_subvol(struct btrfs_root *root)
4399 struct btrfs_fs_info *fs_info = root->fs_info;
4400 struct btrfs_path *path;
4401 struct btrfs_dir_item *di;
4402 struct btrfs_key key;
4403 struct fscrypt_str name = FSTR_INIT("default", 7);
4407 path = btrfs_alloc_path();
4411 /* Make sure this root isn't set as the default subvol */
4412 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4413 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4415 if (di && !IS_ERR(di)) {
4416 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4417 if (key.objectid == btrfs_root_id(root)) {
4420 "deleting default subvolume %llu is not allowed",
4424 btrfs_release_path(path);
4427 key.objectid = btrfs_root_id(root);
4428 key.type = BTRFS_ROOT_REF_KEY;
4429 key.offset = (u64)-1;
4431 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4436 * Key with offset -1 found, there would have to exist a root
4437 * with such id, but this is out of valid range.
4444 if (path->slots[0] > 0) {
4446 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4447 if (key.objectid == btrfs_root_id(root) && key.type == BTRFS_ROOT_REF_KEY)
4451 btrfs_free_path(path);
4455 /* Delete all dentries for inodes belonging to the root */
4456 static void btrfs_prune_dentries(struct btrfs_root *root)
4458 struct btrfs_fs_info *fs_info = root->fs_info;
4459 struct btrfs_inode *inode;
4462 if (!BTRFS_FS_ERROR(fs_info))
4463 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4465 inode = btrfs_find_first_inode(root, min_ino);
4467 if (atomic_read(&inode->vfs_inode.i_count) > 1)
4468 d_prune_aliases(&inode->vfs_inode);
4470 min_ino = btrfs_ino(inode) + 1;
4472 * btrfs_drop_inode() will have it removed from the inode
4473 * cache when its usage count hits zero.
4475 iput(&inode->vfs_inode);
4477 inode = btrfs_find_first_inode(root, min_ino);
4481 int btrfs_delete_subvolume(struct btrfs_inode *dir, struct dentry *dentry)
4483 struct btrfs_root *root = dir->root;
4484 struct btrfs_fs_info *fs_info = root->fs_info;
4485 struct inode *inode = d_inode(dentry);
4486 struct btrfs_root *dest = BTRFS_I(inode)->root;
4487 struct btrfs_trans_handle *trans;
4488 struct btrfs_block_rsv block_rsv;
4490 u64 qgroup_reserved = 0;
4493 down_write(&fs_info->subvol_sem);
4496 * Don't allow to delete a subvolume with send in progress. This is
4497 * inside the inode lock so the error handling that has to drop the bit
4498 * again is not run concurrently.
4500 spin_lock(&dest->root_item_lock);
4501 if (dest->send_in_progress) {
4502 spin_unlock(&dest->root_item_lock);
4504 "attempt to delete subvolume %llu during send",
4505 btrfs_root_id(dest));
4509 if (atomic_read(&dest->nr_swapfiles)) {
4510 spin_unlock(&dest->root_item_lock);
4512 "attempt to delete subvolume %llu with active swapfile",
4513 btrfs_root_id(root));
4517 root_flags = btrfs_root_flags(&dest->root_item);
4518 btrfs_set_root_flags(&dest->root_item,
4519 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4520 spin_unlock(&dest->root_item_lock);
4522 ret = may_destroy_subvol(dest);
4526 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4528 * One for dir inode,
4529 * two for dir entries,
4530 * two for root ref/backref.
4532 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
4535 qgroup_reserved = block_rsv.qgroup_rsv_reserved;
4537 trans = btrfs_start_transaction(root, 0);
4538 if (IS_ERR(trans)) {
4539 ret = PTR_ERR(trans);
4542 ret = btrfs_record_root_in_trans(trans, root);
4544 btrfs_abort_transaction(trans, ret);
4547 btrfs_qgroup_convert_reserved_meta(root, qgroup_reserved);
4548 qgroup_reserved = 0;
4549 trans->block_rsv = &block_rsv;
4550 trans->bytes_reserved = block_rsv.size;
4552 btrfs_record_snapshot_destroy(trans, dir);
4554 ret = btrfs_unlink_subvol(trans, dir, dentry);
4556 btrfs_abort_transaction(trans, ret);
4560 ret = btrfs_record_root_in_trans(trans, dest);
4562 btrfs_abort_transaction(trans, ret);
4566 memset(&dest->root_item.drop_progress, 0,
4567 sizeof(dest->root_item.drop_progress));
4568 btrfs_set_root_drop_level(&dest->root_item, 0);
4569 btrfs_set_root_refs(&dest->root_item, 0);
4571 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4572 ret = btrfs_insert_orphan_item(trans,
4574 btrfs_root_id(dest));
4576 btrfs_abort_transaction(trans, ret);
4581 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
4582 BTRFS_UUID_KEY_SUBVOL, btrfs_root_id(dest));
4583 if (ret && ret != -ENOENT) {
4584 btrfs_abort_transaction(trans, ret);
4587 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
4588 ret = btrfs_uuid_tree_remove(trans,
4589 dest->root_item.received_uuid,
4590 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4591 btrfs_root_id(dest));
4592 if (ret && ret != -ENOENT) {
4593 btrfs_abort_transaction(trans, ret);
4598 free_anon_bdev(dest->anon_dev);
4601 trans->block_rsv = NULL;
4602 trans->bytes_reserved = 0;
4603 ret = btrfs_end_transaction(trans);
4604 inode->i_flags |= S_DEAD;
4606 btrfs_block_rsv_release(fs_info, &block_rsv, (u64)-1, NULL);
4607 if (qgroup_reserved)
4608 btrfs_qgroup_free_meta_prealloc(root, qgroup_reserved);
4611 spin_lock(&dest->root_item_lock);
4612 root_flags = btrfs_root_flags(&dest->root_item);
4613 btrfs_set_root_flags(&dest->root_item,
4614 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4615 spin_unlock(&dest->root_item_lock);
4618 up_write(&fs_info->subvol_sem);
4620 d_invalidate(dentry);
4621 btrfs_prune_dentries(dest);
4622 ASSERT(dest->send_in_progress == 0);
4628 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4630 struct inode *inode = d_inode(dentry);
4631 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
4633 struct btrfs_trans_handle *trans;
4634 u64 last_unlink_trans;
4635 struct fscrypt_name fname;
4637 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
4639 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) {
4640 if (unlikely(btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))) {
4642 "extent tree v2 doesn't support snapshot deletion yet");
4645 return btrfs_delete_subvolume(BTRFS_I(dir), dentry);
4648 ret = fscrypt_setup_filename(dir, &dentry->d_name, 1, &fname);
4652 /* This needs to handle no-key deletions later on */
4654 trans = __unlink_start_trans(BTRFS_I(dir));
4655 if (IS_ERR(trans)) {
4656 ret = PTR_ERR(trans);
4660 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
4661 ret = btrfs_unlink_subvol(trans, BTRFS_I(dir), dentry);
4665 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
4669 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4671 /* now the directory is empty */
4672 ret = btrfs_unlink_inode(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4675 btrfs_i_size_write(BTRFS_I(inode), 0);
4677 * Propagate the last_unlink_trans value of the deleted dir to
4678 * its parent directory. This is to prevent an unrecoverable
4679 * log tree in the case we do something like this:
4681 * 2) create snapshot under dir foo
4682 * 3) delete the snapshot
4685 * 6) fsync foo or some file inside foo
4687 if (last_unlink_trans >= trans->transid)
4688 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4691 btrfs_end_transaction(trans);
4693 btrfs_btree_balance_dirty(fs_info);
4694 fscrypt_free_filename(&fname);
4700 * Read, zero a chunk and write a block.
4702 * @inode - inode that we're zeroing
4703 * @from - the offset to start zeroing
4704 * @len - the length to zero, 0 to zero the entire range respective to the
4706 * @front - zero up to the offset instead of from the offset on
4708 * This will find the block for the "from" offset and cow the block and zero the
4709 * part we want to zero. This is used with truncate and hole punching.
4711 int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
4714 struct btrfs_fs_info *fs_info = inode->root->fs_info;
4715 struct address_space *mapping = inode->vfs_inode.i_mapping;
4716 struct extent_io_tree *io_tree = &inode->io_tree;
4717 struct btrfs_ordered_extent *ordered;
4718 struct extent_state *cached_state = NULL;
4719 struct extent_changeset *data_reserved = NULL;
4720 bool only_release_metadata = false;
4721 u32 blocksize = fs_info->sectorsize;
4722 pgoff_t index = from >> PAGE_SHIFT;
4723 unsigned offset = from & (blocksize - 1);
4724 struct folio *folio;
4725 gfp_t mask = btrfs_alloc_write_mask(mapping);
4726 size_t write_bytes = blocksize;
4731 if (IS_ALIGNED(offset, blocksize) &&
4732 (!len || IS_ALIGNED(len, blocksize)))
4735 block_start = round_down(from, blocksize);
4736 block_end = block_start + blocksize - 1;
4738 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
4741 if (btrfs_check_nocow_lock(inode, block_start, &write_bytes, false) > 0) {
4742 /* For nocow case, no need to reserve data space */
4743 only_release_metadata = true;
4748 ret = btrfs_delalloc_reserve_metadata(inode, blocksize, blocksize, false);
4750 if (!only_release_metadata)
4751 btrfs_free_reserved_data_space(inode, data_reserved,
4752 block_start, blocksize);
4756 folio = __filemap_get_folio(mapping, index,
4757 FGP_LOCK | FGP_ACCESSED | FGP_CREAT, mask);
4758 if (IS_ERR(folio)) {
4759 btrfs_delalloc_release_space(inode, data_reserved, block_start,
4761 btrfs_delalloc_release_extents(inode, blocksize);
4766 if (!folio_test_uptodate(folio)) {
4767 ret = btrfs_read_folio(NULL, folio);
4769 if (folio->mapping != mapping) {
4770 folio_unlock(folio);
4774 if (!folio_test_uptodate(folio)) {
4781 * We unlock the page after the io is completed and then re-lock it
4782 * above. release_folio() could have come in between that and cleared
4783 * folio private, but left the page in the mapping. Set the page mapped
4784 * here to make sure it's properly set for the subpage stuff.
4786 ret = set_folio_extent_mapped(folio);
4790 folio_wait_writeback(folio);
4792 lock_extent(io_tree, block_start, block_end, &cached_state);
4794 ordered = btrfs_lookup_ordered_extent(inode, block_start);
4796 unlock_extent(io_tree, block_start, block_end, &cached_state);
4797 folio_unlock(folio);
4799 btrfs_start_ordered_extent(ordered);
4800 btrfs_put_ordered_extent(ordered);
4804 clear_extent_bit(&inode->io_tree, block_start, block_end,
4805 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4808 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
4811 unlock_extent(io_tree, block_start, block_end, &cached_state);
4815 if (offset != blocksize) {
4817 len = blocksize - offset;
4819 folio_zero_range(folio, block_start - folio_pos(folio),
4822 folio_zero_range(folio,
4823 (block_start - folio_pos(folio)) + offset,
4826 btrfs_folio_clear_checked(fs_info, folio, block_start,
4827 block_end + 1 - block_start);
4828 btrfs_folio_set_dirty(fs_info, folio, block_start,
4829 block_end + 1 - block_start);
4830 unlock_extent(io_tree, block_start, block_end, &cached_state);
4832 if (only_release_metadata)
4833 set_extent_bit(&inode->io_tree, block_start, block_end,
4834 EXTENT_NORESERVE, NULL);
4838 if (only_release_metadata)
4839 btrfs_delalloc_release_metadata(inode, blocksize, true);
4841 btrfs_delalloc_release_space(inode, data_reserved,
4842 block_start, blocksize, true);
4844 btrfs_delalloc_release_extents(inode, blocksize);
4845 folio_unlock(folio);
4848 if (only_release_metadata)
4849 btrfs_check_nocow_unlock(inode);
4850 extent_changeset_free(data_reserved);
4854 static int maybe_insert_hole(struct btrfs_inode *inode, u64 offset, u64 len)
4856 struct btrfs_root *root = inode->root;
4857 struct btrfs_fs_info *fs_info = root->fs_info;
4858 struct btrfs_trans_handle *trans;
4859 struct btrfs_drop_extents_args drop_args = { 0 };
4863 * If NO_HOLES is enabled, we don't need to do anything.
4864 * Later, up in the call chain, either btrfs_set_inode_last_sub_trans()
4865 * or btrfs_update_inode() will be called, which guarantee that the next
4866 * fsync will know this inode was changed and needs to be logged.
4868 if (btrfs_fs_incompat(fs_info, NO_HOLES))
4872 * 1 - for the one we're dropping
4873 * 1 - for the one we're adding
4874 * 1 - for updating the inode.
4876 trans = btrfs_start_transaction(root, 3);
4878 return PTR_ERR(trans);
4880 drop_args.start = offset;
4881 drop_args.end = offset + len;
4882 drop_args.drop_cache = true;
4884 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
4886 btrfs_abort_transaction(trans, ret);
4887 btrfs_end_transaction(trans);
4891 ret = btrfs_insert_hole_extent(trans, root, btrfs_ino(inode), offset, len);
4893 btrfs_abort_transaction(trans, ret);
4895 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
4896 btrfs_update_inode(trans, inode);
4898 btrfs_end_transaction(trans);
4903 * This function puts in dummy file extents for the area we're creating a hole
4904 * for. So if we are truncating this file to a larger size we need to insert
4905 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4906 * the range between oldsize and size
4908 int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)
4910 struct btrfs_root *root = inode->root;
4911 struct btrfs_fs_info *fs_info = root->fs_info;
4912 struct extent_io_tree *io_tree = &inode->io_tree;
4913 struct extent_map *em = NULL;
4914 struct extent_state *cached_state = NULL;
4915 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4916 u64 block_end = ALIGN(size, fs_info->sectorsize);
4923 * If our size started in the middle of a block we need to zero out the
4924 * rest of the block before we expand the i_size, otherwise we could
4925 * expose stale data.
4927 ret = btrfs_truncate_block(inode, oldsize, 0, 0);
4931 if (size <= hole_start)
4934 btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1,
4936 cur_offset = hole_start;
4938 em = btrfs_get_extent(inode, NULL, cur_offset, block_end - cur_offset);
4944 last_byte = min(extent_map_end(em), block_end);
4945 last_byte = ALIGN(last_byte, fs_info->sectorsize);
4946 hole_size = last_byte - cur_offset;
4948 if (!(em->flags & EXTENT_FLAG_PREALLOC)) {
4949 struct extent_map *hole_em;
4951 ret = maybe_insert_hole(inode, cur_offset, hole_size);
4955 ret = btrfs_inode_set_file_extent_range(inode,
4956 cur_offset, hole_size);
4960 hole_em = alloc_extent_map();
4962 btrfs_drop_extent_map_range(inode, cur_offset,
4963 cur_offset + hole_size - 1,
4965 btrfs_set_inode_full_sync(inode);
4968 hole_em->start = cur_offset;
4969 hole_em->len = hole_size;
4970 hole_em->orig_start = cur_offset;
4972 hole_em->block_start = EXTENT_MAP_HOLE;
4973 hole_em->block_len = 0;
4974 hole_em->orig_block_len = 0;
4975 hole_em->ram_bytes = hole_size;
4976 hole_em->generation = btrfs_get_fs_generation(fs_info);
4978 ret = btrfs_replace_extent_map_range(inode, hole_em, true);
4979 free_extent_map(hole_em);
4981 ret = btrfs_inode_set_file_extent_range(inode,
4982 cur_offset, hole_size);
4987 free_extent_map(em);
4989 cur_offset = last_byte;
4990 if (cur_offset >= block_end)
4993 free_extent_map(em);
4994 unlock_extent(io_tree, hole_start, block_end - 1, &cached_state);
4998 static int btrfs_setsize(struct inode *inode, struct iattr *attr)
5000 struct btrfs_root *root = BTRFS_I(inode)->root;
5001 struct btrfs_trans_handle *trans;
5002 loff_t oldsize = i_size_read(inode);
5003 loff_t newsize = attr->ia_size;
5004 int mask = attr->ia_valid;
5008 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5009 * special case where we need to update the times despite not having
5010 * these flags set. For all other operations the VFS set these flags
5011 * explicitly if it wants a timestamp update.
5013 if (newsize != oldsize) {
5014 inode_inc_iversion(inode);
5015 if (!(mask & (ATTR_CTIME | ATTR_MTIME))) {
5016 inode_set_mtime_to_ts(inode,
5017 inode_set_ctime_current(inode));
5021 if (newsize > oldsize) {
5023 * Don't do an expanding truncate while snapshotting is ongoing.
5024 * This is to ensure the snapshot captures a fully consistent
5025 * state of this file - if the snapshot captures this expanding
5026 * truncation, it must capture all writes that happened before
5029 btrfs_drew_write_lock(&root->snapshot_lock);
5030 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize);
5032 btrfs_drew_write_unlock(&root->snapshot_lock);
5036 trans = btrfs_start_transaction(root, 1);
5037 if (IS_ERR(trans)) {
5038 btrfs_drew_write_unlock(&root->snapshot_lock);
5039 return PTR_ERR(trans);
5042 i_size_write(inode, newsize);
5043 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
5044 pagecache_isize_extended(inode, oldsize, newsize);
5045 ret = btrfs_update_inode(trans, BTRFS_I(inode));
5046 btrfs_drew_write_unlock(&root->snapshot_lock);
5047 btrfs_end_transaction(trans);
5049 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
5051 if (btrfs_is_zoned(fs_info)) {
5052 ret = btrfs_wait_ordered_range(inode,
5053 ALIGN(newsize, fs_info->sectorsize),
5060 * We're truncating a file that used to have good data down to
5061 * zero. Make sure any new writes to the file get on disk
5065 set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
5066 &BTRFS_I(inode)->runtime_flags);
5068 truncate_setsize(inode, newsize);
5070 inode_dio_wait(inode);
5072 ret = btrfs_truncate(BTRFS_I(inode), newsize == oldsize);
5073 if (ret && inode->i_nlink) {
5077 * Truncate failed, so fix up the in-memory size. We
5078 * adjusted disk_i_size down as we removed extents, so
5079 * wait for disk_i_size to be stable and then update the
5080 * in-memory size to match.
5082 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5085 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5092 static int btrfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
5095 struct inode *inode = d_inode(dentry);
5096 struct btrfs_root *root = BTRFS_I(inode)->root;
5099 if (btrfs_root_readonly(root))
5102 err = setattr_prepare(idmap, dentry, attr);
5106 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
5107 err = btrfs_setsize(inode, attr);
5112 if (attr->ia_valid) {
5113 setattr_copy(idmap, inode, attr);
5114 inode_inc_iversion(inode);
5115 err = btrfs_dirty_inode(BTRFS_I(inode));
5117 if (!err && attr->ia_valid & ATTR_MODE)
5118 err = posix_acl_chmod(idmap, dentry, inode->i_mode);
5125 * While truncating the inode pages during eviction, we get the VFS
5126 * calling btrfs_invalidate_folio() against each folio of the inode. This
5127 * is slow because the calls to btrfs_invalidate_folio() result in a
5128 * huge amount of calls to lock_extent() and clear_extent_bit(),
5129 * which keep merging and splitting extent_state structures over and over,
5130 * wasting lots of time.
5132 * Therefore if the inode is being evicted, let btrfs_invalidate_folio()
5133 * skip all those expensive operations on a per folio basis and do only
5134 * the ordered io finishing, while we release here the extent_map and
5135 * extent_state structures, without the excessive merging and splitting.
5137 static void evict_inode_truncate_pages(struct inode *inode)
5139 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5140 struct rb_node *node;
5142 ASSERT(inode->i_state & I_FREEING);
5143 truncate_inode_pages_final(&inode->i_data);
5145 btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false);
5148 * Keep looping until we have no more ranges in the io tree.
5149 * We can have ongoing bios started by readahead that have
5150 * their endio callback (extent_io.c:end_bio_extent_readpage)
5151 * still in progress (unlocked the pages in the bio but did not yet
5152 * unlocked the ranges in the io tree). Therefore this means some
5153 * ranges can still be locked and eviction started because before
5154 * submitting those bios, which are executed by a separate task (work
5155 * queue kthread), inode references (inode->i_count) were not taken
5156 * (which would be dropped in the end io callback of each bio).
5157 * Therefore here we effectively end up waiting for those bios and
5158 * anyone else holding locked ranges without having bumped the inode's
5159 * reference count - if we don't do it, when they access the inode's
5160 * io_tree to unlock a range it may be too late, leading to an
5161 * use-after-free issue.
5163 spin_lock(&io_tree->lock);
5164 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5165 struct extent_state *state;
5166 struct extent_state *cached_state = NULL;
5169 unsigned state_flags;
5171 node = rb_first(&io_tree->state);
5172 state = rb_entry(node, struct extent_state, rb_node);
5173 start = state->start;
5175 state_flags = state->state;
5176 spin_unlock(&io_tree->lock);
5178 lock_extent(io_tree, start, end, &cached_state);
5181 * If still has DELALLOC flag, the extent didn't reach disk,
5182 * and its reserved space won't be freed by delayed_ref.
5183 * So we need to free its reserved space here.
5184 * (Refer to comment in btrfs_invalidate_folio, case 2)
5186 * Note, end is the bytenr of last byte, so we need + 1 here.
5188 if (state_flags & EXTENT_DELALLOC)
5189 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5190 end - start + 1, NULL);
5192 clear_extent_bit(io_tree, start, end,
5193 EXTENT_CLEAR_ALL_BITS | EXTENT_DO_ACCOUNTING,
5197 spin_lock(&io_tree->lock);
5199 spin_unlock(&io_tree->lock);
5202 static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
5203 struct btrfs_block_rsv *rsv)
5205 struct btrfs_fs_info *fs_info = root->fs_info;
5206 struct btrfs_trans_handle *trans;
5207 u64 delayed_refs_extra = btrfs_calc_delayed_ref_bytes(fs_info, 1);
5211 * Eviction should be taking place at some place safe because of our
5212 * delayed iputs. However the normal flushing code will run delayed
5213 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5215 * We reserve the delayed_refs_extra here again because we can't use
5216 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5217 * above. We reserve our extra bit here because we generate a ton of
5218 * delayed refs activity by truncating.
5220 * BTRFS_RESERVE_FLUSH_EVICT will steal from the global_rsv if it can,
5221 * if we fail to make this reservation we can re-try without the
5222 * delayed_refs_extra so we can make some forward progress.
5224 ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size + delayed_refs_extra,
5225 BTRFS_RESERVE_FLUSH_EVICT);
5227 ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size,
5228 BTRFS_RESERVE_FLUSH_EVICT);
5231 "could not allocate space for delete; will truncate on mount");
5232 return ERR_PTR(-ENOSPC);
5234 delayed_refs_extra = 0;
5237 trans = btrfs_join_transaction(root);
5241 if (delayed_refs_extra) {
5242 trans->block_rsv = &fs_info->trans_block_rsv;
5243 trans->bytes_reserved = delayed_refs_extra;
5244 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5245 delayed_refs_extra, true);
5250 void btrfs_evict_inode(struct inode *inode)
5252 struct btrfs_fs_info *fs_info;
5253 struct btrfs_trans_handle *trans;
5254 struct btrfs_root *root = BTRFS_I(inode)->root;
5255 struct btrfs_block_rsv *rsv = NULL;
5258 trace_btrfs_inode_evict(inode);
5261 fsverity_cleanup_inode(inode);
5266 fs_info = inode_to_fs_info(inode);
5267 evict_inode_truncate_pages(inode);
5269 if (inode->i_nlink &&
5270 ((btrfs_root_refs(&root->root_item) != 0 &&
5271 btrfs_root_id(root) != BTRFS_ROOT_TREE_OBJECTID) ||
5272 btrfs_is_free_space_inode(BTRFS_I(inode))))
5275 if (is_bad_inode(inode))
5278 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
5281 if (inode->i_nlink > 0) {
5282 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5283 btrfs_root_id(root) != BTRFS_ROOT_TREE_OBJECTID);
5288 * This makes sure the inode item in tree is uptodate and the space for
5289 * the inode update is released.
5291 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
5296 * This drops any pending insert or delete operations we have for this
5297 * inode. We could have a delayed dir index deletion queued up, but
5298 * we're removing the inode completely so that'll be taken care of in
5301 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
5303 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
5306 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
5307 rsv->failfast = true;
5309 btrfs_i_size_write(BTRFS_I(inode), 0);
5312 struct btrfs_truncate_control control = {
5313 .inode = BTRFS_I(inode),
5314 .ino = btrfs_ino(BTRFS_I(inode)),
5319 trans = evict_refill_and_join(root, rsv);
5323 trans->block_rsv = rsv;
5325 ret = btrfs_truncate_inode_items(trans, root, &control);
5326 trans->block_rsv = &fs_info->trans_block_rsv;
5327 btrfs_end_transaction(trans);
5329 * We have not added new delayed items for our inode after we
5330 * have flushed its delayed items, so no need to throttle on
5331 * delayed items. However we have modified extent buffers.
5333 btrfs_btree_balance_dirty_nodelay(fs_info);
5334 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5341 * Errors here aren't a big deal, it just means we leave orphan items in
5342 * the tree. They will be cleaned up on the next mount. If the inode
5343 * number gets reused, cleanup deletes the orphan item without doing
5344 * anything, and unlink reuses the existing orphan item.
5346 * If it turns out that we are dropping too many of these, we might want
5347 * to add a mechanism for retrying these after a commit.
5349 trans = evict_refill_and_join(root, rsv);
5350 if (!IS_ERR(trans)) {
5351 trans->block_rsv = rsv;
5352 btrfs_orphan_del(trans, BTRFS_I(inode));
5353 trans->block_rsv = &fs_info->trans_block_rsv;
5354 btrfs_end_transaction(trans);
5358 btrfs_free_block_rsv(fs_info, rsv);
5360 * If we didn't successfully delete, the orphan item will still be in
5361 * the tree and we'll retry on the next mount. Again, we might also want
5362 * to retry these periodically in the future.
5364 btrfs_remove_delayed_node(BTRFS_I(inode));
5365 fsverity_cleanup_inode(inode);
5370 * Return the key found in the dir entry in the location pointer, fill @type
5371 * with BTRFS_FT_*, and return 0.
5373 * If no dir entries were found, returns -ENOENT.
5374 * If found a corrupted location in dir entry, returns -EUCLEAN.
5376 static int btrfs_inode_by_name(struct btrfs_inode *dir, struct dentry *dentry,
5377 struct btrfs_key *location, u8 *type)
5379 struct btrfs_dir_item *di;
5380 struct btrfs_path *path;
5381 struct btrfs_root *root = dir->root;
5383 struct fscrypt_name fname;
5385 path = btrfs_alloc_path();
5389 ret = fscrypt_setup_filename(&dir->vfs_inode, &dentry->d_name, 1, &fname);
5393 * fscrypt_setup_filename() should never return a positive value, but
5394 * gcc on sparc/parisc thinks it can, so assert that doesn't happen.
5398 /* This needs to handle no-key deletions later on */
5400 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir),
5401 &fname.disk_name, 0);
5402 if (IS_ERR_OR_NULL(di)) {
5403 ret = di ? PTR_ERR(di) : -ENOENT;
5407 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
5408 if (location->type != BTRFS_INODE_ITEM_KEY &&
5409 location->type != BTRFS_ROOT_ITEM_KEY) {
5411 btrfs_warn(root->fs_info,
5412 "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5413 __func__, fname.disk_name.name, btrfs_ino(dir),
5414 location->objectid, location->type, location->offset);
5417 *type = btrfs_dir_ftype(path->nodes[0], di);
5419 fscrypt_free_filename(&fname);
5420 btrfs_free_path(path);
5425 * when we hit a tree root in a directory, the btrfs part of the inode
5426 * needs to be changed to reflect the root directory of the tree root. This
5427 * is kind of like crossing a mount point.
5429 static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
5430 struct btrfs_inode *dir,
5431 struct dentry *dentry,
5432 struct btrfs_key *location,
5433 struct btrfs_root **sub_root)
5435 struct btrfs_path *path;
5436 struct btrfs_root *new_root;
5437 struct btrfs_root_ref *ref;
5438 struct extent_buffer *leaf;
5439 struct btrfs_key key;
5442 struct fscrypt_name fname;
5444 ret = fscrypt_setup_filename(&dir->vfs_inode, &dentry->d_name, 0, &fname);
5448 path = btrfs_alloc_path();
5455 key.objectid = btrfs_root_id(dir->root);
5456 key.type = BTRFS_ROOT_REF_KEY;
5457 key.offset = location->objectid;
5459 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
5466 leaf = path->nodes[0];
5467 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
5468 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
5469 btrfs_root_ref_name_len(leaf, ref) != fname.disk_name.len)
5472 ret = memcmp_extent_buffer(leaf, fname.disk_name.name,
5473 (unsigned long)(ref + 1), fname.disk_name.len);
5477 btrfs_release_path(path);
5479 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
5480 if (IS_ERR(new_root)) {
5481 err = PTR_ERR(new_root);
5485 *sub_root = new_root;
5486 location->objectid = btrfs_root_dirid(&new_root->root_item);
5487 location->type = BTRFS_INODE_ITEM_KEY;
5488 location->offset = 0;
5491 btrfs_free_path(path);
5492 fscrypt_free_filename(&fname);
5496 static void inode_tree_add(struct btrfs_inode *inode)
5498 struct btrfs_root *root = inode->root;
5499 struct btrfs_inode *entry;
5501 struct rb_node *parent;
5502 struct rb_node *new = &inode->rb_node;
5503 u64 ino = btrfs_ino(inode);
5505 if (inode_unhashed(&inode->vfs_inode))
5508 spin_lock(&root->inode_lock);
5509 p = &root->inode_tree.rb_node;
5512 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5514 if (ino < btrfs_ino(entry))
5515 p = &parent->rb_left;
5516 else if (ino > btrfs_ino(entry))
5517 p = &parent->rb_right;
5519 WARN_ON(!(entry->vfs_inode.i_state &
5520 (I_WILL_FREE | I_FREEING)));
5521 rb_replace_node(parent, new, &root->inode_tree);
5522 RB_CLEAR_NODE(parent);
5523 spin_unlock(&root->inode_lock);
5527 rb_link_node(new, parent, p);
5528 rb_insert_color(new, &root->inode_tree);
5529 spin_unlock(&root->inode_lock);
5532 static void inode_tree_del(struct btrfs_inode *inode)
5534 struct btrfs_root *root = inode->root;
5537 spin_lock(&root->inode_lock);
5538 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5539 rb_erase(&inode->rb_node, &root->inode_tree);
5540 RB_CLEAR_NODE(&inode->rb_node);
5541 empty = RB_EMPTY_ROOT(&root->inode_tree);
5543 spin_unlock(&root->inode_lock);
5545 if (empty && btrfs_root_refs(&root->root_item) == 0) {
5546 spin_lock(&root->inode_lock);
5547 empty = RB_EMPTY_ROOT(&root->inode_tree);
5548 spin_unlock(&root->inode_lock);
5550 btrfs_add_dead_root(root);
5555 static int btrfs_init_locked_inode(struct inode *inode, void *p)
5557 struct btrfs_iget_args *args = p;
5559 inode->i_ino = args->ino;
5560 BTRFS_I(inode)->location.objectid = args->ino;
5561 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5562 BTRFS_I(inode)->location.offset = 0;
5563 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5565 if (args->root && args->root == args->root->fs_info->tree_root &&
5566 args->ino != BTRFS_BTREE_INODE_OBJECTID)
5567 set_bit(BTRFS_INODE_FREE_SPACE_INODE,
5568 &BTRFS_I(inode)->runtime_flags);
5572 static int btrfs_find_actor(struct inode *inode, void *opaque)
5574 struct btrfs_iget_args *args = opaque;
5576 return args->ino == BTRFS_I(inode)->location.objectid &&
5577 args->root == BTRFS_I(inode)->root;
5580 static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
5581 struct btrfs_root *root)
5583 struct inode *inode;
5584 struct btrfs_iget_args args;
5585 unsigned long hashval = btrfs_inode_hash(ino, root);
5590 inode = iget5_locked(s, hashval, btrfs_find_actor,
5591 btrfs_init_locked_inode,
5597 * Get an inode object given its inode number and corresponding root.
5598 * Path can be preallocated to prevent recursing back to iget through
5599 * allocator. NULL is also valid but may require an additional allocation
5602 struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
5603 struct btrfs_root *root, struct btrfs_path *path)
5605 struct inode *inode;
5607 inode = btrfs_iget_locked(s, ino, root);
5609 return ERR_PTR(-ENOMEM);
5611 if (inode->i_state & I_NEW) {
5614 ret = btrfs_read_locked_inode(inode, path);
5616 inode_tree_add(BTRFS_I(inode));
5617 unlock_new_inode(inode);
5621 * ret > 0 can come from btrfs_search_slot called by
5622 * btrfs_read_locked_inode, this means the inode item
5627 inode = ERR_PTR(ret);
5634 struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
5636 return btrfs_iget_path(s, ino, root, NULL);
5639 static struct inode *new_simple_dir(struct inode *dir,
5640 struct btrfs_key *key,
5641 struct btrfs_root *root)
5643 struct timespec64 ts;
5644 struct inode *inode = new_inode(dir->i_sb);
5647 return ERR_PTR(-ENOMEM);
5649 BTRFS_I(inode)->root = btrfs_grab_root(root);
5650 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
5651 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
5653 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
5655 * We only need lookup, the rest is read-only and there's no inode
5656 * associated with the dentry
5658 inode->i_op = &simple_dir_inode_operations;
5659 inode->i_opflags &= ~IOP_XATTR;
5660 inode->i_fop = &simple_dir_operations;
5661 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5663 ts = inode_set_ctime_current(inode);
5664 inode_set_mtime_to_ts(inode, ts);
5665 inode_set_atime_to_ts(inode, inode_get_atime(dir));
5666 BTRFS_I(inode)->i_otime_sec = ts.tv_sec;
5667 BTRFS_I(inode)->i_otime_nsec = ts.tv_nsec;
5669 inode->i_uid = dir->i_uid;
5670 inode->i_gid = dir->i_gid;
5675 static_assert(BTRFS_FT_UNKNOWN == FT_UNKNOWN);
5676 static_assert(BTRFS_FT_REG_FILE == FT_REG_FILE);
5677 static_assert(BTRFS_FT_DIR == FT_DIR);
5678 static_assert(BTRFS_FT_CHRDEV == FT_CHRDEV);
5679 static_assert(BTRFS_FT_BLKDEV == FT_BLKDEV);
5680 static_assert(BTRFS_FT_FIFO == FT_FIFO);
5681 static_assert(BTRFS_FT_SOCK == FT_SOCK);
5682 static_assert(BTRFS_FT_SYMLINK == FT_SYMLINK);
5684 static inline u8 btrfs_inode_type(struct inode *inode)
5686 return fs_umode_to_ftype(inode->i_mode);
5689 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
5691 struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
5692 struct inode *inode;
5693 struct btrfs_root *root = BTRFS_I(dir)->root;
5694 struct btrfs_root *sub_root = root;
5695 struct btrfs_key location;
5699 if (dentry->d_name.len > BTRFS_NAME_LEN)
5700 return ERR_PTR(-ENAMETOOLONG);
5702 ret = btrfs_inode_by_name(BTRFS_I(dir), dentry, &location, &di_type);
5704 return ERR_PTR(ret);
5706 if (location.type == BTRFS_INODE_ITEM_KEY) {
5707 inode = btrfs_iget(dir->i_sb, location.objectid, root);
5711 /* Do extra check against inode mode with di_type */
5712 if (btrfs_inode_type(inode) != di_type) {
5714 "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5715 inode->i_mode, btrfs_inode_type(inode),
5718 return ERR_PTR(-EUCLEAN);
5723 ret = fixup_tree_root_location(fs_info, BTRFS_I(dir), dentry,
5724 &location, &sub_root);
5727 inode = ERR_PTR(ret);
5729 inode = new_simple_dir(dir, &location, root);
5731 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
5732 btrfs_put_root(sub_root);
5737 down_read(&fs_info->cleanup_work_sem);
5738 if (!sb_rdonly(inode->i_sb))
5739 ret = btrfs_orphan_cleanup(sub_root);
5740 up_read(&fs_info->cleanup_work_sem);
5743 inode = ERR_PTR(ret);
5750 static int btrfs_dentry_delete(const struct dentry *dentry)
5752 struct btrfs_root *root;
5753 struct inode *inode = d_inode(dentry);
5755 if (!inode && !IS_ROOT(dentry))
5756 inode = d_inode(dentry->d_parent);
5759 root = BTRFS_I(inode)->root;
5760 if (btrfs_root_refs(&root->root_item) == 0)
5763 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5769 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
5772 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
5774 if (inode == ERR_PTR(-ENOENT))
5776 return d_splice_alias(inode, dentry);
5780 * Find the highest existing sequence number in a directory and then set the
5781 * in-memory index_cnt variable to the first free sequence number.
5783 static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
5785 struct btrfs_root *root = inode->root;
5786 struct btrfs_key key, found_key;
5787 struct btrfs_path *path;
5788 struct extent_buffer *leaf;
5791 key.objectid = btrfs_ino(inode);
5792 key.type = BTRFS_DIR_INDEX_KEY;
5793 key.offset = (u64)-1;
5795 path = btrfs_alloc_path();
5799 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5802 /* FIXME: we should be able to handle this */
5807 if (path->slots[0] == 0) {
5808 inode->index_cnt = BTRFS_DIR_START_INDEX;
5814 leaf = path->nodes[0];
5815 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5817 if (found_key.objectid != btrfs_ino(inode) ||
5818 found_key.type != BTRFS_DIR_INDEX_KEY) {
5819 inode->index_cnt = BTRFS_DIR_START_INDEX;
5823 inode->index_cnt = found_key.offset + 1;
5825 btrfs_free_path(path);
5829 static int btrfs_get_dir_last_index(struct btrfs_inode *dir, u64 *index)
5833 btrfs_inode_lock(dir, 0);
5834 if (dir->index_cnt == (u64)-1) {
5835 ret = btrfs_inode_delayed_dir_index_count(dir);
5837 ret = btrfs_set_inode_index_count(dir);
5843 /* index_cnt is the index number of next new entry, so decrement it. */
5844 *index = dir->index_cnt - 1;
5846 btrfs_inode_unlock(dir, 0);
5852 * All this infrastructure exists because dir_emit can fault, and we are holding
5853 * the tree lock when doing readdir. For now just allocate a buffer and copy
5854 * our information into that, and then dir_emit from the buffer. This is
5855 * similar to what NFS does, only we don't keep the buffer around in pagecache
5856 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5857 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5860 static int btrfs_opendir(struct inode *inode, struct file *file)
5862 struct btrfs_file_private *private;
5866 ret = btrfs_get_dir_last_index(BTRFS_I(inode), &last_index);
5870 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5873 private->last_index = last_index;
5874 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5875 if (!private->filldir_buf) {
5879 file->private_data = private;
5883 static loff_t btrfs_dir_llseek(struct file *file, loff_t offset, int whence)
5885 struct btrfs_file_private *private = file->private_data;
5888 ret = btrfs_get_dir_last_index(BTRFS_I(file_inode(file)),
5889 &private->last_index);
5893 return generic_file_llseek(file, offset, whence);
5903 static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5906 struct dir_entry *entry = addr;
5907 char *name = (char *)(entry + 1);
5909 ctx->pos = get_unaligned(&entry->offset);
5910 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5911 get_unaligned(&entry->ino),
5912 get_unaligned(&entry->type)))
5914 addr += sizeof(struct dir_entry) +
5915 get_unaligned(&entry->name_len);
5921 static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
5923 struct inode *inode = file_inode(file);
5924 struct btrfs_root *root = BTRFS_I(inode)->root;
5925 struct btrfs_file_private *private = file->private_data;
5926 struct btrfs_dir_item *di;
5927 struct btrfs_key key;
5928 struct btrfs_key found_key;
5929 struct btrfs_path *path;
5931 LIST_HEAD(ins_list);
5932 LIST_HEAD(del_list);
5939 struct btrfs_key location;
5941 if (!dir_emit_dots(file, ctx))
5944 path = btrfs_alloc_path();
5948 addr = private->filldir_buf;
5949 path->reada = READA_FORWARD;
5951 put = btrfs_readdir_get_delayed_items(inode, private->last_index,
5952 &ins_list, &del_list);
5955 key.type = BTRFS_DIR_INDEX_KEY;
5956 key.offset = ctx->pos;
5957 key.objectid = btrfs_ino(BTRFS_I(inode));
5959 btrfs_for_each_slot(root, &key, &found_key, path, ret) {
5960 struct dir_entry *entry;
5961 struct extent_buffer *leaf = path->nodes[0];
5964 if (found_key.objectid != key.objectid)
5966 if (found_key.type != BTRFS_DIR_INDEX_KEY)
5968 if (found_key.offset < ctx->pos)
5970 if (found_key.offset > private->last_index)
5972 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
5974 di = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
5975 name_len = btrfs_dir_name_len(leaf, di);
5976 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5978 btrfs_release_path(path);
5979 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5982 addr = private->filldir_buf;
5988 ftype = btrfs_dir_flags_to_ftype(btrfs_dir_flags(leaf, di));
5990 name_ptr = (char *)(entry + 1);
5991 read_extent_buffer(leaf, name_ptr,
5992 (unsigned long)(di + 1), name_len);
5993 put_unaligned(name_len, &entry->name_len);
5994 put_unaligned(fs_ftype_to_dtype(ftype), &entry->type);
5995 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5996 put_unaligned(location.objectid, &entry->ino);
5997 put_unaligned(found_key.offset, &entry->offset);
5999 addr += sizeof(struct dir_entry) + name_len;
6000 total_len += sizeof(struct dir_entry) + name_len;
6002 /* Catch error encountered during iteration */
6006 btrfs_release_path(path);
6008 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6012 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
6017 * Stop new entries from being returned after we return the last
6020 * New directory entries are assigned a strictly increasing
6021 * offset. This means that new entries created during readdir
6022 * are *guaranteed* to be seen in the future by that readdir.
6023 * This has broken buggy programs which operate on names as
6024 * they're returned by readdir. Until we re-use freed offsets
6025 * we have this hack to stop new entries from being returned
6026 * under the assumption that they'll never reach this huge
6029 * This is being careful not to overflow 32bit loff_t unless the
6030 * last entry requires it because doing so has broken 32bit apps
6033 if (ctx->pos >= INT_MAX)
6034 ctx->pos = LLONG_MAX;
6041 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
6042 btrfs_free_path(path);
6047 * This is somewhat expensive, updating the tree every time the
6048 * inode changes. But, it is most likely to find the inode in cache.
6049 * FIXME, needs more benchmarking...there are no reasons other than performance
6050 * to keep or drop this code.
6052 static int btrfs_dirty_inode(struct btrfs_inode *inode)
6054 struct btrfs_root *root = inode->root;
6055 struct btrfs_fs_info *fs_info = root->fs_info;
6056 struct btrfs_trans_handle *trans;
6059 if (test_bit(BTRFS_INODE_DUMMY, &inode->runtime_flags))
6062 trans = btrfs_join_transaction(root);
6064 return PTR_ERR(trans);
6066 ret = btrfs_update_inode(trans, inode);
6067 if (ret == -ENOSPC || ret == -EDQUOT) {
6068 /* whoops, lets try again with the full transaction */
6069 btrfs_end_transaction(trans);
6070 trans = btrfs_start_transaction(root, 1);
6072 return PTR_ERR(trans);
6074 ret = btrfs_update_inode(trans, inode);
6076 btrfs_end_transaction(trans);
6077 if (inode->delayed_node)
6078 btrfs_balance_delayed_items(fs_info);
6084 * This is a copy of file_update_time. We need this so we can return error on
6085 * ENOSPC for updating the inode in the case of file write and mmap writes.
6087 static int btrfs_update_time(struct inode *inode, int flags)
6089 struct btrfs_root *root = BTRFS_I(inode)->root;
6092 if (btrfs_root_readonly(root))
6095 dirty = inode_update_timestamps(inode, flags);
6096 return dirty ? btrfs_dirty_inode(BTRFS_I(inode)) : 0;
6100 * helper to find a free sequence number in a given directory. This current
6101 * code is very simple, later versions will do smarter things in the btree
6103 int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
6107 if (dir->index_cnt == (u64)-1) {
6108 ret = btrfs_inode_delayed_dir_index_count(dir);
6110 ret = btrfs_set_inode_index_count(dir);
6116 *index = dir->index_cnt;
6122 static int btrfs_insert_inode_locked(struct inode *inode)
6124 struct btrfs_iget_args args;
6126 args.ino = BTRFS_I(inode)->location.objectid;
6127 args.root = BTRFS_I(inode)->root;
6129 return insert_inode_locked4(inode,
6130 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6131 btrfs_find_actor, &args);
6134 int btrfs_new_inode_prepare(struct btrfs_new_inode_args *args,
6135 unsigned int *trans_num_items)
6137 struct inode *dir = args->dir;
6138 struct inode *inode = args->inode;
6141 if (!args->orphan) {
6142 ret = fscrypt_setup_filename(dir, &args->dentry->d_name, 0,
6148 ret = posix_acl_create(dir, &inode->i_mode, &args->default_acl, &args->acl);
6150 fscrypt_free_filename(&args->fname);
6154 /* 1 to add inode item */
6155 *trans_num_items = 1;
6156 /* 1 to add compression property */
6157 if (BTRFS_I(dir)->prop_compress)
6158 (*trans_num_items)++;
6159 /* 1 to add default ACL xattr */
6160 if (args->default_acl)
6161 (*trans_num_items)++;
6162 /* 1 to add access ACL xattr */
6164 (*trans_num_items)++;
6165 #ifdef CONFIG_SECURITY
6166 /* 1 to add LSM xattr */
6167 if (dir->i_security)
6168 (*trans_num_items)++;
6171 /* 1 to add orphan item */
6172 (*trans_num_items)++;
6176 * 1 to add dir index
6177 * 1 to update parent inode item
6179 * No need for 1 unit for the inode ref item because it is
6180 * inserted in a batch together with the inode item at
6181 * btrfs_create_new_inode().
6183 *trans_num_items += 3;
6188 void btrfs_new_inode_args_destroy(struct btrfs_new_inode_args *args)
6190 posix_acl_release(args->acl);
6191 posix_acl_release(args->default_acl);
6192 fscrypt_free_filename(&args->fname);
6196 * Inherit flags from the parent inode.
6198 * Currently only the compression flags and the cow flags are inherited.
6200 static void btrfs_inherit_iflags(struct btrfs_inode *inode, struct btrfs_inode *dir)
6206 if (flags & BTRFS_INODE_NOCOMPRESS) {
6207 inode->flags &= ~BTRFS_INODE_COMPRESS;
6208 inode->flags |= BTRFS_INODE_NOCOMPRESS;
6209 } else if (flags & BTRFS_INODE_COMPRESS) {
6210 inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
6211 inode->flags |= BTRFS_INODE_COMPRESS;
6214 if (flags & BTRFS_INODE_NODATACOW) {
6215 inode->flags |= BTRFS_INODE_NODATACOW;
6216 if (S_ISREG(inode->vfs_inode.i_mode))
6217 inode->flags |= BTRFS_INODE_NODATASUM;
6220 btrfs_sync_inode_flags_to_i_flags(&inode->vfs_inode);
6223 int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
6224 struct btrfs_new_inode_args *args)
6226 struct timespec64 ts;
6227 struct inode *dir = args->dir;
6228 struct inode *inode = args->inode;
6229 const struct fscrypt_str *name = args->orphan ? NULL : &args->fname.disk_name;
6230 struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
6231 struct btrfs_root *root;
6232 struct btrfs_inode_item *inode_item;
6233 struct btrfs_key *location;
6234 struct btrfs_path *path;
6236 struct btrfs_inode_ref *ref;
6237 struct btrfs_key key[2];
6239 struct btrfs_item_batch batch;
6243 path = btrfs_alloc_path();
6248 BTRFS_I(inode)->root = btrfs_grab_root(BTRFS_I(dir)->root);
6249 root = BTRFS_I(inode)->root;
6251 ret = btrfs_get_free_objectid(root, &objectid);
6254 inode->i_ino = objectid;
6258 * O_TMPFILE, set link count to 0, so that after this point, we
6259 * fill in an inode item with the correct link count.
6261 set_nlink(inode, 0);
6263 trace_btrfs_inode_request(dir);
6265 ret = btrfs_set_inode_index(BTRFS_I(dir), &BTRFS_I(inode)->dir_index);
6269 /* index_cnt is ignored for everything but a dir. */
6270 BTRFS_I(inode)->index_cnt = BTRFS_DIR_START_INDEX;
6271 BTRFS_I(inode)->generation = trans->transid;
6272 inode->i_generation = BTRFS_I(inode)->generation;
6275 * We don't have any capability xattrs set here yet, shortcut any
6276 * queries for the xattrs here. If we add them later via the inode
6277 * security init path or any other path this flag will be cleared.
6279 set_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags);
6282 * Subvolumes don't inherit flags from their parent directory.
6283 * Originally this was probably by accident, but we probably can't
6284 * change it now without compatibility issues.
6287 btrfs_inherit_iflags(BTRFS_I(inode), BTRFS_I(dir));
6289 if (S_ISREG(inode->i_mode)) {
6290 if (btrfs_test_opt(fs_info, NODATASUM))
6291 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6292 if (btrfs_test_opt(fs_info, NODATACOW))
6293 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6294 BTRFS_INODE_NODATASUM;
6297 location = &BTRFS_I(inode)->location;
6298 location->objectid = objectid;
6299 location->offset = 0;
6300 location->type = BTRFS_INODE_ITEM_KEY;
6302 ret = btrfs_insert_inode_locked(inode);
6305 BTRFS_I(dir)->index_cnt--;
6310 * We could have gotten an inode number from somebody who was fsynced
6311 * and then removed in this same transaction, so let's just set full
6312 * sync since it will be a full sync anyway and this will blow away the
6313 * old info in the log.
6315 btrfs_set_inode_full_sync(BTRFS_I(inode));
6317 key[0].objectid = objectid;
6318 key[0].type = BTRFS_INODE_ITEM_KEY;
6321 sizes[0] = sizeof(struct btrfs_inode_item);
6323 if (!args->orphan) {
6325 * Start new inodes with an inode_ref. This is slightly more
6326 * efficient for small numbers of hard links since they will
6327 * be packed into one item. Extended refs will kick in if we
6328 * add more hard links than can fit in the ref item.
6330 key[1].objectid = objectid;
6331 key[1].type = BTRFS_INODE_REF_KEY;
6333 key[1].offset = objectid;
6334 sizes[1] = 2 + sizeof(*ref);
6336 key[1].offset = btrfs_ino(BTRFS_I(dir));
6337 sizes[1] = name->len + sizeof(*ref);
6341 batch.keys = &key[0];
6342 batch.data_sizes = &sizes[0];
6343 batch.total_data_size = sizes[0] + (args->orphan ? 0 : sizes[1]);
6344 batch.nr = args->orphan ? 1 : 2;
6345 ret = btrfs_insert_empty_items(trans, root, path, &batch);
6347 btrfs_abort_transaction(trans, ret);
6351 ts = simple_inode_init_ts(inode);
6352 BTRFS_I(inode)->i_otime_sec = ts.tv_sec;
6353 BTRFS_I(inode)->i_otime_nsec = ts.tv_nsec;
6356 * We're going to fill the inode item now, so at this point the inode
6357 * must be fully initialized.
6360 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6361 struct btrfs_inode_item);
6362 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
6363 sizeof(*inode_item));
6364 fill_inode_item(trans, path->nodes[0], inode_item, inode);
6366 if (!args->orphan) {
6367 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6368 struct btrfs_inode_ref);
6369 ptr = (unsigned long)(ref + 1);
6371 btrfs_set_inode_ref_name_len(path->nodes[0], ref, 2);
6372 btrfs_set_inode_ref_index(path->nodes[0], ref, 0);
6373 write_extent_buffer(path->nodes[0], "..", ptr, 2);
6375 btrfs_set_inode_ref_name_len(path->nodes[0], ref,
6377 btrfs_set_inode_ref_index(path->nodes[0], ref,
6378 BTRFS_I(inode)->dir_index);
6379 write_extent_buffer(path->nodes[0], name->name, ptr,
6384 btrfs_mark_buffer_dirty(trans, path->nodes[0]);
6386 * We don't need the path anymore, plus inheriting properties, adding
6387 * ACLs, security xattrs, orphan item or adding the link, will result in
6388 * allocating yet another path. So just free our path.
6390 btrfs_free_path(path);
6394 struct inode *parent;
6397 * Subvolumes inherit properties from their parent subvolume,
6398 * not the directory they were created in.
6400 parent = btrfs_iget(fs_info->sb, BTRFS_FIRST_FREE_OBJECTID,
6401 BTRFS_I(dir)->root);
6402 if (IS_ERR(parent)) {
6403 ret = PTR_ERR(parent);
6405 ret = btrfs_inode_inherit_props(trans, inode, parent);
6409 ret = btrfs_inode_inherit_props(trans, inode, dir);
6413 "error inheriting props for ino %llu (root %llu): %d",
6414 btrfs_ino(BTRFS_I(inode)), btrfs_root_id(root), ret);
6418 * Subvolumes don't inherit ACLs or get passed to the LSM. This is
6421 if (!args->subvol) {
6422 ret = btrfs_init_inode_security(trans, args);
6424 btrfs_abort_transaction(trans, ret);
6429 inode_tree_add(BTRFS_I(inode));
6431 trace_btrfs_inode_new(inode);
6432 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
6434 btrfs_update_root_times(trans, root);
6437 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
6439 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
6440 0, BTRFS_I(inode)->dir_index);
6443 btrfs_abort_transaction(trans, ret);
6451 * discard_new_inode() calls iput(), but the caller owns the reference
6455 discard_new_inode(inode);
6457 btrfs_free_path(path);
6462 * utility function to add 'inode' into 'parent_inode' with
6463 * a give name and a given sequence number.
6464 * if 'add_backref' is true, also insert a backref from the
6465 * inode to the parent directory.
6467 int btrfs_add_link(struct btrfs_trans_handle *trans,
6468 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
6469 const struct fscrypt_str *name, int add_backref, u64 index)
6472 struct btrfs_key key;
6473 struct btrfs_root *root = parent_inode->root;
6474 u64 ino = btrfs_ino(inode);
6475 u64 parent_ino = btrfs_ino(parent_inode);
6477 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6478 memcpy(&key, &inode->root->root_key, sizeof(key));
6481 key.type = BTRFS_INODE_ITEM_KEY;
6485 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6486 ret = btrfs_add_root_ref(trans, key.objectid,
6487 btrfs_root_id(root), parent_ino,
6489 } else if (add_backref) {
6490 ret = btrfs_insert_inode_ref(trans, root, name,
6491 ino, parent_ino, index);
6494 /* Nothing to clean up yet */
6498 ret = btrfs_insert_dir_item(trans, name, parent_inode, &key,
6499 btrfs_inode_type(&inode->vfs_inode), index);
6500 if (ret == -EEXIST || ret == -EOVERFLOW)
6503 btrfs_abort_transaction(trans, ret);
6507 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
6509 inode_inc_iversion(&parent_inode->vfs_inode);
6511 * If we are replaying a log tree, we do not want to update the mtime
6512 * and ctime of the parent directory with the current time, since the
6513 * log replay procedure is responsible for setting them to their correct
6514 * values (the ones it had when the fsync was done).
6516 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags))
6517 inode_set_mtime_to_ts(&parent_inode->vfs_inode,
6518 inode_set_ctime_current(&parent_inode->vfs_inode));
6520 ret = btrfs_update_inode(trans, parent_inode);
6522 btrfs_abort_transaction(trans, ret);
6526 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6529 err = btrfs_del_root_ref(trans, key.objectid,
6530 btrfs_root_id(root), parent_ino,
6531 &local_index, name);
6533 btrfs_abort_transaction(trans, err);
6534 } else if (add_backref) {
6538 err = btrfs_del_inode_ref(trans, root, name, ino, parent_ino,
6541 btrfs_abort_transaction(trans, err);
6544 /* Return the original error code */
6548 static int btrfs_create_common(struct inode *dir, struct dentry *dentry,
6549 struct inode *inode)
6551 struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
6552 struct btrfs_root *root = BTRFS_I(dir)->root;
6553 struct btrfs_new_inode_args new_inode_args = {
6558 unsigned int trans_num_items;
6559 struct btrfs_trans_handle *trans;
6562 err = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items);
6566 trans = btrfs_start_transaction(root, trans_num_items);
6567 if (IS_ERR(trans)) {
6568 err = PTR_ERR(trans);
6569 goto out_new_inode_args;
6572 err = btrfs_create_new_inode(trans, &new_inode_args);
6574 d_instantiate_new(dentry, inode);
6576 btrfs_end_transaction(trans);
6577 btrfs_btree_balance_dirty(fs_info);
6579 btrfs_new_inode_args_destroy(&new_inode_args);
6586 static int btrfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
6587 struct dentry *dentry, umode_t mode, dev_t rdev)
6589 struct inode *inode;
6591 inode = new_inode(dir->i_sb);
6594 inode_init_owner(idmap, inode, dir, mode);
6595 inode->i_op = &btrfs_special_inode_operations;
6596 init_special_inode(inode, inode->i_mode, rdev);
6597 return btrfs_create_common(dir, dentry, inode);
6600 static int btrfs_create(struct mnt_idmap *idmap, struct inode *dir,
6601 struct dentry *dentry, umode_t mode, bool excl)
6603 struct inode *inode;
6605 inode = new_inode(dir->i_sb);
6608 inode_init_owner(idmap, inode, dir, mode);
6609 inode->i_fop = &btrfs_file_operations;
6610 inode->i_op = &btrfs_file_inode_operations;
6611 inode->i_mapping->a_ops = &btrfs_aops;
6612 return btrfs_create_common(dir, dentry, inode);
6615 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6616 struct dentry *dentry)
6618 struct btrfs_trans_handle *trans = NULL;
6619 struct btrfs_root *root = BTRFS_I(dir)->root;
6620 struct inode *inode = d_inode(old_dentry);
6621 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
6622 struct fscrypt_name fname;
6627 /* do not allow sys_link's with other subvols of the same device */
6628 if (btrfs_root_id(root) != btrfs_root_id(BTRFS_I(inode)->root))
6631 if (inode->i_nlink >= BTRFS_LINK_MAX)
6634 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &fname);
6638 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
6643 * 2 items for inode and inode ref
6644 * 2 items for dir items
6645 * 1 item for parent inode
6646 * 1 item for orphan item deletion if O_TMPFILE
6648 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
6649 if (IS_ERR(trans)) {
6650 err = PTR_ERR(trans);
6655 /* There are several dir indexes for this inode, clear the cache. */
6656 BTRFS_I(inode)->dir_index = 0ULL;
6658 inode_inc_iversion(inode);
6659 inode_set_ctime_current(inode);
6661 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
6663 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6664 &fname.disk_name, 1, index);
6669 struct dentry *parent = dentry->d_parent;
6671 err = btrfs_update_inode(trans, BTRFS_I(inode));
6674 if (inode->i_nlink == 1) {
6676 * If new hard link count is 1, it's a file created
6677 * with open(2) O_TMPFILE flag.
6679 err = btrfs_orphan_del(trans, BTRFS_I(inode));
6683 d_instantiate(dentry, inode);
6684 btrfs_log_new_name(trans, old_dentry, NULL, 0, parent);
6688 fscrypt_free_filename(&fname);
6690 btrfs_end_transaction(trans);
6692 inode_dec_link_count(inode);
6695 btrfs_btree_balance_dirty(fs_info);
6699 static int btrfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
6700 struct dentry *dentry, umode_t mode)
6702 struct inode *inode;
6704 inode = new_inode(dir->i_sb);
6707 inode_init_owner(idmap, inode, dir, S_IFDIR | mode);
6708 inode->i_op = &btrfs_dir_inode_operations;
6709 inode->i_fop = &btrfs_dir_file_operations;
6710 return btrfs_create_common(dir, dentry, inode);
6713 static noinline int uncompress_inline(struct btrfs_path *path,
6715 struct btrfs_file_extent_item *item)
6718 struct extent_buffer *leaf = path->nodes[0];
6721 unsigned long inline_size;
6725 compress_type = btrfs_file_extent_compression(leaf, item);
6726 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6727 inline_size = btrfs_file_extent_inline_item_len(leaf, path->slots[0]);
6728 tmp = kmalloc(inline_size, GFP_NOFS);
6731 ptr = btrfs_file_extent_inline_start(item);
6733 read_extent_buffer(leaf, tmp, ptr, inline_size);
6735 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
6736 ret = btrfs_decompress(compress_type, tmp, page, 0, inline_size, max_size);
6739 * decompression code contains a memset to fill in any space between the end
6740 * of the uncompressed data and the end of max_size in case the decompressed
6741 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6742 * the end of an inline extent and the beginning of the next block, so we
6743 * cover that region here.
6746 if (max_size < PAGE_SIZE)
6747 memzero_page(page, max_size, PAGE_SIZE - max_size);
6752 static int read_inline_extent(struct btrfs_inode *inode, struct btrfs_path *path,
6755 struct btrfs_file_extent_item *fi;
6759 if (!page || PageUptodate(page))
6762 ASSERT(page_offset(page) == 0);
6764 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
6765 struct btrfs_file_extent_item);
6766 if (btrfs_file_extent_compression(path->nodes[0], fi) != BTRFS_COMPRESS_NONE)
6767 return uncompress_inline(path, page, fi);
6769 copy_size = min_t(u64, PAGE_SIZE,
6770 btrfs_file_extent_ram_bytes(path->nodes[0], fi));
6771 kaddr = kmap_local_page(page);
6772 read_extent_buffer(path->nodes[0], kaddr,
6773 btrfs_file_extent_inline_start(fi), copy_size);
6774 kunmap_local(kaddr);
6775 if (copy_size < PAGE_SIZE)
6776 memzero_page(page, copy_size, PAGE_SIZE - copy_size);
6781 * Lookup the first extent overlapping a range in a file.
6783 * @inode: file to search in
6784 * @page: page to read extent data into if the extent is inline
6785 * @start: file offset
6786 * @len: length of range starting at @start
6788 * Return the first &struct extent_map which overlaps the given range, reading
6789 * it from the B-tree and caching it if necessary. Note that there may be more
6790 * extents which overlap the given range after the returned extent_map.
6792 * If @page is not NULL and the extent is inline, this also reads the extent
6793 * data directly into the page and marks the extent up to date in the io_tree.
6795 * Return: ERR_PTR on error, non-NULL extent_map on success.
6797 struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6798 struct page *page, u64 start, u64 len)
6800 struct btrfs_fs_info *fs_info = inode->root->fs_info;
6802 u64 extent_start = 0;
6804 u64 objectid = btrfs_ino(inode);
6805 int extent_type = -1;
6806 struct btrfs_path *path = NULL;
6807 struct btrfs_root *root = inode->root;
6808 struct btrfs_file_extent_item *item;
6809 struct extent_buffer *leaf;
6810 struct btrfs_key found_key;
6811 struct extent_map *em = NULL;
6812 struct extent_map_tree *em_tree = &inode->extent_tree;
6814 read_lock(&em_tree->lock);
6815 em = lookup_extent_mapping(em_tree, start, len);
6816 read_unlock(&em_tree->lock);
6819 if (em->start > start || em->start + em->len <= start)
6820 free_extent_map(em);
6821 else if (em->block_start == EXTENT_MAP_INLINE && page)
6822 free_extent_map(em);
6826 em = alloc_extent_map();
6831 em->start = EXTENT_MAP_HOLE;
6832 em->orig_start = EXTENT_MAP_HOLE;
6834 em->block_len = (u64)-1;
6836 path = btrfs_alloc_path();
6842 /* Chances are we'll be called again, so go ahead and do readahead */
6843 path->reada = READA_FORWARD;
6846 * The same explanation in load_free_space_cache applies here as well,
6847 * we only read when we're loading the free space cache, and at that
6848 * point the commit_root has everything we need.
6850 if (btrfs_is_free_space_inode(inode)) {
6851 path->search_commit_root = 1;
6852 path->skip_locking = 1;
6855 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
6858 } else if (ret > 0) {
6859 if (path->slots[0] == 0)
6865 leaf = path->nodes[0];
6866 item = btrfs_item_ptr(leaf, path->slots[0],
6867 struct btrfs_file_extent_item);
6868 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6869 if (found_key.objectid != objectid ||
6870 found_key.type != BTRFS_EXTENT_DATA_KEY) {
6872 * If we backup past the first extent we want to move forward
6873 * and see if there is an extent in front of us, otherwise we'll
6874 * say there is a hole for our whole search range which can
6881 extent_type = btrfs_file_extent_type(leaf, item);
6882 extent_start = found_key.offset;
6883 extent_end = btrfs_file_extent_end(path);
6884 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6885 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
6886 /* Only regular file could have regular/prealloc extent */
6887 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6890 "regular/prealloc extent found for non-regular inode %llu",
6894 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6896 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
6897 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6902 if (start >= extent_end) {
6904 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6905 ret = btrfs_next_leaf(root, path);
6911 leaf = path->nodes[0];
6913 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6914 if (found_key.objectid != objectid ||
6915 found_key.type != BTRFS_EXTENT_DATA_KEY)
6917 if (start + len <= found_key.offset)
6919 if (start > found_key.offset)
6922 /* New extent overlaps with existing one */
6924 em->orig_start = start;
6925 em->len = found_key.offset - start;
6926 em->block_start = EXTENT_MAP_HOLE;
6930 btrfs_extent_item_to_extent_map(inode, path, item, em);
6932 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6933 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
6935 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
6937 * Inline extent can only exist at file offset 0. This is
6938 * ensured by tree-checker and inline extent creation path.
6939 * Thus all members representing file offsets should be zero.
6941 ASSERT(extent_start == 0);
6942 ASSERT(em->start == 0);
6945 * btrfs_extent_item_to_extent_map() should have properly
6946 * initialized em members already.
6948 * Other members are not utilized for inline extents.
6950 ASSERT(em->block_start == EXTENT_MAP_INLINE);
6951 ASSERT(em->len == fs_info->sectorsize);
6953 ret = read_inline_extent(inode, path, page);
6960 em->orig_start = start;
6962 em->block_start = EXTENT_MAP_HOLE;
6965 btrfs_release_path(path);
6966 if (em->start > start || extent_map_end(em) <= start) {
6968 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6969 em->start, em->len, start, len);
6974 write_lock(&em_tree->lock);
6975 ret = btrfs_add_extent_mapping(inode, &em, start, len);
6976 write_unlock(&em_tree->lock);
6978 btrfs_free_path(path);
6980 trace_btrfs_get_extent(root, inode, em);
6983 free_extent_map(em);
6984 return ERR_PTR(ret);
6989 static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
6990 struct btrfs_dio_data *dio_data,
6993 const u64 orig_start,
6994 const u64 block_start,
6995 const u64 block_len,
6996 const u64 orig_block_len,
6997 const u64 ram_bytes,
7000 struct extent_map *em = NULL;
7001 struct btrfs_ordered_extent *ordered;
7003 if (type != BTRFS_ORDERED_NOCOW) {
7004 em = create_io_em(inode, start, len, orig_start, block_start,
7005 block_len, orig_block_len, ram_bytes,
7006 BTRFS_COMPRESS_NONE, /* compress_type */
7011 ordered = btrfs_alloc_ordered_extent(inode, start, len, len,
7012 block_start, block_len, 0,
7014 (1 << BTRFS_ORDERED_DIRECT),
7015 BTRFS_COMPRESS_NONE);
7016 if (IS_ERR(ordered)) {
7018 free_extent_map(em);
7019 btrfs_drop_extent_map_range(inode, start,
7020 start + len - 1, false);
7022 em = ERR_CAST(ordered);
7024 ASSERT(!dio_data->ordered);
7025 dio_data->ordered = ordered;
7032 static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
7033 struct btrfs_dio_data *dio_data,
7036 struct btrfs_root *root = inode->root;
7037 struct btrfs_fs_info *fs_info = root->fs_info;
7038 struct extent_map *em;
7039 struct btrfs_key ins;
7043 alloc_hint = get_extent_allocation_hint(inode, start, len);
7045 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
7046 0, alloc_hint, &ins, 1, 1);
7047 if (ret == -EAGAIN) {
7048 ASSERT(btrfs_is_zoned(fs_info));
7049 wait_on_bit_io(&inode->root->fs_info->flags, BTRFS_FS_NEED_ZONE_FINISH,
7050 TASK_UNINTERRUPTIBLE);
7054 return ERR_PTR(ret);
7056 em = btrfs_create_dio_extent(inode, dio_data, start, ins.offset, start,
7057 ins.objectid, ins.offset, ins.offset,
7058 ins.offset, BTRFS_ORDERED_REGULAR);
7059 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
7061 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7067 static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
7069 struct btrfs_block_group *block_group;
7070 bool readonly = false;
7072 block_group = btrfs_lookup_block_group(fs_info, bytenr);
7073 if (!block_group || block_group->ro)
7076 btrfs_put_block_group(block_group);
7081 * Check if we can do nocow write into the range [@offset, @offset + @len)
7083 * @offset: File offset
7084 * @len: The length to write, will be updated to the nocow writeable
7086 * @orig_start: (optional) Return the original file offset of the file extent
7087 * @orig_len: (optional) Return the original on-disk length of the file extent
7088 * @ram_bytes: (optional) Return the ram_bytes of the file extent
7089 * @strict: if true, omit optimizations that might force us into unnecessary
7090 * cow. e.g., don't trust generation number.
7093 * >0 and update @len if we can do nocow write
7094 * 0 if we can't do nocow write
7095 * <0 if error happened
7097 * NOTE: This only checks the file extents, caller is responsible to wait for
7098 * any ordered extents.
7100 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
7101 u64 *orig_start, u64 *orig_block_len,
7102 u64 *ram_bytes, bool nowait, bool strict)
7104 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
7105 struct can_nocow_file_extent_args nocow_args = { 0 };
7106 struct btrfs_path *path;
7108 struct extent_buffer *leaf;
7109 struct btrfs_root *root = BTRFS_I(inode)->root;
7110 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7111 struct btrfs_file_extent_item *fi;
7112 struct btrfs_key key;
7115 path = btrfs_alloc_path();
7118 path->nowait = nowait;
7120 ret = btrfs_lookup_file_extent(NULL, root, path,
7121 btrfs_ino(BTRFS_I(inode)), offset, 0);
7126 if (path->slots[0] == 0) {
7127 /* can't find the item, must cow */
7134 leaf = path->nodes[0];
7135 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7136 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
7137 key.type != BTRFS_EXTENT_DATA_KEY) {
7138 /* not our file or wrong item type, must cow */
7142 if (key.offset > offset) {
7143 /* Wrong offset, must cow */
7147 if (btrfs_file_extent_end(path) <= offset)
7150 fi = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
7151 found_type = btrfs_file_extent_type(leaf, fi);
7153 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7155 nocow_args.start = offset;
7156 nocow_args.end = offset + *len - 1;
7157 nocow_args.strict = strict;
7158 nocow_args.free_path = true;
7160 ret = can_nocow_file_extent(path, &key, BTRFS_I(inode), &nocow_args);
7161 /* can_nocow_file_extent() has freed the path. */
7165 /* Treat errors as not being able to NOCOW. */
7171 if (btrfs_extent_readonly(fs_info, nocow_args.disk_bytenr))
7174 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7175 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7178 range_end = round_up(offset + nocow_args.num_bytes,
7179 root->fs_info->sectorsize) - 1;
7180 ret = test_range_bit_exists(io_tree, offset, range_end, EXTENT_DELALLOC);
7188 *orig_start = key.offset - nocow_args.extent_offset;
7190 *orig_block_len = nocow_args.disk_num_bytes;
7192 *len = nocow_args.num_bytes;
7195 btrfs_free_path(path);
7199 static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7200 struct extent_state **cached_state,
7201 unsigned int iomap_flags)
7203 const bool writing = (iomap_flags & IOMAP_WRITE);
7204 const bool nowait = (iomap_flags & IOMAP_NOWAIT);
7205 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7206 struct btrfs_ordered_extent *ordered;
7211 if (!try_lock_extent(io_tree, lockstart, lockend,
7215 lock_extent(io_tree, lockstart, lockend, cached_state);
7218 * We're concerned with the entire range that we're going to be
7219 * doing DIO to, so we need to make sure there's no ordered
7220 * extents in this range.
7222 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
7223 lockend - lockstart + 1);
7226 * We need to make sure there are no buffered pages in this
7227 * range either, we could have raced between the invalidate in
7228 * generic_file_direct_write and locking the extent. The
7229 * invalidate needs to happen so that reads after a write do not
7233 (!writing || !filemap_range_has_page(inode->i_mapping,
7234 lockstart, lockend)))
7237 unlock_extent(io_tree, lockstart, lockend, cached_state);
7241 btrfs_put_ordered_extent(ordered);
7246 * If we are doing a DIO read and the ordered extent we
7247 * found is for a buffered write, we can not wait for it
7248 * to complete and retry, because if we do so we can
7249 * deadlock with concurrent buffered writes on page
7250 * locks. This happens only if our DIO read covers more
7251 * than one extent map, if at this point has already
7252 * created an ordered extent for a previous extent map
7253 * and locked its range in the inode's io tree, and a
7254 * concurrent write against that previous extent map's
7255 * range and this range started (we unlock the ranges
7256 * in the io tree only when the bios complete and
7257 * buffered writes always lock pages before attempting
7258 * to lock range in the io tree).
7261 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7262 btrfs_start_ordered_extent(ordered);
7264 ret = nowait ? -EAGAIN : -ENOTBLK;
7265 btrfs_put_ordered_extent(ordered);
7268 * We could trigger writeback for this range (and wait
7269 * for it to complete) and then invalidate the pages for
7270 * this range (through invalidate_inode_pages2_range()),
7271 * but that can lead us to a deadlock with a concurrent
7272 * call to readahead (a buffered read or a defrag call
7273 * triggered a readahead) on a page lock due to an
7274 * ordered dio extent we created before but did not have
7275 * yet a corresponding bio submitted (whence it can not
7276 * complete), which makes readahead wait for that
7277 * ordered extent to complete while holding a lock on
7280 ret = nowait ? -EAGAIN : -ENOTBLK;
7292 /* The callers of this must take lock_extent() */
7293 static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7294 u64 len, u64 orig_start, u64 block_start,
7295 u64 block_len, u64 orig_block_len,
7296 u64 ram_bytes, int compress_type,
7299 struct extent_map *em;
7303 * Note the missing NOCOW type.
7305 * For pure NOCOW writes, we should not create an io extent map, but
7306 * just reusing the existing one.
7307 * Only PREALLOC writes (NOCOW write into preallocated range) can
7308 * create an io extent map.
7310 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7311 type == BTRFS_ORDERED_COMPRESSED ||
7312 type == BTRFS_ORDERED_REGULAR);
7315 case BTRFS_ORDERED_PREALLOC:
7316 /* Uncompressed extents. */
7317 ASSERT(block_len == len);
7319 /* We're only referring part of a larger preallocated extent. */
7320 ASSERT(block_len <= ram_bytes);
7322 case BTRFS_ORDERED_REGULAR:
7323 /* Uncompressed extents. */
7324 ASSERT(block_len == len);
7326 /* COW results a new extent matching our file extent size. */
7327 ASSERT(orig_block_len == len);
7328 ASSERT(ram_bytes == len);
7330 /* Since it's a new extent, we should not have any offset. */
7331 ASSERT(orig_start == start);
7333 case BTRFS_ORDERED_COMPRESSED:
7334 /* Must be compressed. */
7335 ASSERT(compress_type != BTRFS_COMPRESS_NONE);
7338 * Encoded write can make us to refer to part of the
7339 * uncompressed extent.
7341 ASSERT(len <= ram_bytes);
7345 em = alloc_extent_map();
7347 return ERR_PTR(-ENOMEM);
7350 em->orig_start = orig_start;
7352 em->block_len = block_len;
7353 em->block_start = block_start;
7354 em->orig_block_len = orig_block_len;
7355 em->ram_bytes = ram_bytes;
7356 em->generation = -1;
7357 em->flags |= EXTENT_FLAG_PINNED;
7358 if (type == BTRFS_ORDERED_COMPRESSED)
7359 extent_map_set_compression(em, compress_type);
7361 ret = btrfs_replace_extent_map_range(inode, em, true);
7363 free_extent_map(em);
7364 return ERR_PTR(ret);
7367 /* em got 2 refs now, callers needs to do free_extent_map once. */
7372 static int btrfs_get_blocks_direct_write(struct extent_map **map,
7373 struct inode *inode,
7374 struct btrfs_dio_data *dio_data,
7375 u64 start, u64 *lenp,
7376 unsigned int iomap_flags)
7378 const bool nowait = (iomap_flags & IOMAP_NOWAIT);
7379 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
7380 struct extent_map *em = *map;
7382 u64 block_start, orig_start, orig_block_len, ram_bytes;
7383 struct btrfs_block_group *bg;
7384 bool can_nocow = false;
7385 bool space_reserved = false;
7391 * We don't allocate a new extent in the following cases
7393 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7395 * 2) The extent is marked as PREALLOC. We're good to go here and can
7396 * just use the extent.
7399 if ((em->flags & EXTENT_FLAG_PREALLOC) ||
7400 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7401 em->block_start != EXTENT_MAP_HOLE)) {
7402 if (em->flags & EXTENT_FLAG_PREALLOC)
7403 type = BTRFS_ORDERED_PREALLOC;
7405 type = BTRFS_ORDERED_NOCOW;
7406 len = min(len, em->len - (start - em->start));
7407 block_start = em->block_start + (start - em->start);
7409 if (can_nocow_extent(inode, start, &len, &orig_start,
7410 &orig_block_len, &ram_bytes, false, false) == 1) {
7411 bg = btrfs_inc_nocow_writers(fs_info, block_start);
7419 struct extent_map *em2;
7421 /* We can NOCOW, so only need to reserve metadata space. */
7422 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len, len,
7425 /* Our caller expects us to free the input extent map. */
7426 free_extent_map(em);
7428 btrfs_dec_nocow_writers(bg);
7429 if (nowait && (ret == -ENOSPC || ret == -EDQUOT))
7433 space_reserved = true;
7435 em2 = btrfs_create_dio_extent(BTRFS_I(inode), dio_data, start, len,
7436 orig_start, block_start,
7437 len, orig_block_len,
7439 btrfs_dec_nocow_writers(bg);
7440 if (type == BTRFS_ORDERED_PREALLOC) {
7441 free_extent_map(em);
7451 dio_data->nocow_done = true;
7453 /* Our caller expects us to free the input extent map. */
7454 free_extent_map(em);
7463 * If we could not allocate data space before locking the file
7464 * range and we can't do a NOCOW write, then we have to fail.
7466 if (!dio_data->data_space_reserved) {
7472 * We have to COW and we have already reserved data space before,
7473 * so now we reserve only metadata.
7475 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len, len,
7479 space_reserved = true;
7481 em = btrfs_new_extent_direct(BTRFS_I(inode), dio_data, start, len);
7487 len = min(len, em->len - (start - em->start));
7489 btrfs_delalloc_release_metadata(BTRFS_I(inode),
7490 prev_len - len, true);
7494 * We have created our ordered extent, so we can now release our reservation
7495 * for an outstanding extent.
7497 btrfs_delalloc_release_extents(BTRFS_I(inode), prev_len);
7500 * Need to update the i_size under the extent lock so buffered
7501 * readers will get the updated i_size when we unlock.
7503 if (start + len > i_size_read(inode))
7504 i_size_write(inode, start + len);
7506 if (ret && space_reserved) {
7507 btrfs_delalloc_release_extents(BTRFS_I(inode), len);
7508 btrfs_delalloc_release_metadata(BTRFS_I(inode), len, true);
7514 static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7515 loff_t length, unsigned int flags, struct iomap *iomap,
7516 struct iomap *srcmap)
7518 struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap);
7519 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
7520 struct extent_map *em;
7521 struct extent_state *cached_state = NULL;
7522 struct btrfs_dio_data *dio_data = iter->private;
7523 u64 lockstart, lockend;
7524 const bool write = !!(flags & IOMAP_WRITE);
7527 const u64 data_alloc_len = length;
7528 bool unlock_extents = false;
7531 * We could potentially fault if we have a buffer > PAGE_SIZE, and if
7532 * we're NOWAIT we may submit a bio for a partial range and return
7533 * EIOCBQUEUED, which would result in an errant short read.
7535 * The best way to handle this would be to allow for partial completions
7536 * of iocb's, so we could submit the partial bio, return and fault in
7537 * the rest of the pages, and then submit the io for the rest of the
7538 * range. However we don't have that currently, so simply return
7539 * -EAGAIN at this point so that the normal path is used.
7541 if (!write && (flags & IOMAP_NOWAIT) && length > PAGE_SIZE)
7545 * Cap the size of reads to that usually seen in buffered I/O as we need
7546 * to allocate a contiguous array for the checksums.
7549 len = min_t(u64, len, fs_info->sectorsize * BTRFS_MAX_BIO_SECTORS);
7552 lockend = start + len - 1;
7555 * iomap_dio_rw() only does filemap_write_and_wait_range(), which isn't
7556 * enough if we've written compressed pages to this area, so we need to
7557 * flush the dirty pages again to make absolutely sure that any
7558 * outstanding dirty pages are on disk - the first flush only starts
7559 * compression on the data, while keeping the pages locked, so by the
7560 * time the second flush returns we know bios for the compressed pages
7561 * were submitted and finished, and the pages no longer under writeback.
7563 * If we have a NOWAIT request and we have any pages in the range that
7564 * are locked, likely due to compression still in progress, we don't want
7565 * to block on page locks. We also don't want to block on pages marked as
7566 * dirty or under writeback (same as for the non-compression case).
7567 * iomap_dio_rw() did the same check, but after that and before we got
7568 * here, mmap'ed writes may have happened or buffered reads started
7569 * (readpage() and readahead(), which lock pages), as we haven't locked
7570 * the file range yet.
7572 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7573 &BTRFS_I(inode)->runtime_flags)) {
7574 if (flags & IOMAP_NOWAIT) {
7575 if (filemap_range_needs_writeback(inode->i_mapping,
7576 lockstart, lockend))
7579 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7580 start + length - 1);
7586 memset(dio_data, 0, sizeof(*dio_data));
7589 * We always try to allocate data space and must do it before locking
7590 * the file range, to avoid deadlocks with concurrent writes to the same
7591 * range if the range has several extents and the writes don't expand the
7592 * current i_size (the inode lock is taken in shared mode). If we fail to
7593 * allocate data space here we continue and later, after locking the
7594 * file range, we fail with ENOSPC only if we figure out we can not do a
7597 if (write && !(flags & IOMAP_NOWAIT)) {
7598 ret = btrfs_check_data_free_space(BTRFS_I(inode),
7599 &dio_data->data_reserved,
7600 start, data_alloc_len, false);
7602 dio_data->data_space_reserved = true;
7603 else if (ret && !(BTRFS_I(inode)->flags &
7604 (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)))
7609 * If this errors out it's because we couldn't invalidate pagecache for
7610 * this range and we need to fallback to buffered IO, or we are doing a
7611 * NOWAIT read/write and we need to block.
7613 ret = lock_extent_direct(inode, lockstart, lockend, &cached_state, flags);
7617 em = btrfs_get_extent(BTRFS_I(inode), NULL, start, len);
7624 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7625 * io. INLINE is special, and we could probably kludge it in here, but
7626 * it's still buffered so for safety lets just fall back to the generic
7629 * For COMPRESSED we _have_ to read the entire extent in so we can
7630 * decompress it, so there will be buffering required no matter what we
7631 * do, so go ahead and fallback to buffered.
7633 * We return -ENOTBLK because that's what makes DIO go ahead and go back
7634 * to buffered IO. Don't blame me, this is the price we pay for using
7637 if (extent_map_is_compressed(em) ||
7638 em->block_start == EXTENT_MAP_INLINE) {
7639 free_extent_map(em);
7641 * If we are in a NOWAIT context, return -EAGAIN in order to
7642 * fallback to buffered IO. This is not only because we can
7643 * block with buffered IO (no support for NOWAIT semantics at
7644 * the moment) but also to avoid returning short reads to user
7645 * space - this happens if we were able to read some data from
7646 * previous non-compressed extents and then when we fallback to
7647 * buffered IO, at btrfs_file_read_iter() by calling
7648 * filemap_read(), we fail to fault in pages for the read buffer,
7649 * in which case filemap_read() returns a short read (the number
7650 * of bytes previously read is > 0, so it does not return -EFAULT).
7652 ret = (flags & IOMAP_NOWAIT) ? -EAGAIN : -ENOTBLK;
7656 len = min(len, em->len - (start - em->start));
7659 * If we have a NOWAIT request and the range contains multiple extents
7660 * (or a mix of extents and holes), then we return -EAGAIN to make the
7661 * caller fallback to a context where it can do a blocking (without
7662 * NOWAIT) request. This way we avoid doing partial IO and returning
7663 * success to the caller, which is not optimal for writes and for reads
7664 * it can result in unexpected behaviour for an application.
7666 * When doing a read, because we use IOMAP_DIO_PARTIAL when calling
7667 * iomap_dio_rw(), we can end up returning less data then what the caller
7668 * asked for, resulting in an unexpected, and incorrect, short read.
7669 * That is, the caller asked to read N bytes and we return less than that,
7670 * which is wrong unless we are crossing EOF. This happens if we get a
7671 * page fault error when trying to fault in pages for the buffer that is
7672 * associated to the struct iov_iter passed to iomap_dio_rw(), and we
7673 * have previously submitted bios for other extents in the range, in
7674 * which case iomap_dio_rw() may return us EIOCBQUEUED if not all of
7675 * those bios have completed by the time we get the page fault error,
7676 * which we return back to our caller - we should only return EIOCBQUEUED
7677 * after we have submitted bios for all the extents in the range.
7679 if ((flags & IOMAP_NOWAIT) && len < length) {
7680 free_extent_map(em);
7686 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7687 start, &len, flags);
7690 unlock_extents = true;
7691 /* Recalc len in case the new em is smaller than requested */
7692 len = min(len, em->len - (start - em->start));
7693 if (dio_data->data_space_reserved) {
7695 u64 release_len = 0;
7697 if (dio_data->nocow_done) {
7698 release_offset = start;
7699 release_len = data_alloc_len;
7700 } else if (len < data_alloc_len) {
7701 release_offset = start + len;
7702 release_len = data_alloc_len - len;
7705 if (release_len > 0)
7706 btrfs_free_reserved_data_space(BTRFS_I(inode),
7707 dio_data->data_reserved,
7713 * We need to unlock only the end area that we aren't using.
7714 * The rest is going to be unlocked by the endio routine.
7716 lockstart = start + len;
7717 if (lockstart < lockend)
7718 unlock_extents = true;
7722 unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7725 free_extent_state(cached_state);
7728 * Translate extent map information to iomap.
7729 * We trim the extents (and move the addr) even though iomap code does
7730 * that, since we have locked only the parts we are performing I/O in.
7732 if ((em->block_start == EXTENT_MAP_HOLE) ||
7733 ((em->flags & EXTENT_FLAG_PREALLOC) && !write)) {
7734 iomap->addr = IOMAP_NULL_ADDR;
7735 iomap->type = IOMAP_HOLE;
7737 iomap->addr = em->block_start + (start - em->start);
7738 iomap->type = IOMAP_MAPPED;
7740 iomap->offset = start;
7741 iomap->bdev = fs_info->fs_devices->latest_dev->bdev;
7742 iomap->length = len;
7743 free_extent_map(em);
7748 unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7751 if (dio_data->data_space_reserved) {
7752 btrfs_free_reserved_data_space(BTRFS_I(inode),
7753 dio_data->data_reserved,
7754 start, data_alloc_len);
7755 extent_changeset_free(dio_data->data_reserved);
7761 static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7762 ssize_t written, unsigned int flags, struct iomap *iomap)
7764 struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap);
7765 struct btrfs_dio_data *dio_data = iter->private;
7766 size_t submitted = dio_data->submitted;
7767 const bool write = !!(flags & IOMAP_WRITE);
7770 if (!write && (iomap->type == IOMAP_HOLE)) {
7771 /* If reading from a hole, unlock and return */
7772 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1,
7777 if (submitted < length) {
7779 length -= submitted;
7781 btrfs_finish_ordered_extent(dio_data->ordered, NULL,
7782 pos, length, false);
7784 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7785 pos + length - 1, NULL);
7789 btrfs_put_ordered_extent(dio_data->ordered);
7790 dio_data->ordered = NULL;
7794 extent_changeset_free(dio_data->data_reserved);
7798 static void btrfs_dio_end_io(struct btrfs_bio *bbio)
7800 struct btrfs_dio_private *dip =
7801 container_of(bbio, struct btrfs_dio_private, bbio);
7802 struct btrfs_inode *inode = bbio->inode;
7803 struct bio *bio = &bbio->bio;
7805 if (bio->bi_status) {
7806 btrfs_warn(inode->root->fs_info,
7807 "direct IO failed ino %llu op 0x%0x offset %#llx len %u err no %d",
7808 btrfs_ino(inode), bio->bi_opf,
7809 dip->file_offset, dip->bytes, bio->bi_status);
7812 if (btrfs_op(bio) == BTRFS_MAP_WRITE) {
7813 btrfs_finish_ordered_extent(bbio->ordered, NULL,
7814 dip->file_offset, dip->bytes,
7817 unlock_extent(&inode->io_tree, dip->file_offset,
7818 dip->file_offset + dip->bytes - 1, NULL);
7821 bbio->bio.bi_private = bbio->private;
7822 iomap_dio_bio_end_io(bio);
7825 static void btrfs_dio_submit_io(const struct iomap_iter *iter, struct bio *bio,
7828 struct btrfs_bio *bbio = btrfs_bio(bio);
7829 struct btrfs_dio_private *dip =
7830 container_of(bbio, struct btrfs_dio_private, bbio);
7831 struct btrfs_dio_data *dio_data = iter->private;
7833 btrfs_bio_init(bbio, BTRFS_I(iter->inode)->root->fs_info,
7834 btrfs_dio_end_io, bio->bi_private);
7835 bbio->inode = BTRFS_I(iter->inode);
7836 bbio->file_offset = file_offset;
7838 dip->file_offset = file_offset;
7839 dip->bytes = bio->bi_iter.bi_size;
7841 dio_data->submitted += bio->bi_iter.bi_size;
7844 * Check if we are doing a partial write. If we are, we need to split
7845 * the ordered extent to match the submitted bio. Hang on to the
7846 * remaining unfinishable ordered_extent in dio_data so that it can be
7847 * cancelled in iomap_end to avoid a deadlock wherein faulting the
7848 * remaining pages is blocked on the outstanding ordered extent.
7850 if (iter->flags & IOMAP_WRITE) {
7853 ret = btrfs_extract_ordered_extent(bbio, dio_data->ordered);
7855 btrfs_finish_ordered_extent(dio_data->ordered, NULL,
7856 file_offset, dip->bytes,
7858 bio->bi_status = errno_to_blk_status(ret);
7859 iomap_dio_bio_end_io(bio);
7864 btrfs_submit_bio(bbio, 0);
7867 static const struct iomap_ops btrfs_dio_iomap_ops = {
7868 .iomap_begin = btrfs_dio_iomap_begin,
7869 .iomap_end = btrfs_dio_iomap_end,
7872 static const struct iomap_dio_ops btrfs_dio_ops = {
7873 .submit_io = btrfs_dio_submit_io,
7874 .bio_set = &btrfs_dio_bioset,
7877 ssize_t btrfs_dio_read(struct kiocb *iocb, struct iov_iter *iter, size_t done_before)
7879 struct btrfs_dio_data data = { 0 };
7881 return iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops, &btrfs_dio_ops,
7882 IOMAP_DIO_PARTIAL, &data, done_before);
7885 struct iomap_dio *btrfs_dio_write(struct kiocb *iocb, struct iov_iter *iter,
7888 struct btrfs_dio_data data = { 0 };
7890 return __iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops, &btrfs_dio_ops,
7891 IOMAP_DIO_PARTIAL, &data, done_before);
7894 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7897 struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
7900 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
7905 * fiemap_prep() called filemap_write_and_wait() for the whole possible
7906 * file range (0 to LLONG_MAX), but that is not enough if we have
7907 * compression enabled. The first filemap_fdatawrite_range() only kicks
7908 * in the compression of data (in an async thread) and will return
7909 * before the compression is done and writeback is started. A second
7910 * filemap_fdatawrite_range() is needed to wait for the compression to
7911 * complete and writeback to start. We also need to wait for ordered
7912 * extents to complete, because our fiemap implementation uses mainly
7913 * file extent items to list the extents, searching for extent maps
7914 * only for file ranges with holes or prealloc extents to figure out
7915 * if we have delalloc in those ranges.
7917 if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC) {
7918 ret = btrfs_wait_ordered_range(inode, 0, LLONG_MAX);
7923 btrfs_inode_lock(btrfs_inode, BTRFS_ILOCK_SHARED);
7926 * We did an initial flush to avoid holding the inode's lock while
7927 * triggering writeback and waiting for the completion of IO and ordered
7928 * extents. Now after we locked the inode we do it again, because it's
7929 * possible a new write may have happened in between those two steps.
7931 if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC) {
7932 ret = btrfs_wait_ordered_range(inode, 0, LLONG_MAX);
7934 btrfs_inode_unlock(btrfs_inode, BTRFS_ILOCK_SHARED);
7939 ret = extent_fiemap(btrfs_inode, fieinfo, start, len);
7940 btrfs_inode_unlock(btrfs_inode, BTRFS_ILOCK_SHARED);
7946 * For release_folio() and invalidate_folio() we have a race window where
7947 * folio_end_writeback() is called but the subpage spinlock is not yet released.
7948 * If we continue to release/invalidate the page, we could cause use-after-free
7949 * for subpage spinlock. So this function is to spin and wait for subpage
7952 static void wait_subpage_spinlock(struct page *page)
7954 struct btrfs_fs_info *fs_info = page_to_fs_info(page);
7955 struct folio *folio = page_folio(page);
7956 struct btrfs_subpage *subpage;
7958 if (!btrfs_is_subpage(fs_info, page->mapping))
7961 ASSERT(folio_test_private(folio) && folio_get_private(folio));
7962 subpage = folio_get_private(folio);
7965 * This may look insane as we just acquire the spinlock and release it,
7966 * without doing anything. But we just want to make sure no one is
7967 * still holding the subpage spinlock.
7968 * And since the page is not dirty nor writeback, and we have page
7969 * locked, the only possible way to hold a spinlock is from the endio
7970 * function to clear page writeback.
7972 * Here we just acquire the spinlock so that all existing callers
7973 * should exit and we're safe to release/invalidate the page.
7975 spin_lock_irq(&subpage->lock);
7976 spin_unlock_irq(&subpage->lock);
7979 static bool __btrfs_release_folio(struct folio *folio, gfp_t gfp_flags)
7981 if (try_release_extent_mapping(&folio->page, gfp_flags)) {
7982 wait_subpage_spinlock(&folio->page);
7983 clear_page_extent_mapped(&folio->page);
7989 static bool btrfs_release_folio(struct folio *folio, gfp_t gfp_flags)
7991 if (folio_test_writeback(folio) || folio_test_dirty(folio))
7993 return __btrfs_release_folio(folio, gfp_flags);
7996 #ifdef CONFIG_MIGRATION
7997 static int btrfs_migrate_folio(struct address_space *mapping,
7998 struct folio *dst, struct folio *src,
7999 enum migrate_mode mode)
8001 int ret = filemap_migrate_folio(mapping, dst, src, mode);
8003 if (ret != MIGRATEPAGE_SUCCESS)
8006 if (folio_test_ordered(src)) {
8007 folio_clear_ordered(src);
8008 folio_set_ordered(dst);
8011 return MIGRATEPAGE_SUCCESS;
8014 #define btrfs_migrate_folio NULL
8017 static void btrfs_invalidate_folio(struct folio *folio, size_t offset,
8020 struct btrfs_inode *inode = folio_to_inode(folio);
8021 struct btrfs_fs_info *fs_info = inode->root->fs_info;
8022 struct extent_io_tree *tree = &inode->io_tree;
8023 struct extent_state *cached_state = NULL;
8024 u64 page_start = folio_pos(folio);
8025 u64 page_end = page_start + folio_size(folio) - 1;
8027 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
8030 * We have folio locked so no new ordered extent can be created on this
8031 * page, nor bio can be submitted for this folio.
8033 * But already submitted bio can still be finished on this folio.
8034 * Furthermore, endio function won't skip folio which has Ordered
8035 * (Private2) already cleared, so it's possible for endio and
8036 * invalidate_folio to do the same ordered extent accounting twice
8039 * So here we wait for any submitted bios to finish, so that we won't
8040 * do double ordered extent accounting on the same folio.
8042 folio_wait_writeback(folio);
8043 wait_subpage_spinlock(&folio->page);
8046 * For subpage case, we have call sites like
8047 * btrfs_punch_hole_lock_range() which passes range not aligned to
8049 * If the range doesn't cover the full folio, we don't need to and
8050 * shouldn't clear page extent mapped, as folio->private can still
8051 * record subpage dirty bits for other part of the range.
8053 * For cases that invalidate the full folio even the range doesn't
8054 * cover the full folio, like invalidating the last folio, we're
8055 * still safe to wait for ordered extent to finish.
8057 if (!(offset == 0 && length == folio_size(folio))) {
8058 btrfs_release_folio(folio, GFP_NOFS);
8062 if (!inode_evicting)
8063 lock_extent(tree, page_start, page_end, &cached_state);
8066 while (cur < page_end) {
8067 struct btrfs_ordered_extent *ordered;
8070 u32 extra_flags = 0;
8072 ordered = btrfs_lookup_first_ordered_range(inode, cur,
8073 page_end + 1 - cur);
8075 range_end = page_end;
8077 * No ordered extent covering this range, we are safe
8078 * to delete all extent states in the range.
8080 extra_flags = EXTENT_CLEAR_ALL_BITS;
8083 if (ordered->file_offset > cur) {
8085 * There is a range between [cur, oe->file_offset) not
8086 * covered by any ordered extent.
8087 * We are safe to delete all extent states, and handle
8088 * the ordered extent in the next iteration.
8090 range_end = ordered->file_offset - 1;
8091 extra_flags = EXTENT_CLEAR_ALL_BITS;
8095 range_end = min(ordered->file_offset + ordered->num_bytes - 1,
8097 ASSERT(range_end + 1 - cur < U32_MAX);
8098 range_len = range_end + 1 - cur;
8099 if (!btrfs_folio_test_ordered(fs_info, folio, cur, range_len)) {
8101 * If Ordered (Private2) is cleared, it means endio has
8102 * already been executed for the range.
8103 * We can't delete the extent states as
8104 * btrfs_finish_ordered_io() may still use some of them.
8108 btrfs_folio_clear_ordered(fs_info, folio, cur, range_len);
8111 * IO on this page will never be started, so we need to account
8112 * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
8113 * here, must leave that up for the ordered extent completion.
8115 * This will also unlock the range for incoming
8116 * btrfs_finish_ordered_io().
8118 if (!inode_evicting)
8119 clear_extent_bit(tree, cur, range_end,
8121 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8122 EXTENT_DEFRAG, &cached_state);
8124 spin_lock_irq(&inode->ordered_tree_lock);
8125 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8126 ordered->truncated_len = min(ordered->truncated_len,
8127 cur - ordered->file_offset);
8128 spin_unlock_irq(&inode->ordered_tree_lock);
8131 * If the ordered extent has finished, we're safe to delete all
8132 * the extent states of the range, otherwise
8133 * btrfs_finish_ordered_io() will get executed by endio for
8134 * other pages, so we can't delete extent states.
8136 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8137 cur, range_end + 1 - cur)) {
8138 btrfs_finish_ordered_io(ordered);
8140 * The ordered extent has finished, now we're again
8141 * safe to delete all extent states of the range.
8143 extra_flags = EXTENT_CLEAR_ALL_BITS;
8147 btrfs_put_ordered_extent(ordered);
8149 * Qgroup reserved space handler
8150 * Sector(s) here will be either:
8152 * 1) Already written to disk or bio already finished
8153 * Then its QGROUP_RESERVED bit in io_tree is already cleared.
8154 * Qgroup will be handled by its qgroup_record then.
8155 * btrfs_qgroup_free_data() call will do nothing here.
8157 * 2) Not written to disk yet
8158 * Then btrfs_qgroup_free_data() call will clear the
8159 * QGROUP_RESERVED bit of its io_tree, and free the qgroup
8160 * reserved data space.
8161 * Since the IO will never happen for this page.
8163 btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur, NULL);
8164 if (!inode_evicting) {
8165 clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED |
8166 EXTENT_DELALLOC | EXTENT_UPTODATE |
8167 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG |
8168 extra_flags, &cached_state);
8170 cur = range_end + 1;
8173 * We have iterated through all ordered extents of the page, the page
8174 * should not have Ordered (Private2) anymore, or the above iteration
8175 * did something wrong.
8177 ASSERT(!folio_test_ordered(folio));
8178 btrfs_folio_clear_checked(fs_info, folio, folio_pos(folio), folio_size(folio));
8179 if (!inode_evicting)
8180 __btrfs_release_folio(folio, GFP_NOFS);
8181 clear_page_extent_mapped(&folio->page);
8184 static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback)
8186 struct btrfs_truncate_control control = {
8188 .ino = btrfs_ino(inode),
8189 .min_type = BTRFS_EXTENT_DATA_KEY,
8190 .clear_extent_range = true,
8192 struct btrfs_root *root = inode->root;
8193 struct btrfs_fs_info *fs_info = root->fs_info;
8194 struct btrfs_block_rsv *rsv;
8196 struct btrfs_trans_handle *trans;
8197 u64 mask = fs_info->sectorsize - 1;
8198 const u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
8200 if (!skip_writeback) {
8201 ret = btrfs_wait_ordered_range(&inode->vfs_inode,
8202 inode->vfs_inode.i_size & (~mask),
8209 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8210 * things going on here:
8212 * 1) We need to reserve space to update our inode.
8214 * 2) We need to have something to cache all the space that is going to
8215 * be free'd up by the truncate operation, but also have some slack
8216 * space reserved in case it uses space during the truncate (thank you
8217 * very much snapshotting).
8219 * And we need these to be separate. The fact is we can use a lot of
8220 * space doing the truncate, and we have no earthly idea how much space
8221 * we will use, so we need the truncate reservation to be separate so it
8222 * doesn't end up using space reserved for updating the inode. We also
8223 * need to be able to stop the transaction and start a new one, which
8224 * means we need to be able to update the inode several times, and we
8225 * have no idea of knowing how many times that will be, so we can't just
8226 * reserve 1 item for the entirety of the operation, so that has to be
8227 * done separately as well.
8229 * So that leaves us with
8231 * 1) rsv - for the truncate reservation, which we will steal from the
8232 * transaction reservation.
8233 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
8234 * updating the inode.
8236 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
8239 rsv->size = min_size;
8240 rsv->failfast = true;
8243 * 1 for the truncate slack space
8244 * 1 for updating the inode.
8246 trans = btrfs_start_transaction(root, 2);
8247 if (IS_ERR(trans)) {
8248 ret = PTR_ERR(trans);
8252 /* Migrate the slack space for the truncate to our reserve */
8253 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
8256 * We have reserved 2 metadata units when we started the transaction and
8257 * min_size matches 1 unit, so this should never fail, but if it does,
8258 * it's not critical we just fail truncation.
8261 btrfs_end_transaction(trans);
8265 trans->block_rsv = rsv;
8268 struct extent_state *cached_state = NULL;
8269 const u64 new_size = inode->vfs_inode.i_size;
8270 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
8272 control.new_size = new_size;
8273 lock_extent(&inode->io_tree, lock_start, (u64)-1, &cached_state);
8275 * We want to drop from the next block forward in case this new
8276 * size is not block aligned since we will be keeping the last
8277 * block of the extent just the way it is.
8279 btrfs_drop_extent_map_range(inode,
8280 ALIGN(new_size, fs_info->sectorsize),
8283 ret = btrfs_truncate_inode_items(trans, root, &control);
8285 inode_sub_bytes(&inode->vfs_inode, control.sub_bytes);
8286 btrfs_inode_safe_disk_i_size_write(inode, control.last_size);
8288 unlock_extent(&inode->io_tree, lock_start, (u64)-1, &cached_state);
8290 trans->block_rsv = &fs_info->trans_block_rsv;
8291 if (ret != -ENOSPC && ret != -EAGAIN)
8294 ret = btrfs_update_inode(trans, inode);
8298 btrfs_end_transaction(trans);
8299 btrfs_btree_balance_dirty(fs_info);
8301 trans = btrfs_start_transaction(root, 2);
8302 if (IS_ERR(trans)) {
8303 ret = PTR_ERR(trans);
8308 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
8309 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
8310 rsv, min_size, false);
8312 * We have reserved 2 metadata units when we started the
8313 * transaction and min_size matches 1 unit, so this should never
8314 * fail, but if it does, it's not critical we just fail truncation.
8319 trans->block_rsv = rsv;
8323 * We can't call btrfs_truncate_block inside a trans handle as we could
8324 * deadlock with freeze, if we got BTRFS_NEED_TRUNCATE_BLOCK then we
8325 * know we've truncated everything except the last little bit, and can
8326 * do btrfs_truncate_block and then update the disk_i_size.
8328 if (ret == BTRFS_NEED_TRUNCATE_BLOCK) {
8329 btrfs_end_transaction(trans);
8330 btrfs_btree_balance_dirty(fs_info);
8332 ret = btrfs_truncate_block(inode, inode->vfs_inode.i_size, 0, 0);
8335 trans = btrfs_start_transaction(root, 1);
8336 if (IS_ERR(trans)) {
8337 ret = PTR_ERR(trans);
8340 btrfs_inode_safe_disk_i_size_write(inode, 0);
8346 trans->block_rsv = &fs_info->trans_block_rsv;
8347 ret2 = btrfs_update_inode(trans, inode);
8351 ret2 = btrfs_end_transaction(trans);
8354 btrfs_btree_balance_dirty(fs_info);
8357 btrfs_free_block_rsv(fs_info, rsv);
8359 * So if we truncate and then write and fsync we normally would just
8360 * write the extents that changed, which is a problem if we need to
8361 * first truncate that entire inode. So set this flag so we write out
8362 * all of the extents in the inode to the sync log so we're completely
8365 * If no extents were dropped or trimmed we don't need to force the next
8366 * fsync to truncate all the inode's items from the log and re-log them
8367 * all. This means the truncate operation did not change the file size,
8368 * or changed it to a smaller size but there was only an implicit hole
8369 * between the old i_size and the new i_size, and there were no prealloc
8370 * extents beyond i_size to drop.
8372 if (control.extents_found > 0)
8373 btrfs_set_inode_full_sync(inode);
8378 struct inode *btrfs_new_subvol_inode(struct mnt_idmap *idmap,
8381 struct inode *inode;
8383 inode = new_inode(dir->i_sb);
8386 * Subvolumes don't inherit the sgid bit or the parent's gid if
8387 * the parent's sgid bit is set. This is probably a bug.
8389 inode_init_owner(idmap, inode, NULL,
8390 S_IFDIR | (~current_umask() & S_IRWXUGO));
8391 inode->i_op = &btrfs_dir_inode_operations;
8392 inode->i_fop = &btrfs_dir_file_operations;
8397 struct inode *btrfs_alloc_inode(struct super_block *sb)
8399 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
8400 struct btrfs_inode *ei;
8401 struct inode *inode;
8402 struct extent_io_tree *file_extent_tree = NULL;
8404 /* Self tests may pass a NULL fs_info. */
8405 if (fs_info && !btrfs_fs_incompat(fs_info, NO_HOLES)) {
8406 file_extent_tree = kmalloc(sizeof(struct extent_io_tree), GFP_KERNEL);
8407 if (!file_extent_tree)
8411 ei = alloc_inode_sb(sb, btrfs_inode_cachep, GFP_KERNEL);
8413 kfree(file_extent_tree);
8420 ei->last_sub_trans = 0;
8421 ei->logged_trans = 0;
8422 ei->delalloc_bytes = 0;
8423 ei->new_delalloc_bytes = 0;
8424 ei->defrag_bytes = 0;
8425 ei->disk_i_size = 0;
8429 ei->index_cnt = (u64)-1;
8431 ei->last_unlink_trans = 0;
8432 ei->last_reflink_trans = 0;
8433 ei->last_log_commit = 0;
8435 spin_lock_init(&ei->lock);
8436 ei->outstanding_extents = 0;
8437 if (sb->s_magic != BTRFS_TEST_MAGIC)
8438 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8439 BTRFS_BLOCK_RSV_DELALLOC);
8440 ei->runtime_flags = 0;
8441 ei->prop_compress = BTRFS_COMPRESS_NONE;
8442 ei->defrag_compress = BTRFS_COMPRESS_NONE;
8444 ei->delayed_node = NULL;
8446 ei->i_otime_sec = 0;
8447 ei->i_otime_nsec = 0;
8449 inode = &ei->vfs_inode;
8450 extent_map_tree_init(&ei->extent_tree);
8452 /* This io tree sets the valid inode. */
8453 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO);
8454 ei->io_tree.inode = ei;
8456 ei->file_extent_tree = file_extent_tree;
8457 if (file_extent_tree) {
8458 extent_io_tree_init(fs_info, ei->file_extent_tree,
8459 IO_TREE_INODE_FILE_EXTENT);
8460 /* Lockdep class is set only for the file extent tree. */
8461 lockdep_set_class(&ei->file_extent_tree->lock, &file_extent_tree_class);
8463 mutex_init(&ei->log_mutex);
8464 spin_lock_init(&ei->ordered_tree_lock);
8465 ei->ordered_tree = RB_ROOT;
8466 ei->ordered_tree_last = NULL;
8467 INIT_LIST_HEAD(&ei->delalloc_inodes);
8468 INIT_LIST_HEAD(&ei->delayed_iput);
8469 RB_CLEAR_NODE(&ei->rb_node);
8470 init_rwsem(&ei->i_mmap_lock);
8475 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8476 void btrfs_test_destroy_inode(struct inode *inode)
8478 btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false);
8479 kfree(BTRFS_I(inode)->file_extent_tree);
8480 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8484 void btrfs_free_inode(struct inode *inode)
8486 kfree(BTRFS_I(inode)->file_extent_tree);
8487 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8490 void btrfs_destroy_inode(struct inode *vfs_inode)
8492 struct btrfs_ordered_extent *ordered;
8493 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
8494 struct btrfs_root *root = inode->root;
8495 bool freespace_inode;
8497 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
8498 WARN_ON(vfs_inode->i_data.nrpages);
8499 WARN_ON(inode->block_rsv.reserved);
8500 WARN_ON(inode->block_rsv.size);
8501 WARN_ON(inode->outstanding_extents);
8502 if (!S_ISDIR(vfs_inode->i_mode)) {
8503 WARN_ON(inode->delalloc_bytes);
8504 WARN_ON(inode->new_delalloc_bytes);
8506 WARN_ON(inode->csum_bytes);
8507 WARN_ON(inode->defrag_bytes);
8510 * This can happen where we create an inode, but somebody else also
8511 * created the same inode and we need to destroy the one we already
8518 * If this is a free space inode do not take the ordered extents lockdep
8521 freespace_inode = btrfs_is_free_space_inode(inode);
8524 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8528 btrfs_err(root->fs_info,
8529 "found ordered extent %llu %llu on inode cleanup",
8530 ordered->file_offset, ordered->num_bytes);
8532 if (!freespace_inode)
8533 btrfs_lockdep_acquire(root->fs_info, btrfs_ordered_extent);
8535 btrfs_remove_ordered_extent(inode, ordered);
8536 btrfs_put_ordered_extent(ordered);
8537 btrfs_put_ordered_extent(ordered);
8540 btrfs_qgroup_check_reserved_leak(inode);
8541 inode_tree_del(inode);
8542 btrfs_drop_extent_map_range(inode, 0, (u64)-1, false);
8543 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
8544 btrfs_put_root(inode->root);
8547 int btrfs_drop_inode(struct inode *inode)
8549 struct btrfs_root *root = BTRFS_I(inode)->root;
8554 /* the snap/subvol tree is on deleting */
8555 if (btrfs_root_refs(&root->root_item) == 0)
8558 return generic_drop_inode(inode);
8561 static void init_once(void *foo)
8563 struct btrfs_inode *ei = foo;
8565 inode_init_once(&ei->vfs_inode);
8568 void __cold btrfs_destroy_cachep(void)
8571 * Make sure all delayed rcu free inodes are flushed before we
8575 bioset_exit(&btrfs_dio_bioset);
8576 kmem_cache_destroy(btrfs_inode_cachep);
8579 int __init btrfs_init_cachep(void)
8581 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
8582 sizeof(struct btrfs_inode), 0,
8583 SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
8585 if (!btrfs_inode_cachep)
8588 if (bioset_init(&btrfs_dio_bioset, BIO_POOL_SIZE,
8589 offsetof(struct btrfs_dio_private, bbio.bio),
8595 btrfs_destroy_cachep();
8599 static int btrfs_getattr(struct mnt_idmap *idmap,
8600 const struct path *path, struct kstat *stat,
8601 u32 request_mask, unsigned int flags)
8605 struct inode *inode = d_inode(path->dentry);
8606 u32 blocksize = btrfs_sb(inode->i_sb)->sectorsize;
8607 u32 bi_flags = BTRFS_I(inode)->flags;
8608 u32 bi_ro_flags = BTRFS_I(inode)->ro_flags;
8610 stat->result_mask |= STATX_BTIME;
8611 stat->btime.tv_sec = BTRFS_I(inode)->i_otime_sec;
8612 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime_nsec;
8613 if (bi_flags & BTRFS_INODE_APPEND)
8614 stat->attributes |= STATX_ATTR_APPEND;
8615 if (bi_flags & BTRFS_INODE_COMPRESS)
8616 stat->attributes |= STATX_ATTR_COMPRESSED;
8617 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8618 stat->attributes |= STATX_ATTR_IMMUTABLE;
8619 if (bi_flags & BTRFS_INODE_NODUMP)
8620 stat->attributes |= STATX_ATTR_NODUMP;
8621 if (bi_ro_flags & BTRFS_INODE_RO_VERITY)
8622 stat->attributes |= STATX_ATTR_VERITY;
8624 stat->attributes_mask |= (STATX_ATTR_APPEND |
8625 STATX_ATTR_COMPRESSED |
8626 STATX_ATTR_IMMUTABLE |
8629 generic_fillattr(idmap, request_mask, inode, stat);
8630 stat->dev = BTRFS_I(inode)->root->anon_dev;
8632 stat->subvol = BTRFS_I(inode)->root->root_key.objectid;
8633 stat->result_mask |= STATX_SUBVOL;
8635 spin_lock(&BTRFS_I(inode)->lock);
8636 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
8637 inode_bytes = inode_get_bytes(inode);
8638 spin_unlock(&BTRFS_I(inode)->lock);
8639 stat->blocks = (ALIGN(inode_bytes, blocksize) +
8640 ALIGN(delalloc_bytes, blocksize)) >> SECTOR_SHIFT;
8644 static int btrfs_rename_exchange(struct inode *old_dir,
8645 struct dentry *old_dentry,
8646 struct inode *new_dir,
8647 struct dentry *new_dentry)
8649 struct btrfs_fs_info *fs_info = inode_to_fs_info(old_dir);
8650 struct btrfs_trans_handle *trans;
8651 unsigned int trans_num_items;
8652 struct btrfs_root *root = BTRFS_I(old_dir)->root;
8653 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
8654 struct inode *new_inode = new_dentry->d_inode;
8655 struct inode *old_inode = old_dentry->d_inode;
8656 struct btrfs_rename_ctx old_rename_ctx;
8657 struct btrfs_rename_ctx new_rename_ctx;
8658 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8659 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
8664 bool need_abort = false;
8665 struct fscrypt_name old_fname, new_fname;
8666 struct fscrypt_str *old_name, *new_name;
8669 * For non-subvolumes allow exchange only within one subvolume, in the
8670 * same inode namespace. Two subvolumes (represented as directory) can
8671 * be exchanged as they're a logical link and have a fixed inode number.
8674 (old_ino != BTRFS_FIRST_FREE_OBJECTID ||
8675 new_ino != BTRFS_FIRST_FREE_OBJECTID))
8678 ret = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_fname);
8682 ret = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_fname);
8684 fscrypt_free_filename(&old_fname);
8688 old_name = &old_fname.disk_name;
8689 new_name = &new_fname.disk_name;
8691 /* close the race window with snapshot create/destroy ioctl */
8692 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8693 new_ino == BTRFS_FIRST_FREE_OBJECTID)
8694 down_read(&fs_info->subvol_sem);
8698 * 1 to remove old dir item
8699 * 1 to remove old dir index
8700 * 1 to add new dir item
8701 * 1 to add new dir index
8702 * 1 to update parent inode
8704 * If the parents are the same, we only need to account for one
8706 trans_num_items = (old_dir == new_dir ? 9 : 10);
8707 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8709 * 1 to remove old root ref
8710 * 1 to remove old root backref
8711 * 1 to add new root ref
8712 * 1 to add new root backref
8714 trans_num_items += 4;
8717 * 1 to update inode item
8718 * 1 to remove old inode ref
8719 * 1 to add new inode ref
8721 trans_num_items += 3;
8723 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
8724 trans_num_items += 4;
8726 trans_num_items += 3;
8727 trans = btrfs_start_transaction(root, trans_num_items);
8728 if (IS_ERR(trans)) {
8729 ret = PTR_ERR(trans);
8734 ret = btrfs_record_root_in_trans(trans, dest);
8740 * We need to find a free sequence number both in the source and
8741 * in the destination directory for the exchange.
8743 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
8746 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
8750 BTRFS_I(old_inode)->dir_index = 0ULL;
8751 BTRFS_I(new_inode)->dir_index = 0ULL;
8753 /* Reference for the source. */
8754 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8755 /* force full log commit if subvolume involved. */
8756 btrfs_set_log_full_commit(trans);
8758 ret = btrfs_insert_inode_ref(trans, dest, new_name, old_ino,
8759 btrfs_ino(BTRFS_I(new_dir)),
8766 /* And now for the dest. */
8767 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8768 /* force full log commit if subvolume involved. */
8769 btrfs_set_log_full_commit(trans);
8771 ret = btrfs_insert_inode_ref(trans, root, old_name, new_ino,
8772 btrfs_ino(BTRFS_I(old_dir)),
8776 btrfs_abort_transaction(trans, ret);
8781 /* Update inode version and ctime/mtime. */
8782 inode_inc_iversion(old_dir);
8783 inode_inc_iversion(new_dir);
8784 inode_inc_iversion(old_inode);
8785 inode_inc_iversion(new_inode);
8786 simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
8788 if (old_dentry->d_parent != new_dentry->d_parent) {
8789 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8790 BTRFS_I(old_inode), true);
8791 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8792 BTRFS_I(new_inode), true);
8795 /* src is a subvolume */
8796 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8797 ret = btrfs_unlink_subvol(trans, BTRFS_I(old_dir), old_dentry);
8798 } else { /* src is an inode */
8799 ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir),
8800 BTRFS_I(old_dentry->d_inode),
8801 old_name, &old_rename_ctx);
8803 ret = btrfs_update_inode(trans, BTRFS_I(old_inode));
8806 btrfs_abort_transaction(trans, ret);
8810 /* dest is a subvolume */
8811 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8812 ret = btrfs_unlink_subvol(trans, BTRFS_I(new_dir), new_dentry);
8813 } else { /* dest is an inode */
8814 ret = __btrfs_unlink_inode(trans, BTRFS_I(new_dir),
8815 BTRFS_I(new_dentry->d_inode),
8816 new_name, &new_rename_ctx);
8818 ret = btrfs_update_inode(trans, BTRFS_I(new_inode));
8821 btrfs_abort_transaction(trans, ret);
8825 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
8826 new_name, 0, old_idx);
8828 btrfs_abort_transaction(trans, ret);
8832 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
8833 old_name, 0, new_idx);
8835 btrfs_abort_transaction(trans, ret);
8839 if (old_inode->i_nlink == 1)
8840 BTRFS_I(old_inode)->dir_index = old_idx;
8841 if (new_inode->i_nlink == 1)
8842 BTRFS_I(new_inode)->dir_index = new_idx;
8845 * Now pin the logs of the roots. We do it to ensure that no other task
8846 * can sync the logs while we are in progress with the rename, because
8847 * that could result in an inconsistency in case any of the inodes that
8848 * are part of this rename operation were logged before.
8850 if (old_ino != BTRFS_FIRST_FREE_OBJECTID)
8851 btrfs_pin_log_trans(root);
8852 if (new_ino != BTRFS_FIRST_FREE_OBJECTID)
8853 btrfs_pin_log_trans(dest);
8855 /* Do the log updates for all inodes. */
8856 if (old_ino != BTRFS_FIRST_FREE_OBJECTID)
8857 btrfs_log_new_name(trans, old_dentry, BTRFS_I(old_dir),
8858 old_rename_ctx.index, new_dentry->d_parent);
8859 if (new_ino != BTRFS_FIRST_FREE_OBJECTID)
8860 btrfs_log_new_name(trans, new_dentry, BTRFS_I(new_dir),
8861 new_rename_ctx.index, old_dentry->d_parent);
8863 /* Now unpin the logs. */
8864 if (old_ino != BTRFS_FIRST_FREE_OBJECTID)
8865 btrfs_end_log_trans(root);
8866 if (new_ino != BTRFS_FIRST_FREE_OBJECTID)
8867 btrfs_end_log_trans(dest);
8869 ret2 = btrfs_end_transaction(trans);
8870 ret = ret ? ret : ret2;
8872 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
8873 old_ino == BTRFS_FIRST_FREE_OBJECTID)
8874 up_read(&fs_info->subvol_sem);
8876 fscrypt_free_filename(&new_fname);
8877 fscrypt_free_filename(&old_fname);
8881 static struct inode *new_whiteout_inode(struct mnt_idmap *idmap,
8884 struct inode *inode;
8886 inode = new_inode(dir->i_sb);
8888 inode_init_owner(idmap, inode, dir,
8889 S_IFCHR | WHITEOUT_MODE);
8890 inode->i_op = &btrfs_special_inode_operations;
8891 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
8896 static int btrfs_rename(struct mnt_idmap *idmap,
8897 struct inode *old_dir, struct dentry *old_dentry,
8898 struct inode *new_dir, struct dentry *new_dentry,
8901 struct btrfs_fs_info *fs_info = inode_to_fs_info(old_dir);
8902 struct btrfs_new_inode_args whiteout_args = {
8904 .dentry = old_dentry,
8906 struct btrfs_trans_handle *trans;
8907 unsigned int trans_num_items;
8908 struct btrfs_root *root = BTRFS_I(old_dir)->root;
8909 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
8910 struct inode *new_inode = d_inode(new_dentry);
8911 struct inode *old_inode = d_inode(old_dentry);
8912 struct btrfs_rename_ctx rename_ctx;
8916 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8917 struct fscrypt_name old_fname, new_fname;
8919 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
8922 /* we only allow rename subvolume link between subvolumes */
8923 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8926 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8927 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
8930 if (S_ISDIR(old_inode->i_mode) && new_inode &&
8931 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
8934 ret = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_fname);
8938 ret = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_fname);
8940 fscrypt_free_filename(&old_fname);
8944 /* check for collisions, even if the name isn't there */
8945 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, &new_fname.disk_name);
8947 if (ret == -EEXIST) {
8949 * eexist without a new_inode */
8950 if (WARN_ON(!new_inode)) {
8951 goto out_fscrypt_names;
8954 /* maybe -EOVERFLOW */
8955 goto out_fscrypt_names;
8961 * we're using rename to replace one file with another. Start IO on it
8962 * now so we don't add too much work to the end of the transaction
8964 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
8965 filemap_flush(old_inode->i_mapping);
8967 if (flags & RENAME_WHITEOUT) {
8968 whiteout_args.inode = new_whiteout_inode(idmap, old_dir);
8969 if (!whiteout_args.inode) {
8971 goto out_fscrypt_names;
8973 ret = btrfs_new_inode_prepare(&whiteout_args, &trans_num_items);
8975 goto out_whiteout_inode;
8977 /* 1 to update the old parent inode. */
8978 trans_num_items = 1;
8981 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8982 /* Close the race window with snapshot create/destroy ioctl */
8983 down_read(&fs_info->subvol_sem);
8985 * 1 to remove old root ref
8986 * 1 to remove old root backref
8987 * 1 to add new root ref
8988 * 1 to add new root backref
8990 trans_num_items += 4;
8994 * 1 to remove old inode ref
8995 * 1 to add new inode ref
8997 trans_num_items += 3;
9000 * 1 to remove old dir item
9001 * 1 to remove old dir index
9002 * 1 to add new dir item
9003 * 1 to add new dir index
9005 trans_num_items += 4;
9006 /* 1 to update new parent inode if it's not the same as the old parent */
9007 if (new_dir != old_dir)
9012 * 1 to remove inode ref
9013 * 1 to remove dir item
9014 * 1 to remove dir index
9015 * 1 to possibly add orphan item
9017 trans_num_items += 5;
9019 trans = btrfs_start_transaction(root, trans_num_items);
9020 if (IS_ERR(trans)) {
9021 ret = PTR_ERR(trans);
9026 ret = btrfs_record_root_in_trans(trans, dest);
9031 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
9035 BTRFS_I(old_inode)->dir_index = 0ULL;
9036 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
9037 /* force full log commit if subvolume involved. */
9038 btrfs_set_log_full_commit(trans);
9040 ret = btrfs_insert_inode_ref(trans, dest, &new_fname.disk_name,
9041 old_ino, btrfs_ino(BTRFS_I(new_dir)),
9047 inode_inc_iversion(old_dir);
9048 inode_inc_iversion(new_dir);
9049 inode_inc_iversion(old_inode);
9050 simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
9052 if (old_dentry->d_parent != new_dentry->d_parent)
9053 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9054 BTRFS_I(old_inode), true);
9056 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
9057 ret = btrfs_unlink_subvol(trans, BTRFS_I(old_dir), old_dentry);
9059 ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir),
9060 BTRFS_I(d_inode(old_dentry)),
9061 &old_fname.disk_name, &rename_ctx);
9063 ret = btrfs_update_inode(trans, BTRFS_I(old_inode));
9066 btrfs_abort_transaction(trans, ret);
9071 inode_inc_iversion(new_inode);
9072 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
9073 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9074 ret = btrfs_unlink_subvol(trans, BTRFS_I(new_dir), new_dentry);
9075 BUG_ON(new_inode->i_nlink == 0);
9077 ret = btrfs_unlink_inode(trans, BTRFS_I(new_dir),
9078 BTRFS_I(d_inode(new_dentry)),
9079 &new_fname.disk_name);
9081 if (!ret && new_inode->i_nlink == 0)
9082 ret = btrfs_orphan_add(trans,
9083 BTRFS_I(d_inode(new_dentry)));
9085 btrfs_abort_transaction(trans, ret);
9090 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
9091 &new_fname.disk_name, 0, index);
9093 btrfs_abort_transaction(trans, ret);
9097 if (old_inode->i_nlink == 1)
9098 BTRFS_I(old_inode)->dir_index = index;
9100 if (old_ino != BTRFS_FIRST_FREE_OBJECTID)
9101 btrfs_log_new_name(trans, old_dentry, BTRFS_I(old_dir),
9102 rename_ctx.index, new_dentry->d_parent);
9104 if (flags & RENAME_WHITEOUT) {
9105 ret = btrfs_create_new_inode(trans, &whiteout_args);
9107 btrfs_abort_transaction(trans, ret);
9110 unlock_new_inode(whiteout_args.inode);
9111 iput(whiteout_args.inode);
9112 whiteout_args.inode = NULL;
9116 ret2 = btrfs_end_transaction(trans);
9117 ret = ret ? ret : ret2;
9119 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9120 up_read(&fs_info->subvol_sem);
9121 if (flags & RENAME_WHITEOUT)
9122 btrfs_new_inode_args_destroy(&whiteout_args);
9124 if (flags & RENAME_WHITEOUT)
9125 iput(whiteout_args.inode);
9127 fscrypt_free_filename(&old_fname);
9128 fscrypt_free_filename(&new_fname);
9132 static int btrfs_rename2(struct mnt_idmap *idmap, struct inode *old_dir,
9133 struct dentry *old_dentry, struct inode *new_dir,
9134 struct dentry *new_dentry, unsigned int flags)
9138 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
9141 if (flags & RENAME_EXCHANGE)
9142 ret = btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9145 ret = btrfs_rename(idmap, old_dir, old_dentry, new_dir,
9148 btrfs_btree_balance_dirty(BTRFS_I(new_dir)->root->fs_info);
9153 struct btrfs_delalloc_work {
9154 struct inode *inode;
9155 struct completion completion;
9156 struct list_head list;
9157 struct btrfs_work work;
9160 static void btrfs_run_delalloc_work(struct btrfs_work *work)
9162 struct btrfs_delalloc_work *delalloc_work;
9163 struct inode *inode;
9165 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9167 inode = delalloc_work->inode;
9168 filemap_flush(inode->i_mapping);
9169 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9170 &BTRFS_I(inode)->runtime_flags))
9171 filemap_flush(inode->i_mapping);
9174 complete(&delalloc_work->completion);
9177 static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
9179 struct btrfs_delalloc_work *work;
9181 work = kmalloc(sizeof(*work), GFP_NOFS);
9185 init_completion(&work->completion);
9186 INIT_LIST_HEAD(&work->list);
9187 work->inode = inode;
9188 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL);
9194 * some fairly slow code that needs optimization. This walks the list
9195 * of all the inodes with pending delalloc and forces them to disk.
9197 static int start_delalloc_inodes(struct btrfs_root *root,
9198 struct writeback_control *wbc, bool snapshot,
9199 bool in_reclaim_context)
9201 struct btrfs_inode *binode;
9202 struct inode *inode;
9203 struct btrfs_delalloc_work *work, *next;
9207 bool full_flush = wbc->nr_to_write == LONG_MAX;
9209 mutex_lock(&root->delalloc_mutex);
9210 spin_lock(&root->delalloc_lock);
9211 list_splice_init(&root->delalloc_inodes, &splice);
9212 while (!list_empty(&splice)) {
9213 binode = list_entry(splice.next, struct btrfs_inode,
9216 list_move_tail(&binode->delalloc_inodes,
9217 &root->delalloc_inodes);
9219 if (in_reclaim_context &&
9220 test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
9223 inode = igrab(&binode->vfs_inode);
9225 cond_resched_lock(&root->delalloc_lock);
9228 spin_unlock(&root->delalloc_lock);
9231 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9232 &binode->runtime_flags);
9234 work = btrfs_alloc_delalloc_work(inode);
9240 list_add_tail(&work->list, &works);
9241 btrfs_queue_work(root->fs_info->flush_workers,
9244 ret = filemap_fdatawrite_wbc(inode->i_mapping, wbc);
9245 btrfs_add_delayed_iput(BTRFS_I(inode));
9246 if (ret || wbc->nr_to_write <= 0)
9250 spin_lock(&root->delalloc_lock);
9252 spin_unlock(&root->delalloc_lock);
9255 list_for_each_entry_safe(work, next, &works, list) {
9256 list_del_init(&work->list);
9257 wait_for_completion(&work->completion);
9261 if (!list_empty(&splice)) {
9262 spin_lock(&root->delalloc_lock);
9263 list_splice_tail(&splice, &root->delalloc_inodes);
9264 spin_unlock(&root->delalloc_lock);
9266 mutex_unlock(&root->delalloc_mutex);
9270 int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context)
9272 struct writeback_control wbc = {
9273 .nr_to_write = LONG_MAX,
9274 .sync_mode = WB_SYNC_NONE,
9276 .range_end = LLONG_MAX,
9278 struct btrfs_fs_info *fs_info = root->fs_info;
9280 if (BTRFS_FS_ERROR(fs_info))
9283 return start_delalloc_inodes(root, &wbc, true, in_reclaim_context);
9286 int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
9287 bool in_reclaim_context)
9289 struct writeback_control wbc = {
9291 .sync_mode = WB_SYNC_NONE,
9293 .range_end = LLONG_MAX,
9295 struct btrfs_root *root;
9299 if (BTRFS_FS_ERROR(fs_info))
9302 mutex_lock(&fs_info->delalloc_root_mutex);
9303 spin_lock(&fs_info->delalloc_root_lock);
9304 list_splice_init(&fs_info->delalloc_roots, &splice);
9305 while (!list_empty(&splice)) {
9307 * Reset nr_to_write here so we know that we're doing a full
9311 wbc.nr_to_write = LONG_MAX;
9313 root = list_first_entry(&splice, struct btrfs_root,
9315 root = btrfs_grab_root(root);
9317 list_move_tail(&root->delalloc_root,
9318 &fs_info->delalloc_roots);
9319 spin_unlock(&fs_info->delalloc_root_lock);
9321 ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
9322 btrfs_put_root(root);
9323 if (ret < 0 || wbc.nr_to_write <= 0)
9325 spin_lock(&fs_info->delalloc_root_lock);
9327 spin_unlock(&fs_info->delalloc_root_lock);
9331 if (!list_empty(&splice)) {
9332 spin_lock(&fs_info->delalloc_root_lock);
9333 list_splice_tail(&splice, &fs_info->delalloc_roots);
9334 spin_unlock(&fs_info->delalloc_root_lock);
9336 mutex_unlock(&fs_info->delalloc_root_mutex);
9340 static int btrfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
9341 struct dentry *dentry, const char *symname)
9343 struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
9344 struct btrfs_trans_handle *trans;
9345 struct btrfs_root *root = BTRFS_I(dir)->root;
9346 struct btrfs_path *path;
9347 struct btrfs_key key;
9348 struct inode *inode;
9349 struct btrfs_new_inode_args new_inode_args = {
9353 unsigned int trans_num_items;
9358 struct btrfs_file_extent_item *ei;
9359 struct extent_buffer *leaf;
9361 name_len = strlen(symname);
9362 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
9363 return -ENAMETOOLONG;
9365 inode = new_inode(dir->i_sb);
9368 inode_init_owner(idmap, inode, dir, S_IFLNK | S_IRWXUGO);
9369 inode->i_op = &btrfs_symlink_inode_operations;
9370 inode_nohighmem(inode);
9371 inode->i_mapping->a_ops = &btrfs_aops;
9372 btrfs_i_size_write(BTRFS_I(inode), name_len);
9373 inode_set_bytes(inode, name_len);
9375 new_inode_args.inode = inode;
9376 err = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items);
9379 /* 1 additional item for the inline extent */
9382 trans = btrfs_start_transaction(root, trans_num_items);
9383 if (IS_ERR(trans)) {
9384 err = PTR_ERR(trans);
9385 goto out_new_inode_args;
9388 err = btrfs_create_new_inode(trans, &new_inode_args);
9392 path = btrfs_alloc_path();
9395 btrfs_abort_transaction(trans, err);
9396 discard_new_inode(inode);
9400 key.objectid = btrfs_ino(BTRFS_I(inode));
9402 key.type = BTRFS_EXTENT_DATA_KEY;
9403 datasize = btrfs_file_extent_calc_inline_size(name_len);
9404 err = btrfs_insert_empty_item(trans, root, path, &key,
9407 btrfs_abort_transaction(trans, err);
9408 btrfs_free_path(path);
9409 discard_new_inode(inode);
9413 leaf = path->nodes[0];
9414 ei = btrfs_item_ptr(leaf, path->slots[0],
9415 struct btrfs_file_extent_item);
9416 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9417 btrfs_set_file_extent_type(leaf, ei,
9418 BTRFS_FILE_EXTENT_INLINE);
9419 btrfs_set_file_extent_encryption(leaf, ei, 0);
9420 btrfs_set_file_extent_compression(leaf, ei, 0);
9421 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9422 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9424 ptr = btrfs_file_extent_inline_start(ei);
9425 write_extent_buffer(leaf, symname, ptr, name_len);
9426 btrfs_mark_buffer_dirty(trans, leaf);
9427 btrfs_free_path(path);
9429 d_instantiate_new(dentry, inode);
9432 btrfs_end_transaction(trans);
9433 btrfs_btree_balance_dirty(fs_info);
9435 btrfs_new_inode_args_destroy(&new_inode_args);
9442 static struct btrfs_trans_handle *insert_prealloc_file_extent(
9443 struct btrfs_trans_handle *trans_in,
9444 struct btrfs_inode *inode,
9445 struct btrfs_key *ins,
9448 struct btrfs_file_extent_item stack_fi;
9449 struct btrfs_replace_extent_info extent_info;
9450 struct btrfs_trans_handle *trans = trans_in;
9451 struct btrfs_path *path;
9452 u64 start = ins->objectid;
9453 u64 len = ins->offset;
9454 u64 qgroup_released = 0;
9457 memset(&stack_fi, 0, sizeof(stack_fi));
9459 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9460 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9461 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9462 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9463 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9464 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9465 /* Encryption and other encoding is reserved and all 0 */
9467 ret = btrfs_qgroup_release_data(inode, file_offset, len, &qgroup_released);
9469 return ERR_PTR(ret);
9472 ret = insert_reserved_file_extent(trans, inode,
9473 file_offset, &stack_fi,
9474 true, qgroup_released);
9480 extent_info.disk_offset = start;
9481 extent_info.disk_len = len;
9482 extent_info.data_offset = 0;
9483 extent_info.data_len = len;
9484 extent_info.file_offset = file_offset;
9485 extent_info.extent_buf = (char *)&stack_fi;
9486 extent_info.is_new_extent = true;
9487 extent_info.update_times = true;
9488 extent_info.qgroup_reserved = qgroup_released;
9489 extent_info.insertions = 0;
9491 path = btrfs_alloc_path();
9497 ret = btrfs_replace_file_extents(inode, path, file_offset,
9498 file_offset + len - 1, &extent_info,
9500 btrfs_free_path(path);
9507 * We have released qgroup data range at the beginning of the function,
9508 * and normally qgroup_released bytes will be freed when committing
9510 * But if we error out early, we have to free what we have released
9511 * or we leak qgroup data reservation.
9513 btrfs_qgroup_free_refroot(inode->root->fs_info,
9514 btrfs_root_id(inode->root), qgroup_released,
9515 BTRFS_QGROUP_RSV_DATA);
9516 return ERR_PTR(ret);
9519 static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9520 u64 start, u64 num_bytes, u64 min_size,
9521 loff_t actual_len, u64 *alloc_hint,
9522 struct btrfs_trans_handle *trans)
9524 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
9525 struct extent_map *em;
9526 struct btrfs_root *root = BTRFS_I(inode)->root;
9527 struct btrfs_key ins;
9528 u64 cur_offset = start;
9529 u64 clear_offset = start;
9532 u64 last_alloc = (u64)-1;
9534 bool own_trans = true;
9535 u64 end = start + num_bytes - 1;
9539 while (num_bytes > 0) {
9540 cur_bytes = min_t(u64, num_bytes, SZ_256M);
9541 cur_bytes = max(cur_bytes, min_size);
9543 * If we are severely fragmented we could end up with really
9544 * small allocations, so if the allocator is returning small
9545 * chunks lets make its job easier by only searching for those
9548 cur_bytes = min(cur_bytes, last_alloc);
9549 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9550 min_size, 0, *alloc_hint, &ins, 1, 0);
9555 * We've reserved this space, and thus converted it from
9556 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9557 * from here on out we will only need to clear our reservation
9558 * for the remaining unreserved area, so advance our
9559 * clear_offset by our extent size.
9561 clear_offset += ins.offset;
9563 last_alloc = ins.offset;
9564 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
9567 * Now that we inserted the prealloc extent we can finally
9568 * decrement the number of reservations in the block group.
9569 * If we did it before, we could race with relocation and have
9570 * relocation miss the reserved extent, making it fail later.
9572 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
9573 if (IS_ERR(trans)) {
9574 ret = PTR_ERR(trans);
9575 btrfs_free_reserved_extent(fs_info, ins.objectid,
9580 em = alloc_extent_map();
9582 btrfs_drop_extent_map_range(BTRFS_I(inode), cur_offset,
9583 cur_offset + ins.offset - 1, false);
9584 btrfs_set_inode_full_sync(BTRFS_I(inode));
9588 em->start = cur_offset;
9589 em->orig_start = cur_offset;
9590 em->len = ins.offset;
9591 em->block_start = ins.objectid;
9592 em->block_len = ins.offset;
9593 em->orig_block_len = ins.offset;
9594 em->ram_bytes = ins.offset;
9595 em->flags |= EXTENT_FLAG_PREALLOC;
9596 em->generation = trans->transid;
9598 ret = btrfs_replace_extent_map_range(BTRFS_I(inode), em, true);
9599 free_extent_map(em);
9601 num_bytes -= ins.offset;
9602 cur_offset += ins.offset;
9603 *alloc_hint = ins.objectid + ins.offset;
9605 inode_inc_iversion(inode);
9606 inode_set_ctime_current(inode);
9607 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
9608 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
9609 (actual_len > inode->i_size) &&
9610 (cur_offset > inode->i_size)) {
9611 if (cur_offset > actual_len)
9612 i_size = actual_len;
9614 i_size = cur_offset;
9615 i_size_write(inode, i_size);
9616 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
9619 ret = btrfs_update_inode(trans, BTRFS_I(inode));
9622 btrfs_abort_transaction(trans, ret);
9624 btrfs_end_transaction(trans);
9629 btrfs_end_transaction(trans);
9633 if (clear_offset < end)
9634 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
9635 end - clear_offset + 1);
9639 int btrfs_prealloc_file_range(struct inode *inode, int mode,
9640 u64 start, u64 num_bytes, u64 min_size,
9641 loff_t actual_len, u64 *alloc_hint)
9643 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9644 min_size, actual_len, alloc_hint,
9648 int btrfs_prealloc_file_range_trans(struct inode *inode,
9649 struct btrfs_trans_handle *trans, int mode,
9650 u64 start, u64 num_bytes, u64 min_size,
9651 loff_t actual_len, u64 *alloc_hint)
9653 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9654 min_size, actual_len, alloc_hint, trans);
9657 static int btrfs_permission(struct mnt_idmap *idmap,
9658 struct inode *inode, int mask)
9660 struct btrfs_root *root = BTRFS_I(inode)->root;
9661 umode_t mode = inode->i_mode;
9663 if (mask & MAY_WRITE &&
9664 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9665 if (btrfs_root_readonly(root))
9667 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9670 return generic_permission(idmap, inode, mask);
9673 static int btrfs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
9674 struct file *file, umode_t mode)
9676 struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
9677 struct btrfs_trans_handle *trans;
9678 struct btrfs_root *root = BTRFS_I(dir)->root;
9679 struct inode *inode;
9680 struct btrfs_new_inode_args new_inode_args = {
9682 .dentry = file->f_path.dentry,
9685 unsigned int trans_num_items;
9688 inode = new_inode(dir->i_sb);
9691 inode_init_owner(idmap, inode, dir, mode);
9692 inode->i_fop = &btrfs_file_operations;
9693 inode->i_op = &btrfs_file_inode_operations;
9694 inode->i_mapping->a_ops = &btrfs_aops;
9696 new_inode_args.inode = inode;
9697 ret = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items);
9701 trans = btrfs_start_transaction(root, trans_num_items);
9702 if (IS_ERR(trans)) {
9703 ret = PTR_ERR(trans);
9704 goto out_new_inode_args;
9707 ret = btrfs_create_new_inode(trans, &new_inode_args);
9710 * We set number of links to 0 in btrfs_create_new_inode(), and here we
9711 * set it to 1 because d_tmpfile() will issue a warning if the count is
9714 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9716 set_nlink(inode, 1);
9719 d_tmpfile(file, inode);
9720 unlock_new_inode(inode);
9721 mark_inode_dirty(inode);
9724 btrfs_end_transaction(trans);
9725 btrfs_btree_balance_dirty(fs_info);
9727 btrfs_new_inode_args_destroy(&new_inode_args);
9731 return finish_open_simple(file, ret);
9734 void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end)
9736 struct btrfs_fs_info *fs_info = inode->root->fs_info;
9737 unsigned long index = start >> PAGE_SHIFT;
9738 unsigned long end_index = end >> PAGE_SHIFT;
9742 ASSERT(end + 1 - start <= U32_MAX);
9743 len = end + 1 - start;
9744 while (index <= end_index) {
9745 page = find_get_page(inode->vfs_inode.i_mapping, index);
9746 ASSERT(page); /* Pages should be in the extent_io_tree */
9748 /* This is for data, which doesn't yet support larger folio. */
9749 ASSERT(folio_order(page_folio(page)) == 0);
9750 btrfs_folio_set_writeback(fs_info, page_folio(page), start, len);
9756 int btrfs_encoded_io_compression_from_extent(struct btrfs_fs_info *fs_info,
9759 switch (compress_type) {
9760 case BTRFS_COMPRESS_NONE:
9761 return BTRFS_ENCODED_IO_COMPRESSION_NONE;
9762 case BTRFS_COMPRESS_ZLIB:
9763 return BTRFS_ENCODED_IO_COMPRESSION_ZLIB;
9764 case BTRFS_COMPRESS_LZO:
9766 * The LZO format depends on the sector size. 64K is the maximum
9767 * sector size that we support.
9769 if (fs_info->sectorsize < SZ_4K || fs_info->sectorsize > SZ_64K)
9771 return BTRFS_ENCODED_IO_COMPRESSION_LZO_4K +
9772 (fs_info->sectorsize_bits - 12);
9773 case BTRFS_COMPRESS_ZSTD:
9774 return BTRFS_ENCODED_IO_COMPRESSION_ZSTD;
9780 static ssize_t btrfs_encoded_read_inline(
9782 struct iov_iter *iter, u64 start,
9784 struct extent_state **cached_state,
9785 u64 extent_start, size_t count,
9786 struct btrfs_ioctl_encoded_io_args *encoded,
9789 struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
9790 struct btrfs_root *root = inode->root;
9791 struct btrfs_fs_info *fs_info = root->fs_info;
9792 struct extent_io_tree *io_tree = &inode->io_tree;
9793 struct btrfs_path *path;
9794 struct extent_buffer *leaf;
9795 struct btrfs_file_extent_item *item;
9801 path = btrfs_alloc_path();
9806 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
9810 /* The extent item disappeared? */
9815 leaf = path->nodes[0];
9816 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
9818 ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
9819 ptr = btrfs_file_extent_inline_start(item);
9821 encoded->len = min_t(u64, extent_start + ram_bytes,
9822 inode->vfs_inode.i_size) - iocb->ki_pos;
9823 ret = btrfs_encoded_io_compression_from_extent(fs_info,
9824 btrfs_file_extent_compression(leaf, item));
9827 encoded->compression = ret;
9828 if (encoded->compression) {
9831 inline_size = btrfs_file_extent_inline_item_len(leaf,
9833 if (inline_size > count) {
9837 count = inline_size;
9838 encoded->unencoded_len = ram_bytes;
9839 encoded->unencoded_offset = iocb->ki_pos - extent_start;
9841 count = min_t(u64, count, encoded->len);
9842 encoded->len = count;
9843 encoded->unencoded_len = count;
9844 ptr += iocb->ki_pos - extent_start;
9847 tmp = kmalloc(count, GFP_NOFS);
9852 read_extent_buffer(leaf, tmp, ptr, count);
9853 btrfs_release_path(path);
9854 unlock_extent(io_tree, start, lockend, cached_state);
9855 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
9858 ret = copy_to_iter(tmp, count, iter);
9863 btrfs_free_path(path);
9867 struct btrfs_encoded_read_private {
9868 wait_queue_head_t wait;
9870 blk_status_t status;
9873 static void btrfs_encoded_read_endio(struct btrfs_bio *bbio)
9875 struct btrfs_encoded_read_private *priv = bbio->private;
9877 if (bbio->bio.bi_status) {
9879 * The memory barrier implied by the atomic_dec_return() here
9880 * pairs with the memory barrier implied by the
9881 * atomic_dec_return() or io_wait_event() in
9882 * btrfs_encoded_read_regular_fill_pages() to ensure that this
9883 * write is observed before the load of status in
9884 * btrfs_encoded_read_regular_fill_pages().
9886 WRITE_ONCE(priv->status, bbio->bio.bi_status);
9888 if (!atomic_dec_return(&priv->pending))
9889 wake_up(&priv->wait);
9890 bio_put(&bbio->bio);
9893 int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode,
9894 u64 file_offset, u64 disk_bytenr,
9895 u64 disk_io_size, struct page **pages)
9897 struct btrfs_fs_info *fs_info = inode->root->fs_info;
9898 struct btrfs_encoded_read_private priv = {
9899 .pending = ATOMIC_INIT(1),
9901 unsigned long i = 0;
9902 struct btrfs_bio *bbio;
9904 init_waitqueue_head(&priv.wait);
9906 bbio = btrfs_bio_alloc(BIO_MAX_VECS, REQ_OP_READ, fs_info,
9907 btrfs_encoded_read_endio, &priv);
9908 bbio->bio.bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT;
9909 bbio->inode = inode;
9912 size_t bytes = min_t(u64, disk_io_size, PAGE_SIZE);
9914 if (bio_add_page(&bbio->bio, pages[i], bytes, 0) < bytes) {
9915 atomic_inc(&priv.pending);
9916 btrfs_submit_bio(bbio, 0);
9918 bbio = btrfs_bio_alloc(BIO_MAX_VECS, REQ_OP_READ, fs_info,
9919 btrfs_encoded_read_endio, &priv);
9920 bbio->bio.bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT;
9921 bbio->inode = inode;
9926 disk_bytenr += bytes;
9927 disk_io_size -= bytes;
9928 } while (disk_io_size);
9930 atomic_inc(&priv.pending);
9931 btrfs_submit_bio(bbio, 0);
9933 if (atomic_dec_return(&priv.pending))
9934 io_wait_event(priv.wait, !atomic_read(&priv.pending));
9935 /* See btrfs_encoded_read_endio() for ordering. */
9936 return blk_status_to_errno(READ_ONCE(priv.status));
9939 static ssize_t btrfs_encoded_read_regular(struct kiocb *iocb,
9940 struct iov_iter *iter,
9941 u64 start, u64 lockend,
9942 struct extent_state **cached_state,
9943 u64 disk_bytenr, u64 disk_io_size,
9944 size_t count, bool compressed,
9947 struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
9948 struct extent_io_tree *io_tree = &inode->io_tree;
9949 struct page **pages;
9950 unsigned long nr_pages, i;
9955 nr_pages = DIV_ROUND_UP(disk_io_size, PAGE_SIZE);
9956 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
9959 ret = btrfs_alloc_page_array(nr_pages, pages, 0);
9965 ret = btrfs_encoded_read_regular_fill_pages(inode, start, disk_bytenr,
9966 disk_io_size, pages);
9970 unlock_extent(io_tree, start, lockend, cached_state);
9971 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
9978 i = (iocb->ki_pos - start) >> PAGE_SHIFT;
9979 page_offset = (iocb->ki_pos - start) & (PAGE_SIZE - 1);
9982 while (cur < count) {
9983 size_t bytes = min_t(size_t, count - cur,
9984 PAGE_SIZE - page_offset);
9986 if (copy_page_to_iter(pages[i], page_offset, bytes,
9997 for (i = 0; i < nr_pages; i++) {
9999 __free_page(pages[i]);
10005 ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter,
10006 struct btrfs_ioctl_encoded_io_args *encoded)
10008 struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
10009 struct btrfs_fs_info *fs_info = inode->root->fs_info;
10010 struct extent_io_tree *io_tree = &inode->io_tree;
10012 size_t count = iov_iter_count(iter);
10013 u64 start, lockend, disk_bytenr, disk_io_size;
10014 struct extent_state *cached_state = NULL;
10015 struct extent_map *em;
10016 bool unlocked = false;
10018 file_accessed(iocb->ki_filp);
10020 btrfs_inode_lock(inode, BTRFS_ILOCK_SHARED);
10022 if (iocb->ki_pos >= inode->vfs_inode.i_size) {
10023 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
10026 start = ALIGN_DOWN(iocb->ki_pos, fs_info->sectorsize);
10028 * We don't know how long the extent containing iocb->ki_pos is, but if
10029 * it's compressed we know that it won't be longer than this.
10031 lockend = start + BTRFS_MAX_UNCOMPRESSED - 1;
10034 struct btrfs_ordered_extent *ordered;
10036 ret = btrfs_wait_ordered_range(&inode->vfs_inode, start,
10037 lockend - start + 1);
10039 goto out_unlock_inode;
10040 lock_extent(io_tree, start, lockend, &cached_state);
10041 ordered = btrfs_lookup_ordered_range(inode, start,
10042 lockend - start + 1);
10045 btrfs_put_ordered_extent(ordered);
10046 unlock_extent(io_tree, start, lockend, &cached_state);
10050 em = btrfs_get_extent(inode, NULL, start, lockend - start + 1);
10053 goto out_unlock_extent;
10056 if (em->block_start == EXTENT_MAP_INLINE) {
10057 u64 extent_start = em->start;
10060 * For inline extents we get everything we need out of the
10063 free_extent_map(em);
10065 ret = btrfs_encoded_read_inline(iocb, iter, start, lockend,
10066 &cached_state, extent_start,
10067 count, encoded, &unlocked);
10072 * We only want to return up to EOF even if the extent extends beyond
10075 encoded->len = min_t(u64, extent_map_end(em),
10076 inode->vfs_inode.i_size) - iocb->ki_pos;
10077 if (em->block_start == EXTENT_MAP_HOLE ||
10078 (em->flags & EXTENT_FLAG_PREALLOC)) {
10079 disk_bytenr = EXTENT_MAP_HOLE;
10080 count = min_t(u64, count, encoded->len);
10081 encoded->len = count;
10082 encoded->unencoded_len = count;
10083 } else if (extent_map_is_compressed(em)) {
10084 disk_bytenr = em->block_start;
10086 * Bail if the buffer isn't large enough to return the whole
10087 * compressed extent.
10089 if (em->block_len > count) {
10093 disk_io_size = em->block_len;
10094 count = em->block_len;
10095 encoded->unencoded_len = em->ram_bytes;
10096 encoded->unencoded_offset = iocb->ki_pos - em->orig_start;
10097 ret = btrfs_encoded_io_compression_from_extent(fs_info,
10098 extent_map_compression(em));
10101 encoded->compression = ret;
10103 disk_bytenr = em->block_start + (start - em->start);
10104 if (encoded->len > count)
10105 encoded->len = count;
10107 * Don't read beyond what we locked. This also limits the page
10108 * allocations that we'll do.
10110 disk_io_size = min(lockend + 1, iocb->ki_pos + encoded->len) - start;
10111 count = start + disk_io_size - iocb->ki_pos;
10112 encoded->len = count;
10113 encoded->unencoded_len = count;
10114 disk_io_size = ALIGN(disk_io_size, fs_info->sectorsize);
10116 free_extent_map(em);
10119 if (disk_bytenr == EXTENT_MAP_HOLE) {
10120 unlock_extent(io_tree, start, lockend, &cached_state);
10121 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
10123 ret = iov_iter_zero(count, iter);
10127 ret = btrfs_encoded_read_regular(iocb, iter, start, lockend,
10128 &cached_state, disk_bytenr,
10129 disk_io_size, count,
10130 encoded->compression,
10136 iocb->ki_pos += encoded->len;
10138 free_extent_map(em);
10141 unlock_extent(io_tree, start, lockend, &cached_state);
10144 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
10148 ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from,
10149 const struct btrfs_ioctl_encoded_io_args *encoded)
10151 struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
10152 struct btrfs_root *root = inode->root;
10153 struct btrfs_fs_info *fs_info = root->fs_info;
10154 struct extent_io_tree *io_tree = &inode->io_tree;
10155 struct extent_changeset *data_reserved = NULL;
10156 struct extent_state *cached_state = NULL;
10157 struct btrfs_ordered_extent *ordered;
10161 u64 num_bytes, ram_bytes, disk_num_bytes;
10162 unsigned long nr_folios, i;
10163 struct folio **folios;
10164 struct btrfs_key ins;
10165 bool extent_reserved = false;
10166 struct extent_map *em;
10169 switch (encoded->compression) {
10170 case BTRFS_ENCODED_IO_COMPRESSION_ZLIB:
10171 compression = BTRFS_COMPRESS_ZLIB;
10173 case BTRFS_ENCODED_IO_COMPRESSION_ZSTD:
10174 compression = BTRFS_COMPRESS_ZSTD;
10176 case BTRFS_ENCODED_IO_COMPRESSION_LZO_4K:
10177 case BTRFS_ENCODED_IO_COMPRESSION_LZO_8K:
10178 case BTRFS_ENCODED_IO_COMPRESSION_LZO_16K:
10179 case BTRFS_ENCODED_IO_COMPRESSION_LZO_32K:
10180 case BTRFS_ENCODED_IO_COMPRESSION_LZO_64K:
10181 /* The sector size must match for LZO. */
10182 if (encoded->compression -
10183 BTRFS_ENCODED_IO_COMPRESSION_LZO_4K + 12 !=
10184 fs_info->sectorsize_bits)
10186 compression = BTRFS_COMPRESS_LZO;
10191 if (encoded->encryption != BTRFS_ENCODED_IO_ENCRYPTION_NONE)
10195 * Compressed extents should always have checksums, so error out if we
10196 * have a NOCOW file or inode was created while mounted with NODATASUM.
10198 if (inode->flags & BTRFS_INODE_NODATASUM)
10201 orig_count = iov_iter_count(from);
10203 /* The extent size must be sane. */
10204 if (encoded->unencoded_len > BTRFS_MAX_UNCOMPRESSED ||
10205 orig_count > BTRFS_MAX_COMPRESSED || orig_count == 0)
10209 * The compressed data must be smaller than the decompressed data.
10211 * It's of course possible for data to compress to larger or the same
10212 * size, but the buffered I/O path falls back to no compression for such
10213 * data, and we don't want to break any assumptions by creating these
10216 * Note that this is less strict than the current check we have that the
10217 * compressed data must be at least one sector smaller than the
10218 * decompressed data. We only want to enforce the weaker requirement
10219 * from old kernels that it is at least one byte smaller.
10221 if (orig_count >= encoded->unencoded_len)
10224 /* The extent must start on a sector boundary. */
10225 start = iocb->ki_pos;
10226 if (!IS_ALIGNED(start, fs_info->sectorsize))
10230 * The extent must end on a sector boundary. However, we allow a write
10231 * which ends at or extends i_size to have an unaligned length; we round
10232 * up the extent size and set i_size to the unaligned end.
10234 if (start + encoded->len < inode->vfs_inode.i_size &&
10235 !IS_ALIGNED(start + encoded->len, fs_info->sectorsize))
10238 /* Finally, the offset in the unencoded data must be sector-aligned. */
10239 if (!IS_ALIGNED(encoded->unencoded_offset, fs_info->sectorsize))
10242 num_bytes = ALIGN(encoded->len, fs_info->sectorsize);
10243 ram_bytes = ALIGN(encoded->unencoded_len, fs_info->sectorsize);
10244 end = start + num_bytes - 1;
10247 * If the extent cannot be inline, the compressed data on disk must be
10248 * sector-aligned. For convenience, we extend it with zeroes if it
10251 disk_num_bytes = ALIGN(orig_count, fs_info->sectorsize);
10252 nr_folios = DIV_ROUND_UP(disk_num_bytes, PAGE_SIZE);
10253 folios = kvcalloc(nr_folios, sizeof(struct page *), GFP_KERNEL_ACCOUNT);
10256 for (i = 0; i < nr_folios; i++) {
10257 size_t bytes = min_t(size_t, PAGE_SIZE, iov_iter_count(from));
10260 folios[i] = folio_alloc(GFP_KERNEL_ACCOUNT, 0);
10265 kaddr = kmap_local_folio(folios[i], 0);
10266 if (copy_from_iter(kaddr, bytes, from) != bytes) {
10267 kunmap_local(kaddr);
10271 if (bytes < PAGE_SIZE)
10272 memset(kaddr + bytes, 0, PAGE_SIZE - bytes);
10273 kunmap_local(kaddr);
10277 struct btrfs_ordered_extent *ordered;
10279 ret = btrfs_wait_ordered_range(&inode->vfs_inode, start, num_bytes);
10282 ret = invalidate_inode_pages2_range(inode->vfs_inode.i_mapping,
10283 start >> PAGE_SHIFT,
10284 end >> PAGE_SHIFT);
10287 lock_extent(io_tree, start, end, &cached_state);
10288 ordered = btrfs_lookup_ordered_range(inode, start, num_bytes);
10290 !filemap_range_has_page(inode->vfs_inode.i_mapping, start, end))
10293 btrfs_put_ordered_extent(ordered);
10294 unlock_extent(io_tree, start, end, &cached_state);
10299 * We don't use the higher-level delalloc space functions because our
10300 * num_bytes and disk_num_bytes are different.
10302 ret = btrfs_alloc_data_chunk_ondemand(inode, disk_num_bytes);
10305 ret = btrfs_qgroup_reserve_data(inode, &data_reserved, start, num_bytes);
10307 goto out_free_data_space;
10308 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes, disk_num_bytes,
10311 goto out_qgroup_free_data;
10313 /* Try an inline extent first. */
10314 if (encoded->unencoded_len == encoded->len &&
10315 encoded->unencoded_offset == 0 &&
10316 can_cow_file_range_inline(inode, start, encoded->len, orig_count)) {
10317 ret = __cow_file_range_inline(inode, start, encoded->len,
10318 orig_count, compression, folios[0],
10323 goto out_delalloc_release;
10327 ret = btrfs_reserve_extent(root, disk_num_bytes, disk_num_bytes,
10328 disk_num_bytes, 0, 0, &ins, 1, 1);
10330 goto out_delalloc_release;
10331 extent_reserved = true;
10333 em = create_io_em(inode, start, num_bytes,
10334 start - encoded->unencoded_offset, ins.objectid,
10335 ins.offset, ins.offset, ram_bytes, compression,
10336 BTRFS_ORDERED_COMPRESSED);
10339 goto out_free_reserved;
10341 free_extent_map(em);
10343 ordered = btrfs_alloc_ordered_extent(inode, start, num_bytes, ram_bytes,
10344 ins.objectid, ins.offset,
10345 encoded->unencoded_offset,
10346 (1 << BTRFS_ORDERED_ENCODED) |
10347 (1 << BTRFS_ORDERED_COMPRESSED),
10349 if (IS_ERR(ordered)) {
10350 btrfs_drop_extent_map_range(inode, start, end, false);
10351 ret = PTR_ERR(ordered);
10352 goto out_free_reserved;
10354 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
10356 if (start + encoded->len > inode->vfs_inode.i_size)
10357 i_size_write(&inode->vfs_inode, start + encoded->len);
10359 unlock_extent(io_tree, start, end, &cached_state);
10361 btrfs_delalloc_release_extents(inode, num_bytes);
10363 btrfs_submit_compressed_write(ordered, folios, nr_folios, 0, false);
10368 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
10369 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
10370 out_delalloc_release:
10371 btrfs_delalloc_release_extents(inode, num_bytes);
10372 btrfs_delalloc_release_metadata(inode, disk_num_bytes, ret < 0);
10373 out_qgroup_free_data:
10375 btrfs_qgroup_free_data(inode, data_reserved, start, num_bytes, NULL);
10376 out_free_data_space:
10378 * If btrfs_reserve_extent() succeeded, then we already decremented
10381 if (!extent_reserved)
10382 btrfs_free_reserved_data_space_noquota(fs_info, disk_num_bytes);
10384 unlock_extent(io_tree, start, end, &cached_state);
10386 for (i = 0; i < nr_folios; i++) {
10388 folio_put(folios[i]);
10393 iocb->ki_pos += encoded->len;
10399 * Add an entry indicating a block group or device which is pinned by a
10400 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10401 * negative errno on failure.
10403 static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10404 bool is_block_group)
10406 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10407 struct btrfs_swapfile_pin *sp, *entry;
10408 struct rb_node **p;
10409 struct rb_node *parent = NULL;
10411 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10416 sp->is_block_group = is_block_group;
10417 sp->bg_extent_count = 1;
10419 spin_lock(&fs_info->swapfile_pins_lock);
10420 p = &fs_info->swapfile_pins.rb_node;
10423 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10424 if (sp->ptr < entry->ptr ||
10425 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10426 p = &(*p)->rb_left;
10427 } else if (sp->ptr > entry->ptr ||
10428 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10429 p = &(*p)->rb_right;
10431 if (is_block_group)
10432 entry->bg_extent_count++;
10433 spin_unlock(&fs_info->swapfile_pins_lock);
10438 rb_link_node(&sp->node, parent, p);
10439 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10440 spin_unlock(&fs_info->swapfile_pins_lock);
10444 /* Free all of the entries pinned by this swapfile. */
10445 static void btrfs_free_swapfile_pins(struct inode *inode)
10447 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10448 struct btrfs_swapfile_pin *sp;
10449 struct rb_node *node, *next;
10451 spin_lock(&fs_info->swapfile_pins_lock);
10452 node = rb_first(&fs_info->swapfile_pins);
10454 next = rb_next(node);
10455 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10456 if (sp->inode == inode) {
10457 rb_erase(&sp->node, &fs_info->swapfile_pins);
10458 if (sp->is_block_group) {
10459 btrfs_dec_block_group_swap_extents(sp->ptr,
10460 sp->bg_extent_count);
10461 btrfs_put_block_group(sp->ptr);
10467 spin_unlock(&fs_info->swapfile_pins_lock);
10470 struct btrfs_swap_info {
10476 unsigned long nr_pages;
10480 static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10481 struct btrfs_swap_info *bsi)
10483 unsigned long nr_pages;
10484 unsigned long max_pages;
10485 u64 first_ppage, first_ppage_reported, next_ppage;
10489 * Our swapfile may have had its size extended after the swap header was
10490 * written. In that case activating the swapfile should not go beyond
10491 * the max size set in the swap header.
10493 if (bsi->nr_pages >= sis->max)
10496 max_pages = sis->max - bsi->nr_pages;
10497 first_ppage = PAGE_ALIGN(bsi->block_start) >> PAGE_SHIFT;
10498 next_ppage = PAGE_ALIGN_DOWN(bsi->block_start + bsi->block_len) >> PAGE_SHIFT;
10500 if (first_ppage >= next_ppage)
10502 nr_pages = next_ppage - first_ppage;
10503 nr_pages = min(nr_pages, max_pages);
10505 first_ppage_reported = first_ppage;
10506 if (bsi->start == 0)
10507 first_ppage_reported++;
10508 if (bsi->lowest_ppage > first_ppage_reported)
10509 bsi->lowest_ppage = first_ppage_reported;
10510 if (bsi->highest_ppage < (next_ppage - 1))
10511 bsi->highest_ppage = next_ppage - 1;
10513 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10516 bsi->nr_extents += ret;
10517 bsi->nr_pages += nr_pages;
10521 static void btrfs_swap_deactivate(struct file *file)
10523 struct inode *inode = file_inode(file);
10525 btrfs_free_swapfile_pins(inode);
10526 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10529 static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10532 struct inode *inode = file_inode(file);
10533 struct btrfs_root *root = BTRFS_I(inode)->root;
10534 struct btrfs_fs_info *fs_info = root->fs_info;
10535 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10536 struct extent_state *cached_state = NULL;
10537 struct extent_map *em = NULL;
10538 struct btrfs_chunk_map *map = NULL;
10539 struct btrfs_device *device = NULL;
10540 struct btrfs_swap_info bsi = {
10541 .lowest_ppage = (sector_t)-1ULL,
10548 * If the swap file was just created, make sure delalloc is done. If the
10549 * file changes again after this, the user is doing something stupid and
10550 * we don't really care.
10552 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10557 * The inode is locked, so these flags won't change after we check them.
10559 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10560 btrfs_warn(fs_info, "swapfile must not be compressed");
10563 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10564 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10567 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10568 btrfs_warn(fs_info, "swapfile must not be checksummed");
10573 * Balance or device remove/replace/resize can move stuff around from
10574 * under us. The exclop protection makes sure they aren't running/won't
10575 * run concurrently while we are mapping the swap extents, and
10576 * fs_info->swapfile_pins prevents them from running while the swap
10577 * file is active and moving the extents. Note that this also prevents
10578 * a concurrent device add which isn't actually necessary, but it's not
10579 * really worth the trouble to allow it.
10581 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
10582 btrfs_warn(fs_info,
10583 "cannot activate swapfile while exclusive operation is running");
10588 * Prevent snapshot creation while we are activating the swap file.
10589 * We do not want to race with snapshot creation. If snapshot creation
10590 * already started before we bumped nr_swapfiles from 0 to 1 and
10591 * completes before the first write into the swap file after it is
10592 * activated, than that write would fallback to COW.
10594 if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) {
10595 btrfs_exclop_finish(fs_info);
10596 btrfs_warn(fs_info,
10597 "cannot activate swapfile because snapshot creation is in progress");
10601 * Snapshots can create extents which require COW even if NODATACOW is
10602 * set. We use this counter to prevent snapshots. We must increment it
10603 * before walking the extents because we don't want a concurrent
10604 * snapshot to run after we've already checked the extents.
10606 * It is possible that subvolume is marked for deletion but still not
10607 * removed yet. To prevent this race, we check the root status before
10608 * activating the swapfile.
10610 spin_lock(&root->root_item_lock);
10611 if (btrfs_root_dead(root)) {
10612 spin_unlock(&root->root_item_lock);
10614 btrfs_exclop_finish(fs_info);
10615 btrfs_warn(fs_info,
10616 "cannot activate swapfile because subvolume %llu is being deleted",
10617 btrfs_root_id(root));
10620 atomic_inc(&root->nr_swapfiles);
10621 spin_unlock(&root->root_item_lock);
10623 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10625 lock_extent(io_tree, 0, isize - 1, &cached_state);
10627 while (start < isize) {
10628 u64 logical_block_start, physical_block_start;
10629 struct btrfs_block_group *bg;
10630 u64 len = isize - start;
10632 em = btrfs_get_extent(BTRFS_I(inode), NULL, start, len);
10638 if (em->block_start == EXTENT_MAP_HOLE) {
10639 btrfs_warn(fs_info, "swapfile must not have holes");
10643 if (em->block_start == EXTENT_MAP_INLINE) {
10645 * It's unlikely we'll ever actually find ourselves
10646 * here, as a file small enough to fit inline won't be
10647 * big enough to store more than the swap header, but in
10648 * case something changes in the future, let's catch it
10649 * here rather than later.
10651 btrfs_warn(fs_info, "swapfile must not be inline");
10655 if (extent_map_is_compressed(em)) {
10656 btrfs_warn(fs_info, "swapfile must not be compressed");
10661 logical_block_start = em->block_start + (start - em->start);
10662 len = min(len, em->len - (start - em->start));
10663 free_extent_map(em);
10666 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, false, true);
10672 btrfs_warn(fs_info,
10673 "swapfile must not be copy-on-write");
10678 map = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10680 ret = PTR_ERR(map);
10684 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10685 btrfs_warn(fs_info,
10686 "swapfile must have single data profile");
10691 if (device == NULL) {
10692 device = map->stripes[0].dev;
10693 ret = btrfs_add_swapfile_pin(inode, device, false);
10698 } else if (device != map->stripes[0].dev) {
10699 btrfs_warn(fs_info, "swapfile must be on one device");
10704 physical_block_start = (map->stripes[0].physical +
10705 (logical_block_start - map->start));
10706 len = min(len, map->chunk_len - (logical_block_start - map->start));
10707 btrfs_free_chunk_map(map);
10710 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10712 btrfs_warn(fs_info,
10713 "could not find block group containing swapfile");
10718 if (!btrfs_inc_block_group_swap_extents(bg)) {
10719 btrfs_warn(fs_info,
10720 "block group for swapfile at %llu is read-only%s",
10722 atomic_read(&fs_info->scrubs_running) ?
10723 " (scrub running)" : "");
10724 btrfs_put_block_group(bg);
10729 ret = btrfs_add_swapfile_pin(inode, bg, true);
10731 btrfs_put_block_group(bg);
10738 if (bsi.block_len &&
10739 bsi.block_start + bsi.block_len == physical_block_start) {
10740 bsi.block_len += len;
10742 if (bsi.block_len) {
10743 ret = btrfs_add_swap_extent(sis, &bsi);
10748 bsi.block_start = physical_block_start;
10749 bsi.block_len = len;
10756 ret = btrfs_add_swap_extent(sis, &bsi);
10759 if (!IS_ERR_OR_NULL(em))
10760 free_extent_map(em);
10761 if (!IS_ERR_OR_NULL(map))
10762 btrfs_free_chunk_map(map);
10764 unlock_extent(io_tree, 0, isize - 1, &cached_state);
10767 btrfs_swap_deactivate(file);
10769 btrfs_drew_write_unlock(&root->snapshot_lock);
10771 btrfs_exclop_finish(fs_info);
10777 sis->bdev = device->bdev;
10778 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10779 sis->max = bsi.nr_pages;
10780 sis->pages = bsi.nr_pages - 1;
10781 sis->highest_bit = bsi.nr_pages - 1;
10782 return bsi.nr_extents;
10785 static void btrfs_swap_deactivate(struct file *file)
10789 static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10792 return -EOPNOTSUPP;
10797 * Update the number of bytes used in the VFS' inode. When we replace extents in
10798 * a range (clone, dedupe, fallocate's zero range), we must update the number of
10799 * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
10800 * always get a correct value.
10802 void btrfs_update_inode_bytes(struct btrfs_inode *inode,
10803 const u64 add_bytes,
10804 const u64 del_bytes)
10806 if (add_bytes == del_bytes)
10809 spin_lock(&inode->lock);
10811 inode_sub_bytes(&inode->vfs_inode, del_bytes);
10813 inode_add_bytes(&inode->vfs_inode, add_bytes);
10814 spin_unlock(&inode->lock);
10818 * Verify that there are no ordered extents for a given file range.
10820 * @inode: The target inode.
10821 * @start: Start offset of the file range, should be sector size aligned.
10822 * @end: End offset (inclusive) of the file range, its value +1 should be
10823 * sector size aligned.
10825 * This should typically be used for cases where we locked an inode's VFS lock in
10826 * exclusive mode, we have also locked the inode's i_mmap_lock in exclusive mode,
10827 * we have flushed all delalloc in the range, we have waited for all ordered
10828 * extents in the range to complete and finally we have locked the file range in
10829 * the inode's io_tree.
10831 void btrfs_assert_inode_range_clean(struct btrfs_inode *inode, u64 start, u64 end)
10833 struct btrfs_root *root = inode->root;
10834 struct btrfs_ordered_extent *ordered;
10836 if (!IS_ENABLED(CONFIG_BTRFS_ASSERT))
10839 ordered = btrfs_lookup_first_ordered_range(inode, start, end + 1 - start);
10841 btrfs_err(root->fs_info,
10842 "found unexpected ordered extent in file range [%llu, %llu] for inode %llu root %llu (ordered range [%llu, %llu])",
10843 start, end, btrfs_ino(inode), btrfs_root_id(root),
10844 ordered->file_offset,
10845 ordered->file_offset + ordered->num_bytes - 1);
10846 btrfs_put_ordered_extent(ordered);
10849 ASSERT(ordered == NULL);
10853 * Find the first inode with a minimum number.
10855 * @root: The root to search for.
10856 * @min_ino: The minimum inode number.
10858 * Find the first inode in the @root with a number >= @min_ino and return it.
10859 * Returns NULL if no such inode found.
10861 struct btrfs_inode *btrfs_find_first_inode(struct btrfs_root *root, u64 min_ino)
10863 struct rb_node *node;
10864 struct rb_node *prev;
10865 struct btrfs_inode *inode;
10867 spin_lock(&root->inode_lock);
10869 node = root->inode_tree.rb_node;
10873 inode = rb_entry(node, struct btrfs_inode, rb_node);
10874 if (min_ino < btrfs_ino(inode))
10875 node = node->rb_left;
10876 else if (min_ino > btrfs_ino(inode))
10877 node = node->rb_right;
10884 inode = rb_entry(prev, struct btrfs_inode, rb_node);
10885 if (min_ino <= btrfs_ino(inode)) {
10889 prev = rb_next(prev);
10894 inode = rb_entry(prev, struct btrfs_inode, rb_node);
10895 if (igrab(&inode->vfs_inode)) {
10896 spin_unlock(&root->inode_lock);
10900 min_ino = btrfs_ino(inode) + 1;
10901 if (cond_resched_lock(&root->inode_lock))
10904 node = rb_next(node);
10906 spin_unlock(&root->inode_lock);
10911 static const struct inode_operations btrfs_dir_inode_operations = {
10912 .getattr = btrfs_getattr,
10913 .lookup = btrfs_lookup,
10914 .create = btrfs_create,
10915 .unlink = btrfs_unlink,
10916 .link = btrfs_link,
10917 .mkdir = btrfs_mkdir,
10918 .rmdir = btrfs_rmdir,
10919 .rename = btrfs_rename2,
10920 .symlink = btrfs_symlink,
10921 .setattr = btrfs_setattr,
10922 .mknod = btrfs_mknod,
10923 .listxattr = btrfs_listxattr,
10924 .permission = btrfs_permission,
10925 .get_inode_acl = btrfs_get_acl,
10926 .set_acl = btrfs_set_acl,
10927 .update_time = btrfs_update_time,
10928 .tmpfile = btrfs_tmpfile,
10929 .fileattr_get = btrfs_fileattr_get,
10930 .fileattr_set = btrfs_fileattr_set,
10933 static const struct file_operations btrfs_dir_file_operations = {
10934 .llseek = btrfs_dir_llseek,
10935 .read = generic_read_dir,
10936 .iterate_shared = btrfs_real_readdir,
10937 .open = btrfs_opendir,
10938 .unlocked_ioctl = btrfs_ioctl,
10939 #ifdef CONFIG_COMPAT
10940 .compat_ioctl = btrfs_compat_ioctl,
10942 .release = btrfs_release_file,
10943 .fsync = btrfs_sync_file,
10947 * btrfs doesn't support the bmap operation because swapfiles
10948 * use bmap to make a mapping of extents in the file. They assume
10949 * these extents won't change over the life of the file and they
10950 * use the bmap result to do IO directly to the drive.
10952 * the btrfs bmap call would return logical addresses that aren't
10953 * suitable for IO and they also will change frequently as COW
10954 * operations happen. So, swapfile + btrfs == corruption.
10956 * For now we're avoiding this by dropping bmap.
10958 static const struct address_space_operations btrfs_aops = {
10959 .read_folio = btrfs_read_folio,
10960 .writepages = btrfs_writepages,
10961 .readahead = btrfs_readahead,
10962 .invalidate_folio = btrfs_invalidate_folio,
10963 .release_folio = btrfs_release_folio,
10964 .migrate_folio = btrfs_migrate_folio,
10965 .dirty_folio = filemap_dirty_folio,
10966 .error_remove_folio = generic_error_remove_folio,
10967 .swap_activate = btrfs_swap_activate,
10968 .swap_deactivate = btrfs_swap_deactivate,
10971 static const struct inode_operations btrfs_file_inode_operations = {
10972 .getattr = btrfs_getattr,
10973 .setattr = btrfs_setattr,
10974 .listxattr = btrfs_listxattr,
10975 .permission = btrfs_permission,
10976 .fiemap = btrfs_fiemap,
10977 .get_inode_acl = btrfs_get_acl,
10978 .set_acl = btrfs_set_acl,
10979 .update_time = btrfs_update_time,
10980 .fileattr_get = btrfs_fileattr_get,
10981 .fileattr_set = btrfs_fileattr_set,
10983 static const struct inode_operations btrfs_special_inode_operations = {
10984 .getattr = btrfs_getattr,
10985 .setattr = btrfs_setattr,
10986 .permission = btrfs_permission,
10987 .listxattr = btrfs_listxattr,
10988 .get_inode_acl = btrfs_get_acl,
10989 .set_acl = btrfs_set_acl,
10990 .update_time = btrfs_update_time,
10992 static const struct inode_operations btrfs_symlink_inode_operations = {
10993 .get_link = page_get_link,
10994 .getattr = btrfs_getattr,
10995 .setattr = btrfs_setattr,
10996 .permission = btrfs_permission,
10997 .listxattr = btrfs_listxattr,
10998 .update_time = btrfs_update_time,
11001 const struct dentry_operations btrfs_dentry_operations = {
11002 .d_delete = btrfs_dentry_delete,