Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300     1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300     2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300     3)  * Copyright (C) 2007 Oracle.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300     4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300     5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300     6) #include <crypto/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300     7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300     8) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300     9) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    10) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    11) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    12) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    13) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    15) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    16) #include <linux/backing-dev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    17) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    18) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    19) #include <linux/xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    20) #include <linux/posix_acl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    21) #include <linux/falloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    22) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    23) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    24) #include <linux/btrfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    25) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    26) #include <linux/posix_acl_xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    27) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    28) #include <linux/magic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    29) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    30) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    31) #include <linux/migrate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    32) #include <linux/sched/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    33) #include <linux/iomap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    34) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    35) #include "misc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    36) #include "ctree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    37) #include "disk-io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    38) #include "transaction.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    39) #include "btrfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    40) #include "print-tree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    41) #include "ordered-data.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    42) #include "xattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    43) #include "tree-log.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    44) #include "volumes.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    45) #include "compression.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    46) #include "locking.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    47) #include "free-space-cache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    48) #include "inode-map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    49) #include "props.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    50) #include "qgroup.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    51) #include "delalloc-space.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    52) #include "block-group.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    53) #include "space-info.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    55) struct btrfs_iget_args {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    56) 	u64 ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    57) 	struct btrfs_root *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    58) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    60) struct btrfs_dio_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    61) 	u64 reserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    62) 	loff_t length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    63) 	ssize_t submitted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    64) 	struct extent_changeset *data_reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    65) 	bool sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    68) static const struct inode_operations btrfs_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    69) static const struct inode_operations btrfs_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    70) static const struct inode_operations btrfs_special_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    71) static const struct inode_operations btrfs_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    72) static const struct address_space_operations btrfs_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    73) static const struct file_operations btrfs_dir_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    75) static struct kmem_cache *btrfs_inode_cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    76) struct kmem_cache *btrfs_trans_handle_cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    77) struct kmem_cache *btrfs_path_cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    78) struct kmem_cache *btrfs_free_space_cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    79) struct kmem_cache *btrfs_free_space_bitmap_cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    81) static int btrfs_setsize(struct inode *inode, struct iattr *attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    82) static int btrfs_truncate(struct inode *inode, bool skip_writeback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    83) static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    84) static noinline int cow_file_range(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    85) 				   struct page *locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    86) 				   u64 start, u64 end, int *page_started,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    87) 				   unsigned long *nr_written, int unlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    88) static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    89) 				       u64 len, u64 orig_start, u64 block_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    90) 				       u64 block_len, u64 orig_block_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    91) 				       u64 ram_bytes, int compress_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    92) 				       int type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    94) static void __endio_write_update_ordered(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    95) 					 const u64 offset, const u64 bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    96) 					 const bool uptodate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    98) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    99)  * Cleanup all submitted ordered extents in specified range to handle errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   100)  * from the btrfs_run_delalloc_range() callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   101)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   102)  * NOTE: caller must ensure that when an error happens, it can not call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   103)  * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   104)  * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   105)  * to be released, which we want to happen only when finishing the ordered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   106)  * extent (btrfs_finish_ordered_io()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   107)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   108) static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   109) 						 struct page *locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   110) 						 u64 offset, u64 bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   112) 	unsigned long index = offset >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   113) 	unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   114) 	u64 page_start = page_offset(locked_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   115) 	u64 page_end = page_start + PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   117) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   119) 	while (index <= end_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   120) 		page = find_get_page(inode->vfs_inode.i_mapping, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   121) 		index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   122) 		if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   123) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   124) 		ClearPagePrivate2(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   125) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   128) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   129) 	 * In case this page belongs to the delalloc range being instantiated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   130) 	 * then skip it, since the first page of a range is going to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   131) 	 * properly cleaned up by the caller of run_delalloc_range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   132) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   133) 	if (page_start >= offset && page_end <= (offset + bytes - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   134) 		offset += PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   135) 		bytes -= PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   136) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   138) 	return __endio_write_update_ordered(inode, offset, bytes, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   141) static int btrfs_dirty_inode(struct inode *inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   143) static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   144) 				     struct inode *inode,  struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   145) 				     const struct qstr *qstr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   147) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   149) 	err = btrfs_init_acl(trans, inode, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   150) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   151) 		err = btrfs_xattr_security_init(trans, inode, dir, qstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   152) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   155) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   156)  * this does all the hard work for inserting an inline extent into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   157)  * the btree.  The caller should have done a btrfs_drop_extents so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   158)  * no overlapping inline items exist in the btree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   159)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   160) static int insert_inline_extent(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   161) 				struct btrfs_path *path, int extent_inserted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   162) 				struct btrfs_root *root, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   163) 				u64 start, size_t size, size_t compressed_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   164) 				int compress_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   165) 				struct page **compressed_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   167) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   168) 	struct page *page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   169) 	char *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   170) 	unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   171) 	struct btrfs_file_extent_item *ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   172) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   173) 	size_t cur_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   174) 	unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   176) 	ASSERT((compressed_size > 0 && compressed_pages) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   177) 	       (compressed_size == 0 && !compressed_pages));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   179) 	if (compressed_size && compressed_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   180) 		cur_size = compressed_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   182) 	inode_add_bytes(inode, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   184) 	if (!extent_inserted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   185) 		struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   186) 		size_t datasize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   188) 		key.objectid = btrfs_ino(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   189) 		key.offset = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   190) 		key.type = BTRFS_EXTENT_DATA_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   192) 		datasize = btrfs_file_extent_calc_inline_size(cur_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   193) 		path->leave_spinning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   194) 		ret = btrfs_insert_empty_item(trans, root, path, &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   195) 					      datasize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   196) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   197) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   199) 	leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   200) 	ei = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   201) 			    struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   202) 	btrfs_set_file_extent_generation(leaf, ei, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   203) 	btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   204) 	btrfs_set_file_extent_encryption(leaf, ei, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   205) 	btrfs_set_file_extent_other_encoding(leaf, ei, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   206) 	btrfs_set_file_extent_ram_bytes(leaf, ei, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   207) 	ptr = btrfs_file_extent_inline_start(ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   209) 	if (compress_type != BTRFS_COMPRESS_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   210) 		struct page *cpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   211) 		int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   212) 		while (compressed_size > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   213) 			cpage = compressed_pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   214) 			cur_size = min_t(unsigned long, compressed_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   215) 				       PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   217) 			kaddr = kmap_atomic(cpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   218) 			write_extent_buffer(leaf, kaddr, ptr, cur_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   219) 			kunmap_atomic(kaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   221) 			i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   222) 			ptr += cur_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   223) 			compressed_size -= cur_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   224) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   225) 		btrfs_set_file_extent_compression(leaf, ei,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   226) 						  compress_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   227) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   228) 		page = find_get_page(inode->i_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   229) 				     start >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   230) 		btrfs_set_file_extent_compression(leaf, ei, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   231) 		kaddr = kmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   232) 		offset = offset_in_page(start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   233) 		write_extent_buffer(leaf, kaddr + offset, ptr, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   234) 		kunmap_atomic(kaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   235) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   236) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   237) 	btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   238) 	btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   240) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   241) 	 * We align size to sectorsize for inline extents just for simplicity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   242) 	 * sake.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   243) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   244) 	size = ALIGN(size, root->fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   245) 	ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   246) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   247) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   249) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   250) 	 * we're an inline extent, so nobody can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   251) 	 * extend the file past i_size without locking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   252) 	 * a page we already have locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   253) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   254) 	 * We must do any isize and inode updates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   255) 	 * before we unlock the pages.  Otherwise we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   256) 	 * could end up racing with unlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   257) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   258) 	BTRFS_I(inode)->disk_i_size = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   259) 	ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   261) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   262) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   266) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   267)  * conditionally insert an inline extent into the file.  This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   268)  * does the checks required to make sure the data is small enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   269)  * to fit as an inline extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   270)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   271) static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   272) 					  u64 end, size_t compressed_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   273) 					  int compress_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   274) 					  struct page **compressed_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   276) 	struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   277) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   278) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   279) 	u64 isize = i_size_read(&inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   280) 	u64 actual_end = min(end + 1, isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   281) 	u64 inline_len = actual_end - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   282) 	u64 aligned_end = ALIGN(end, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   283) 	u64 data_len = inline_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   284) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   285) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   286) 	int extent_inserted = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   287) 	u32 extent_item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   289) 	if (compressed_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   290) 		data_len = compressed_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   292) 	if (start > 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   293) 	    actual_end > fs_info->sectorsize ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   294) 	    data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   295) 	    (!compressed_size &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   296) 	    (actual_end & (fs_info->sectorsize - 1)) == 0) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   297) 	    end + 1 < isize ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   298) 	    data_len > fs_info->max_inline) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   299) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   300) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   302) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   303) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   304) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   306) 	trans = btrfs_join_transaction(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   307) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   308) 		btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   309) 		return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   311) 	trans->block_rsv = &inode->block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   313) 	if (compressed_size && compressed_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   314) 		extent_item_size = btrfs_file_extent_calc_inline_size(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   315) 		   compressed_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   316) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   317) 		extent_item_size = btrfs_file_extent_calc_inline_size(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   318) 		    inline_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   320) 	ret = __btrfs_drop_extents(trans, root, inode, path, start, aligned_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   321) 				   NULL, 1, 1, extent_item_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   322) 				   &extent_inserted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   323) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   324) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   325) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   326) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   328) 	if (isize > actual_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   329) 		inline_len = min_t(u64, isize, actual_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   330) 	ret = insert_inline_extent(trans, path, extent_inserted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   331) 				   root, &inode->vfs_inode, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   332) 				   inline_len, compressed_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   333) 				   compress_type, compressed_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   334) 	if (ret && ret != -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   335) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   336) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   337) 	} else if (ret == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   338) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   339) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   342) 	set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   343) 	btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   344) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   345) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   346) 	 * Don't forget to free the reserved space, as for inlined extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   347) 	 * it won't count as data extent, free them directly here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   348) 	 * And at reserve time, it's always aligned to page size, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   349) 	 * just free one page here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   350) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   351) 	btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   352) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   353) 	btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   354) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   357) struct async_extent {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   358) 	u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   359) 	u64 ram_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   360) 	u64 compressed_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   361) 	struct page **pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   362) 	unsigned long nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   363) 	int compress_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   364) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   365) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   367) struct async_chunk {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   368) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   369) 	struct page *locked_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   370) 	u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   371) 	u64 end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   372) 	unsigned int write_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   373) 	struct list_head extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   374) 	struct cgroup_subsys_state *blkcg_css;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   375) 	struct btrfs_work work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   376) 	atomic_t *pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   377) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   379) struct async_cow {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   380) 	/* Number of chunks in flight; must be first in the structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   381) 	atomic_t num_chunks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   382) 	struct async_chunk chunks[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   383) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   385) static noinline int add_async_extent(struct async_chunk *cow,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   386) 				     u64 start, u64 ram_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   387) 				     u64 compressed_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   388) 				     struct page **pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   389) 				     unsigned long nr_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   390) 				     int compress_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   392) 	struct async_extent *async_extent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   394) 	async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   395) 	BUG_ON(!async_extent); /* -ENOMEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   396) 	async_extent->start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   397) 	async_extent->ram_size = ram_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   398) 	async_extent->compressed_size = compressed_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   399) 	async_extent->pages = pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   400) 	async_extent->nr_pages = nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   401) 	async_extent->compress_type = compress_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   402) 	list_add_tail(&async_extent->list, &cow->extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   403) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   406) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   407)  * Check if the inode has flags compatible with compression
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   408)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   409) static inline bool inode_can_compress(struct btrfs_inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   411) 	if (inode->flags & BTRFS_INODE_NODATACOW ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   412) 	    inode->flags & BTRFS_INODE_NODATASUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   413) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   414) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   417) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   418)  * Check if the inode needs to be submitted to compression, based on mount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   419)  * options, defragmentation, properties or heuristics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   420)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   421) static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   422) 				      u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   424) 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   426) 	if (!inode_can_compress(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   427) 		WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   428) 			KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   429) 			btrfs_ino(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   430) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   431) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   432) 	/* force compress */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   433) 	if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   434) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   435) 	/* defrag ioctl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   436) 	if (inode->defrag_compress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   437) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   438) 	/* bad compression ratios */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   439) 	if (inode->flags & BTRFS_INODE_NOCOMPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   440) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   441) 	if (btrfs_test_opt(fs_info, COMPRESS) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   442) 	    inode->flags & BTRFS_INODE_COMPRESS ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   443) 	    inode->prop_compress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   444) 		return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   445) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   448) static inline void inode_should_defrag(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   449) 		u64 start, u64 end, u64 num_bytes, u64 small_write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   451) 	/* If this is a small write inside eof, kick off a defrag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   452) 	if (num_bytes < small_write &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   453) 	    (start > 0 || end + 1 < inode->disk_i_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   454) 		btrfs_add_inode_defrag(NULL, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   457) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   458)  * we create compressed extents in two phases.  The first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   459)  * phase compresses a range of pages that have already been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   460)  * locked (both pages and state bits are locked).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   461)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   462)  * This is done inside an ordered work queue, and the compression
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   463)  * is spread across many cpus.  The actual IO submission is step
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   464)  * two, and the ordered work queue takes care of making sure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   465)  * happens in the same order things were put onto the queue by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   466)  * writepages and friends.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   467)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   468)  * If this code finds it can't get good compression, it puts an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   469)  * entry onto the work queue to write the uncompressed bytes.  This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   470)  * makes sure that both compressed inodes and uncompressed inodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   471)  * are written in the same order that the flusher thread sent them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   472)  * down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   473)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   474) static noinline int compress_file_range(struct async_chunk *async_chunk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   475) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   476) 	struct inode *inode = async_chunk->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   477) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   478) 	u64 blocksize = fs_info->sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   479) 	u64 start = async_chunk->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   480) 	u64 end = async_chunk->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   481) 	u64 actual_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   482) 	u64 i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   483) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   484) 	struct page **pages = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   485) 	unsigned long nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   486) 	unsigned long total_compressed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   487) 	unsigned long total_in = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   488) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   489) 	int will_compress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   490) 	int compress_type = fs_info->compress_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   491) 	int compressed_extents = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   492) 	int redirty = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   494) 	inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   495) 			SZ_16K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   497) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   498) 	 * We need to save i_size before now because it could change in between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   499) 	 * us evaluating the size and assigning it.  This is because we lock and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   500) 	 * unlock the page in truncate and fallocate, and then modify the i_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   501) 	 * later on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   502) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   503) 	 * The barriers are to emulate READ_ONCE, remove that once i_size_read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   504) 	 * does that for us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   505) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   506) 	barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   507) 	i_size = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   508) 	barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   509) 	actual_end = min_t(u64, i_size, end + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   510) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   511) 	will_compress = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   512) 	nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   513) 	BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   514) 	nr_pages = min_t(unsigned long, nr_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   515) 			BTRFS_MAX_COMPRESSED / PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   517) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   518) 	 * we don't want to send crud past the end of i_size through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   519) 	 * compression, that's just a waste of CPU time.  So, if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   520) 	 * end of the file is before the start of our current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   521) 	 * requested range of bytes, we bail out to the uncompressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   522) 	 * cleanup code that can deal with all of this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   523) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   524) 	 * It isn't really the fastest way to fix things, but this is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   525) 	 * very uncommon corner.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   526) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   527) 	if (actual_end <= start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   528) 		goto cleanup_and_bail_uncompressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   530) 	total_compressed = actual_end - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   532) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   533) 	 * skip compression for a small file range(<=blocksize) that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   534) 	 * isn't an inline extent, since it doesn't save disk space at all.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   535) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   536) 	if (total_compressed <= blocksize &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   537) 	   (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   538) 		goto cleanup_and_bail_uncompressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   540) 	total_compressed = min_t(unsigned long, total_compressed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   541) 			BTRFS_MAX_UNCOMPRESSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   542) 	total_in = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   543) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   545) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   546) 	 * we do compression for mount -o compress and when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   547) 	 * inode has not been flagged as nocompress.  This flag can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   548) 	 * change at any time if we discover bad compression ratios.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   549) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   550) 	if (inode_need_compress(BTRFS_I(inode), start, end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   551) 		WARN_ON(pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   552) 		pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   553) 		if (!pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   554) 			/* just bail out to the uncompressed code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   555) 			nr_pages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   556) 			goto cont;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   557) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   559) 		if (BTRFS_I(inode)->defrag_compress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   560) 			compress_type = BTRFS_I(inode)->defrag_compress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   561) 		else if (BTRFS_I(inode)->prop_compress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   562) 			compress_type = BTRFS_I(inode)->prop_compress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   564) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   565) 		 * we need to call clear_page_dirty_for_io on each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   566) 		 * page in the range.  Otherwise applications with the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   567) 		 * mmap'd can wander in and change the page contents while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   568) 		 * we are compressing them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   569) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   570) 		 * If the compression fails for any reason, we set the pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   571) 		 * dirty again later on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   572) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   573) 		 * Note that the remaining part is redirtied, the start pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   574) 		 * has moved, the end is the original one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   575) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   576) 		if (!redirty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   577) 			extent_range_clear_dirty_for_io(inode, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   578) 			redirty = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   579) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   581) 		/* Compression level is applied here and only here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   582) 		ret = btrfs_compress_pages(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   583) 			compress_type | (fs_info->compress_level << 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   584) 					   inode->i_mapping, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   585) 					   pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   586) 					   &nr_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   587) 					   &total_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   588) 					   &total_compressed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   590) 		if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   591) 			unsigned long offset = offset_in_page(total_compressed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   592) 			struct page *page = pages[nr_pages - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   593) 			char *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   595) 			/* zero the tail end of the last page, we might be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   596) 			 * sending it down to disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   597) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   598) 			if (offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   599) 				kaddr = kmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   600) 				memset(kaddr + offset, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   601) 				       PAGE_SIZE - offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   602) 				kunmap_atomic(kaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   603) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   604) 			will_compress = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   605) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   606) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   607) cont:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   608) 	if (start == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   609) 		/* lets try to make an inline extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   610) 		if (ret || total_in < actual_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   611) 			/* we didn't compress the entire range, try
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   612) 			 * to make an uncompressed inline extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   613) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   614) 			ret = cow_file_range_inline(BTRFS_I(inode), start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   615) 						    0, BTRFS_COMPRESS_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   616) 						    NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   617) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   618) 			/* try making a compressed inline extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   619) 			ret = cow_file_range_inline(BTRFS_I(inode), start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   620) 						    total_compressed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   621) 						    compress_type, pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   622) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   623) 		if (ret <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   624) 			unsigned long clear_flags = EXTENT_DELALLOC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   625) 				EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   626) 				EXTENT_DO_ACCOUNTING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   627) 			unsigned long page_error_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   629) 			page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   631) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   632) 			 * inline extent creation worked or returned error,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   633) 			 * we don't need to create any more async work items.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   634) 			 * Unlock and free up our temp pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   635) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   636) 			 * We use DO_ACCOUNTING here because we need the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   637) 			 * delalloc_release_metadata to be done _after_ we drop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   638) 			 * our outstanding extent for clearing delalloc for this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   639) 			 * range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   640) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   641) 			extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   642) 						     NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   643) 						     clear_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   644) 						     PAGE_UNLOCK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   645) 						     PAGE_CLEAR_DIRTY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   646) 						     PAGE_SET_WRITEBACK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   647) 						     page_error_op |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   648) 						     PAGE_END_WRITEBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   650) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   651) 			 * Ensure we only free the compressed pages if we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   652) 			 * them allocated, as we can still reach here with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   653) 			 * inode_need_compress() == false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   654) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   655) 			if (pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   656) 				for (i = 0; i < nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   657) 					WARN_ON(pages[i]->mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   658) 					put_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   659) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   660) 				kfree(pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   661) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   662) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   663) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   664) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   666) 	if (will_compress) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   667) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   668) 		 * we aren't doing an inline extent round the compressed size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   669) 		 * up to a block size boundary so the allocator does sane
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   670) 		 * things
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   671) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   672) 		total_compressed = ALIGN(total_compressed, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   674) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   675) 		 * one last check to make sure the compression is really a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   676) 		 * win, compare the page count read with the blocks on disk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   677) 		 * compression must free at least one sector size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   678) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   679) 		total_in = ALIGN(total_in, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   680) 		if (total_compressed + blocksize <= total_in) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   681) 			compressed_extents++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   683) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   684) 			 * The async work queues will take care of doing actual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   685) 			 * allocation on disk for these compressed pages, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   686) 			 * will submit them to the elevator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   687) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   688) 			add_async_extent(async_chunk, start, total_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   689) 					total_compressed, pages, nr_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   690) 					compress_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   692) 			if (start + total_in < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   693) 				start += total_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   694) 				pages = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   695) 				cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   696) 				goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   697) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   698) 			return compressed_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   699) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   700) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   701) 	if (pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   702) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   703) 		 * the compression code ran but failed to make things smaller,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   704) 		 * free any pages it allocated and our page pointer array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   705) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   706) 		for (i = 0; i < nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   707) 			WARN_ON(pages[i]->mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   708) 			put_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   709) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   710) 		kfree(pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   711) 		pages = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   712) 		total_compressed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   713) 		nr_pages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   715) 		/* flag the file so we don't compress in the future */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   716) 		if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   717) 		    !(BTRFS_I(inode)->prop_compress)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   718) 			BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   719) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   720) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   721) cleanup_and_bail_uncompressed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   722) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   723) 	 * No compression, but we still need to write the pages in the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   724) 	 * we've been given so far.  redirty the locked page if it corresponds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   725) 	 * to our extent and set things up for the async work queue to run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   726) 	 * cow_file_range to do the normal delalloc dance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   727) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   728) 	if (async_chunk->locked_page &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   729) 	    (page_offset(async_chunk->locked_page) >= start &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   730) 	     page_offset(async_chunk->locked_page)) <= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   731) 		__set_page_dirty_nobuffers(async_chunk->locked_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   732) 		/* unlocked later on in the async handlers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   733) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   735) 	if (redirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   736) 		extent_range_redirty_for_io(inode, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   737) 	add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   738) 			 BTRFS_COMPRESS_NONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   739) 	compressed_extents++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   741) 	return compressed_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   744) static void free_async_extent_pages(struct async_extent *async_extent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   745) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   746) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   748) 	if (!async_extent->pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   749) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   751) 	for (i = 0; i < async_extent->nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   752) 		WARN_ON(async_extent->pages[i]->mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   753) 		put_page(async_extent->pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   754) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   755) 	kfree(async_extent->pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   756) 	async_extent->nr_pages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   757) 	async_extent->pages = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   760) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   761)  * phase two of compressed writeback.  This is the ordered portion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   762)  * of the code, which only gets called in the order the work was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   763)  * queued.  We walk all the async extents created by compress_file_range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   764)  * and send them down to the disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   765)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   766) static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   767) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   768) 	struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   769) 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   770) 	struct async_extent *async_extent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   771) 	u64 alloc_hint = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   772) 	struct btrfs_key ins;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   773) 	struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   774) 	struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   775) 	struct extent_io_tree *io_tree = &inode->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   776) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   778) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   779) 	while (!list_empty(&async_chunk->extents)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   780) 		async_extent = list_entry(async_chunk->extents.next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   781) 					  struct async_extent, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   782) 		list_del(&async_extent->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   784) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   785) 		lock_extent(io_tree, async_extent->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   786) 			    async_extent->start + async_extent->ram_size - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   787) 		/* did the compression code fall back to uncompressed IO? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   788) 		if (!async_extent->pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   789) 			int page_started = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   790) 			unsigned long nr_written = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   792) 			/* allocate blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   793) 			ret = cow_file_range(inode, async_chunk->locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   794) 					     async_extent->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   795) 					     async_extent->start +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   796) 					     async_extent->ram_size - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   797) 					     &page_started, &nr_written, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   799) 			/* JDM XXX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   801) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   802) 			 * if page_started, cow_file_range inserted an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   803) 			 * inline extent and took care of all the unlocking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   804) 			 * and IO for us.  Otherwise, we need to submit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   805) 			 * all those pages down to the drive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   806) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   807) 			if (!page_started && !ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   808) 				extent_write_locked_range(&inode->vfs_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   809) 						  async_extent->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   810) 						  async_extent->start +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   811) 						  async_extent->ram_size - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   812) 						  WB_SYNC_ALL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   813) 			else if (ret && async_chunk->locked_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   814) 				unlock_page(async_chunk->locked_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   815) 			kfree(async_extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   816) 			cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   817) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   818) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   820) 		ret = btrfs_reserve_extent(root, async_extent->ram_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   821) 					   async_extent->compressed_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   822) 					   async_extent->compressed_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   823) 					   0, alloc_hint, &ins, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   824) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   825) 			free_async_extent_pages(async_extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   827) 			if (ret == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   828) 				unlock_extent(io_tree, async_extent->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   829) 					      async_extent->start +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   830) 					      async_extent->ram_size - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   832) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   833) 				 * we need to redirty the pages if we decide to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   834) 				 * fallback to uncompressed IO, otherwise we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   835) 				 * will not submit these pages down to lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   836) 				 * layers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   837) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   838) 				extent_range_redirty_for_io(&inode->vfs_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   839) 						async_extent->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   840) 						async_extent->start +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   841) 						async_extent->ram_size - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   843) 				goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   844) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   845) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   846) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   847) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   848) 		 * here we're doing allocation and writeback of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   849) 		 * compressed pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   850) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   851) 		em = create_io_em(inode, async_extent->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   852) 				  async_extent->ram_size, /* len */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   853) 				  async_extent->start, /* orig_start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   854) 				  ins.objectid, /* block_start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   855) 				  ins.offset, /* block_len */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   856) 				  ins.offset, /* orig_block_len */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   857) 				  async_extent->ram_size, /* ram_bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   858) 				  async_extent->compress_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   859) 				  BTRFS_ORDERED_COMPRESSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   860) 		if (IS_ERR(em))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   861) 			/* ret value is not necessary due to void function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   862) 			goto out_free_reserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   863) 		free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   865) 		ret = btrfs_add_ordered_extent_compress(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   866) 						async_extent->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   867) 						ins.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   868) 						async_extent->ram_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   869) 						ins.offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   870) 						BTRFS_ORDERED_COMPRESSED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   871) 						async_extent->compress_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   872) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   873) 			btrfs_drop_extent_cache(inode, async_extent->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   874) 						async_extent->start +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   875) 						async_extent->ram_size - 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   876) 			goto out_free_reserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   877) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   878) 		btrfs_dec_block_group_reservations(fs_info, ins.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   880) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   881) 		 * clear dirty, set writeback and unlock the pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   882) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   883) 		extent_clear_unlock_delalloc(inode, async_extent->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   884) 				async_extent->start +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   885) 				async_extent->ram_size - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   886) 				NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   887) 				PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   888) 				PAGE_SET_WRITEBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   889) 		if (btrfs_submit_compressed_write(inode, async_extent->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   890) 				    async_extent->ram_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   891) 				    ins.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   892) 				    ins.offset, async_extent->pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   893) 				    async_extent->nr_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   894) 				    async_chunk->write_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   895) 				    async_chunk->blkcg_css)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   896) 			struct page *p = async_extent->pages[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   897) 			const u64 start = async_extent->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   898) 			const u64 end = start + async_extent->ram_size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   900) 			p->mapping = inode->vfs_inode.i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   901) 			btrfs_writepage_endio_finish_ordered(p, start, end, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   903) 			p->mapping = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   904) 			extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   905) 						     PAGE_END_WRITEBACK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   906) 						     PAGE_SET_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   907) 			free_async_extent_pages(async_extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   908) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   909) 		alloc_hint = ins.objectid + ins.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   910) 		kfree(async_extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   911) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   912) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   913) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   914) out_free_reserve:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   915) 	btrfs_dec_block_group_reservations(fs_info, ins.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   916) 	btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   917) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   918) 	extent_clear_unlock_delalloc(inode, async_extent->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   919) 				     async_extent->start +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   920) 				     async_extent->ram_size - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   921) 				     NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   922) 				     EXTENT_DELALLOC_NEW |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   923) 				     EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   924) 				     PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   925) 				     PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   926) 				     PAGE_SET_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   927) 	free_async_extent_pages(async_extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   928) 	kfree(async_extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   929) 	goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   930) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   932) static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   933) 				      u64 num_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   934) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   935) 	struct extent_map_tree *em_tree = &inode->extent_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   936) 	struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   937) 	u64 alloc_hint = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   939) 	read_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   940) 	em = search_extent_mapping(em_tree, start, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   941) 	if (em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   942) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   943) 		 * if block start isn't an actual block number then find the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   944) 		 * first block in this inode and use that as a hint.  If that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   945) 		 * block is also bogus then just don't worry about it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   946) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   947) 		if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   948) 			free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   949) 			em = search_extent_mapping(em_tree, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   950) 			if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   951) 				alloc_hint = em->block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   952) 			if (em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   953) 				free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   954) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   955) 			alloc_hint = em->block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   956) 			free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   957) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   958) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   959) 	read_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   961) 	return alloc_hint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   962) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   964) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   965)  * when extent_io.c finds a delayed allocation range in the file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   966)  * the call backs end up in this code.  The basic idea is to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   967)  * allocate extents on disk for the range, and create ordered data structs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   968)  * in ram to track those extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   969)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   970)  * locked_page is the page that writepage had locked already.  We use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   971)  * it to make sure we don't do extra locks or unlocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   972)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   973)  * *page_started is set to one if we unlock locked_page and do everything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   974)  * required to start IO on it.  It may be clean and already done with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   975)  * IO when we return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   976)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   977) static noinline int cow_file_range(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   978) 				   struct page *locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   979) 				   u64 start, u64 end, int *page_started,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   980) 				   unsigned long *nr_written, int unlock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   981) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   982) 	struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   983) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   984) 	u64 alloc_hint = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   985) 	u64 num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   986) 	unsigned long ram_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   987) 	u64 cur_alloc_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   988) 	u64 min_alloc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   989) 	u64 blocksize = fs_info->sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   990) 	struct btrfs_key ins;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   991) 	struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   992) 	unsigned clear_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   993) 	unsigned long page_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   994) 	bool extent_reserved = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   995) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   997) 	if (btrfs_is_free_space_inode(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   998) 		WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   999) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1000) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1001) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1003) 	num_bytes = ALIGN(end - start + 1, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1004) 	num_bytes = max(blocksize,  num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1005) 	ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1007) 	inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1009) 	if (start == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1010) 		/* lets try to make an inline extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1011) 		ret = cow_file_range_inline(inode, start, end, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1012) 					    BTRFS_COMPRESS_NONE, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1013) 		if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1014) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1015) 			 * We use DO_ACCOUNTING here because we need the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1016) 			 * delalloc_release_metadata to be run _after_ we drop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1017) 			 * our outstanding extent for clearing delalloc for this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1018) 			 * range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1019) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1020) 			extent_clear_unlock_delalloc(inode, start, end, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1021) 				     EXTENT_LOCKED | EXTENT_DELALLOC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1022) 				     EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1023) 				     EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1024) 				     PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1025) 				     PAGE_END_WRITEBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1026) 			*nr_written = *nr_written +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1027) 			     (end - start + PAGE_SIZE) / PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1028) 			*page_started = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1029) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1030) 		} else if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1031) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1032) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1033) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1035) 	alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1036) 	btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1038) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1039) 	 * Relocation relies on the relocated extents to have exactly the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1040) 	 * size as the original extents. Normally writeback for relocation data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1041) 	 * extents follows a NOCOW path because relocation preallocates the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1042) 	 * extents. However, due to an operation such as scrub turning a block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1043) 	 * group to RO mode, it may fallback to COW mode, so we must make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1044) 	 * an extent allocated during COW has exactly the requested size and can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1045) 	 * not be split into smaller extents, otherwise relocation breaks and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1046) 	 * fails during the stage where it updates the bytenr of file extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1047) 	 * items.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1048) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1049) 	if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1050) 		min_alloc_size = num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1051) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1052) 		min_alloc_size = fs_info->sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1054) 	while (num_bytes > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1055) 		cur_alloc_size = num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1056) 		ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1057) 					   min_alloc_size, 0, alloc_hint,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1058) 					   &ins, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1059) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1060) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1061) 		cur_alloc_size = ins.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1062) 		extent_reserved = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1064) 		ram_size = ins.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1065) 		em = create_io_em(inode, start, ins.offset, /* len */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1066) 				  start, /* orig_start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1067) 				  ins.objectid, /* block_start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1068) 				  ins.offset, /* block_len */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1069) 				  ins.offset, /* orig_block_len */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1070) 				  ram_size, /* ram_bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1071) 				  BTRFS_COMPRESS_NONE, /* compress_type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1072) 				  BTRFS_ORDERED_REGULAR /* type */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1073) 		if (IS_ERR(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1074) 			ret = PTR_ERR(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1075) 			goto out_reserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1076) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1077) 		free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1079) 		ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1080) 					       ram_size, cur_alloc_size, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1081) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1082) 			goto out_drop_extent_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1084) 		if (root->root_key.objectid ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1085) 		    BTRFS_DATA_RELOC_TREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1086) 			ret = btrfs_reloc_clone_csums(inode, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1087) 						      cur_alloc_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1088) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1089) 			 * Only drop cache here, and process as normal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1090) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1091) 			 * We must not allow extent_clear_unlock_delalloc()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1092) 			 * at out_unlock label to free meta of this ordered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1093) 			 * extent, as its meta should be freed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1094) 			 * btrfs_finish_ordered_io().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1095) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1096) 			 * So we must continue until @start is increased to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1097) 			 * skip current ordered extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1098) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1099) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1100) 				btrfs_drop_extent_cache(inode, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1101) 						start + ram_size - 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1102) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1104) 		btrfs_dec_block_group_reservations(fs_info, ins.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1106) 		/* we're not doing compressed IO, don't unlock the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1107) 		 * page (which the caller expects to stay locked), don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1108) 		 * clear any dirty bits and don't set any writeback bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1109) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1110) 		 * Do set the Private2 bit so we know this page was properly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1111) 		 * setup for writepage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1112) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1113) 		page_ops = unlock ? PAGE_UNLOCK : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1114) 		page_ops |= PAGE_SET_PRIVATE2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1116) 		extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1117) 					     locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1118) 					     EXTENT_LOCKED | EXTENT_DELALLOC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1119) 					     page_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1120) 		if (num_bytes < cur_alloc_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1121) 			num_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1122) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1123) 			num_bytes -= cur_alloc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1124) 		alloc_hint = ins.objectid + ins.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1125) 		start += cur_alloc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1126) 		extent_reserved = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1128) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1129) 		 * btrfs_reloc_clone_csums() error, since start is increased
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1130) 		 * extent_clear_unlock_delalloc() at out_unlock label won't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1131) 		 * free metadata of current ordered extent, we're OK to exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1132) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1133) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1134) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1136) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1137) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1139) out_drop_extent_cache:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1140) 	btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1141) out_reserve:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1142) 	btrfs_dec_block_group_reservations(fs_info, ins.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1143) 	btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1144) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1145) 	clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1146) 		EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1147) 	page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1148) 		PAGE_END_WRITEBACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1149) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1150) 	 * If we reserved an extent for our delalloc range (or a subrange) and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1151) 	 * failed to create the respective ordered extent, then it means that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1152) 	 * when we reserved the extent we decremented the extent's size from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1153) 	 * the data space_info's bytes_may_use counter and incremented the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1154) 	 * space_info's bytes_reserved counter by the same amount. We must make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1155) 	 * sure extent_clear_unlock_delalloc() does not try to decrement again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1156) 	 * the data space_info's bytes_may_use counter, therefore we do not pass
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1157) 	 * it the flag EXTENT_CLEAR_DATA_RESV.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1158) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1159) 	if (extent_reserved) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1160) 		extent_clear_unlock_delalloc(inode, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1161) 					     start + cur_alloc_size - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1162) 					     locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1163) 					     clear_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1164) 					     page_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1165) 		start += cur_alloc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1166) 		if (start >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1167) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1168) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1169) 	extent_clear_unlock_delalloc(inode, start, end, locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1170) 				     clear_bits | EXTENT_CLEAR_DATA_RESV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1171) 				     page_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1172) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1175) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1176)  * work queue call back to started compression on a file and pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1177)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1178) static noinline void async_cow_start(struct btrfs_work *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1180) 	struct async_chunk *async_chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1181) 	int compressed_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1183) 	async_chunk = container_of(work, struct async_chunk, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1185) 	compressed_extents = compress_file_range(async_chunk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1186) 	if (compressed_extents == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1187) 		btrfs_add_delayed_iput(async_chunk->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1188) 		async_chunk->inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1192) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1193)  * work queue call back to submit previously compressed pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1194)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1195) static noinline void async_cow_submit(struct btrfs_work *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1197) 	struct async_chunk *async_chunk = container_of(work, struct async_chunk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1198) 						     work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1199) 	struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1200) 	unsigned long nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1202) 	nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1203) 		PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1205) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1206) 	 * ->inode could be NULL if async_chunk_start has failed to compress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1207) 	 * in which case we don't have anything to submit, yet we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1208) 	 * always adjust ->async_delalloc_pages as its paired with the init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1209) 	 * happening in cow_file_range_async
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1210) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1211) 	if (async_chunk->inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1212) 		submit_compressed_extents(async_chunk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1214) 	/* atomic_sub_return implies a barrier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1215) 	if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1216) 	    5 * SZ_1M)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1217) 		cond_wake_up_nomb(&fs_info->async_submit_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1220) static noinline void async_cow_free(struct btrfs_work *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1222) 	struct async_chunk *async_chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1224) 	async_chunk = container_of(work, struct async_chunk, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1225) 	if (async_chunk->inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1226) 		btrfs_add_delayed_iput(async_chunk->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1227) 	if (async_chunk->blkcg_css)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1228) 		css_put(async_chunk->blkcg_css);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1229) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1230) 	 * Since the pointer to 'pending' is at the beginning of the array of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1231) 	 * async_chunk's, freeing it ensures the whole array has been freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1232) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1233) 	if (atomic_dec_and_test(async_chunk->pending))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1234) 		kvfree(async_chunk->pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1237) static int cow_file_range_async(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1238) 				struct writeback_control *wbc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1239) 				struct page *locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1240) 				u64 start, u64 end, int *page_started,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1241) 				unsigned long *nr_written)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1243) 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1244) 	struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1245) 	struct async_cow *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1246) 	struct async_chunk *async_chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1247) 	unsigned long nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1248) 	u64 cur_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1249) 	u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1250) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1251) 	bool should_compress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1252) 	unsigned nofs_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1253) 	const unsigned int write_flags = wbc_to_write_flags(wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1255) 	unlock_extent(&inode->io_tree, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1257) 	if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1258) 	    !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1259) 		num_chunks = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1260) 		should_compress = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1261) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1262) 		should_compress = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1265) 	nofs_flag = memalloc_nofs_save();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1266) 	ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1267) 	memalloc_nofs_restore(nofs_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1269) 	if (!ctx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1270) 		unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1271) 			EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1272) 			EXTENT_DO_ACCOUNTING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1273) 		unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1274) 			PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1275) 			PAGE_SET_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1277) 		extent_clear_unlock_delalloc(inode, start, end, locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1278) 					     clear_bits, page_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1279) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1280) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1282) 	async_chunk = ctx->chunks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1283) 	atomic_set(&ctx->num_chunks, num_chunks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1285) 	for (i = 0; i < num_chunks; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1286) 		if (should_compress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1287) 			cur_end = min(end, start + SZ_512K - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1288) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1289) 			cur_end = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1291) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1292) 		 * igrab is called higher up in the call chain, take only the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1293) 		 * lightweight reference for the callback lifetime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1294) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1295) 		ihold(&inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1296) 		async_chunk[i].pending = &ctx->num_chunks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1297) 		async_chunk[i].inode = &inode->vfs_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1298) 		async_chunk[i].start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1299) 		async_chunk[i].end = cur_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1300) 		async_chunk[i].write_flags = write_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1301) 		INIT_LIST_HEAD(&async_chunk[i].extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1303) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1304) 		 * The locked_page comes all the way from writepage and its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1305) 		 * the original page we were actually given.  As we spread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1306) 		 * this large delalloc region across multiple async_chunk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1307) 		 * structs, only the first struct needs a pointer to locked_page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1308) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1309) 		 * This way we don't need racey decisions about who is supposed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1310) 		 * to unlock it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1311) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1312) 		if (locked_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1313) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1314) 			 * Depending on the compressibility, the pages might or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1315) 			 * might not go through async.  We want all of them to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1316) 			 * be accounted against wbc once.  Let's do it here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1317) 			 * before the paths diverge.  wbc accounting is used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1318) 			 * only for foreign writeback detection and doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1319) 			 * need full accuracy.  Just account the whole thing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1320) 			 * against the first page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1321) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1322) 			wbc_account_cgroup_owner(wbc, locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1323) 						 cur_end - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1324) 			async_chunk[i].locked_page = locked_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1325) 			locked_page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1326) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1327) 			async_chunk[i].locked_page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1328) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1330) 		if (blkcg_css != blkcg_root_css) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1331) 			css_get(blkcg_css);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1332) 			async_chunk[i].blkcg_css = blkcg_css;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1333) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1334) 			async_chunk[i].blkcg_css = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1335) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1337) 		btrfs_init_work(&async_chunk[i].work, async_cow_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1338) 				async_cow_submit, async_cow_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1340) 		nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1341) 		atomic_add(nr_pages, &fs_info->async_delalloc_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1343) 		btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1345) 		*nr_written += nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1346) 		start = cur_end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1347) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1348) 	*page_started = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1349) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1352) static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1353) 					u64 bytenr, u64 num_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1355) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1356) 	struct btrfs_ordered_sum *sums;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1357) 	LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1359) 	ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1360) 				       bytenr + num_bytes - 1, &list, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1361) 	if (ret == 0 && list_empty(&list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1362) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1364) 	while (!list_empty(&list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1365) 		sums = list_entry(list.next, struct btrfs_ordered_sum, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1366) 		list_del(&sums->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1367) 		kfree(sums);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1368) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1369) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1370) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1371) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1374) static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1375) 			   const u64 start, const u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1376) 			   int *page_started, unsigned long *nr_written)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1378) 	const bool is_space_ino = btrfs_is_free_space_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1379) 	const bool is_reloc_ino = (inode->root->root_key.objectid ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1380) 				   BTRFS_DATA_RELOC_TREE_OBJECTID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1381) 	const u64 range_bytes = end + 1 - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1382) 	struct extent_io_tree *io_tree = &inode->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1383) 	u64 range_start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1384) 	u64 count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1386) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1387) 	 * If EXTENT_NORESERVE is set it means that when the buffered write was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1388) 	 * made we had not enough available data space and therefore we did not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1389) 	 * reserve data space for it, since we though we could do NOCOW for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1390) 	 * respective file range (either there is prealloc extent or the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1391) 	 * has the NOCOW bit set).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1392) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1393) 	 * However when we need to fallback to COW mode (because for example the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1394) 	 * block group for the corresponding extent was turned to RO mode by a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1395) 	 * scrub or relocation) we need to do the following:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1396) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1397) 	 * 1) We increment the bytes_may_use counter of the data space info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1398) 	 *    If COW succeeds, it allocates a new data extent and after doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1399) 	 *    that it decrements the space info's bytes_may_use counter and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1400) 	 *    increments its bytes_reserved counter by the same amount (we do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1401) 	 *    this at btrfs_add_reserved_bytes()). So we need to increment the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1402) 	 *    bytes_may_use counter to compensate (when space is reserved at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1403) 	 *    buffered write time, the bytes_may_use counter is incremented);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1404) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1405) 	 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1406) 	 *    that if the COW path fails for any reason, it decrements (through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1407) 	 *    extent_clear_unlock_delalloc()) the bytes_may_use counter of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1408) 	 *    data space info, which we incremented in the step above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1409) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1410) 	 * If we need to fallback to cow and the inode corresponds to a free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1411) 	 * space cache inode or an inode of the data relocation tree, we must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1412) 	 * also increment bytes_may_use of the data space_info for the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1413) 	 * reason. Space caches and relocated data extents always get a prealloc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1414) 	 * extent for them, however scrub or balance may have set the block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1415) 	 * group that contains that extent to RO mode and therefore force COW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1416) 	 * when starting writeback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1417) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1418) 	count = count_range_bits(io_tree, &range_start, end, range_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1419) 				 EXTENT_NORESERVE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1420) 	if (count > 0 || is_space_ino || is_reloc_ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1421) 		u64 bytes = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1422) 		struct btrfs_fs_info *fs_info = inode->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1423) 		struct btrfs_space_info *sinfo = fs_info->data_sinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1425) 		if (is_space_ino || is_reloc_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1426) 			bytes = range_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1428) 		spin_lock(&sinfo->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1429) 		btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1430) 		spin_unlock(&sinfo->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1432) 		if (count > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1433) 			clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1434) 					 0, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1437) 	return cow_file_range(inode, locked_page, start, end, page_started,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1438) 			      nr_written, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1441) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1442)  * when nowcow writeback call back.  This checks for snapshots or COW copies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1443)  * of the extents that exist in the file, and COWs the file as required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1444)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1445)  * If no cow copies or snapshots exist, we write directly to the existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1446)  * blocks on disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1447)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1448) static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1449) 				       struct page *locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1450) 				       const u64 start, const u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1451) 				       int *page_started, int force,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1452) 				       unsigned long *nr_written)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1454) 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1455) 	struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1456) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1457) 	u64 cow_start = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1458) 	u64 cur_offset = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1459) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1460) 	bool check_prev = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1461) 	const bool freespace_inode = btrfs_is_free_space_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1462) 	u64 ino = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1463) 	bool nocow = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1464) 	u64 disk_bytenr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1466) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1467) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1468) 		extent_clear_unlock_delalloc(inode, start, end, locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1469) 					     EXTENT_LOCKED | EXTENT_DELALLOC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1470) 					     EXTENT_DO_ACCOUNTING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1471) 					     EXTENT_DEFRAG, PAGE_UNLOCK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1472) 					     PAGE_CLEAR_DIRTY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1473) 					     PAGE_SET_WRITEBACK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1474) 					     PAGE_END_WRITEBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1475) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1476) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1478) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1479) 		struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1480) 		struct btrfs_file_extent_item *fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1481) 		struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1482) 		u64 extent_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1483) 		u64 extent_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1484) 		u64 num_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1485) 		u64 disk_num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1486) 		u64 ram_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1487) 		int extent_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1489) 		nocow = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1491) 		ret = btrfs_lookup_file_extent(NULL, root, path, ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1492) 					       cur_offset, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1493) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1494) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1496) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1497) 		 * If there is no extent for our range when doing the initial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1498) 		 * search, then go back to the previous slot as it will be the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1499) 		 * one containing the search offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1500) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1501) 		if (ret > 0 && path->slots[0] > 0 && check_prev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1502) 			leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1503) 			btrfs_item_key_to_cpu(leaf, &found_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1504) 					      path->slots[0] - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1505) 			if (found_key.objectid == ino &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1506) 			    found_key.type == BTRFS_EXTENT_DATA_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1507) 				path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1508) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1509) 		check_prev = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1510) next_slot:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1511) 		/* Go to next leaf if we have exhausted the current one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1512) 		leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1513) 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1514) 			ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1515) 			if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1516) 				if (cow_start != (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1517) 					cur_offset = cow_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1518) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1519) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1520) 			if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1521) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1522) 			leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1523) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1525) 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1527) 		/* Didn't find anything for our INO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1528) 		if (found_key.objectid > ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1529) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1530) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1531) 		 * Keep searching until we find an EXTENT_ITEM or there are no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1532) 		 * more extents for this inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1533) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1534) 		if (WARN_ON_ONCE(found_key.objectid < ino) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1535) 		    found_key.type < BTRFS_EXTENT_DATA_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1536) 			path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1537) 			goto next_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1538) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1540) 		/* Found key is not EXTENT_DATA_KEY or starts after req range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1541) 		if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1542) 		    found_key.offset > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1543) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1545) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1546) 		 * If the found extent starts after requested offset, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1547) 		 * adjust extent_end to be right before this extent begins
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1548) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1549) 		if (found_key.offset > cur_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1550) 			extent_end = found_key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1551) 			extent_type = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1552) 			goto out_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1553) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1555) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1556) 		 * Found extent which begins before our range and potentially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1557) 		 * intersect it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1558) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1559) 		fi = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1560) 				    struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1561) 		extent_type = btrfs_file_extent_type(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1563) 		ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1564) 		if (extent_type == BTRFS_FILE_EXTENT_REG ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1565) 		    extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1566) 			disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1567) 			extent_offset = btrfs_file_extent_offset(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1568) 			extent_end = found_key.offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1569) 				btrfs_file_extent_num_bytes(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1570) 			disk_num_bytes =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1571) 				btrfs_file_extent_disk_num_bytes(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1572) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1573) 			 * If the extent we got ends before our current offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1574) 			 * skip to the next extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1575) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1576) 			if (extent_end <= cur_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1577) 				path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1578) 				goto next_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1579) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1580) 			/* Skip holes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1581) 			if (disk_bytenr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1582) 				goto out_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1583) 			/* Skip compressed/encrypted/encoded extents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1584) 			if (btrfs_file_extent_compression(leaf, fi) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1585) 			    btrfs_file_extent_encryption(leaf, fi) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1586) 			    btrfs_file_extent_other_encoding(leaf, fi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1587) 				goto out_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1588) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1589) 			 * If extent is created before the last volume's snapshot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1590) 			 * this implies the extent is shared, hence we can't do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1591) 			 * nocow. This is the same check as in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1592) 			 * btrfs_cross_ref_exist but without calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1593) 			 * btrfs_search_slot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1594) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1595) 			if (!freespace_inode &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1596) 			    btrfs_file_extent_generation(leaf, fi) <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1597) 			    btrfs_root_last_snapshot(&root->root_item))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1598) 				goto out_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1599) 			if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1600) 				goto out_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1601) 			/* If extent is RO, we must COW it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1602) 			if (btrfs_extent_readonly(fs_info, disk_bytenr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1603) 				goto out_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1604) 			ret = btrfs_cross_ref_exist(root, ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1605) 						    found_key.offset -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1606) 						    extent_offset, disk_bytenr, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1607) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1608) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1609) 				 * ret could be -EIO if the above fails to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1610) 				 * metadata.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1611) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1612) 				if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1613) 					if (cow_start != (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1614) 						cur_offset = cow_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1615) 					goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1616) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1618) 				WARN_ON_ONCE(freespace_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1619) 				goto out_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1620) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1621) 			disk_bytenr += extent_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1622) 			disk_bytenr += cur_offset - found_key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1623) 			num_bytes = min(end + 1, extent_end) - cur_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1624) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1625) 			 * If there are pending snapshots for this root, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1626) 			 * fall into common COW way
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1627) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1628) 			if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1629) 				goto out_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1630) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1631) 			 * force cow if csum exists in the range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1632) 			 * this ensure that csum for a given extent are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1633) 			 * either valid or do not exist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1634) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1635) 			ret = csum_exist_in_range(fs_info, disk_bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1636) 						  num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1637) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1638) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1639) 				 * ret could be -EIO if the above fails to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1640) 				 * metadata.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1641) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1642) 				if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1643) 					if (cow_start != (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1644) 						cur_offset = cow_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1645) 					goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1646) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1647) 				WARN_ON_ONCE(freespace_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1648) 				goto out_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1649) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1650) 			if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1651) 				goto out_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1652) 			nocow = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1653) 		} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1654) 			extent_end = found_key.offset + ram_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1655) 			extent_end = ALIGN(extent_end, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1656) 			/* Skip extents outside of our requested range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1657) 			if (extent_end <= start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1658) 				path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1659) 				goto next_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1660) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1661) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1662) 			/* If this triggers then we have a memory corruption */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1663) 			BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1664) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1665) out_check:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1666) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1667) 		 * If nocow is false then record the beginning of the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1668) 		 * that needs to be COWed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1669) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1670) 		if (!nocow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1671) 			if (cow_start == (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1672) 				cow_start = cur_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1673) 			cur_offset = extent_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1674) 			if (cur_offset > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1675) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1676) 			path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1677) 			goto next_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1678) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1680) 		btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1682) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1683) 		 * COW range from cow_start to found_key.offset - 1. As the key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1684) 		 * will contain the beginning of the first extent that can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1685) 		 * NOCOW, following one which needs to be COW'ed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1686) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1687) 		if (cow_start != (u64)-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1688) 			ret = fallback_to_cow(inode, locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1689) 					      cow_start, found_key.offset - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1690) 					      page_started, nr_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1691) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1692) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1693) 			cow_start = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1694) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1696) 		if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1697) 			u64 orig_start = found_key.offset - extent_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1698) 			struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1700) 			em = create_io_em(inode, cur_offset, num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1701) 					  orig_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1702) 					  disk_bytenr, /* block_start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1703) 					  num_bytes, /* block_len */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1704) 					  disk_num_bytes, /* orig_block_len */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1705) 					  ram_bytes, BTRFS_COMPRESS_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1706) 					  BTRFS_ORDERED_PREALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1707) 			if (IS_ERR(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1708) 				ret = PTR_ERR(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1709) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1710) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1711) 			free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1712) 			ret = btrfs_add_ordered_extent(inode, cur_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1713) 						       disk_bytenr, num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1714) 						       num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1715) 						       BTRFS_ORDERED_PREALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1716) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1717) 				btrfs_drop_extent_cache(inode, cur_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1718) 							cur_offset + num_bytes - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1719) 							0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1720) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1721) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1722) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1723) 			ret = btrfs_add_ordered_extent(inode, cur_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1724) 						       disk_bytenr, num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1725) 						       num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1726) 						       BTRFS_ORDERED_NOCOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1727) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1728) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1729) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1731) 		if (nocow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1732) 			btrfs_dec_nocow_writers(fs_info, disk_bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1733) 		nocow = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1735) 		if (root->root_key.objectid ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1736) 		    BTRFS_DATA_RELOC_TREE_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1737) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1738) 			 * Error handled later, as we must prevent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1739) 			 * extent_clear_unlock_delalloc() in error handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1740) 			 * from freeing metadata of created ordered extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1741) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1742) 			ret = btrfs_reloc_clone_csums(inode, cur_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1743) 						      num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1745) 		extent_clear_unlock_delalloc(inode, cur_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1746) 					     cur_offset + num_bytes - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1747) 					     locked_page, EXTENT_LOCKED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1748) 					     EXTENT_DELALLOC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1749) 					     EXTENT_CLEAR_DATA_RESV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1750) 					     PAGE_UNLOCK | PAGE_SET_PRIVATE2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1752) 		cur_offset = extent_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1754) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1755) 		 * btrfs_reloc_clone_csums() error, now we're OK to call error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1756) 		 * handler, as metadata for created ordered extent will only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1757) 		 * be freed by btrfs_finish_ordered_io().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1758) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1759) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1760) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1761) 		if (cur_offset > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1762) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1763) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1764) 	btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1766) 	if (cur_offset <= end && cow_start == (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1767) 		cow_start = cur_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1769) 	if (cow_start != (u64)-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1770) 		cur_offset = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1771) 		ret = fallback_to_cow(inode, locked_page, cow_start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1772) 				      page_started, nr_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1773) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1774) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1775) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1777) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1778) 	if (nocow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1779) 		btrfs_dec_nocow_writers(fs_info, disk_bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1781) 	if (ret && cur_offset < end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1782) 		extent_clear_unlock_delalloc(inode, cur_offset, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1783) 					     locked_page, EXTENT_LOCKED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1784) 					     EXTENT_DELALLOC | EXTENT_DEFRAG |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1785) 					     EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1786) 					     PAGE_CLEAR_DIRTY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1787) 					     PAGE_SET_WRITEBACK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1788) 					     PAGE_END_WRITEBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1789) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1790) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1793) static inline int need_force_cow(struct btrfs_inode *inode, u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1794) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1796) 	if (!(inode->flags & BTRFS_INODE_NODATACOW) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1797) 	    !(inode->flags & BTRFS_INODE_PREALLOC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1798) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1800) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1801) 	 * @defrag_bytes is a hint value, no spinlock held here,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1802) 	 * if is not zero, it means the file is defragging.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1803) 	 * Force cow if given extent needs to be defragged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1804) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1805) 	if (inode->defrag_bytes &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1806) 	    test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1807) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1809) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1812) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1813)  * Function to process delayed allocation (create CoW) for ranges which are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1814)  * being touched for the first time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1815)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1816) int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1817) 		u64 start, u64 end, int *page_started, unsigned long *nr_written,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1818) 		struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1819) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1820) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1821) 	int force_cow = need_force_cow(inode, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1823) 	if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1824) 		ret = run_delalloc_nocow(inode, locked_page, start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1825) 					 page_started, 1, nr_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1826) 	} else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1827) 		ret = run_delalloc_nocow(inode, locked_page, start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1828) 					 page_started, 0, nr_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1829) 	} else if (!inode_can_compress(inode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1830) 		   !inode_need_compress(inode, start, end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1831) 		ret = cow_file_range(inode, locked_page, start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1832) 				     page_started, nr_written, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1833) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1834) 		set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1835) 		ret = cow_file_range_async(inode, wbc, locked_page, start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1836) 					   page_started, nr_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1837) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1838) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1839) 		btrfs_cleanup_ordered_extents(inode, locked_page, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1840) 					      end - start + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1841) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1844) void btrfs_split_delalloc_extent(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1845) 				 struct extent_state *orig, u64 split)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1846) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1847) 	u64 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1849) 	/* not delalloc, ignore it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1850) 	if (!(orig->state & EXTENT_DELALLOC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1851) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1853) 	size = orig->end - orig->start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1854) 	if (size > BTRFS_MAX_EXTENT_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1855) 		u32 num_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1856) 		u64 new_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1858) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1859) 		 * See the explanation in btrfs_merge_delalloc_extent, the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1860) 		 * applies here, just in reverse.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1861) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1862) 		new_size = orig->end - split + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1863) 		num_extents = count_max_extents(new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1864) 		new_size = split - orig->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1865) 		num_extents += count_max_extents(new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1866) 		if (count_max_extents(size) >= num_extents)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1867) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1868) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1870) 	spin_lock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1871) 	btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1872) 	spin_unlock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1873) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1875) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1876)  * Handle merged delayed allocation extents so we can keep track of new extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1877)  * that are just merged onto old extents, such as when we are doing sequential
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1878)  * writes, so we can properly account for the metadata space we'll need.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1879)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1880) void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1881) 				 struct extent_state *other)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1882) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1883) 	u64 new_size, old_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1884) 	u32 num_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1886) 	/* not delalloc, ignore it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1887) 	if (!(other->state & EXTENT_DELALLOC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1888) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1890) 	if (new->start > other->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1891) 		new_size = new->end - other->start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1892) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1893) 		new_size = other->end - new->start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1895) 	/* we're not bigger than the max, unreserve the space and go */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1896) 	if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1897) 		spin_lock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1898) 		btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1899) 		spin_unlock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1900) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1901) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1903) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1904) 	 * We have to add up either side to figure out how many extents were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1905) 	 * accounted for before we merged into one big extent.  If the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1906) 	 * extents we accounted for is <= the amount we need for the new range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1907) 	 * then we can return, otherwise drop.  Think of it like this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1908) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1909) 	 * [ 4k][MAX_SIZE]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1910) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1911) 	 * So we've grown the extent by a MAX_SIZE extent, this would mean we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1912) 	 * need 2 outstanding extents, on one side we have 1 and the other side
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1913) 	 * we have 1 so they are == and we can return.  But in this case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1914) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1915) 	 * [MAX_SIZE+4k][MAX_SIZE+4k]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1916) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1917) 	 * Each range on their own accounts for 2 extents, but merged together
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1918) 	 * they are only 3 extents worth of accounting, so we need to drop in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1919) 	 * this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1920) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1921) 	old_size = other->end - other->start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1922) 	num_extents = count_max_extents(old_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1923) 	old_size = new->end - new->start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1924) 	num_extents += count_max_extents(old_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1925) 	if (count_max_extents(new_size) >= num_extents)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1926) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1928) 	spin_lock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1929) 	btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1930) 	spin_unlock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1931) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1933) static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1934) 				      struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1935) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1936) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1938) 	spin_lock(&root->delalloc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1939) 	if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1940) 		list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1941) 			      &root->delalloc_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1942) 		set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1943) 			&BTRFS_I(inode)->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1944) 		root->nr_delalloc_inodes++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1945) 		if (root->nr_delalloc_inodes == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1946) 			spin_lock(&fs_info->delalloc_root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1947) 			BUG_ON(!list_empty(&root->delalloc_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1948) 			list_add_tail(&root->delalloc_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1949) 				      &fs_info->delalloc_roots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1950) 			spin_unlock(&fs_info->delalloc_root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1951) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1952) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1953) 	spin_unlock(&root->delalloc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1957) void __btrfs_del_delalloc_inode(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1958) 				struct btrfs_inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1959) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1960) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1962) 	if (!list_empty(&inode->delalloc_inodes)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1963) 		list_del_init(&inode->delalloc_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1964) 		clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1965) 			  &inode->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1966) 		root->nr_delalloc_inodes--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1967) 		if (!root->nr_delalloc_inodes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1968) 			ASSERT(list_empty(&root->delalloc_inodes));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1969) 			spin_lock(&fs_info->delalloc_root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1970) 			BUG_ON(list_empty(&root->delalloc_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1971) 			list_del_init(&root->delalloc_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1972) 			spin_unlock(&fs_info->delalloc_root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1973) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1974) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1977) static void btrfs_del_delalloc_inode(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1978) 				     struct btrfs_inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1979) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1980) 	spin_lock(&root->delalloc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1981) 	__btrfs_del_delalloc_inode(root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1982) 	spin_unlock(&root->delalloc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1983) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1985) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1986)  * Properly track delayed allocation bytes in the inode and to maintain the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1987)  * list of inodes that have pending delalloc work to be done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1988)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1989) void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1990) 			       unsigned *bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1991) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1992) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1994) 	if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1995) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1996) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1997) 	 * set_bit and clear bit hooks normally require _irqsave/restore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1998) 	 * but in this case, we are only testing for the DELALLOC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1999) 	 * bit, which is only set or cleared with irqs on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2000) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2001) 	if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2002) 		struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2003) 		u64 len = state->end + 1 - state->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2004) 		u32 num_extents = count_max_extents(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2005) 		bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2007) 		spin_lock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2008) 		btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2009) 		spin_unlock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2011) 		/* For sanity tests */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2012) 		if (btrfs_is_testing(fs_info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2013) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2015) 		percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2016) 					 fs_info->delalloc_batch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2017) 		spin_lock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2018) 		BTRFS_I(inode)->delalloc_bytes += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2019) 		if (*bits & EXTENT_DEFRAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2020) 			BTRFS_I(inode)->defrag_bytes += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2021) 		if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2022) 					 &BTRFS_I(inode)->runtime_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2023) 			btrfs_add_delalloc_inodes(root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2024) 		spin_unlock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2025) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2027) 	if (!(state->state & EXTENT_DELALLOC_NEW) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2028) 	    (*bits & EXTENT_DELALLOC_NEW)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2029) 		spin_lock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2030) 		BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2031) 			state->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2032) 		spin_unlock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2033) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2034) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2036) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2037)  * Once a range is no longer delalloc this function ensures that proper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2038)  * accounting happens.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2039)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2040) void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2041) 				 struct extent_state *state, unsigned *bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2042) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2043) 	struct btrfs_inode *inode = BTRFS_I(vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2044) 	struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2045) 	u64 len = state->end + 1 - state->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2046) 	u32 num_extents = count_max_extents(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2048) 	if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2049) 		spin_lock(&inode->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2050) 		inode->defrag_bytes -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2051) 		spin_unlock(&inode->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2052) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2054) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2055) 	 * set_bit and clear bit hooks normally require _irqsave/restore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2056) 	 * but in this case, we are only testing for the DELALLOC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2057) 	 * bit, which is only set or cleared with irqs on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2058) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2059) 	if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2060) 		struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2061) 		bool do_list = !btrfs_is_free_space_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2063) 		spin_lock(&inode->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2064) 		btrfs_mod_outstanding_extents(inode, -num_extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2065) 		spin_unlock(&inode->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2067) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2068) 		 * We don't reserve metadata space for space cache inodes so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2069) 		 * don't need to call delalloc_release_metadata if there is an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2070) 		 * error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2071) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2072) 		if (*bits & EXTENT_CLEAR_META_RESV &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2073) 		    root != fs_info->tree_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2074) 			btrfs_delalloc_release_metadata(inode, len, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2076) 		/* For sanity tests. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2077) 		if (btrfs_is_testing(fs_info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2078) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2080) 		if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2081) 		    do_list && !(state->state & EXTENT_NORESERVE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2082) 		    (*bits & EXTENT_CLEAR_DATA_RESV))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2083) 			btrfs_free_reserved_data_space_noquota(fs_info, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2085) 		percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2086) 					 fs_info->delalloc_batch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2087) 		spin_lock(&inode->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2088) 		inode->delalloc_bytes -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2089) 		if (do_list && inode->delalloc_bytes == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2090) 		    test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2091) 					&inode->runtime_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2092) 			btrfs_del_delalloc_inode(root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2093) 		spin_unlock(&inode->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2094) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2096) 	if ((state->state & EXTENT_DELALLOC_NEW) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2097) 	    (*bits & EXTENT_DELALLOC_NEW)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2098) 		spin_lock(&inode->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2099) 		ASSERT(inode->new_delalloc_bytes >= len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2100) 		inode->new_delalloc_bytes -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2101) 		spin_unlock(&inode->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2105) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2106)  * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2107)  * in a chunk's stripe. This function ensures that bios do not span a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2108)  * stripe/chunk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2109)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2110)  * @page - The page we are about to add to the bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2111)  * @size - size we want to add to the bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2112)  * @bio - bio we want to ensure is smaller than a stripe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2113)  * @bio_flags - flags of the bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2114)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2115)  * return 1 if page cannot be added to the bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2116)  * return 0 if page can be added to the bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2117)  * return error otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2118)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2119) int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2120) 			     unsigned long bio_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2122) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2123) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2124) 	u64 logical = (u64)bio->bi_iter.bi_sector << 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2125) 	u64 length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2126) 	u64 map_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2127) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2128) 	struct btrfs_io_geometry geom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2130) 	if (bio_flags & EXTENT_BIO_COMPRESSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2131) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2133) 	length = bio->bi_iter.bi_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2134) 	map_length = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2135) 	ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2136) 				    &geom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2137) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2138) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2140) 	if (geom.len < length + size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2141) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2142) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2145) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2146)  * in order to insert checksums into the metadata in large chunks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2147)  * we wait until bio submission time.   All the pages in the bio are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2148)  * checksummed and sums are attached onto the ordered extent record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2149)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2150)  * At IO completion time the cums attached on the ordered extent record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2151)  * are inserted into the btree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2152)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2153) static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2154) 				    u64 bio_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2156) 	struct inode *inode = private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2158) 	return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2161) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2162)  * extent_io.c submission hook. This does the right thing for csum calculation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2163)  * on write, or reading the csums from the tree before a read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2164)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2165)  * Rules about async/sync submit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2166)  * a) read:				sync submit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2167)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2168)  * b) write without checksum:		sync submit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2169)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2170)  * c) write with checksum:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2171)  *    c-1) if bio is issued by fsync:	sync submit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2172)  *         (sync_writers != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2173)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2174)  *    c-2) if root is reloc root:	sync submit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2175)  *         (only in case of buffered IO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2176)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2177)  *    c-3) otherwise:			async submit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2178)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2179) blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2180) 				   int mirror_num, unsigned long bio_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2183) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2184) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2185) 	enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2186) 	blk_status_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2187) 	int skip_sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2188) 	int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2190) 	skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2192) 	if (btrfs_is_free_space_inode(BTRFS_I(inode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2193) 		metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2195) 	if (bio_op(bio) != REQ_OP_WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2196) 		ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2197) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2198) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2200) 		if (bio_flags & EXTENT_BIO_COMPRESSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2201) 			ret = btrfs_submit_compressed_read(inode, bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2202) 							   mirror_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2203) 							   bio_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2204) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2205) 		} else if (!skip_sum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2206) 			ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2207) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2208) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2209) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2210) 		goto mapit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2211) 	} else if (async && !skip_sum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2212) 		/* csum items have already been cloned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2213) 		if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2214) 			goto mapit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2215) 		/* we're doing a write, do the async checksumming */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2216) 		ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2217) 					  0, inode, btrfs_submit_bio_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2218) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2219) 	} else if (!skip_sum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2220) 		ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2221) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2222) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2225) mapit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2226) 	ret = btrfs_map_bio(fs_info, bio, mirror_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2228) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2229) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2230) 		bio->bi_status = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2231) 		bio_endio(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2233) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2236) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2237)  * given a list of ordered sums record them in the inode.  This happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2238)  * at IO completion time based on sums calculated at bio submission time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2239)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2240) static int add_pending_csums(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2241) 			     struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2243) 	struct btrfs_ordered_sum *sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2244) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2246) 	list_for_each_entry(sum, list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2247) 		trans->adding_csums = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2248) 		ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2249) 		trans->adding_csums = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2250) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2251) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2252) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2253) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2256) static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2257) 					 const u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2258) 					 const u64 len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2259) 					 struct extent_state **cached_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2261) 	u64 search_start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2262) 	const u64 end = start + len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2264) 	while (search_start < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2265) 		const u64 search_len = end - search_start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2266) 		struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2267) 		u64 em_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2268) 		int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2270) 		em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2271) 		if (IS_ERR(em))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2272) 			return PTR_ERR(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2274) 		if (em->block_start != EXTENT_MAP_HOLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2275) 			goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2277) 		em_len = em->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2278) 		if (em->start < search_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2279) 			em_len -= search_start - em->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2280) 		if (em_len > search_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2281) 			em_len = search_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2283) 		ret = set_extent_bit(&inode->io_tree, search_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2284) 				     search_start + em_len - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2285) 				     EXTENT_DELALLOC_NEW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2286) 				     NULL, cached_state, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2287) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2288) 		search_start = extent_map_end(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2289) 		free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2290) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2291) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2292) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2293) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2296) int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2297) 			      unsigned int extra_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2298) 			      struct extent_state **cached_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2300) 	WARN_ON(PAGE_ALIGNED(end));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2302) 	if (start >= i_size_read(&inode->vfs_inode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2303) 	    !(inode->flags & BTRFS_INODE_PREALLOC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2304) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2305) 		 * There can't be any extents following eof in this case so just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2306) 		 * set the delalloc new bit for the range directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2307) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2308) 		extra_bits |= EXTENT_DELALLOC_NEW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2309) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2310) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2312) 		ret = btrfs_find_new_delalloc_bytes(inode, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2313) 						    end + 1 - start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2314) 						    cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2315) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2316) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2319) 	return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2320) 				   cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2323) /* see btrfs_writepage_start_hook for details on why this is required */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2324) struct btrfs_writepage_fixup {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2325) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2326) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2327) 	struct btrfs_work work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2328) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2330) static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2332) 	struct btrfs_writepage_fixup *fixup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2333) 	struct btrfs_ordered_extent *ordered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2334) 	struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2335) 	struct extent_changeset *data_reserved = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2336) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2337) 	struct btrfs_inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2338) 	u64 page_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2339) 	u64 page_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2340) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2341) 	bool free_delalloc_space = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2343) 	fixup = container_of(work, struct btrfs_writepage_fixup, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2344) 	page = fixup->page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2345) 	inode = BTRFS_I(fixup->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2346) 	page_start = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2347) 	page_end = page_offset(page) + PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2349) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2350) 	 * This is similar to page_mkwrite, we need to reserve the space before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2351) 	 * we take the page lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2352) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2353) 	ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2354) 					   PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2355) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2356) 	lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2358) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2359) 	 * Before we queued this fixup, we took a reference on the page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2360) 	 * page->mapping may go NULL, but it shouldn't be moved to a different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2361) 	 * address space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2362) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2363) 	if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2364) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2365) 		 * Unfortunately this is a little tricky, either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2366) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2367) 		 * 1) We got here and our page had already been dealt with and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2368) 		 *    we reserved our space, thus ret == 0, so we need to just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2369) 		 *    drop our space reservation and bail.  This can happen the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2370) 		 *    first time we come into the fixup worker, or could happen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2371) 		 *    while waiting for the ordered extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2372) 		 * 2) Our page was already dealt with, but we happened to get an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2373) 		 *    ENOSPC above from the btrfs_delalloc_reserve_space.  In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2374) 		 *    this case we obviously don't have anything to release, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2375) 		 *    because the page was already dealt with we don't want to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2376) 		 *    mark the page with an error, so make sure we're resetting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2377) 		 *    ret to 0.  This is why we have this check _before_ the ret
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2378) 		 *    check, because we do not want to have a surprise ENOSPC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2379) 		 *    when the page was already properly dealt with.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2380) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2381) 		if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2382) 			btrfs_delalloc_release_extents(inode, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2383) 			btrfs_delalloc_release_space(inode, data_reserved,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2384) 						     page_start, PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2385) 						     true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2386) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2387) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2388) 		goto out_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2389) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2391) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2392) 	 * We can't mess with the page state unless it is locked, so now that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2393) 	 * it is locked bail if we failed to make our space reservation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2394) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2395) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2396) 		goto out_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2398) 	lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2400) 	/* already ordered? We're done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2401) 	if (PagePrivate2(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2402) 		goto out_reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2404) 	ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2405) 	if (ordered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2406) 		unlock_extent_cached(&inode->io_tree, page_start, page_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2407) 				     &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2408) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2409) 		btrfs_start_ordered_extent(ordered, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2410) 		btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2411) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2412) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2414) 	ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2415) 					&cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2416) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2417) 		goto out_reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2419) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2420) 	 * Everything went as planned, we're now the owner of a dirty page with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2421) 	 * delayed allocation bits set and space reserved for our COW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2422) 	 * destination.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2423) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2424) 	 * The page was dirty when we started, nothing should have cleaned it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2425) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2426) 	BUG_ON(!PageDirty(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2427) 	free_delalloc_space = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2428) out_reserved:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2429) 	btrfs_delalloc_release_extents(inode, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2430) 	if (free_delalloc_space)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2431) 		btrfs_delalloc_release_space(inode, data_reserved, page_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2432) 					     PAGE_SIZE, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2433) 	unlock_extent_cached(&inode->io_tree, page_start, page_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2434) 			     &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2435) out_page:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2436) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2437) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2438) 		 * We hit ENOSPC or other errors.  Update the mapping and page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2439) 		 * to reflect the errors and clean the page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2440) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2441) 		mapping_set_error(page->mapping, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2442) 		end_extent_writepage(page, ret, page_start, page_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2443) 		clear_page_dirty_for_io(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2444) 		SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2445) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2446) 	ClearPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2447) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2448) 	put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2449) 	kfree(fixup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2450) 	extent_changeset_free(data_reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2451) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2452) 	 * As a precaution, do a delayed iput in case it would be the last iput
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2453) 	 * that could need flushing space. Recursing back to fixup worker would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2454) 	 * deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2455) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2456) 	btrfs_add_delayed_iput(&inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2459) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2460)  * There are a few paths in the higher layers of the kernel that directly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2461)  * set the page dirty bit without asking the filesystem if it is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2462)  * good idea.  This causes problems because we want to make sure COW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2463)  * properly happens and the data=ordered rules are followed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2464)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2465)  * In our case any range that doesn't have the ORDERED bit set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2466)  * hasn't been properly setup for IO.  We kick off an async process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2467)  * to fix it up.  The async helper will wait for ordered extents, set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2468)  * the delalloc bit and make it safe to write the page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2469)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2470) int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2472) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2473) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2474) 	struct btrfs_writepage_fixup *fixup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2476) 	/* this page is properly in the ordered list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2477) 	if (TestClearPagePrivate2(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2478) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2480) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2481) 	 * PageChecked is set below when we create a fixup worker for this page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2482) 	 * don't try to create another one if we're already PageChecked()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2483) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2484) 	 * The extent_io writepage code will redirty the page if we send back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2485) 	 * EAGAIN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2486) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2487) 	if (PageChecked(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2488) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2490) 	fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2491) 	if (!fixup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2492) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2494) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2495) 	 * We are already holding a reference to this inode from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2496) 	 * write_cache_pages.  We need to hold it because the space reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2497) 	 * takes place outside of the page lock, and we can't trust
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2498) 	 * page->mapping outside of the page lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2499) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2500) 	ihold(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2501) 	SetPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2502) 	get_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2503) 	btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2504) 	fixup->page = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2505) 	fixup->inode = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2506) 	btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2508) 	return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2511) static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2512) 				       struct btrfs_inode *inode, u64 file_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2513) 				       struct btrfs_file_extent_item *stack_fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2514) 				       u64 qgroup_reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2516) 	struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2517) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2518) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2519) 	struct btrfs_key ins;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2520) 	u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2521) 	u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2522) 	u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2523) 	u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2524) 	int extent_inserted = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2525) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2527) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2528) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2529) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2531) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2532) 	 * we may be replacing one extent in the tree with another.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2533) 	 * The new extent is pinned in the extent map, and we don't want
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2534) 	 * to drop it from the cache until it is completely in the btree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2535) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2536) 	 * So, tell btrfs_drop_extents to leave this extent in the cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2537) 	 * the caller is expected to unpin it and allow it to be merged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2538) 	 * with the others.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2539) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2540) 	ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2541) 				   file_pos + num_bytes, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2542) 				   1, sizeof(*stack_fi), &extent_inserted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2543) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2544) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2546) 	if (!extent_inserted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2547) 		ins.objectid = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2548) 		ins.offset = file_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2549) 		ins.type = BTRFS_EXTENT_DATA_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2551) 		path->leave_spinning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2552) 		ret = btrfs_insert_empty_item(trans, root, path, &ins,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2553) 					      sizeof(*stack_fi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2554) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2555) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2556) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2557) 	leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2558) 	btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2559) 	write_extent_buffer(leaf, stack_fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2560) 			btrfs_item_ptr_offset(leaf, path->slots[0]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2561) 			sizeof(struct btrfs_file_extent_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2563) 	btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2564) 	btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2566) 	inode_add_bytes(&inode->vfs_inode, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2568) 	ins.objectid = disk_bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2569) 	ins.offset = disk_num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2570) 	ins.type = BTRFS_EXTENT_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2572) 	ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2573) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2574) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2576) 	ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2577) 					       file_pos, qgroup_reserved, &ins);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2578) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2579) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2581) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2584) static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2585) 					 u64 start, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2587) 	struct btrfs_block_group *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2589) 	cache = btrfs_lookup_block_group(fs_info, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2590) 	ASSERT(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2592) 	spin_lock(&cache->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2593) 	cache->delalloc_bytes -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2594) 	spin_unlock(&cache->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2596) 	btrfs_put_block_group(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2599) static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2600) 					     struct btrfs_ordered_extent *oe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2602) 	struct btrfs_file_extent_item stack_fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2603) 	u64 logical_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2605) 	memset(&stack_fi, 0, sizeof(stack_fi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2606) 	btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2607) 	btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2608) 	btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2609) 						   oe->disk_num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2610) 	if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2611) 		logical_len = oe->truncated_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2612) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2613) 		logical_len = oe->num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2614) 	btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2615) 	btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2616) 	btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2617) 	/* Encryption and other encoding is reserved and all 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2619) 	return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2620) 					   oe->file_offset, &stack_fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2621) 					   oe->qgroup_rsv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2624) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2625)  * As ordered data IO finishes, this gets called so we can finish
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2626)  * an ordered extent if the range of bytes in the file it covers are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2627)  * fully written.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2628)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2629) static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2630) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2631) 	struct inode *inode = ordered_extent->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2632) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2633) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2634) 	struct btrfs_trans_handle *trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2635) 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2636) 	struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2637) 	u64 start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2638) 	int compress_type = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2639) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2640) 	u64 logical_len = ordered_extent->num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2641) 	bool freespace_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2642) 	bool truncated = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2643) 	bool range_locked = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2644) 	bool clear_new_delalloc_bytes = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2645) 	bool clear_reserved_extent = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2646) 	unsigned int clear_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2648) 	start = ordered_extent->file_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2649) 	end = start + ordered_extent->num_bytes - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2651) 	if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2652) 	    !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2653) 	    !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2654) 		clear_new_delalloc_bytes = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2656) 	freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2658) 	if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2659) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2660) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2661) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2663) 	btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2665) 	if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2666) 		truncated = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2667) 		logical_len = ordered_extent->truncated_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2668) 		/* Truncated the entire extent, don't bother adding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2669) 		if (!logical_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2670) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2671) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2673) 	if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2674) 		BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2676) 		btrfs_inode_safe_disk_i_size_write(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2677) 		if (freespace_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2678) 			trans = btrfs_join_transaction_spacecache(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2679) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2680) 			trans = btrfs_join_transaction(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2681) 		if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2682) 			ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2683) 			trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2684) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2685) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2686) 		trans->block_rsv = &BTRFS_I(inode)->block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2687) 		ret = btrfs_update_inode_fallback(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2688) 		if (ret) /* -ENOMEM or corruption */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2689) 			btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2690) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2691) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2693) 	range_locked = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2694) 	lock_extent_bits(io_tree, start, end, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2696) 	if (freespace_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2697) 		trans = btrfs_join_transaction_spacecache(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2698) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2699) 		trans = btrfs_join_transaction(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2700) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2701) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2702) 		trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2703) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2704) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2706) 	trans->block_rsv = &BTRFS_I(inode)->block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2708) 	if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2709) 		compress_type = ordered_extent->compress_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2710) 	if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2711) 		BUG_ON(compress_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2712) 		ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2713) 						ordered_extent->file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2714) 						ordered_extent->file_offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2715) 						logical_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2716) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2717) 		BUG_ON(root == fs_info->tree_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2718) 		ret = insert_ordered_extent_file_extent(trans, ordered_extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2719) 		if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2720) 			clear_reserved_extent = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2721) 			btrfs_release_delalloc_bytes(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2722) 						ordered_extent->disk_bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2723) 						ordered_extent->disk_num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2724) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2725) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2726) 	unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2727) 			   ordered_extent->file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2728) 			   ordered_extent->num_bytes, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2729) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2730) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2731) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2732) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2734) 	ret = add_pending_csums(trans, &ordered_extent->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2735) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2736) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2737) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2738) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2740) 	btrfs_inode_safe_disk_i_size_write(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2741) 	ret = btrfs_update_inode_fallback(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2742) 	if (ret) { /* -ENOMEM or corruption */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2743) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2744) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2745) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2746) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2747) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2748) 	clear_bits = EXTENT_DEFRAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2749) 	if (range_locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2750) 		clear_bits |= EXTENT_LOCKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2751) 	if (clear_new_delalloc_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2752) 		clear_bits |= EXTENT_DELALLOC_NEW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2753) 	clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2754) 			 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2755) 			 &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2757) 	if (trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2758) 		btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2760) 	if (ret || truncated) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2761) 		u64 unwritten_start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2763) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2764) 		 * If we failed to finish this ordered extent for any reason we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2765) 		 * need to make sure BTRFS_ORDERED_IOERR is set on the ordered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2766) 		 * extent, and mark the inode with the error if it wasn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2767) 		 * already set.  Any error during writeback would have already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2768) 		 * set the mapping error, so we need to set it if we're the ones
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2769) 		 * marking this ordered extent as failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2770) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2771) 		if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2772) 					     &ordered_extent->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2773) 			mapping_set_error(ordered_extent->inode->i_mapping, -EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2775) 		if (truncated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2776) 			unwritten_start += logical_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2777) 		clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2779) 		/* Drop the cache for the part of the extent we didn't write. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2780) 		btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2782) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2783) 		 * If the ordered extent had an IOERR or something else went
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2784) 		 * wrong we need to return the space for this ordered extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2785) 		 * back to the allocator.  We only free the extent in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2786) 		 * truncated case if we didn't write out the extent at all.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2787) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2788) 		 * If we made it past insert_reserved_file_extent before we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2789) 		 * errored out then we don't need to do this as the accounting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2790) 		 * has already been done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2791) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2792) 		if ((ret || !logical_len) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2793) 		    clear_reserved_extent &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2794) 		    !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2795) 		    !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2796) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2797) 			 * Discard the range before returning it back to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2798) 			 * free space pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2799) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2800) 			if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2801) 				btrfs_discard_extent(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2802) 						ordered_extent->disk_bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2803) 						ordered_extent->disk_num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2804) 						NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2805) 			btrfs_free_reserved_extent(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2806) 					ordered_extent->disk_bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2807) 					ordered_extent->disk_num_bytes, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2808) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2809) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2811) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2812) 	 * This needs to be done to make sure anybody waiting knows we are done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2813) 	 * updating everything for this ordered extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2814) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2815) 	btrfs_remove_ordered_extent(BTRFS_I(inode), ordered_extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2817) 	/* once for us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2818) 	btrfs_put_ordered_extent(ordered_extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2819) 	/* once for the tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2820) 	btrfs_put_ordered_extent(ordered_extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2822) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2825) static void finish_ordered_fn(struct btrfs_work *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2827) 	struct btrfs_ordered_extent *ordered_extent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2828) 	ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2829) 	btrfs_finish_ordered_io(ordered_extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2832) void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2833) 					  u64 end, int uptodate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2834) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2835) 	struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2836) 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2837) 	struct btrfs_ordered_extent *ordered_extent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2838) 	struct btrfs_workqueue *wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2840) 	trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2842) 	ClearPagePrivate2(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2843) 	if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2844) 					    end - start + 1, uptodate))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2845) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2847) 	if (btrfs_is_free_space_inode(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2848) 		wq = fs_info->endio_freespace_worker;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2849) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2850) 		wq = fs_info->endio_write_workers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2852) 	btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2853) 	btrfs_queue_work(wq, &ordered_extent->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2856) static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2857) 			   int icsum, struct page *page, int pgoff, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2858) 			   size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2859) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2860) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2861) 	SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2862) 	char *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2863) 	u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2864) 	u8 *csum_expected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2865) 	u8 csum[BTRFS_CSUM_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2867) 	csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2869) 	kaddr = kmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2870) 	shash->tfm = fs_info->csum_shash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2872) 	crypto_shash_digest(shash, kaddr + pgoff, len, csum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2874) 	if (memcmp(csum, csum_expected, csum_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2875) 		goto zeroit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2877) 	kunmap_atomic(kaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2878) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2879) zeroit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2880) 	btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2881) 				    io_bio->mirror_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2882) 	if (io_bio->device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2883) 		btrfs_dev_stat_inc_and_print(io_bio->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2884) 					     BTRFS_DEV_STAT_CORRUPTION_ERRS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2885) 	memset(kaddr + pgoff, 1, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2886) 	flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2887) 	kunmap_atomic(kaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2888) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2891) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2892)  * when reads are done, we need to check csums to verify the data is correct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2893)  * if there's a match, we allow the bio to finish.  If not, the code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2894)  * extent_io.c will try to find good copies for us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2895)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2896) int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u64 phy_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2897) 			   struct page *page, u64 start, u64 end, int mirror)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2899) 	size_t offset = start - page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2900) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2901) 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2902) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2904) 	if (PageChecked(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2905) 		ClearPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2906) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2907) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2909) 	if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2910) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2912) 	if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2913) 	    test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2914) 		clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2915) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2916) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2918) 	phy_offset >>= inode->i_sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2919) 	return check_data_csum(inode, io_bio, phy_offset, page, offset, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2920) 			       (size_t)(end - start + 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2923) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2924)  * btrfs_add_delayed_iput - perform a delayed iput on @inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2925)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2926)  * @inode: The inode we want to perform iput on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2927)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2928)  * This function uses the generic vfs_inode::i_count to track whether we should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2929)  * just decrement it (in case it's > 1) or if this is the last iput then link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2930)  * the inode to the delayed iput machinery. Delayed iputs are processed at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2931)  * transaction commit time/superblock commit/cleaner kthread.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2932)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2933) void btrfs_add_delayed_iput(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2934) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2935) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2936) 	struct btrfs_inode *binode = BTRFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2938) 	if (atomic_add_unless(&inode->i_count, -1, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2939) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2941) 	atomic_inc(&fs_info->nr_delayed_iputs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2942) 	spin_lock(&fs_info->delayed_iput_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2943) 	ASSERT(list_empty(&binode->delayed_iput));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2944) 	list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2945) 	spin_unlock(&fs_info->delayed_iput_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2946) 	if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2947) 		wake_up_process(fs_info->cleaner_kthread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2950) static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2951) 				    struct btrfs_inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2952) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2953) 	list_del_init(&inode->delayed_iput);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2954) 	spin_unlock(&fs_info->delayed_iput_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2955) 	iput(&inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2956) 	if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2957) 		wake_up(&fs_info->delayed_iputs_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2958) 	spin_lock(&fs_info->delayed_iput_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2961) static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2962) 				   struct btrfs_inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2963) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2964) 	if (!list_empty(&inode->delayed_iput)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2965) 		spin_lock(&fs_info->delayed_iput_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2966) 		if (!list_empty(&inode->delayed_iput))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2967) 			run_delayed_iput_locked(fs_info, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2968) 		spin_unlock(&fs_info->delayed_iput_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2969) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2972) void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2973) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2975) 	spin_lock(&fs_info->delayed_iput_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2976) 	while (!list_empty(&fs_info->delayed_iputs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2977) 		struct btrfs_inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2979) 		inode = list_first_entry(&fs_info->delayed_iputs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2980) 				struct btrfs_inode, delayed_iput);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2981) 		run_delayed_iput_locked(fs_info, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2982) 		cond_resched_lock(&fs_info->delayed_iput_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2983) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2984) 	spin_unlock(&fs_info->delayed_iput_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2987) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2988)  * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2989)  * @fs_info - the fs_info for this fs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2990)  * @return - EINTR if we were killed, 0 if nothing's pending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2991)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2992)  * This will wait on any delayed iputs that are currently running with KILLABLE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2993)  * set.  Once they are all done running we will return, unless we are killed in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2994)  * which case we return EINTR. This helps in user operations like fallocate etc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2995)  * that might get blocked on the iputs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2996)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2997) int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2998) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2999) 	int ret = wait_event_killable(fs_info->delayed_iputs_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3000) 			atomic_read(&fs_info->nr_delayed_iputs) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3001) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3002) 		return -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3003) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3004) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3006) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3007)  * This creates an orphan entry for the given inode in case something goes wrong
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3008)  * in the middle of an unlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3009)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3010) int btrfs_orphan_add(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3011) 		     struct btrfs_inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3012) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3013) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3015) 	ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3016) 	if (ret && ret != -EEXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3017) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3018) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3019) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3021) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3022) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3024) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3025)  * We have done the delete so we can go ahead and remove the orphan item for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3026)  * this particular inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3027)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3028) static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3029) 			    struct btrfs_inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3030) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3031) 	return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3032) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3034) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3035)  * this cleans up any orphans that may be left on the list from the last use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3036)  * of this root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3037)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3038) int btrfs_orphan_cleanup(struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3039) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3040) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3041) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3042) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3043) 	struct btrfs_key key, found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3044) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3045) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3046) 	u64 last_objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3047) 	int ret = 0, nr_unlink = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3049) 	if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3050) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3052) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3053) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3054) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3055) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3056) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3057) 	path->reada = READA_BACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3059) 	key.objectid = BTRFS_ORPHAN_OBJECTID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3060) 	key.type = BTRFS_ORPHAN_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3061) 	key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3063) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3064) 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3065) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3066) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3068) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3069) 		 * if ret == 0 means we found what we were searching for, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3070) 		 * is weird, but possible, so only screw with path if we didn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3071) 		 * find the key and see if we have stuff that matches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3072) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3073) 		if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3074) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3075) 			if (path->slots[0] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3076) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3077) 			path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3078) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3080) 		/* pull out the item */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3081) 		leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3082) 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3084) 		/* make sure the item matches what we want */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3085) 		if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3086) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3087) 		if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3088) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3090) 		/* release the path since we're done with it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3091) 		btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3093) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3094) 		 * this is where we are basically btrfs_lookup, without the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3095) 		 * crossing root thing.  we store the inode number in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3096) 		 * offset of the orphan item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3097) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3099) 		if (found_key.offset == last_objectid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3100) 			btrfs_err(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3101) 				  "Error removing orphan entry, stopping orphan cleanup");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3102) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3103) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3104) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3106) 		last_objectid = found_key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3108) 		found_key.objectid = found_key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3109) 		found_key.type = BTRFS_INODE_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3110) 		found_key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3111) 		inode = btrfs_iget(fs_info->sb, last_objectid, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3112) 		ret = PTR_ERR_OR_ZERO(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3113) 		if (ret && ret != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3114) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3116) 		if (ret == -ENOENT && root == fs_info->tree_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3117) 			struct btrfs_root *dead_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3118) 			int is_dead_root = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3120) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3121) 			 * this is an orphan in the tree root. Currently these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3122) 			 * could come from 2 sources:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3123) 			 *  a) a snapshot deletion in progress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3124) 			 *  b) a free space cache inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3125) 			 * We need to distinguish those two, as the snapshot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3126) 			 * orphan must not get deleted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3127) 			 * find_dead_roots already ran before us, so if this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3128) 			 * is a snapshot deletion, we should find the root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3129) 			 * in the fs_roots radix tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3130) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3132) 			spin_lock(&fs_info->fs_roots_radix_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3133) 			dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3134) 							 (unsigned long)found_key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3135) 			if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3136) 				is_dead_root = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3137) 			spin_unlock(&fs_info->fs_roots_radix_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3139) 			if (is_dead_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3140) 				/* prevent this orphan from being found again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3141) 				key.offset = found_key.objectid - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3142) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3143) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3145) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3147) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3148) 		 * If we have an inode with links, there are a couple of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3149) 		 * possibilities. Old kernels (before v3.12) used to create an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3150) 		 * orphan item for truncate indicating that there were possibly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3151) 		 * extent items past i_size that needed to be deleted. In v3.12,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3152) 		 * truncate was changed to update i_size in sync with the extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3153) 		 * items, but the (useless) orphan item was still created. Since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3154) 		 * v4.18, we don't create the orphan item for truncate at all.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3155) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3156) 		 * So, this item could mean that we need to do a truncate, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3157) 		 * only if this filesystem was last used on a pre-v3.12 kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3158) 		 * and was not cleanly unmounted. The odds of that are quite
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3159) 		 * slim, and it's a pain to do the truncate now, so just delete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3160) 		 * the orphan item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3161) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3162) 		 * It's also possible that this orphan item was supposed to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3163) 		 * deleted but wasn't. The inode number may have been reused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3164) 		 * but either way, we can delete the orphan item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3165) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3166) 		if (ret == -ENOENT || inode->i_nlink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3167) 			if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3168) 				iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3169) 			trans = btrfs_start_transaction(root, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3170) 			if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3171) 				ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3172) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3173) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3174) 			btrfs_debug(fs_info, "auto deleting %Lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3175) 				    found_key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3176) 			ret = btrfs_del_orphan_item(trans, root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3177) 						    found_key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3178) 			btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3179) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3180) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3181) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3182) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3184) 		nr_unlink++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3186) 		/* this will do delete_inode and everything for us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3187) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3189) 	/* release the path since we're done with it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3190) 	btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3192) 	root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3194) 	if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3195) 		trans = btrfs_join_transaction(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3196) 		if (!IS_ERR(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3197) 			btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3200) 	if (nr_unlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3201) 		btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3203) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3204) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3205) 		btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3206) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3207) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3210) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3211)  * very simple check to peek ahead in the leaf looking for xattrs.  If we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3212)  * don't find any xattrs, we know there can't be any acls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3213)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3214)  * slot is the slot the inode is in, objectid is the objectid of the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3215)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3216) static noinline int acls_after_inode_item(struct extent_buffer *leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3217) 					  int slot, u64 objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3218) 					  int *first_xattr_slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3220) 	u32 nritems = btrfs_header_nritems(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3221) 	struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3222) 	static u64 xattr_access = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3223) 	static u64 xattr_default = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3224) 	int scanned = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3226) 	if (!xattr_access) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3227) 		xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3228) 					strlen(XATTR_NAME_POSIX_ACL_ACCESS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3229) 		xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3230) 					strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3233) 	slot++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3234) 	*first_xattr_slot = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3235) 	while (slot < nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3236) 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3238) 		/* we found a different objectid, there must not be acls */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3239) 		if (found_key.objectid != objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3240) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3242) 		/* we found an xattr, assume we've got an acl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3243) 		if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3244) 			if (*first_xattr_slot == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3245) 				*first_xattr_slot = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3246) 			if (found_key.offset == xattr_access ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3247) 			    found_key.offset == xattr_default)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3248) 				return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3249) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3251) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3252) 		 * we found a key greater than an xattr key, there can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3253) 		 * be any acls later on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3254) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3255) 		if (found_key.type > BTRFS_XATTR_ITEM_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3256) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3258) 		slot++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3259) 		scanned++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3261) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3262) 		 * it goes inode, inode backrefs, xattrs, extents,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3263) 		 * so if there are a ton of hard links to an inode there can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3264) 		 * be a lot of backrefs.  Don't waste time searching too hard,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3265) 		 * this is just an optimization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3266) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3267) 		if (scanned >= 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3268) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3269) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3270) 	/* we hit the end of the leaf before we found an xattr or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3271) 	 * something larger than an xattr.  We have to assume the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3272) 	 * has acls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3273) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3274) 	if (*first_xattr_slot == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3275) 		*first_xattr_slot = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3276) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3279) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3280)  * read an inode from the btree into the in-memory inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3281)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3282) static int btrfs_read_locked_inode(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3283) 				   struct btrfs_path *in_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3285) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3286) 	struct btrfs_path *path = in_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3287) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3288) 	struct btrfs_inode_item *inode_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3289) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3290) 	struct btrfs_key location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3291) 	unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3292) 	int maybe_acls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3293) 	u32 rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3294) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3295) 	bool filled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3296) 	int first_xattr_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3298) 	ret = btrfs_fill_inode(inode, &rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3299) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3300) 		filled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3302) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3303) 		path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3304) 		if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3305) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3306) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3308) 	memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3310) 	ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3311) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3312) 		if (path != in_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3313) 			btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3314) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3317) 	leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3319) 	if (filled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3320) 		goto cache_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3322) 	inode_item = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3323) 				    struct btrfs_inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3324) 	inode->i_mode = btrfs_inode_mode(leaf, inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3325) 	set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3326) 	i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3327) 	i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3328) 	btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3329) 	btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3330) 			round_up(i_size_read(inode), fs_info->sectorsize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3332) 	inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3333) 	inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3335) 	inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3336) 	inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3338) 	inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3339) 	inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3341) 	BTRFS_I(inode)->i_otime.tv_sec =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3342) 		btrfs_timespec_sec(leaf, &inode_item->otime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3343) 	BTRFS_I(inode)->i_otime.tv_nsec =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3344) 		btrfs_timespec_nsec(leaf, &inode_item->otime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3346) 	inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3347) 	BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3348) 	BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3350) 	inode_set_iversion_queried(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3351) 				   btrfs_inode_sequence(leaf, inode_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3352) 	inode->i_generation = BTRFS_I(inode)->generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3353) 	inode->i_rdev = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3354) 	rdev = btrfs_inode_rdev(leaf, inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3356) 	BTRFS_I(inode)->index_cnt = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3357) 	BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3359) cache_index:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3360) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3361) 	 * If we were modified in the current generation and evicted from memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3362) 	 * and then re-read we need to do a full sync since we don't have any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3363) 	 * idea about which extents were modified before we were evicted from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3364) 	 * cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3365) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3366) 	 * This is required for both inode re-read from disk and delayed inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3367) 	 * in delayed_nodes_tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3368) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3369) 	if (BTRFS_I(inode)->last_trans == fs_info->generation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3370) 		set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3371) 			&BTRFS_I(inode)->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3373) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3374) 	 * We don't persist the id of the transaction where an unlink operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3375) 	 * against the inode was last made. So here we assume the inode might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3376) 	 * have been evicted, and therefore the exact value of last_unlink_trans
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3377) 	 * lost, and set it to last_trans to avoid metadata inconsistencies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3378) 	 * between the inode and its parent if the inode is fsync'ed and the log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3379) 	 * replayed. For example, in the scenario:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3380) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3381) 	 * touch mydir/foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3382) 	 * ln mydir/foo mydir/bar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3383) 	 * sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3384) 	 * unlink mydir/bar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3385) 	 * echo 2 > /proc/sys/vm/drop_caches   # evicts inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3386) 	 * xfs_io -c fsync mydir/foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3387) 	 * <power failure>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3388) 	 * mount fs, triggers fsync log replay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3389) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3390) 	 * We must make sure that when we fsync our inode foo we also log its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3391) 	 * parent inode, otherwise after log replay the parent still has the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3392) 	 * dentry with the "bar" name but our inode foo has a link count of 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3393) 	 * and doesn't have an inode ref with the name "bar" anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3394) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3395) 	 * Setting last_unlink_trans to last_trans is a pessimistic approach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3396) 	 * but it guarantees correctness at the expense of occasional full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3397) 	 * transaction commits on fsync if our inode is a directory, or if our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3398) 	 * inode is not a directory, logging its parent unnecessarily.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3399) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3400) 	BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3402) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3403) 	 * Same logic as for last_unlink_trans. We don't persist the generation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3404) 	 * of the last transaction where this inode was used for a reflink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3405) 	 * operation, so after eviction and reloading the inode we must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3406) 	 * pessimistic and assume the last transaction that modified the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3407) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3408) 	BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3410) 	path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3411) 	if (inode->i_nlink != 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3412) 	    path->slots[0] >= btrfs_header_nritems(leaf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3413) 		goto cache_acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3415) 	btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3416) 	if (location.objectid != btrfs_ino(BTRFS_I(inode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3417) 		goto cache_acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3419) 	ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3420) 	if (location.type == BTRFS_INODE_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3421) 		struct btrfs_inode_ref *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3423) 		ref = (struct btrfs_inode_ref *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3424) 		BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3425) 	} else if (location.type == BTRFS_INODE_EXTREF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3426) 		struct btrfs_inode_extref *extref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3428) 		extref = (struct btrfs_inode_extref *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3429) 		BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3430) 								     extref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3431) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3432) cache_acl:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3433) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3434) 	 * try to precache a NULL acl entry for files that don't have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3435) 	 * any xattrs or acls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3436) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3437) 	maybe_acls = acls_after_inode_item(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3438) 			btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3439) 	if (first_xattr_slot != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3440) 		path->slots[0] = first_xattr_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3441) 		ret = btrfs_load_inode_props(inode, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3442) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3443) 			btrfs_err(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3444) 				  "error loading props for ino %llu (root %llu): %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3445) 				  btrfs_ino(BTRFS_I(inode)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3446) 				  root->root_key.objectid, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3447) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3448) 	if (path != in_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3449) 		btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3451) 	if (!maybe_acls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3452) 		cache_no_acl(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3454) 	switch (inode->i_mode & S_IFMT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3455) 	case S_IFREG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3456) 		inode->i_mapping->a_ops = &btrfs_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3457) 		inode->i_fop = &btrfs_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3458) 		inode->i_op = &btrfs_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3459) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3460) 	case S_IFDIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3461) 		inode->i_fop = &btrfs_dir_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3462) 		inode->i_op = &btrfs_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3463) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3464) 	case S_IFLNK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3465) 		inode->i_op = &btrfs_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3466) 		inode_nohighmem(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3467) 		inode->i_mapping->a_ops = &btrfs_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3468) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3469) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3470) 		inode->i_op = &btrfs_special_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3471) 		init_special_inode(inode, inode->i_mode, rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3472) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3473) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3475) 	btrfs_sync_inode_flags_to_i_flags(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3476) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3479) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3480)  * given a leaf and an inode, copy the inode fields into the leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3481)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3482) static void fill_inode_item(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3483) 			    struct extent_buffer *leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3484) 			    struct btrfs_inode_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3485) 			    struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3487) 	struct btrfs_map_token token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3489) 	btrfs_init_map_token(&token, leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3491) 	btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3492) 	btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3493) 	btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3494) 	btrfs_set_token_inode_mode(&token, item, inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3495) 	btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3497) 	btrfs_set_token_timespec_sec(&token, &item->atime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3498) 				     inode->i_atime.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3499) 	btrfs_set_token_timespec_nsec(&token, &item->atime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3500) 				      inode->i_atime.tv_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3502) 	btrfs_set_token_timespec_sec(&token, &item->mtime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3503) 				     inode->i_mtime.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3504) 	btrfs_set_token_timespec_nsec(&token, &item->mtime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3505) 				      inode->i_mtime.tv_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3507) 	btrfs_set_token_timespec_sec(&token, &item->ctime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3508) 				     inode->i_ctime.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3509) 	btrfs_set_token_timespec_nsec(&token, &item->ctime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3510) 				      inode->i_ctime.tv_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3512) 	btrfs_set_token_timespec_sec(&token, &item->otime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3513) 				     BTRFS_I(inode)->i_otime.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3514) 	btrfs_set_token_timespec_nsec(&token, &item->otime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3515) 				      BTRFS_I(inode)->i_otime.tv_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3517) 	btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3518) 	btrfs_set_token_inode_generation(&token, item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3519) 					 BTRFS_I(inode)->generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3520) 	btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3521) 	btrfs_set_token_inode_transid(&token, item, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3522) 	btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3523) 	btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3524) 	btrfs_set_token_inode_block_group(&token, item, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3527) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3528)  * copy everything in the in-memory inode into the btree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3529)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3530) static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3531) 				struct btrfs_root *root, struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3532) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3533) 	struct btrfs_inode_item *inode_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3534) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3535) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3536) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3538) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3539) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3540) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3542) 	path->leave_spinning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3543) 	ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3544) 				 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3545) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3546) 		if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3547) 			ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3548) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3549) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3551) 	leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3552) 	inode_item = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3553) 				    struct btrfs_inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3555) 	fill_inode_item(trans, leaf, inode_item, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3556) 	btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3557) 	btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3558) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3559) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3560) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3561) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3564) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3565)  * copy everything in the in-memory inode into the btree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3566)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3567) noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3568) 				struct btrfs_root *root, struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3570) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3571) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3573) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3574) 	 * If the inode is a free space inode, we can deadlock during commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3575) 	 * if we put it into the delayed code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3576) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3577) 	 * The data relocation inode should also be directly updated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3578) 	 * without delay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3579) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3580) 	if (!btrfs_is_free_space_inode(BTRFS_I(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3581) 	    && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3582) 	    && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3583) 		btrfs_update_root_times(trans, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3585) 		ret = btrfs_delayed_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3586) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3587) 			btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3588) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3589) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3591) 	return btrfs_update_inode_item(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3594) noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3595) 					 struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3596) 					 struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3597) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3598) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3600) 	ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3601) 	if (ret == -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3602) 		return btrfs_update_inode_item(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3603) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3606) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3607)  * unlink helper that gets used here in inode.c and in the tree logging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3608)  * recovery code.  It remove a link in a directory with a given name, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3609)  * also drops the back refs in the inode to the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3610)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3611) static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3612) 				struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3613) 				struct btrfs_inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3614) 				struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3615) 				const char *name, int name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3617) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3618) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3619) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3620) 	struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3621) 	u64 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3622) 	u64 ino = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3623) 	u64 dir_ino = btrfs_ino(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3625) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3626) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3627) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3628) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3629) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3631) 	path->leave_spinning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3632) 	di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3633) 				    name, name_len, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3634) 	if (IS_ERR_OR_NULL(di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3635) 		ret = di ? PTR_ERR(di) : -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3636) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3637) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3638) 	ret = btrfs_delete_one_dir_name(trans, root, path, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3639) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3640) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3641) 	btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3643) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3644) 	 * If we don't have dir index, we have to get it by looking up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3645) 	 * the inode ref, since we get the inode ref, remove it directly,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3646) 	 * it is unnecessary to do delayed deletion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3647) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3648) 	 * But if we have dir index, needn't search inode ref to get it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3649) 	 * Since the inode ref is close to the inode item, it is better
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3650) 	 * that we delay to delete it, and just do this deletion when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3651) 	 * we update the inode item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3652) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3653) 	if (inode->dir_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3654) 		ret = btrfs_delayed_delete_inode_ref(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3655) 		if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3656) 			index = inode->dir_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3657) 			goto skip_backref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3658) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3659) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3661) 	ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3662) 				  dir_ino, &index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3663) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3664) 		btrfs_info(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3665) 			"failed to delete reference to %.*s, inode %llu parent %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3666) 			name_len, name, ino, dir_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3667) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3668) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3669) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3670) skip_backref:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3671) 	ret = btrfs_delete_delayed_dir_index(trans, dir, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3672) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3673) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3674) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3675) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3677) 	ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3678) 			dir_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3679) 	if (ret != 0 && ret != -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3680) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3681) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3682) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3684) 	ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3685) 			index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3686) 	if (ret == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3687) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3688) 	else if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3689) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3691) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3692) 	 * If we have a pending delayed iput we could end up with the final iput
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3693) 	 * being run in btrfs-cleaner context.  If we have enough of these built
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3694) 	 * up we can end up burning a lot of time in btrfs-cleaner without any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3695) 	 * way to throttle the unlinks.  Since we're currently holding a ref on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3696) 	 * the inode we can run the delayed iput here without any issues as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3697) 	 * final iput won't be done until after we drop the ref we're currently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3698) 	 * holding.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3699) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3700) 	btrfs_run_delayed_iput(fs_info, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3701) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3702) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3703) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3704) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3706) 	btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3707) 	inode_inc_iversion(&inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3708) 	inode_inc_iversion(&dir->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3709) 	inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3710) 		dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3711) 	ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3712) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3713) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3716) int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3717) 		       struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3718) 		       struct btrfs_inode *dir, struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3719) 		       const char *name, int name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3720) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3721) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3722) 	ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3723) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3724) 		drop_nlink(&inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3725) 		ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3726) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3727) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3730) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3731)  * helper to start transaction for unlink and rmdir.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3732)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3733)  * unlink and rmdir are special in btrfs, they do not always free space, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3734)  * if we cannot make our reservations the normal way try and see if there is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3735)  * plenty of slack room in the global reserve to migrate, otherwise we cannot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3736)  * allow the unlink to occur.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3737)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3738) static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3739) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3740) 	struct btrfs_root *root = BTRFS_I(dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3742) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3743) 	 * 1 for the possible orphan item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3744) 	 * 1 for the dir item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3745) 	 * 1 for the dir index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3746) 	 * 1 for the inode ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3747) 	 * 1 for the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3748) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3749) 	return btrfs_start_transaction_fallback_global_rsv(root, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3750) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3752) static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3753) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3754) 	struct btrfs_root *root = BTRFS_I(dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3755) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3756) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3757) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3759) 	trans = __unlink_start_trans(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3760) 	if (IS_ERR(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3761) 		return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3763) 	btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3764) 			0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3766) 	ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3767) 			BTRFS_I(d_inode(dentry)), dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3768) 			dentry->d_name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3769) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3770) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3772) 	if (inode->i_nlink == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3773) 		ret = btrfs_orphan_add(trans, BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3774) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3775) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3776) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3778) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3779) 	btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3780) 	btrfs_btree_balance_dirty(root->fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3781) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3784) static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3785) 			       struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3786) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3787) 	struct btrfs_root *root = BTRFS_I(dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3788) 	struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3789) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3790) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3791) 	struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3792) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3793) 	const char *name = dentry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3794) 	int name_len = dentry->d_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3795) 	u64 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3796) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3797) 	u64 objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3798) 	u64 dir_ino = btrfs_ino(BTRFS_I(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3800) 	if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3801) 		objectid = inode->root->root_key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3802) 	} else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3803) 		objectid = inode->location.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3804) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3805) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3806) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3807) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3809) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3810) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3811) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3813) 	di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3814) 				   name, name_len, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3815) 	if (IS_ERR_OR_NULL(di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3816) 		ret = di ? PTR_ERR(di) : -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3817) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3818) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3820) 	leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3821) 	btrfs_dir_item_key_to_cpu(leaf, di, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3822) 	WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3823) 	ret = btrfs_delete_one_dir_name(trans, root, path, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3824) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3825) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3826) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3827) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3828) 	btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3830) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3831) 	 * This is a placeholder inode for a subvolume we didn't have a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3832) 	 * reference to at the time of the snapshot creation.  In the meantime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3833) 	 * we could have renamed the real subvol link into our snapshot, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3834) 	 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3835) 	 * Instead simply lookup the dir_index_item for this entry so we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3836) 	 * remove it.  Otherwise we know we have a ref to the root and we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3837) 	 * call btrfs_del_root_ref, and it _shouldn't_ fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3838) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3839) 	if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3840) 		di = btrfs_search_dir_index_item(root, path, dir_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3841) 						 name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3842) 		if (IS_ERR_OR_NULL(di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3843) 			if (!di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3844) 				ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3845) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3846) 				ret = PTR_ERR(di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3847) 			btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3848) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3849) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3851) 		leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3852) 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3853) 		index = key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3854) 		btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3855) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3856) 		ret = btrfs_del_root_ref(trans, objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3857) 					 root->root_key.objectid, dir_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3858) 					 &index, name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3859) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3860) 			btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3861) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3862) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3863) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3865) 	ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3866) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3867) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3868) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3869) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3871) 	btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3872) 	inode_inc_iversion(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3873) 	dir->i_mtime = dir->i_ctime = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3874) 	ret = btrfs_update_inode_fallback(trans, root, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3875) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3876) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3877) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3878) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3879) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3880) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3882) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3883)  * Helper to check if the subvolume references other subvolumes or if it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3884)  * default.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3885)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3886) static noinline int may_destroy_subvol(struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3887) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3888) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3889) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3890) 	struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3891) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3892) 	u64 dir_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3893) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3895) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3896) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3897) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3899) 	/* Make sure this root isn't set as the default subvol */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3900) 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3901) 	di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3902) 				   dir_id, "default", 7, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3903) 	if (di && !IS_ERR(di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3904) 		btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3905) 		if (key.objectid == root->root_key.objectid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3906) 			ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3907) 			btrfs_err(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3908) 				  "deleting default subvolume %llu is not allowed",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3909) 				  key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3910) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3911) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3912) 		btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3913) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3915) 	key.objectid = root->root_key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3916) 	key.type = BTRFS_ROOT_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3917) 	key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3919) 	ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3920) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3921) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3922) 	BUG_ON(ret == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3924) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3925) 	if (path->slots[0] > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3926) 		path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3927) 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3928) 		if (key.objectid == root->root_key.objectid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3929) 		    key.type == BTRFS_ROOT_REF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3930) 			ret = -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3931) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3932) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3933) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3934) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3937) /* Delete all dentries for inodes belonging to the root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3938) static void btrfs_prune_dentries(struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3939) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3940) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3941) 	struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3942) 	struct rb_node *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3943) 	struct btrfs_inode *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3944) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3945) 	u64 objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3947) 	if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3948) 		WARN_ON(btrfs_root_refs(&root->root_item) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3950) 	spin_lock(&root->inode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3951) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3952) 	node = root->inode_tree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3953) 	prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3954) 	while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3955) 		prev = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3956) 		entry = rb_entry(node, struct btrfs_inode, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3958) 		if (objectid < btrfs_ino(entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3959) 			node = node->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3960) 		else if (objectid > btrfs_ino(entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3961) 			node = node->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3962) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3963) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3964) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3965) 	if (!node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3966) 		while (prev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3967) 			entry = rb_entry(prev, struct btrfs_inode, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3968) 			if (objectid <= btrfs_ino(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3969) 				node = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3970) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3971) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3972) 			prev = rb_next(prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3973) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3974) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3975) 	while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3976) 		entry = rb_entry(node, struct btrfs_inode, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3977) 		objectid = btrfs_ino(entry) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3978) 		inode = igrab(&entry->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3979) 		if (inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3980) 			spin_unlock(&root->inode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3981) 			if (atomic_read(&inode->i_count) > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3982) 				d_prune_aliases(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3983) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3984) 			 * btrfs_drop_inode will have it removed from the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3985) 			 * cache when its usage count hits zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3986) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3987) 			iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3988) 			cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3989) 			spin_lock(&root->inode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3990) 			goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3991) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3993) 		if (cond_resched_lock(&root->inode_lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3994) 			goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3996) 		node = rb_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3997) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3998) 	spin_unlock(&root->inode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3999) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4001) int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4002) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4003) 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4004) 	struct btrfs_root *root = BTRFS_I(dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4005) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4006) 	struct btrfs_root *dest = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4007) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4008) 	struct btrfs_block_rsv block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4009) 	u64 root_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4010) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4011) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4013) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4014) 	 * Don't allow to delete a subvolume with send in progress. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4015) 	 * inside the inode lock so the error handling that has to drop the bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4016) 	 * again is not run concurrently.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4017) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4018) 	spin_lock(&dest->root_item_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4019) 	if (dest->send_in_progress) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4020) 		spin_unlock(&dest->root_item_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4021) 		btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4022) 			   "attempt to delete subvolume %llu during send",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4023) 			   dest->root_key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4024) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4025) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4026) 	root_flags = btrfs_root_flags(&dest->root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4027) 	btrfs_set_root_flags(&dest->root_item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4028) 			     root_flags | BTRFS_ROOT_SUBVOL_DEAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4029) 	spin_unlock(&dest->root_item_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4031) 	down_write(&fs_info->subvol_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4033) 	err = may_destroy_subvol(dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4034) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4035) 		goto out_up_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4037) 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4038) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4039) 	 * One for dir inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4040) 	 * two for dir entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4041) 	 * two for root ref/backref.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4042) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4043) 	err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4044) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4045) 		goto out_up_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4047) 	trans = btrfs_start_transaction(root, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4048) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4049) 		err = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4050) 		goto out_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4051) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4052) 	trans->block_rsv = &block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4053) 	trans->bytes_reserved = block_rsv.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4055) 	btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4057) 	ret = btrfs_unlink_subvol(trans, dir, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4058) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4059) 		err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4060) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4061) 		goto out_end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4062) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4064) 	btrfs_record_root_in_trans(trans, dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4066) 	memset(&dest->root_item.drop_progress, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4067) 		sizeof(dest->root_item.drop_progress));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4068) 	dest->root_item.drop_level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4069) 	btrfs_set_root_refs(&dest->root_item, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4071) 	if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4072) 		ret = btrfs_insert_orphan_item(trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4073) 					fs_info->tree_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4074) 					dest->root_key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4075) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4076) 			btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4077) 			err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4078) 			goto out_end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4079) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4080) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4082) 	ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4083) 				  BTRFS_UUID_KEY_SUBVOL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4084) 				  dest->root_key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4085) 	if (ret && ret != -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4086) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4087) 		err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4088) 		goto out_end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4089) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4090) 	if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4091) 		ret = btrfs_uuid_tree_remove(trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4092) 					  dest->root_item.received_uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4093) 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4094) 					  dest->root_key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4095) 		if (ret && ret != -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4096) 			btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4097) 			err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4098) 			goto out_end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4099) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4100) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4102) 	free_anon_bdev(dest->anon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4103) 	dest->anon_dev = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4104) out_end_trans:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4105) 	trans->block_rsv = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4106) 	trans->bytes_reserved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4107) 	ret = btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4108) 	if (ret && !err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4109) 		err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4110) 	inode->i_flags |= S_DEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4111) out_release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4112) 	btrfs_subvolume_release_metadata(root, &block_rsv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4113) out_up_write:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4114) 	up_write(&fs_info->subvol_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4115) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4116) 		spin_lock(&dest->root_item_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4117) 		root_flags = btrfs_root_flags(&dest->root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4118) 		btrfs_set_root_flags(&dest->root_item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4119) 				root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4120) 		spin_unlock(&dest->root_item_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4121) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4122) 		d_invalidate(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4123) 		btrfs_prune_dentries(dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4124) 		ASSERT(dest->send_in_progress == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4126) 		/* the last ref */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4127) 		if (dest->ino_cache_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4128) 			iput(dest->ino_cache_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4129) 			dest->ino_cache_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4130) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4133) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4136) static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4138) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4139) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4140) 	struct btrfs_root *root = BTRFS_I(dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4141) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4142) 	u64 last_unlink_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4144) 	if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4145) 		return -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4146) 	if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4147) 		return btrfs_delete_subvolume(dir, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4149) 	trans = __unlink_start_trans(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4150) 	if (IS_ERR(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4151) 		return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4153) 	if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4154) 		err = btrfs_unlink_subvol(trans, dir, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4155) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4158) 	err = btrfs_orphan_add(trans, BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4159) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4160) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4162) 	last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4164) 	/* now the directory is empty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4165) 	err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4166) 			BTRFS_I(d_inode(dentry)), dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4167) 			dentry->d_name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4168) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4169) 		btrfs_i_size_write(BTRFS_I(inode), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4170) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4171) 		 * Propagate the last_unlink_trans value of the deleted dir to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4172) 		 * its parent directory. This is to prevent an unrecoverable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4173) 		 * log tree in the case we do something like this:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4174) 		 * 1) create dir foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4175) 		 * 2) create snapshot under dir foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4176) 		 * 3) delete the snapshot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4177) 		 * 4) rmdir foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4178) 		 * 5) mkdir foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4179) 		 * 6) fsync foo or some file inside foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4180) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4181) 		if (last_unlink_trans >= trans->transid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4182) 			BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4184) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4185) 	btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4186) 	btrfs_btree_balance_dirty(root->fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4188) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4191) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4192)  * Return this if we need to call truncate_block for the last bit of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4193)  * truncate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4194)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4195) #define NEED_TRUNCATE_BLOCK 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4197) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4198)  * this can truncate away extent items, csum items and directory items.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4199)  * It starts at a high offset and removes keys until it can't find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4200)  * any higher than new_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4201)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4202)  * csum items that cross the new i_size are truncated to the new size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4203)  * as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4204)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4205)  * min_type is the minimum key type to truncate down to.  If set to 0, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4206)  * will kill all the items on this inode, including the INODE_ITEM_KEY.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4207)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4208) int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4209) 			       struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4210) 			       struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4211) 			       u64 new_size, u32 min_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4213) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4214) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4215) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4216) 	struct btrfs_file_extent_item *fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4217) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4218) 	struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4219) 	u64 extent_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4220) 	u64 extent_num_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4221) 	u64 extent_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4222) 	u64 item_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4223) 	u64 last_size = new_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4224) 	u32 found_type = (u8)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4225) 	int found_extent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4226) 	int del_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4227) 	int pending_del_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4228) 	int pending_del_slot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4229) 	int extent_type = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4230) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4231) 	u64 ino = btrfs_ino(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4232) 	u64 bytes_deleted = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4233) 	bool be_nice = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4234) 	bool should_throttle = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4235) 	const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4236) 	struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4238) 	BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4240) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4241) 	 * For non-free space inodes and non-shareable roots, we want to back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4242) 	 * off from time to time.  This means all inodes in subvolume roots,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4243) 	 * reloc roots, and data reloc roots.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4244) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4245) 	if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4246) 	    test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4247) 		be_nice = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4249) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4250) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4251) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4252) 	path->reada = READA_BACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4254) 	if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4255) 		lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4256) 				 &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4258) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4259) 		 * We want to drop from the next block forward in case this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4260) 		 * new size is not block aligned since we will be keeping the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4261) 		 * last block of the extent just the way it is.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4262) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4263) 		btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4264) 					fs_info->sectorsize),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4265) 					(u64)-1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4266) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4268) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4269) 	 * This function is also used to drop the items in the log tree before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4270) 	 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4271) 	 * it is used to drop the logged items. So we shouldn't kill the delayed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4272) 	 * items.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4273) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4274) 	if (min_type == 0 && root == BTRFS_I(inode)->root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4275) 		btrfs_kill_delayed_inode_items(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4277) 	key.objectid = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4278) 	key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4279) 	key.type = (u8)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4281) search_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4282) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4283) 	 * with a 16K leaf size and 128MB extents, you can actually queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4284) 	 * up a huge file in a single leaf.  Most of the time that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4285) 	 * bytes_deleted is > 0, it will be huge by the time we get here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4286) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4287) 	if (be_nice && bytes_deleted > SZ_32M &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4288) 	    btrfs_should_end_transaction(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4289) 		ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4290) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4291) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4293) 	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4294) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4295) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4297) 	if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4298) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4299) 		/* there are no items in the tree for us to truncate, we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4300) 		 * done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4301) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4302) 		if (path->slots[0] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4303) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4304) 		path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4305) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4307) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4308) 		u64 clear_start = 0, clear_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4310) 		fi = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4311) 		leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4312) 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4313) 		found_type = found_key.type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4315) 		if (found_key.objectid != ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4316) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4318) 		if (found_type < min_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4319) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4321) 		item_end = found_key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4322) 		if (found_type == BTRFS_EXTENT_DATA_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4323) 			fi = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4324) 					    struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4325) 			extent_type = btrfs_file_extent_type(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4326) 			if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4327) 				item_end +=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4328) 				    btrfs_file_extent_num_bytes(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4330) 				trace_btrfs_truncate_show_fi_regular(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4331) 					BTRFS_I(inode), leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4332) 					found_key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4333) 			} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4334) 				item_end += btrfs_file_extent_ram_bytes(leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4335) 									fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4337) 				trace_btrfs_truncate_show_fi_inline(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4338) 					BTRFS_I(inode), leaf, fi, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4339) 					found_key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4340) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4341) 			item_end--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4342) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4343) 		if (found_type > min_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4344) 			del_item = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4345) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4346) 			if (item_end < new_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4347) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4348) 			if (found_key.offset >= new_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4349) 				del_item = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4350) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4351) 				del_item = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4352) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4353) 		found_extent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4354) 		/* FIXME, shrink the extent if the ref count is only 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4355) 		if (found_type != BTRFS_EXTENT_DATA_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4356) 			goto delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4358) 		if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4359) 			u64 num_dec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4361) 			clear_start = found_key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4362) 			extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4363) 			if (!del_item) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4364) 				u64 orig_num_bytes =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4365) 					btrfs_file_extent_num_bytes(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4366) 				extent_num_bytes = ALIGN(new_size -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4367) 						found_key.offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4368) 						fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4369) 				clear_start = ALIGN(new_size, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4370) 				btrfs_set_file_extent_num_bytes(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4371) 							 extent_num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4372) 				num_dec = (orig_num_bytes -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4373) 					   extent_num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4374) 				if (test_bit(BTRFS_ROOT_SHAREABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4375) 					     &root->state) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4376) 				    extent_start != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4377) 					inode_sub_bytes(inode, num_dec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4378) 				btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4379) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4380) 				extent_num_bytes =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4381) 					btrfs_file_extent_disk_num_bytes(leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4382) 									 fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4383) 				extent_offset = found_key.offset -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4384) 					btrfs_file_extent_offset(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4386) 				/* FIXME blocksize != 4096 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4387) 				num_dec = btrfs_file_extent_num_bytes(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4388) 				if (extent_start != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4389) 					found_extent = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4390) 					if (test_bit(BTRFS_ROOT_SHAREABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4391) 						     &root->state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4392) 						inode_sub_bytes(inode, num_dec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4393) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4394) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4395) 			clear_len = num_dec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4396) 		} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4397) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4398) 			 * we can't truncate inline items that have had
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4399) 			 * special encodings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4400) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4401) 			if (!del_item &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4402) 			    btrfs_file_extent_encryption(leaf, fi) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4403) 			    btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4404) 			    btrfs_file_extent_compression(leaf, fi) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4405) 				u32 size = (u32)(new_size - found_key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4407) 				btrfs_set_file_extent_ram_bytes(leaf, fi, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4408) 				size = btrfs_file_extent_calc_inline_size(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4409) 				btrfs_truncate_item(path, size, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4410) 			} else if (!del_item) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4411) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4412) 				 * We have to bail so the last_size is set to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4413) 				 * just before this extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4414) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4415) 				ret = NEED_TRUNCATE_BLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4416) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4417) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4418) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4419) 				 * Inline extents are special, we just treat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4420) 				 * them as a full sector worth in the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4421) 				 * extent tree just for simplicity sake.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4422) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4423) 				clear_len = fs_info->sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4424) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4426) 			if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4427) 				inode_sub_bytes(inode, item_end + 1 - new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4428) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4429) delete:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4430) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4431) 		 * We use btrfs_truncate_inode_items() to clean up log trees for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4432) 		 * multiple fsyncs, and in this case we don't want to clear the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4433) 		 * file extent range because it's just the log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4434) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4435) 		if (root == BTRFS_I(inode)->root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4436) 			ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4437) 						  clear_start, clear_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4438) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4439) 				btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4440) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4441) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4442) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4444) 		if (del_item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4445) 			last_size = found_key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4446) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4447) 			last_size = new_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4448) 		if (del_item) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4449) 			if (!pending_del_nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4450) 				/* no pending yet, add ourselves */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4451) 				pending_del_slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4452) 				pending_del_nr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4453) 			} else if (pending_del_nr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4454) 				   path->slots[0] + 1 == pending_del_slot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4455) 				/* hop on the pending chunk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4456) 				pending_del_nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4457) 				pending_del_slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4458) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4459) 				BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4460) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4461) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4462) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4463) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4464) 		should_throttle = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4466) 		if (found_extent &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4467) 		    root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4468) 			struct btrfs_ref ref = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4470) 			bytes_deleted += extent_num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4472) 			btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4473) 					extent_start, extent_num_bytes, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4474) 			ref.real_root = root->root_key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4475) 			btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4476) 					ino, extent_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4477) 			ret = btrfs_free_extent(trans, &ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4478) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4479) 				btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4480) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4481) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4482) 			if (be_nice) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4483) 				if (btrfs_should_throttle_delayed_refs(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4484) 					should_throttle = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4485) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4486) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4488) 		if (found_type == BTRFS_INODE_ITEM_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4489) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4491) 		if (path->slots[0] == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4492) 		    path->slots[0] != pending_del_slot ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4493) 		    should_throttle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4494) 			if (pending_del_nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4495) 				ret = btrfs_del_items(trans, root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4496) 						pending_del_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4497) 						pending_del_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4498) 				if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4499) 					btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4500) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4501) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4502) 				pending_del_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4503) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4504) 			btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4506) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4507) 			 * We can generate a lot of delayed refs, so we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4508) 			 * throttle every once and a while and make sure we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4509) 			 * adding enough space to keep up with the work we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4510) 			 * generating.  Since we hold a transaction here we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4511) 			 * can't flush, and we don't want to FLUSH_LIMIT because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4512) 			 * we could have generated too many delayed refs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4513) 			 * actually allocate, so just bail if we're short and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4514) 			 * let the normal reservation dance happen higher up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4515) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4516) 			if (should_throttle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4517) 				ret = btrfs_delayed_refs_rsv_refill(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4518) 							BTRFS_RESERVE_NO_FLUSH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4519) 				if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4520) 					ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4521) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4522) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4523) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4524) 			goto search_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4525) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4526) 			path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4527) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4528) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4529) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4530) 	if (ret >= 0 && pending_del_nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4531) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4533) 		err = btrfs_del_items(trans, root, path, pending_del_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4534) 				      pending_del_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4535) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4536) 			btrfs_abort_transaction(trans, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4537) 			ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4538) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4539) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4540) 	if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4541) 		ASSERT(last_size >= new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4542) 		if (!ret && last_size > new_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4543) 			last_size = new_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4544) 		btrfs_inode_safe_disk_i_size_write(inode, last_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4545) 		unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4546) 				     (u64)-1, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4547) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4549) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4550) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4553) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4554)  * btrfs_truncate_block - read, zero a chunk and write a block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4555)  * @inode - inode that we're zeroing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4556)  * @from - the offset to start zeroing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4557)  * @len - the length to zero, 0 to zero the entire range respective to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4558)  *	offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4559)  * @front - zero up to the offset instead of from the offset on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4560)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4561)  * This will find the block for the "from" offset and cow the block and zero the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4562)  * part we want to zero.  This is used with truncate and hole punching.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4563)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4564) int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4565) 			int front)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4567) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4568) 	struct address_space *mapping = inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4569) 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4570) 	struct btrfs_ordered_extent *ordered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4571) 	struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4572) 	struct extent_changeset *data_reserved = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4573) 	char *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4574) 	bool only_release_metadata = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4575) 	u32 blocksize = fs_info->sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4576) 	pgoff_t index = from >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4577) 	unsigned offset = from & (blocksize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4578) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4579) 	gfp_t mask = btrfs_alloc_write_mask(mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4580) 	size_t write_bytes = blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4581) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4582) 	u64 block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4583) 	u64 block_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4585) 	if (IS_ALIGNED(offset, blocksize) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4586) 	    (!len || IS_ALIGNED(len, blocksize)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4587) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4589) 	block_start = round_down(from, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4590) 	block_end = block_start + blocksize - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4592) 	ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4593) 					  block_start, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4594) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4595) 		if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4596) 					   &write_bytes) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4597) 			/* For nocow case, no need to reserve data space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4598) 			only_release_metadata = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4599) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4600) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4601) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4602) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4603) 	ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4604) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4605) 		if (!only_release_metadata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4606) 			btrfs_free_reserved_data_space(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4607) 					data_reserved, block_start, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4608) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4609) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4610) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4611) 	page = find_or_create_page(mapping, index, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4612) 	if (!page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4613) 		btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4614) 					     block_start, blocksize, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4615) 		btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4616) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4617) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4618) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4620) 	if (!PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4621) 		ret = btrfs_readpage(NULL, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4622) 		lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4623) 		if (page->mapping != mapping) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4624) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4625) 			put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4626) 			goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4627) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4628) 		if (!PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4629) 			ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4630) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4631) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4632) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4633) 	wait_on_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4635) 	lock_extent_bits(io_tree, block_start, block_end, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4636) 	set_page_extent_mapped(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4638) 	ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4639) 	if (ordered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4640) 		unlock_extent_cached(io_tree, block_start, block_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4641) 				     &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4642) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4643) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4644) 		btrfs_start_ordered_extent(ordered, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4645) 		btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4646) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4647) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4649) 	clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4650) 			 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4651) 			 0, 0, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4653) 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode), block_start, block_end, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4654) 					&cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4655) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4656) 		unlock_extent_cached(io_tree, block_start, block_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4657) 				     &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4658) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4659) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4661) 	if (offset != blocksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4662) 		if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4663) 			len = blocksize - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4664) 		kaddr = kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4665) 		if (front)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4666) 			memset(kaddr + (block_start - page_offset(page)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4667) 				0, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4668) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4669) 			memset(kaddr + (block_start - page_offset(page)) +  offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4670) 				0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4671) 		flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4672) 		kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4673) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4674) 	ClearPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4675) 	set_page_dirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4676) 	unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4678) 	if (only_release_metadata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4679) 		set_extent_bit(&BTRFS_I(inode)->io_tree, block_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4680) 				block_end, EXTENT_NORESERVE, NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4681) 				GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4683) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4684) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4685) 		if (only_release_metadata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4686) 			btrfs_delalloc_release_metadata(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4687) 					blocksize, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4688) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4689) 			btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4690) 					block_start, blocksize, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4691) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4692) 	btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4693) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4694) 	put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4695) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4696) 	if (only_release_metadata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4697) 		btrfs_check_nocow_unlock(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4698) 	extent_changeset_free(data_reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4699) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4702) static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4703) 			     u64 offset, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4705) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4706) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4707) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4709) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4710) 	 * Still need to make sure the inode looks like it's been updated so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4711) 	 * that any holes get logged if we fsync.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4712) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4713) 	if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4714) 		BTRFS_I(inode)->last_trans = fs_info->generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4715) 		BTRFS_I(inode)->last_sub_trans = root->log_transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4716) 		BTRFS_I(inode)->last_log_commit = root->last_log_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4717) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4718) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4720) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4721) 	 * 1 - for the one we're dropping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4722) 	 * 1 - for the one we're adding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4723) 	 * 1 - for updating the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4724) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4725) 	trans = btrfs_start_transaction(root, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4726) 	if (IS_ERR(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4727) 		return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4729) 	ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4730) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4731) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4732) 		btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4733) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4734) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4736) 	ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4737) 			offset, 0, 0, len, 0, len, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4738) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4739) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4740) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4741) 		btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4742) 	btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4743) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4746) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4747)  * This function puts in dummy file extents for the area we're creating a hole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4748)  * for.  So if we are truncating this file to a larger size we need to insert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4749)  * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4750)  * the range between oldsize and size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4751)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4752) int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4753) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4754) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4755) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4756) 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4757) 	struct extent_map *em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4758) 	struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4759) 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4760) 	u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4761) 	u64 block_end = ALIGN(size, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4762) 	u64 last_byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4763) 	u64 cur_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4764) 	u64 hole_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4765) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4767) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4768) 	 * If our size started in the middle of a block we need to zero out the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4769) 	 * rest of the block before we expand the i_size, otherwise we could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4770) 	 * expose stale data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4771) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4772) 	err = btrfs_truncate_block(inode, oldsize, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4773) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4774) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4776) 	if (size <= hole_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4777) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4779) 	btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4780) 					   block_end - 1, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4781) 	cur_offset = hole_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4782) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4783) 		em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4784) 				      block_end - cur_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4785) 		if (IS_ERR(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4786) 			err = PTR_ERR(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4787) 			em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4788) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4789) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4790) 		last_byte = min(extent_map_end(em), block_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4791) 		last_byte = ALIGN(last_byte, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4792) 		hole_size = last_byte - cur_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4794) 		if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4795) 			struct extent_map *hole_em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4797) 			err = maybe_insert_hole(root, inode, cur_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4798) 						hole_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4799) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4800) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4802) 			err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4803) 							cur_offset, hole_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4804) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4805) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4807) 			btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4808) 						cur_offset + hole_size - 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4809) 			hole_em = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4810) 			if (!hole_em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4811) 				set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4812) 					&BTRFS_I(inode)->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4813) 				goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4814) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4815) 			hole_em->start = cur_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4816) 			hole_em->len = hole_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4817) 			hole_em->orig_start = cur_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4819) 			hole_em->block_start = EXTENT_MAP_HOLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4820) 			hole_em->block_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4821) 			hole_em->orig_block_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4822) 			hole_em->ram_bytes = hole_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4823) 			hole_em->compress_type = BTRFS_COMPRESS_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4824) 			hole_em->generation = fs_info->generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4826) 			while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4827) 				write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4828) 				err = add_extent_mapping(em_tree, hole_em, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4829) 				write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4830) 				if (err != -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4831) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4832) 				btrfs_drop_extent_cache(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4833) 							cur_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4834) 							cur_offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4835) 							hole_size - 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4836) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4837) 			free_extent_map(hole_em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4838) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4839) 			err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4840) 							cur_offset, hole_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4841) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4842) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4843) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4844) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4845) 		free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4846) 		em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4847) 		cur_offset = last_byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4848) 		if (cur_offset >= block_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4849) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4850) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4851) 	free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4852) 	unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4853) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4856) static int btrfs_setsize(struct inode *inode, struct iattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4857) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4858) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4859) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4860) 	loff_t oldsize = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4861) 	loff_t newsize = attr->ia_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4862) 	int mask = attr->ia_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4863) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4865) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4866) 	 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4867) 	 * special case where we need to update the times despite not having
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4868) 	 * these flags set.  For all other operations the VFS set these flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4869) 	 * explicitly if it wants a timestamp update.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4870) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4871) 	if (newsize != oldsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4872) 		inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4873) 		if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4874) 			inode->i_ctime = inode->i_mtime =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4875) 				current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4876) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4878) 	if (newsize > oldsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4879) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4880) 		 * Don't do an expanding truncate while snapshotting is ongoing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4881) 		 * This is to ensure the snapshot captures a fully consistent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4882) 		 * state of this file - if the snapshot captures this expanding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4883) 		 * truncation, it must capture all writes that happened before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4884) 		 * this truncation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4885) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4886) 		btrfs_drew_write_lock(&root->snapshot_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4887) 		ret = btrfs_cont_expand(inode, oldsize, newsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4888) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4889) 			btrfs_drew_write_unlock(&root->snapshot_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4890) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4891) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4893) 		trans = btrfs_start_transaction(root, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4894) 		if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4895) 			btrfs_drew_write_unlock(&root->snapshot_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4896) 			return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4897) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4899) 		i_size_write(inode, newsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4900) 		btrfs_inode_safe_disk_i_size_write(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4901) 		pagecache_isize_extended(inode, oldsize, newsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4902) 		ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4903) 		btrfs_drew_write_unlock(&root->snapshot_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4904) 		btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4905) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4907) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4908) 		 * We're truncating a file that used to have good data down to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4909) 		 * zero. Make sure any new writes to the file get on disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4910) 		 * on close.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4911) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4912) 		if (newsize == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4913) 			set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4914) 				&BTRFS_I(inode)->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4916) 		truncate_setsize(inode, newsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4918) 		inode_dio_wait(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4920) 		ret = btrfs_truncate(inode, newsize == oldsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4921) 		if (ret && inode->i_nlink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4922) 			int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4924) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4925) 			 * Truncate failed, so fix up the in-memory size. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4926) 			 * adjusted disk_i_size down as we removed extents, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4927) 			 * wait for disk_i_size to be stable and then update the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4928) 			 * in-memory size to match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4929) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4930) 			err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4931) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4932) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4933) 			i_size_write(inode, BTRFS_I(inode)->disk_i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4934) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4935) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4937) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4940) static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4941) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4942) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4943) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4944) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4946) 	if (btrfs_root_readonly(root))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4947) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4949) 	err = setattr_prepare(dentry, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4950) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4951) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4953) 	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4954) 		err = btrfs_setsize(inode, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4955) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4956) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4957) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4959) 	if (attr->ia_valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4960) 		setattr_copy(inode, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4961) 		inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4962) 		err = btrfs_dirty_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4964) 		if (!err && attr->ia_valid & ATTR_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4965) 			err = posix_acl_chmod(inode, inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4966) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4968) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4969) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4971) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4972)  * While truncating the inode pages during eviction, we get the VFS calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4973)  * btrfs_invalidatepage() against each page of the inode. This is slow because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4974)  * the calls to btrfs_invalidatepage() result in a huge amount of calls to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4975)  * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4976)  * extent_state structures over and over, wasting lots of time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4977)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4978)  * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4979)  * those expensive operations on a per page basis and do only the ordered io
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4980)  * finishing, while we release here the extent_map and extent_state structures,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4981)  * without the excessive merging and splitting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4982)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4983) static void evict_inode_truncate_pages(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4984) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4985) 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4986) 	struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4987) 	struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4989) 	ASSERT(inode->i_state & I_FREEING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4990) 	truncate_inode_pages_final(&inode->i_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4992) 	write_lock(&map_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4993) 	while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4994) 		struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4996) 		node = rb_first_cached(&map_tree->map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4997) 		em = rb_entry(node, struct extent_map, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4998) 		clear_bit(EXTENT_FLAG_PINNED, &em->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4999) 		clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5000) 		remove_extent_mapping(map_tree, em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5001) 		free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5002) 		if (need_resched()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5003) 			write_unlock(&map_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5004) 			cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5005) 			write_lock(&map_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5006) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5007) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5008) 	write_unlock(&map_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5010) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5011) 	 * Keep looping until we have no more ranges in the io tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5012) 	 * We can have ongoing bios started by readahead that have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5013) 	 * their endio callback (extent_io.c:end_bio_extent_readpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5014) 	 * still in progress (unlocked the pages in the bio but did not yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5015) 	 * unlocked the ranges in the io tree). Therefore this means some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5016) 	 * ranges can still be locked and eviction started because before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5017) 	 * submitting those bios, which are executed by a separate task (work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5018) 	 * queue kthread), inode references (inode->i_count) were not taken
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5019) 	 * (which would be dropped in the end io callback of each bio).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5020) 	 * Therefore here we effectively end up waiting for those bios and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5021) 	 * anyone else holding locked ranges without having bumped the inode's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5022) 	 * reference count - if we don't do it, when they access the inode's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5023) 	 * io_tree to unlock a range it may be too late, leading to an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5024) 	 * use-after-free issue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5025) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5026) 	spin_lock(&io_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5027) 	while (!RB_EMPTY_ROOT(&io_tree->state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5028) 		struct extent_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5029) 		struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5030) 		u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5031) 		u64 end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5032) 		unsigned state_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5034) 		node = rb_first(&io_tree->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5035) 		state = rb_entry(node, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5036) 		start = state->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5037) 		end = state->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5038) 		state_flags = state->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5039) 		spin_unlock(&io_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5041) 		lock_extent_bits(io_tree, start, end, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5043) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5044) 		 * If still has DELALLOC flag, the extent didn't reach disk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5045) 		 * and its reserved space won't be freed by delayed_ref.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5046) 		 * So we need to free its reserved space here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5047) 		 * (Refer to comment in btrfs_invalidatepage, case 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5048) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5049) 		 * Note, end is the bytenr of last byte, so we need + 1 here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5050) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5051) 		if (state_flags & EXTENT_DELALLOC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5052) 			btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5053) 					       end - start + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5055) 		clear_extent_bit(io_tree, start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5056) 				 EXTENT_LOCKED | EXTENT_DELALLOC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5057) 				 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5058) 				 &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5060) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5061) 		spin_lock(&io_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5062) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5063) 	spin_unlock(&io_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5066) static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5067) 							struct btrfs_block_rsv *rsv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5068) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5069) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5070) 	struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5071) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5072) 	u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5073) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5075) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5076) 	 * Eviction should be taking place at some place safe because of our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5077) 	 * delayed iputs.  However the normal flushing code will run delayed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5078) 	 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5079) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5080) 	 * We reserve the delayed_refs_extra here again because we can't use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5081) 	 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5082) 	 * above.  We reserve our extra bit here because we generate a ton of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5083) 	 * delayed refs activity by truncating.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5084) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5085) 	 * If we cannot make our reservation we'll attempt to steal from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5086) 	 * global reserve, because we really want to be able to free up space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5087) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5088) 	ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5089) 				     BTRFS_RESERVE_FLUSH_EVICT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5090) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5091) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5092) 		 * Try to steal from the global reserve if there is space for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5093) 		 * it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5094) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5095) 		if (btrfs_check_space_for_delayed_refs(fs_info) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5096) 		    btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5097) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5098) 				   "could not allocate space for delete; will truncate on mount");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5099) 			return ERR_PTR(-ENOSPC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5100) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5101) 		delayed_refs_extra = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5104) 	trans = btrfs_join_transaction(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5105) 	if (IS_ERR(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5106) 		return trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5108) 	if (delayed_refs_extra) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5109) 		trans->block_rsv = &fs_info->trans_block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5110) 		trans->bytes_reserved = delayed_refs_extra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5111) 		btrfs_block_rsv_migrate(rsv, trans->block_rsv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5112) 					delayed_refs_extra, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5114) 	return trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5117) void btrfs_evict_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5119) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5120) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5121) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5122) 	struct btrfs_block_rsv *rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5123) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5125) 	trace_btrfs_inode_evict(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5127) 	if (!root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5128) 		clear_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5129) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5132) 	evict_inode_truncate_pages(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5134) 	if (inode->i_nlink &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5135) 	    ((btrfs_root_refs(&root->root_item) != 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5136) 	      root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5137) 	     btrfs_is_free_space_inode(BTRFS_I(inode))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5138) 		goto no_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5140) 	if (is_bad_inode(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5141) 		goto no_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5143) 	btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5145) 	if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5146) 		goto no_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5148) 	if (inode->i_nlink > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5149) 		BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5150) 		       root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5151) 		goto no_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5154) 	ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5155) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5156) 		goto no_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5158) 	rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5159) 	if (!rsv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5160) 		goto no_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5161) 	rsv->size = btrfs_calc_metadata_size(fs_info, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5162) 	rsv->failfast = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5164) 	btrfs_i_size_write(BTRFS_I(inode), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5166) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5167) 		trans = evict_refill_and_join(root, rsv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5168) 		if (IS_ERR(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5169) 			goto free_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5171) 		trans->block_rsv = rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5173) 		ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5174) 		trans->block_rsv = &fs_info->trans_block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5175) 		btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5176) 		btrfs_btree_balance_dirty(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5177) 		if (ret && ret != -ENOSPC && ret != -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5178) 			goto free_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5179) 		else if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5180) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5183) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5184) 	 * Errors here aren't a big deal, it just means we leave orphan items in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5185) 	 * the tree. They will be cleaned up on the next mount. If the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5186) 	 * number gets reused, cleanup deletes the orphan item without doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5187) 	 * anything, and unlink reuses the existing orphan item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5188) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5189) 	 * If it turns out that we are dropping too many of these, we might want
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5190) 	 * to add a mechanism for retrying these after a commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5191) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5192) 	trans = evict_refill_and_join(root, rsv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5193) 	if (!IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5194) 		trans->block_rsv = rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5195) 		btrfs_orphan_del(trans, BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5196) 		trans->block_rsv = &fs_info->trans_block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5197) 		btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5200) 	if (!(root == fs_info->tree_root ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5201) 	      root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5202) 		btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5204) free_rsv:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5205) 	btrfs_free_block_rsv(fs_info, rsv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5206) no_delete:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5207) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5208) 	 * If we didn't successfully delete, the orphan item will still be in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5209) 	 * the tree and we'll retry on the next mount. Again, we might also want
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5210) 	 * to retry these periodically in the future.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5211) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5212) 	btrfs_remove_delayed_node(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5213) 	clear_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5216) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5217)  * Return the key found in the dir entry in the location pointer, fill @type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5218)  * with BTRFS_FT_*, and return 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5219)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5220)  * If no dir entries were found, returns -ENOENT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5221)  * If found a corrupted location in dir entry, returns -EUCLEAN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5222)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5223) static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5224) 			       struct btrfs_key *location, u8 *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5226) 	const char *name = dentry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5227) 	int namelen = dentry->d_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5228) 	struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5229) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5230) 	struct btrfs_root *root = BTRFS_I(dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5231) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5233) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5234) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5235) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5237) 	di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5238) 			name, namelen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5239) 	if (IS_ERR_OR_NULL(di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5240) 		ret = di ? PTR_ERR(di) : -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5241) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5242) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5244) 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5245) 	if (location->type != BTRFS_INODE_ITEM_KEY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5246) 	    location->type != BTRFS_ROOT_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5247) 		ret = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5248) 		btrfs_warn(root->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5249) "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5250) 			   __func__, name, btrfs_ino(BTRFS_I(dir)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5251) 			   location->objectid, location->type, location->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5252) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5253) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5254) 		*type = btrfs_dir_type(path->nodes[0], di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5255) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5256) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5257) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5260) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5261)  * when we hit a tree root in a directory, the btrfs part of the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5262)  * needs to be changed to reflect the root directory of the tree root.  This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5263)  * is kind of like crossing a mount point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5264)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5265) static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5266) 				    struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5267) 				    struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5268) 				    struct btrfs_key *location,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5269) 				    struct btrfs_root **sub_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5271) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5272) 	struct btrfs_root *new_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5273) 	struct btrfs_root_ref *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5274) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5275) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5276) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5277) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5279) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5280) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5281) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5282) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5285) 	err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5286) 	key.objectid = BTRFS_I(dir)->root->root_key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5287) 	key.type = BTRFS_ROOT_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5288) 	key.offset = location->objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5290) 	ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5291) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5292) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5293) 			err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5294) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5295) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5297) 	leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5298) 	ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5299) 	if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5300) 	    btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5301) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5303) 	ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5304) 				   (unsigned long)(ref + 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5305) 				   dentry->d_name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5306) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5307) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5309) 	btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5311) 	new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5312) 	if (IS_ERR(new_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5313) 		err = PTR_ERR(new_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5314) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5317) 	*sub_root = new_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5318) 	location->objectid = btrfs_root_dirid(&new_root->root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5319) 	location->type = BTRFS_INODE_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5320) 	location->offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5321) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5322) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5323) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5324) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5327) static void inode_tree_add(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5329) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5330) 	struct btrfs_inode *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5331) 	struct rb_node **p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5332) 	struct rb_node *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5333) 	struct rb_node *new = &BTRFS_I(inode)->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5334) 	u64 ino = btrfs_ino(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5336) 	if (inode_unhashed(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5337) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5338) 	parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5339) 	spin_lock(&root->inode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5340) 	p = &root->inode_tree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5341) 	while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5342) 		parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5343) 		entry = rb_entry(parent, struct btrfs_inode, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5345) 		if (ino < btrfs_ino(entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5346) 			p = &parent->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5347) 		else if (ino > btrfs_ino(entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5348) 			p = &parent->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5349) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5350) 			WARN_ON(!(entry->vfs_inode.i_state &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5351) 				  (I_WILL_FREE | I_FREEING)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5352) 			rb_replace_node(parent, new, &root->inode_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5353) 			RB_CLEAR_NODE(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5354) 			spin_unlock(&root->inode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5355) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5356) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5357) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5358) 	rb_link_node(new, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5359) 	rb_insert_color(new, &root->inode_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5360) 	spin_unlock(&root->inode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5363) static void inode_tree_del(struct btrfs_inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5365) 	struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5366) 	int empty = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5368) 	spin_lock(&root->inode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5369) 	if (!RB_EMPTY_NODE(&inode->rb_node)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5370) 		rb_erase(&inode->rb_node, &root->inode_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5371) 		RB_CLEAR_NODE(&inode->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5372) 		empty = RB_EMPTY_ROOT(&root->inode_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5373) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5374) 	spin_unlock(&root->inode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5376) 	if (empty && btrfs_root_refs(&root->root_item) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5377) 		spin_lock(&root->inode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5378) 		empty = RB_EMPTY_ROOT(&root->inode_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5379) 		spin_unlock(&root->inode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5380) 		if (empty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5381) 			btrfs_add_dead_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5382) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5386) static int btrfs_init_locked_inode(struct inode *inode, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5388) 	struct btrfs_iget_args *args = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5390) 	inode->i_ino = args->ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5391) 	BTRFS_I(inode)->location.objectid = args->ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5392) 	BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5393) 	BTRFS_I(inode)->location.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5394) 	BTRFS_I(inode)->root = btrfs_grab_root(args->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5395) 	BUG_ON(args->root && !BTRFS_I(inode)->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5396) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5399) static int btrfs_find_actor(struct inode *inode, void *opaque)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5401) 	struct btrfs_iget_args *args = opaque;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5403) 	return args->ino == BTRFS_I(inode)->location.objectid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5404) 		args->root == BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5407) static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5408) 				       struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5410) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5411) 	struct btrfs_iget_args args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5412) 	unsigned long hashval = btrfs_inode_hash(ino, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5414) 	args.ino = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5415) 	args.root = root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5417) 	inode = iget5_locked(s, hashval, btrfs_find_actor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5418) 			     btrfs_init_locked_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5419) 			     (void *)&args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5420) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5423) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5424)  * Get an inode object given its inode number and corresponding root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5425)  * Path can be preallocated to prevent recursing back to iget through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5426)  * allocator. NULL is also valid but may require an additional allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5427)  * later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5428)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5429) struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5430) 			      struct btrfs_root *root, struct btrfs_path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5432) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5434) 	inode = btrfs_iget_locked(s, ino, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5435) 	if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5436) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5438) 	if (inode->i_state & I_NEW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5439) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5441) 		ret = btrfs_read_locked_inode(inode, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5442) 		if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5443) 			inode_tree_add(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5444) 			unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5445) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5446) 			iget_failed(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5447) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5448) 			 * ret > 0 can come from btrfs_search_slot called by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5449) 			 * btrfs_read_locked_inode, this means the inode item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5450) 			 * was not found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5451) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5452) 			if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5453) 				ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5454) 			inode = ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5455) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5456) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5458) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5461) struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5463) 	return btrfs_iget_path(s, ino, root, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5466) static struct inode *new_simple_dir(struct super_block *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5467) 				    struct btrfs_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5468) 				    struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5470) 	struct inode *inode = new_inode(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5472) 	if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5473) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5475) 	BTRFS_I(inode)->root = btrfs_grab_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5476) 	memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5477) 	set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5479) 	inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5480) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5481) 	 * We only need lookup, the rest is read-only and there's no inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5482) 	 * associated with the dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5483) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5484) 	inode->i_op = &simple_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5485) 	inode->i_opflags &= ~IOP_XATTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5486) 	inode->i_fop = &simple_dir_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5487) 	inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5488) 	inode->i_mtime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5489) 	inode->i_atime = inode->i_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5490) 	inode->i_ctime = inode->i_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5491) 	BTRFS_I(inode)->i_otime = inode->i_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5493) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5496) static inline u8 btrfs_inode_type(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5498) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5499) 	 * Compile-time asserts that generic FT_* types still match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5500) 	 * BTRFS_FT_* types
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5501) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5502) 	BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5503) 	BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5504) 	BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5505) 	BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5506) 	BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5507) 	BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5508) 	BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5509) 	BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5511) 	return fs_umode_to_ftype(inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5514) struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5516) 	struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5517) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5518) 	struct btrfs_root *root = BTRFS_I(dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5519) 	struct btrfs_root *sub_root = root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5520) 	struct btrfs_key location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5521) 	u8 di_type = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5522) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5524) 	if (dentry->d_name.len > BTRFS_NAME_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5525) 		return ERR_PTR(-ENAMETOOLONG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5527) 	ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5528) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5529) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5531) 	if (location.type == BTRFS_INODE_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5532) 		inode = btrfs_iget(dir->i_sb, location.objectid, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5533) 		if (IS_ERR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5534) 			return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5536) 		/* Do extra check against inode mode with di_type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5537) 		if (btrfs_inode_type(inode) != di_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5538) 			btrfs_crit(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5539) "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5540) 				  inode->i_mode, btrfs_inode_type(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5541) 				  di_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5542) 			iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5543) 			return ERR_PTR(-EUCLEAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5544) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5545) 		return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5546) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5548) 	ret = fixup_tree_root_location(fs_info, dir, dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5549) 				       &location, &sub_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5550) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5551) 		if (ret != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5552) 			inode = ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5553) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5554) 			inode = new_simple_dir(dir->i_sb, &location, sub_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5555) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5556) 		inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5557) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5558) 	if (root != sub_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5559) 		btrfs_put_root(sub_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5561) 	if (!IS_ERR(inode) && root != sub_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5562) 		down_read(&fs_info->cleanup_work_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5563) 		if (!sb_rdonly(inode->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5564) 			ret = btrfs_orphan_cleanup(sub_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5565) 		up_read(&fs_info->cleanup_work_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5566) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5567) 			iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5568) 			inode = ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5569) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5572) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5575) static int btrfs_dentry_delete(const struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5577) 	struct btrfs_root *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5578) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5580) 	if (!inode && !IS_ROOT(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5581) 		inode = d_inode(dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5583) 	if (inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5584) 		root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5585) 		if (btrfs_root_refs(&root->root_item) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5586) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5588) 		if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5589) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5590) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5591) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5594) static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5595) 				   unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5596) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5597) 	struct inode *inode = btrfs_lookup_dentry(dir, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5599) 	if (inode == ERR_PTR(-ENOENT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5600) 		inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5601) 	return d_splice_alias(inode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5604) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5605)  * All this infrastructure exists because dir_emit can fault, and we are holding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5606)  * the tree lock when doing readdir.  For now just allocate a buffer and copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5607)  * our information into that, and then dir_emit from the buffer.  This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5608)  * similar to what NFS does, only we don't keep the buffer around in pagecache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5609)  * because I'm afraid I'll mess that up.  Long term we need to make filldir do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5610)  * copy_to_user_inatomic so we don't have to worry about page faulting under the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5611)  * tree lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5612)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5613) static int btrfs_opendir(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5614) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5615) 	struct btrfs_file_private *private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5617) 	private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5618) 	if (!private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5619) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5620) 	private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5621) 	if (!private->filldir_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5622) 		kfree(private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5623) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5624) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5625) 	file->private_data = private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5626) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5629) struct dir_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5630) 	u64 ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5631) 	u64 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5632) 	unsigned type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5633) 	int name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5634) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5636) static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5637) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5638) 	while (entries--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5639) 		struct dir_entry *entry = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5640) 		char *name = (char *)(entry + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5642) 		ctx->pos = get_unaligned(&entry->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5643) 		if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5644) 					 get_unaligned(&entry->ino),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5645) 					 get_unaligned(&entry->type)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5646) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5647) 		addr += sizeof(struct dir_entry) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5648) 			get_unaligned(&entry->name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5649) 		ctx->pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5650) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5651) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5654) static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5655) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5656) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5657) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5658) 	struct btrfs_file_private *private = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5659) 	struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5660) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5661) 	struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5662) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5663) 	void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5664) 	struct list_head ins_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5665) 	struct list_head del_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5666) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5667) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5668) 	int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5669) 	char *name_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5670) 	int name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5671) 	int entries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5672) 	int total_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5673) 	bool put = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5674) 	struct btrfs_key location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5676) 	if (!dir_emit_dots(file, ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5677) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5679) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5680) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5681) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5683) 	addr = private->filldir_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5684) 	path->reada = READA_FORWARD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5686) 	INIT_LIST_HEAD(&ins_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5687) 	INIT_LIST_HEAD(&del_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5688) 	put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5690) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5691) 	key.type = BTRFS_DIR_INDEX_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5692) 	key.offset = ctx->pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5693) 	key.objectid = btrfs_ino(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5695) 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5696) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5697) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5699) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5700) 		struct dir_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5702) 		leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5703) 		slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5704) 		if (slot >= btrfs_header_nritems(leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5705) 			ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5706) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5707) 				goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5708) 			else if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5709) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5710) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5711) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5713) 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5715) 		if (found_key.objectid != key.objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5716) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5717) 		if (found_key.type != BTRFS_DIR_INDEX_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5718) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5719) 		if (found_key.offset < ctx->pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5720) 			goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5721) 		if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5722) 			goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5723) 		di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5724) 		name_len = btrfs_dir_name_len(leaf, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5725) 		if ((total_len + sizeof(struct dir_entry) + name_len) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5726) 		    PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5727) 			btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5728) 			ret = btrfs_filldir(private->filldir_buf, entries, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5729) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5730) 				goto nopos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5731) 			addr = private->filldir_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5732) 			entries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5733) 			total_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5734) 			goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5735) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5737) 		entry = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5738) 		put_unaligned(name_len, &entry->name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5739) 		name_ptr = (char *)(entry + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5740) 		read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5741) 				   name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5742) 		put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5743) 				&entry->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5744) 		btrfs_dir_item_key_to_cpu(leaf, di, &location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5745) 		put_unaligned(location.objectid, &entry->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5746) 		put_unaligned(found_key.offset, &entry->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5747) 		entries++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5748) 		addr += sizeof(struct dir_entry) + name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5749) 		total_len += sizeof(struct dir_entry) + name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5750) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5751) 		path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5752) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5753) 	btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5755) 	ret = btrfs_filldir(private->filldir_buf, entries, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5756) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5757) 		goto nopos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5759) 	ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5760) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5761) 		goto nopos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5763) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5764) 	 * Stop new entries from being returned after we return the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5765) 	 * entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5766) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5767) 	 * New directory entries are assigned a strictly increasing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5768) 	 * offset.  This means that new entries created during readdir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5769) 	 * are *guaranteed* to be seen in the future by that readdir.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5770) 	 * This has broken buggy programs which operate on names as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5771) 	 * they're returned by readdir.  Until we re-use freed offsets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5772) 	 * we have this hack to stop new entries from being returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5773) 	 * under the assumption that they'll never reach this huge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5774) 	 * offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5775) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5776) 	 * This is being careful not to overflow 32bit loff_t unless the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5777) 	 * last entry requires it because doing so has broken 32bit apps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5778) 	 * in the past.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5779) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5780) 	if (ctx->pos >= INT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5781) 		ctx->pos = LLONG_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5782) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5783) 		ctx->pos = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5784) nopos:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5785) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5786) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5787) 	if (put)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5788) 		btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5789) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5790) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5793) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5794)  * This is somewhat expensive, updating the tree every time the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5795)  * inode changes.  But, it is most likely to find the inode in cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5796)  * FIXME, needs more benchmarking...there are no reasons other than performance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5797)  * to keep or drop this code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5798)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5799) static int btrfs_dirty_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5800) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5801) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5802) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5803) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5804) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5806) 	if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5807) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5809) 	trans = btrfs_join_transaction(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5810) 	if (IS_ERR(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5811) 		return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5813) 	ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5814) 	if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5815) 		/* whoops, lets try again with the full transaction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5816) 		btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5817) 		trans = btrfs_start_transaction(root, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5818) 		if (IS_ERR(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5819) 			return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5821) 		ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5822) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5823) 	btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5824) 	if (BTRFS_I(inode)->delayed_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5825) 		btrfs_balance_delayed_items(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5827) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5828) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5830) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5831)  * This is a copy of file_update_time.  We need this so we can return error on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5832)  * ENOSPC for updating the inode in the case of file write and mmap writes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5833)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5834) static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5835) 			     int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5836) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5837) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5838) 	bool dirty = flags & ~S_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5840) 	if (btrfs_root_readonly(root))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5841) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5843) 	if (flags & S_VERSION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5844) 		dirty |= inode_maybe_inc_iversion(inode, dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5845) 	if (flags & S_CTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5846) 		inode->i_ctime = *now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5847) 	if (flags & S_MTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5848) 		inode->i_mtime = *now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5849) 	if (flags & S_ATIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5850) 		inode->i_atime = *now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5851) 	return dirty ? btrfs_dirty_inode(inode) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5854) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5855)  * find the highest existing sequence number in a directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5856)  * and then set the in-memory index_cnt variable to reflect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5857)  * free sequence numbers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5858)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5859) static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5860) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5861) 	struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5862) 	struct btrfs_key key, found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5863) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5864) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5865) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5867) 	key.objectid = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5868) 	key.type = BTRFS_DIR_INDEX_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5869) 	key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5871) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5872) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5873) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5875) 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5876) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5877) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5878) 	/* FIXME: we should be able to handle this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5879) 	if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5880) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5881) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5883) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5884) 	 * MAGIC NUMBER EXPLANATION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5885) 	 * since we search a directory based on f_pos we have to start at 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5886) 	 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5887) 	 * else has to start at 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5888) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5889) 	if (path->slots[0] == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5890) 		inode->index_cnt = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5891) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5892) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5894) 	path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5896) 	leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5897) 	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5899) 	if (found_key.objectid != btrfs_ino(inode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5900) 	    found_key.type != BTRFS_DIR_INDEX_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5901) 		inode->index_cnt = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5902) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5903) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5905) 	inode->index_cnt = found_key.offset + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5906) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5907) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5908) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5909) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5911) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5912)  * helper to find a free sequence number in a given directory.  This current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5913)  * code is very simple, later versions will do smarter things in the btree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5914)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5915) int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5916) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5917) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5919) 	if (dir->index_cnt == (u64)-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5920) 		ret = btrfs_inode_delayed_dir_index_count(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5921) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5922) 			ret = btrfs_set_inode_index_count(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5923) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5924) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5925) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5926) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5928) 	*index = dir->index_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5929) 	dir->index_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5931) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5932) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5934) static int btrfs_insert_inode_locked(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5935) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5936) 	struct btrfs_iget_args args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5938) 	args.ino = BTRFS_I(inode)->location.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5939) 	args.root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5941) 	return insert_inode_locked4(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5942) 		   btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5943) 		   btrfs_find_actor, &args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5946) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5947)  * Inherit flags from the parent inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5948)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5949)  * Currently only the compression flags and the cow flags are inherited.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5950)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5951) static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5952) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5953) 	unsigned int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5955) 	if (!dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5956) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5958) 	flags = BTRFS_I(dir)->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5960) 	if (flags & BTRFS_INODE_NOCOMPRESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5961) 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5962) 		BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5963) 	} else if (flags & BTRFS_INODE_COMPRESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5964) 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5965) 		BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5966) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5968) 	if (flags & BTRFS_INODE_NODATACOW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5969) 		BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5970) 		if (S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5971) 			BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5972) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5974) 	btrfs_sync_inode_flags_to_i_flags(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5977) static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5978) 				     struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5979) 				     struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5980) 				     const char *name, int name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5981) 				     u64 ref_objectid, u64 objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5982) 				     umode_t mode, u64 *index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5983) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5984) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5985) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5986) 	struct btrfs_inode_item *inode_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5987) 	struct btrfs_key *location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5988) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5989) 	struct btrfs_inode_ref *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5990) 	struct btrfs_key key[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5991) 	u32 sizes[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5992) 	int nitems = name ? 2 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5993) 	unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5994) 	unsigned int nofs_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5995) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5997) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5998) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5999) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6001) 	nofs_flag = memalloc_nofs_save();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6002) 	inode = new_inode(fs_info->sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6003) 	memalloc_nofs_restore(nofs_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6004) 	if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6005) 		btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6006) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6007) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6009) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6010) 	 * O_TMPFILE, set link count to 0, so that after this point,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6011) 	 * we fill in an inode item with the correct link count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6012) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6013) 	if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6014) 		set_nlink(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6016) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6017) 	 * we have to initialize this early, so we can reclaim the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6018) 	 * number if we fail afterwards in this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6019) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6020) 	inode->i_ino = objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6022) 	if (dir && name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6023) 		trace_btrfs_inode_request(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6025) 		ret = btrfs_set_inode_index(BTRFS_I(dir), index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6026) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6027) 			btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6028) 			iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6029) 			return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6030) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6031) 	} else if (dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6032) 		*index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6033) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6034) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6035) 	 * index_cnt is ignored for everything but a dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6036) 	 * btrfs_set_inode_index_count has an explanation for the magic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6037) 	 * number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6038) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6039) 	BTRFS_I(inode)->index_cnt = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6040) 	BTRFS_I(inode)->dir_index = *index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6041) 	BTRFS_I(inode)->root = btrfs_grab_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6042) 	BTRFS_I(inode)->generation = trans->transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6043) 	inode->i_generation = BTRFS_I(inode)->generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6045) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6046) 	 * We could have gotten an inode number from somebody who was fsynced
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6047) 	 * and then removed in this same transaction, so let's just set full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6048) 	 * sync since it will be a full sync anyway and this will blow away the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6049) 	 * old info in the log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6050) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6051) 	set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6053) 	key[0].objectid = objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6054) 	key[0].type = BTRFS_INODE_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6055) 	key[0].offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6057) 	sizes[0] = sizeof(struct btrfs_inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6059) 	if (name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6060) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6061) 		 * Start new inodes with an inode_ref. This is slightly more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6062) 		 * efficient for small numbers of hard links since they will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6063) 		 * be packed into one item. Extended refs will kick in if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6064) 		 * add more hard links than can fit in the ref item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6065) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6066) 		key[1].objectid = objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6067) 		key[1].type = BTRFS_INODE_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6068) 		key[1].offset = ref_objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6070) 		sizes[1] = name_len + sizeof(*ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6071) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6073) 	location = &BTRFS_I(inode)->location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6074) 	location->objectid = objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6075) 	location->offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6076) 	location->type = BTRFS_INODE_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6078) 	ret = btrfs_insert_inode_locked(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6079) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6080) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6081) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6082) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6084) 	path->leave_spinning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6085) 	ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6086) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6087) 		goto fail_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6089) 	inode_init_owner(inode, dir, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6090) 	inode_set_bytes(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6092) 	inode->i_mtime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6093) 	inode->i_atime = inode->i_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6094) 	inode->i_ctime = inode->i_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6095) 	BTRFS_I(inode)->i_otime = inode->i_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6097) 	inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6098) 				  struct btrfs_inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6099) 	memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6100) 			     sizeof(*inode_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6101) 	fill_inode_item(trans, path->nodes[0], inode_item, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6103) 	if (name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6104) 		ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6105) 				     struct btrfs_inode_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6106) 		btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6107) 		btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6108) 		ptr = (unsigned long)(ref + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6109) 		write_extent_buffer(path->nodes[0], name, ptr, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6112) 	btrfs_mark_buffer_dirty(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6113) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6115) 	btrfs_inherit_iflags(inode, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6117) 	if (S_ISREG(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6118) 		if (btrfs_test_opt(fs_info, NODATASUM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6119) 			BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6120) 		if (btrfs_test_opt(fs_info, NODATACOW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6121) 			BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6122) 				BTRFS_INODE_NODATASUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6123) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6125) 	inode_tree_add(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6127) 	trace_btrfs_inode_new(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6128) 	btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6130) 	btrfs_update_root_times(trans, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6132) 	ret = btrfs_inode_inherit_props(trans, inode, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6133) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6134) 		btrfs_err(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6135) 			  "error inheriting props for ino %llu (root %llu): %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6136) 			btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6138) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6140) fail_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6141) 	discard_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6142) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6143) 	if (dir && name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6144) 		BTRFS_I(dir)->index_cnt--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6145) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6146) 	return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6149) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6150)  * utility function to add 'inode' into 'parent_inode' with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6151)  * a give name and a given sequence number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6152)  * if 'add_backref' is true, also insert a backref from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6153)  * inode to the parent directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6154)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6155) int btrfs_add_link(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6156) 		   struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6157) 		   const char *name, int name_len, int add_backref, u64 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6159) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6160) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6161) 	struct btrfs_root *root = parent_inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6162) 	u64 ino = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6163) 	u64 parent_ino = btrfs_ino(parent_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6165) 	if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6166) 		memcpy(&key, &inode->root->root_key, sizeof(key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6167) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6168) 		key.objectid = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6169) 		key.type = BTRFS_INODE_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6170) 		key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6173) 	if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6174) 		ret = btrfs_add_root_ref(trans, key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6175) 					 root->root_key.objectid, parent_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6176) 					 index, name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6177) 	} else if (add_backref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6178) 		ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6179) 					     parent_ino, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6182) 	/* Nothing to clean up yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6183) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6184) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6186) 	ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6187) 				    btrfs_inode_type(&inode->vfs_inode), index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6188) 	if (ret == -EEXIST || ret == -EOVERFLOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6189) 		goto fail_dir_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6190) 	else if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6191) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6192) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6193) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6195) 	btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6196) 			   name_len * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6197) 	inode_inc_iversion(&parent_inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6198) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6199) 	 * If we are replaying a log tree, we do not want to update the mtime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6200) 	 * and ctime of the parent directory with the current time, since the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6201) 	 * log replay procedure is responsible for setting them to their correct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6202) 	 * values (the ones it had when the fsync was done).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6203) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6204) 	if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6205) 		struct timespec64 now = current_time(&parent_inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6207) 		parent_inode->vfs_inode.i_mtime = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6208) 		parent_inode->vfs_inode.i_ctime = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6209) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6210) 	ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6211) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6212) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6213) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6215) fail_dir_item:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6216) 	if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6217) 		u64 local_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6218) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6219) 		err = btrfs_del_root_ref(trans, key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6220) 					 root->root_key.objectid, parent_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6221) 					 &local_index, name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6222) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6223) 			btrfs_abort_transaction(trans, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6224) 	} else if (add_backref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6225) 		u64 local_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6226) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6228) 		err = btrfs_del_inode_ref(trans, root, name, name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6229) 					  ino, parent_ino, &local_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6230) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6231) 			btrfs_abort_transaction(trans, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6234) 	/* Return the original error code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6235) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6238) static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6239) 			    struct btrfs_inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6240) 			    struct btrfs_inode *inode, int backref, u64 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6242) 	int err = btrfs_add_link(trans, dir, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6243) 				 dentry->d_name.name, dentry->d_name.len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6244) 				 backref, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6245) 	if (err > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6246) 		err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6247) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6250) static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6251) 			umode_t mode, dev_t rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6253) 	struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6254) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6255) 	struct btrfs_root *root = BTRFS_I(dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6256) 	struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6257) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6258) 	u64 objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6259) 	u64 index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6261) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6262) 	 * 2 for inode item and ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6263) 	 * 2 for dir items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6264) 	 * 1 for xattr if selinux is on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6265) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6266) 	trans = btrfs_start_transaction(root, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6267) 	if (IS_ERR(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6268) 		return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6270) 	err = btrfs_find_free_ino(root, &objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6271) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6272) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6274) 	inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6275) 			dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6276) 			mode, &index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6277) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6278) 		err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6279) 		inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6280) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6281) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6283) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6284) 	* If the active LSM wants to access the inode during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6285) 	* d_instantiate it needs these. Smack checks to see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6286) 	* if the filesystem supports xattrs by looking at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6287) 	* ops vector.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6288) 	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6289) 	inode->i_op = &btrfs_special_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6290) 	init_special_inode(inode, inode->i_mode, rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6292) 	err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6293) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6294) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6296) 	err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6297) 			0, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6298) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6299) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6301) 	btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6302) 	d_instantiate_new(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6304) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6305) 	btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6306) 	btrfs_btree_balance_dirty(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6307) 	if (err && inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6308) 		inode_dec_link_count(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6309) 		discard_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6311) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6314) static int btrfs_create(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6315) 			umode_t mode, bool excl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6317) 	struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6318) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6319) 	struct btrfs_root *root = BTRFS_I(dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6320) 	struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6321) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6322) 	u64 objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6323) 	u64 index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6325) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6326) 	 * 2 for inode item and ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6327) 	 * 2 for dir items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6328) 	 * 1 for xattr if selinux is on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6329) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6330) 	trans = btrfs_start_transaction(root, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6331) 	if (IS_ERR(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6332) 		return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6334) 	err = btrfs_find_free_ino(root, &objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6335) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6336) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6338) 	inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6339) 			dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6340) 			mode, &index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6341) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6342) 		err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6343) 		inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6344) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6346) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6347) 	* If the active LSM wants to access the inode during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6348) 	* d_instantiate it needs these. Smack checks to see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6349) 	* if the filesystem supports xattrs by looking at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6350) 	* ops vector.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6351) 	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6352) 	inode->i_fop = &btrfs_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6353) 	inode->i_op = &btrfs_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6354) 	inode->i_mapping->a_ops = &btrfs_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6356) 	err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6357) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6358) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6360) 	err = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6361) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6362) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6364) 	err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6365) 			0, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6366) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6367) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6369) 	d_instantiate_new(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6371) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6372) 	btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6373) 	if (err && inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6374) 		inode_dec_link_count(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6375) 		discard_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6376) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6377) 	btrfs_btree_balance_dirty(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6378) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6381) static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6382) 		      struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6384) 	struct btrfs_trans_handle *trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6385) 	struct btrfs_root *root = BTRFS_I(dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6386) 	struct inode *inode = d_inode(old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6387) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6388) 	u64 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6389) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6390) 	int drop_inode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6392) 	/* do not allow sys_link's with other subvols of the same device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6393) 	if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6394) 		return -EXDEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6396) 	if (inode->i_nlink >= BTRFS_LINK_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6397) 		return -EMLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6399) 	err = btrfs_set_inode_index(BTRFS_I(dir), &index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6400) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6401) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6403) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6404) 	 * 2 items for inode and inode ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6405) 	 * 2 items for dir items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6406) 	 * 1 item for parent inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6407) 	 * 1 item for orphan item deletion if O_TMPFILE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6408) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6409) 	trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6410) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6411) 		err = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6412) 		trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6413) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6414) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6416) 	/* There are several dir indexes for this inode, clear the cache. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6417) 	BTRFS_I(inode)->dir_index = 0ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6418) 	inc_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6419) 	inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6420) 	inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6421) 	ihold(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6422) 	set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6424) 	err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6425) 			1, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6427) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6428) 		drop_inode = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6429) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6430) 		struct dentry *parent = dentry->d_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6432) 		err = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6433) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6434) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6435) 		if (inode->i_nlink == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6436) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6437) 			 * If new hard link count is 1, it's a file created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6438) 			 * with open(2) O_TMPFILE flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6439) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6440) 			err = btrfs_orphan_del(trans, BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6441) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6442) 				goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6443) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6444) 		d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6445) 		btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6446) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6448) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6449) 	if (trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6450) 		btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6451) 	if (drop_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6452) 		inode_dec_link_count(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6453) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6454) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6455) 	btrfs_btree_balance_dirty(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6456) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6459) static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6461) 	struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6462) 	struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6463) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6464) 	struct btrfs_root *root = BTRFS_I(dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6465) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6466) 	u64 objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6467) 	u64 index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6469) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6470) 	 * 2 items for inode and ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6471) 	 * 2 items for dir items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6472) 	 * 1 for xattr if selinux is on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6473) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6474) 	trans = btrfs_start_transaction(root, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6475) 	if (IS_ERR(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6476) 		return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6478) 	err = btrfs_find_free_ino(root, &objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6479) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6480) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6482) 	inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6483) 			dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6484) 			S_IFDIR | mode, &index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6485) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6486) 		err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6487) 		inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6488) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6489) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6491) 	/* these must be set before we unlock the inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6492) 	inode->i_op = &btrfs_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6493) 	inode->i_fop = &btrfs_dir_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6495) 	err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6496) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6497) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6499) 	btrfs_i_size_write(BTRFS_I(inode), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6500) 	err = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6501) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6502) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6504) 	err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6505) 			dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6506) 			dentry->d_name.len, 0, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6507) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6508) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6510) 	d_instantiate_new(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6512) out_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6513) 	btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6514) 	if (err && inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6515) 		inode_dec_link_count(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6516) 		discard_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6517) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6518) 	btrfs_btree_balance_dirty(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6519) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6522) static noinline int uncompress_inline(struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6523) 				      struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6524) 				      size_t pg_offset, u64 extent_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6525) 				      struct btrfs_file_extent_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6527) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6528) 	struct extent_buffer *leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6529) 	char *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6530) 	size_t max_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6531) 	unsigned long inline_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6532) 	unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6533) 	int compress_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6535) 	WARN_ON(pg_offset != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6536) 	compress_type = btrfs_file_extent_compression(leaf, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6537) 	max_size = btrfs_file_extent_ram_bytes(leaf, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6538) 	inline_size = btrfs_file_extent_inline_item_len(leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6539) 					btrfs_item_nr(path->slots[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6540) 	tmp = kmalloc(inline_size, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6541) 	if (!tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6542) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6543) 	ptr = btrfs_file_extent_inline_start(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6545) 	read_extent_buffer(leaf, tmp, ptr, inline_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6547) 	max_size = min_t(unsigned long, PAGE_SIZE, max_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6548) 	ret = btrfs_decompress(compress_type, tmp, page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6549) 			       extent_offset, inline_size, max_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6551) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6552) 	 * decompression code contains a memset to fill in any space between the end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6553) 	 * of the uncompressed data and the end of max_size in case the decompressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6554) 	 * data ends up shorter than ram_bytes.  That doesn't cover the hole between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6555) 	 * the end of an inline extent and the beginning of the next block, so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6556) 	 * cover that region here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6557) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6559) 	if (max_size + pg_offset < PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6560) 		char *map = kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6561) 		memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6562) 		kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6563) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6564) 	kfree(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6565) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6568) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6569)  * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6570)  * @inode:	file to search in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6571)  * @page:	page to read extent data into if the extent is inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6572)  * @pg_offset:	offset into @page to copy to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6573)  * @start:	file offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6574)  * @len:	length of range starting at @start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6575)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6576)  * This returns the first &struct extent_map which overlaps with the given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6577)  * range, reading it from the B-tree and caching it if necessary. Note that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6578)  * there may be more extents which overlap the given range after the returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6579)  * extent_map.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6580)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6581)  * If @page is not NULL and the extent is inline, this also reads the extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6582)  * data directly into the page and marks the extent up to date in the io_tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6583)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6584)  * Return: ERR_PTR on error, non-NULL extent_map on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6585)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6586) struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6587) 				    struct page *page, size_t pg_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6588) 				    u64 start, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6590) 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6591) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6592) 	u64 extent_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6593) 	u64 extent_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6594) 	u64 objectid = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6595) 	int extent_type = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6596) 	struct btrfs_path *path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6597) 	struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6598) 	struct btrfs_file_extent_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6599) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6600) 	struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6601) 	struct extent_map *em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6602) 	struct extent_map_tree *em_tree = &inode->extent_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6603) 	struct extent_io_tree *io_tree = &inode->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6605) 	read_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6606) 	em = lookup_extent_mapping(em_tree, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6607) 	read_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6609) 	if (em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6610) 		if (em->start > start || em->start + em->len <= start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6611) 			free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6612) 		else if (em->block_start == EXTENT_MAP_INLINE && page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6613) 			free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6614) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6615) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6616) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6617) 	em = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6618) 	if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6619) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6620) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6622) 	em->start = EXTENT_MAP_HOLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6623) 	em->orig_start = EXTENT_MAP_HOLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6624) 	em->len = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6625) 	em->block_len = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6627) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6628) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6629) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6630) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6631) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6633) 	/* Chances are we'll be called again, so go ahead and do readahead */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6634) 	path->reada = READA_FORWARD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6636) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6637) 	 * Unless we're going to uncompress the inline extent, no sleep would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6638) 	 * happen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6639) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6640) 	path->leave_spinning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6642) 	path->recurse = btrfs_is_free_space_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6644) 	ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6645) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6646) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6647) 	} else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6648) 		if (path->slots[0] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6649) 			goto not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6650) 		path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6651) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6652) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6654) 	leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6655) 	item = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6656) 			      struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6657) 	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6658) 	if (found_key.objectid != objectid ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6659) 	    found_key.type != BTRFS_EXTENT_DATA_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6660) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6661) 		 * If we backup past the first extent we want to move forward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6662) 		 * and see if there is an extent in front of us, otherwise we'll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6663) 		 * say there is a hole for our whole search range which can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6664) 		 * cause problems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6665) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6666) 		extent_end = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6667) 		goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6668) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6670) 	extent_type = btrfs_file_extent_type(leaf, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6671) 	extent_start = found_key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6672) 	extent_end = btrfs_file_extent_end(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6673) 	if (extent_type == BTRFS_FILE_EXTENT_REG ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6674) 	    extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6675) 		/* Only regular file could have regular/prealloc extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6676) 		if (!S_ISREG(inode->vfs_inode.i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6677) 			ret = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6678) 			btrfs_crit(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6679) 		"regular/prealloc extent found for non-regular inode %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6680) 				   btrfs_ino(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6681) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6682) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6683) 		trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6684) 						       extent_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6685) 	} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6686) 		trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6687) 						      path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6688) 						      extent_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6689) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6690) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6691) 	if (start >= extent_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6692) 		path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6693) 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6694) 			ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6695) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6696) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6697) 			else if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6698) 				goto not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6700) 			leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6701) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6702) 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6703) 		if (found_key.objectid != objectid ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6704) 		    found_key.type != BTRFS_EXTENT_DATA_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6705) 			goto not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6706) 		if (start + len <= found_key.offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6707) 			goto not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6708) 		if (start > found_key.offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6709) 			goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6711) 		/* New extent overlaps with existing one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6712) 		em->start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6713) 		em->orig_start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6714) 		em->len = found_key.offset - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6715) 		em->block_start = EXTENT_MAP_HOLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6716) 		goto insert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6717) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6719) 	btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6721) 	if (extent_type == BTRFS_FILE_EXTENT_REG ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6722) 	    extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6723) 		goto insert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6724) 	} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6725) 		unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6726) 		char *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6727) 		size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6728) 		size_t extent_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6729) 		size_t copy_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6731) 		if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6732) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6734) 		size = btrfs_file_extent_ram_bytes(leaf, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6735) 		extent_offset = page_offset(page) + pg_offset - extent_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6736) 		copy_size = min_t(u64, PAGE_SIZE - pg_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6737) 				  size - extent_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6738) 		em->start = extent_start + extent_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6739) 		em->len = ALIGN(copy_size, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6740) 		em->orig_block_len = em->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6741) 		em->orig_start = em->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6742) 		ptr = btrfs_file_extent_inline_start(item) + extent_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6744) 		btrfs_set_path_blocking(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6745) 		if (!PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6746) 			if (btrfs_file_extent_compression(leaf, item) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6747) 			    BTRFS_COMPRESS_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6748) 				ret = uncompress_inline(path, page, pg_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6749) 							extent_offset, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6750) 				if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6751) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6752) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6753) 				map = kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6754) 				read_extent_buffer(leaf, map + pg_offset, ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6755) 						   copy_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6756) 				if (pg_offset + copy_size < PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6757) 					memset(map + pg_offset + copy_size, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6758) 					       PAGE_SIZE - pg_offset -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6759) 					       copy_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6760) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6761) 				kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6762) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6763) 			flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6764) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6765) 		set_extent_uptodate(io_tree, em->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6766) 				    extent_map_end(em) - 1, NULL, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6767) 		goto insert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6768) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6769) not_found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6770) 	em->start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6771) 	em->orig_start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6772) 	em->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6773) 	em->block_start = EXTENT_MAP_HOLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6774) insert:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6775) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6776) 	btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6777) 	if (em->start > start || extent_map_end(em) <= start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6778) 		btrfs_err(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6779) 			  "bad extent! em: [%llu %llu] passed [%llu %llu]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6780) 			  em->start, em->len, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6781) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6782) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6783) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6785) 	write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6786) 	ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6787) 	write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6788) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6789) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6791) 	trace_btrfs_get_extent(root, inode, em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6793) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6794) 		free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6795) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6796) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6797) 	return em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6800) struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6801) 					   u64 start, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6802) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6803) 	struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6804) 	struct extent_map *hole_em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6805) 	u64 delalloc_start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6806) 	u64 end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6807) 	u64 delalloc_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6808) 	u64 delalloc_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6809) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6811) 	em = btrfs_get_extent(inode, NULL, 0, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6812) 	if (IS_ERR(em))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6813) 		return em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6814) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6815) 	 * If our em maps to:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6816) 	 * - a hole or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6817) 	 * - a pre-alloc extent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6818) 	 * there might actually be delalloc bytes behind it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6819) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6820) 	if (em->block_start != EXTENT_MAP_HOLE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6821) 	    !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6822) 		return em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6823) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6824) 		hole_em = em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6826) 	/* check to see if we've wrapped (len == -1 or similar) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6827) 	end = start + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6828) 	if (end < start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6829) 		end = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6830) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6831) 		end -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6833) 	em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6835) 	/* ok, we didn't find anything, lets look for delalloc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6836) 	delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6837) 				 end, len, EXTENT_DELALLOC, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6838) 	delalloc_end = delalloc_start + delalloc_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6839) 	if (delalloc_end < delalloc_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6840) 		delalloc_end = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6842) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6843) 	 * We didn't find anything useful, return the original results from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6844) 	 * get_extent()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6845) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6846) 	if (delalloc_start > end || delalloc_end <= start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6847) 		em = hole_em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6848) 		hole_em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6849) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6850) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6852) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6853) 	 * Adjust the delalloc_start to make sure it doesn't go backwards from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6854) 	 * the start they passed in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6855) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6856) 	delalloc_start = max(start, delalloc_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6857) 	delalloc_len = delalloc_end - delalloc_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6859) 	if (delalloc_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6860) 		u64 hole_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6861) 		u64 hole_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6862) 		const u64 hole_end = extent_map_end(hole_em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6864) 		em = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6865) 		if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6866) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6867) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6868) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6870) 		ASSERT(hole_em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6871) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6872) 		 * When btrfs_get_extent can't find anything it returns one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6873) 		 * huge hole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6874) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6875) 		 * Make sure what it found really fits our range, and adjust to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6876) 		 * make sure it is based on the start from the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6877) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6878) 		if (hole_end <= start || hole_em->start > end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6879) 		       free_extent_map(hole_em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6880) 		       hole_em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6881) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6882) 		       hole_start = max(hole_em->start, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6883) 		       hole_len = hole_end - hole_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6884) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6886) 		if (hole_em && delalloc_start > hole_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6887) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6888) 			 * Our hole starts before our delalloc, so we have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6889) 			 * return just the parts of the hole that go until the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6890) 			 * delalloc starts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6891) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6892) 			em->len = min(hole_len, delalloc_start - hole_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6893) 			em->start = hole_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6894) 			em->orig_start = hole_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6895) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6896) 			 * Don't adjust block start at all, it is fixed at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6897) 			 * EXTENT_MAP_HOLE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6898) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6899) 			em->block_start = hole_em->block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6900) 			em->block_len = hole_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6901) 			if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6902) 				set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6903) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6904) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6905) 			 * Hole is out of passed range or it starts after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6906) 			 * delalloc range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6907) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6908) 			em->start = delalloc_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6909) 			em->len = delalloc_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6910) 			em->orig_start = delalloc_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6911) 			em->block_start = EXTENT_MAP_DELALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6912) 			em->block_len = delalloc_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6913) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6914) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6915) 		return hole_em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6916) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6917) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6919) 	free_extent_map(hole_em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6920) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6921) 		free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6922) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6923) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6924) 	return em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6927) static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6928) 						  const u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6929) 						  const u64 len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6930) 						  const u64 orig_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6931) 						  const u64 block_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6932) 						  const u64 block_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6933) 						  const u64 orig_block_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6934) 						  const u64 ram_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6935) 						  const int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6936) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6937) 	struct extent_map *em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6938) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6940) 	if (type != BTRFS_ORDERED_NOCOW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6941) 		em = create_io_em(inode, start, len, orig_start, block_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6942) 				  block_len, orig_block_len, ram_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6943) 				  BTRFS_COMPRESS_NONE, /* compress_type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6944) 				  type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6945) 		if (IS_ERR(em))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6946) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6947) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6948) 	ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6949) 					   block_len, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6950) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6951) 		if (em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6952) 			free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6953) 			btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6954) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6955) 		em = ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6956) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6957)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6959) 	return em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6960) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6962) static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6963) 						  u64 start, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6964) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6965) 	struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6966) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6967) 	struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6968) 	struct btrfs_key ins;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6969) 	u64 alloc_hint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6970) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6972) 	alloc_hint = get_extent_allocation_hint(inode, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6973) 	ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6974) 				   0, alloc_hint, &ins, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6975) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6976) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6978) 	em = btrfs_create_dio_extent(inode, start, ins.offset, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6979) 				     ins.objectid, ins.offset, ins.offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6980) 				     ins.offset, BTRFS_ORDERED_REGULAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6981) 	btrfs_dec_block_group_reservations(fs_info, ins.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6982) 	if (IS_ERR(em))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6983) 		btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6984) 					   1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6986) 	return em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6989) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6990)  * Check if we can do nocow write into the range [@offset, @offset + @len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6991)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6992)  * @offset:	File offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6993)  * @len:	The length to write, will be updated to the nocow writeable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6994)  *		range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6995)  * @orig_start:	(optional) Return the original file offset of the file extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6996)  * @orig_len:	(optional) Return the original on-disk length of the file extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6997)  * @ram_bytes:	(optional) Return the ram_bytes of the file extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6998)  * @strict:	if true, omit optimizations that might force us into unnecessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6999)  *		cow. e.g., don't trust generation number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7000)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7001)  * This function will flush ordered extents in the range to ensure proper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7002)  * nocow checks for (nowait == false) case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7003)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7004)  * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7005)  * >0	and update @len if we can do nocow write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7006)  *  0	if we can't do nocow write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7007)  * <0	if error happened
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7008)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7009)  * NOTE: This only checks the file extents, caller is responsible to wait for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7010)  *	 any ordered extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7011)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7012) noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7013) 			      u64 *orig_start, u64 *orig_block_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7014) 			      u64 *ram_bytes, bool strict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7015) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7016) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7017) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7018) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7019) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7020) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7021) 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7022) 	struct btrfs_file_extent_item *fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7023) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7024) 	u64 disk_bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7025) 	u64 backref_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7026) 	u64 extent_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7027) 	u64 num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7028) 	int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7029) 	int found_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7030) 	bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7032) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7033) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7034) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7036) 	ret = btrfs_lookup_file_extent(NULL, root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7037) 			btrfs_ino(BTRFS_I(inode)), offset, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7038) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7039) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7041) 	slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7042) 	if (ret == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7043) 		if (slot == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7044) 			/* can't find the item, must cow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7045) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7046) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7047) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7048) 		slot--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7049) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7050) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7051) 	leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7052) 	btrfs_item_key_to_cpu(leaf, &key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7053) 	if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7054) 	    key.type != BTRFS_EXTENT_DATA_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7055) 		/* not our file or wrong item type, must cow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7056) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7057) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7059) 	if (key.offset > offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7060) 		/* Wrong offset, must cow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7061) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7062) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7064) 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7065) 	found_type = btrfs_file_extent_type(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7066) 	if (found_type != BTRFS_FILE_EXTENT_REG &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7067) 	    found_type != BTRFS_FILE_EXTENT_PREALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7068) 		/* not a regular extent, must cow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7069) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7070) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7072) 	if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7073) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7075) 	extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7076) 	if (extent_end <= offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7077) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7079) 	disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7080) 	if (disk_bytenr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7081) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7083) 	if (btrfs_file_extent_compression(leaf, fi) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7084) 	    btrfs_file_extent_encryption(leaf, fi) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7085) 	    btrfs_file_extent_other_encoding(leaf, fi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7086) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7088) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7089) 	 * Do the same check as in btrfs_cross_ref_exist but without the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7090) 	 * unnecessary search.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7091) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7092) 	if (!strict &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7093) 	    (btrfs_file_extent_generation(leaf, fi) <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7094) 	     btrfs_root_last_snapshot(&root->root_item)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7095) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7097) 	backref_offset = btrfs_file_extent_offset(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7099) 	if (orig_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7100) 		*orig_start = key.offset - backref_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7101) 		*orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7102) 		*ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7103) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7105) 	if (btrfs_extent_readonly(fs_info, disk_bytenr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7106) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7108) 	num_bytes = min(offset + *len, extent_end) - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7109) 	if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7110) 		u64 range_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7112) 		range_end = round_up(offset + num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7113) 				     root->fs_info->sectorsize) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7114) 		ret = test_range_bit(io_tree, offset, range_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7115) 				     EXTENT_DELALLOC, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7116) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7117) 			ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7118) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7119) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7122) 	btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7124) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7125) 	 * look for other files referencing this extent, if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7126) 	 * find any we must cow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7127) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7129) 	ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7130) 				    key.offset - backref_offset, disk_bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7131) 				    strict);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7132) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7133) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7134) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7137) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7138) 	 * adjust disk_bytenr and num_bytes to cover just the bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7139) 	 * in this extent we are about to write.  If there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7140) 	 * are any csums in that range we have to cow in order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7141) 	 * to keep the csums correct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7142) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7143) 	disk_bytenr += backref_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7144) 	disk_bytenr += offset - key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7145) 	if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7146) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7147) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7148) 	 * all of the above have passed, it is safe to overwrite this extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7149) 	 * without cow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7150) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7151) 	*len = num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7152) 	ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7153) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7154) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7155) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7158) static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7159) 			      struct extent_state **cached_state, bool writing)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7161) 	struct btrfs_ordered_extent *ordered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7162) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7164) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7165) 		lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7166) 				 cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7167) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7168) 		 * We're concerned with the entire range that we're going to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7169) 		 * doing DIO to, so we need to make sure there's no ordered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7170) 		 * extents in this range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7171) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7172) 		ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7173) 						     lockend - lockstart + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7175) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7176) 		 * We need to make sure there are no buffered pages in this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7177) 		 * range either, we could have raced between the invalidate in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7178) 		 * generic_file_direct_write and locking the extent.  The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7179) 		 * invalidate needs to happen so that reads after a write do not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7180) 		 * get stale data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7181) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7182) 		if (!ordered &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7183) 		    (!writing || !filemap_range_has_page(inode->i_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7184) 							 lockstart, lockend)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7185) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7187) 		unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7188) 				     cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7190) 		if (ordered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7191) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7192) 			 * If we are doing a DIO read and the ordered extent we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7193) 			 * found is for a buffered write, we can not wait for it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7194) 			 * to complete and retry, because if we do so we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7195) 			 * deadlock with concurrent buffered writes on page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7196) 			 * locks. This happens only if our DIO read covers more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7197) 			 * than one extent map, if at this point has already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7198) 			 * created an ordered extent for a previous extent map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7199) 			 * and locked its range in the inode's io tree, and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7200) 			 * concurrent write against that previous extent map's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7201) 			 * range and this range started (we unlock the ranges
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7202) 			 * in the io tree only when the bios complete and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7203) 			 * buffered writes always lock pages before attempting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7204) 			 * to lock range in the io tree).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7205) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7206) 			if (writing ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7207) 			    test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7208) 				btrfs_start_ordered_extent(ordered, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7209) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7210) 				ret = -ENOTBLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7211) 			btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7212) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7213) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7214) 			 * We could trigger writeback for this range (and wait
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7215) 			 * for it to complete) and then invalidate the pages for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7216) 			 * this range (through invalidate_inode_pages2_range()),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7217) 			 * but that can lead us to a deadlock with a concurrent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7218) 			 * call to readahead (a buffered read or a defrag call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7219) 			 * triggered a readahead) on a page lock due to an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7220) 			 * ordered dio extent we created before but did not have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7221) 			 * yet a corresponding bio submitted (whence it can not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7222) 			 * complete), which makes readahead wait for that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7223) 			 * ordered extent to complete while holding a lock on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7224) 			 * that page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7225) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7226) 			ret = -ENOTBLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7227) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7229) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7230) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7232) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7235) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7238) /* The callers of this must take lock_extent() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7239) static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7240) 				       u64 len, u64 orig_start, u64 block_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7241) 				       u64 block_len, u64 orig_block_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7242) 				       u64 ram_bytes, int compress_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7243) 				       int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7245) 	struct extent_map_tree *em_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7246) 	struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7247) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7249) 	ASSERT(type == BTRFS_ORDERED_PREALLOC ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7250) 	       type == BTRFS_ORDERED_COMPRESSED ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7251) 	       type == BTRFS_ORDERED_NOCOW ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7252) 	       type == BTRFS_ORDERED_REGULAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7254) 	em_tree = &inode->extent_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7255) 	em = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7256) 	if (!em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7257) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7259) 	em->start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7260) 	em->orig_start = orig_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7261) 	em->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7262) 	em->block_len = block_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7263) 	em->block_start = block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7264) 	em->orig_block_len = orig_block_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7265) 	em->ram_bytes = ram_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7266) 	em->generation = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7267) 	set_bit(EXTENT_FLAG_PINNED, &em->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7268) 	if (type == BTRFS_ORDERED_PREALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7269) 		set_bit(EXTENT_FLAG_FILLING, &em->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7270) 	} else if (type == BTRFS_ORDERED_COMPRESSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7271) 		set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7272) 		em->compress_type = compress_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7275) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7276) 		btrfs_drop_extent_cache(inode, em->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7277) 					em->start + em->len - 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7278) 		write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7279) 		ret = add_extent_mapping(em_tree, em, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7280) 		write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7281) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7282) 		 * The caller has taken lock_extent(), who could race with us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7283) 		 * to add em?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7284) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7285) 	} while (ret == -EEXIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7287) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7288) 		free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7289) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7290) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7292) 	/* em got 2 refs now, callers needs to do free_extent_map once. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7293) 	return em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7297) static int btrfs_get_blocks_direct_write(struct extent_map **map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7298) 					 struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7299) 					 struct btrfs_dio_data *dio_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7300) 					 u64 start, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7302) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7303) 	struct extent_map *em = *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7304) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7306) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7307) 	 * We don't allocate a new extent in the following cases
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7308) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7309) 	 * 1) The inode is marked as NODATACOW. In this case we'll just use the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7310) 	 * existing extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7311) 	 * 2) The extent is marked as PREALLOC. We're good to go here and can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7312) 	 * just use the extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7313) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7314) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7315) 	if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7316) 	    ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7317) 	     em->block_start != EXTENT_MAP_HOLE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7318) 		int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7319) 		u64 block_start, orig_start, orig_block_len, ram_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7321) 		if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7322) 			type = BTRFS_ORDERED_PREALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7323) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7324) 			type = BTRFS_ORDERED_NOCOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7325) 		len = min(len, em->len - (start - em->start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7326) 		block_start = em->block_start + (start - em->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7328) 		if (can_nocow_extent(inode, start, &len, &orig_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7329) 				     &orig_block_len, &ram_bytes, false) == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7330) 		    btrfs_inc_nocow_writers(fs_info, block_start)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7331) 			struct extent_map *em2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7333) 			em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7334) 						      orig_start, block_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7335) 						      len, orig_block_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7336) 						      ram_bytes, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7337) 			btrfs_dec_nocow_writers(fs_info, block_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7338) 			if (type == BTRFS_ORDERED_PREALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7339) 				free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7340) 				*map = em = em2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7341) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7343) 			if (em2 && IS_ERR(em2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7344) 				ret = PTR_ERR(em2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7345) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7346) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7347) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7348) 			 * For inode marked NODATACOW or extent marked PREALLOC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7349) 			 * use the existing or preallocated extent, so does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7350) 			 * need to adjust btrfs_space_info's bytes_may_use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7351) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7352) 			btrfs_free_reserved_data_space_noquota(fs_info, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7353) 			goto skip_cow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7354) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7355) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7357) 	/* this will cow the extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7358) 	free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7359) 	*map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7360) 	if (IS_ERR(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7361) 		ret = PTR_ERR(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7362) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7365) 	len = min(len, em->len - (start - em->start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7367) skip_cow:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7368) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7369) 	 * Need to update the i_size under the extent lock so buffered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7370) 	 * readers will get the updated i_size when we unlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7371) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7372) 	if (start + len > i_size_read(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7373) 		i_size_write(inode, start + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7375) 	dio_data->reserve -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7376) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7377) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7380) static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7381) 		loff_t length, unsigned int flags, struct iomap *iomap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7382) 		struct iomap *srcmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7384) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7385) 	struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7386) 	struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7387) 	struct btrfs_dio_data *dio_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7388) 	u64 lockstart, lockend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7389) 	const bool write = !!(flags & IOMAP_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7390) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7391) 	u64 len = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7392) 	bool unlock_extents = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7393) 	bool sync = (current->journal_info == BTRFS_DIO_SYNC_STUB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7395) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7396) 	 * We used current->journal_info here to see if we were sync, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7397) 	 * there's a lot of tests in the enospc machinery to not do flushing if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7398) 	 * we have a journal_info set, so we need to clear this out and re-set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7399) 	 * it in iomap_end.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7400) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7401) 	ASSERT(current->journal_info == NULL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7402) 	       current->journal_info == BTRFS_DIO_SYNC_STUB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7403) 	current->journal_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7405) 	if (!write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7406) 		len = min_t(u64, len, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7408) 	lockstart = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7409) 	lockend = start + len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7411) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7412) 	 * The generic stuff only does filemap_write_and_wait_range, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7413) 	 * isn't enough if we've written compressed pages to this area, so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7414) 	 * need to flush the dirty pages again to make absolutely sure that any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7415) 	 * outstanding dirty pages are on disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7416) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7417) 	if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7418) 		     &BTRFS_I(inode)->runtime_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7419) 		ret = filemap_fdatawrite_range(inode->i_mapping, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7420) 					       start + length - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7421) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7422) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7423) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7425) 	dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7426) 	if (!dio_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7427) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7429) 	dio_data->sync = sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7430) 	dio_data->length = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7431) 	if (write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7432) 		dio_data->reserve = round_up(length, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7433) 		ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7434) 				&dio_data->data_reserved,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7435) 				start, dio_data->reserve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7436) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7437) 			extent_changeset_free(dio_data->data_reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7438) 			kfree(dio_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7439) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7440) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7441) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7442) 	iomap->private = dio_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7445) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7446) 	 * If this errors out it's because we couldn't invalidate pagecache for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7447) 	 * this range and we need to fallback to buffered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7448) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7449) 	if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7450) 		ret = -ENOTBLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7451) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7452) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7454) 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7455) 	if (IS_ERR(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7456) 		ret = PTR_ERR(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7457) 		goto unlock_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7458) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7460) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7461) 	 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7462) 	 * io.  INLINE is special, and we could probably kludge it in here, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7463) 	 * it's still buffered so for safety lets just fall back to the generic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7464) 	 * buffered path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7465) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7466) 	 * For COMPRESSED we _have_ to read the entire extent in so we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7467) 	 * decompress it, so there will be buffering required no matter what we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7468) 	 * do, so go ahead and fallback to buffered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7469) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7470) 	 * We return -ENOTBLK because that's what makes DIO go ahead and go back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7471) 	 * to buffered IO.  Don't blame me, this is the price we pay for using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7472) 	 * the generic code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7473) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7474) 	if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7475) 	    em->block_start == EXTENT_MAP_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7476) 		free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7477) 		ret = -ENOTBLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7478) 		goto unlock_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7481) 	len = min(len, em->len - (start - em->start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7482) 	if (write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7483) 		ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7484) 						    start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7485) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7486) 			goto unlock_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7487) 		unlock_extents = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7488) 		/* Recalc len in case the new em is smaller than requested */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7489) 		len = min(len, em->len - (start - em->start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7490) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7491) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7492) 		 * We need to unlock only the end area that we aren't using.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7493) 		 * The rest is going to be unlocked by the endio routine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7494) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7495) 		lockstart = start + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7496) 		if (lockstart < lockend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7497) 			unlock_extents = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7498) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7500) 	if (unlock_extents)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7501) 		unlock_extent_cached(&BTRFS_I(inode)->io_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7502) 				     lockstart, lockend, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7503) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7504) 		free_extent_state(cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7506) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7507) 	 * Translate extent map information to iomap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7508) 	 * We trim the extents (and move the addr) even though iomap code does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7509) 	 * that, since we have locked only the parts we are performing I/O in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7510) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7511) 	if ((em->block_start == EXTENT_MAP_HOLE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7512) 	    (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7513) 		iomap->addr = IOMAP_NULL_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7514) 		iomap->type = IOMAP_HOLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7515) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7516) 		iomap->addr = em->block_start + (start - em->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7517) 		iomap->type = IOMAP_MAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7518) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7519) 	iomap->offset = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7520) 	iomap->bdev = fs_info->fs_devices->latest_bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7521) 	iomap->length = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7523) 	free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7525) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7527) unlock_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7528) 	unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7529) 			     &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7530) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7531) 	if (dio_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7532) 		btrfs_delalloc_release_space(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7533) 				dio_data->data_reserved, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7534) 				dio_data->reserve, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7535) 		btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7536) 		extent_changeset_free(dio_data->data_reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7537) 		kfree(dio_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7538) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7539) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7542) static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7543) 		ssize_t written, unsigned int flags, struct iomap *iomap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7545) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7546) 	struct btrfs_dio_data *dio_data = iomap->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7547) 	size_t submitted = dio_data->submitted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7548) 	const bool write = !!(flags & IOMAP_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7550) 	if (!write && (iomap->type == IOMAP_HOLE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7551) 		/* If reading from a hole, unlock and return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7552) 		unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7553) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7554) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7556) 	if (submitted < length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7557) 		pos += submitted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7558) 		length -= submitted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7559) 		if (write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7560) 			__endio_write_update_ordered(BTRFS_I(inode), pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7561) 					length, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7562) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7563) 			unlock_extent(&BTRFS_I(inode)->io_tree, pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7564) 				      pos + length - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7565) 		ret = -ENOTBLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7566) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7568) 	if (write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7569) 		if (dio_data->reserve)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7570) 			btrfs_delalloc_release_space(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7571) 					dio_data->data_reserved, pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7572) 					dio_data->reserve, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7573) 		btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7574) 		extent_changeset_free(dio_data->data_reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7575) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7576) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7577) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7578) 	 * We're all done, we can re-set the current->journal_info now safely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7579) 	 * for our endio.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7580) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7581) 	if (dio_data->sync) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7582) 		ASSERT(current->journal_info == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7583) 		current->journal_info = BTRFS_DIO_SYNC_STUB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7584) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7585) 	kfree(dio_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7586) 	iomap->private = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7588) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7591) static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7593) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7594) 	 * This implies a barrier so that stores to dio_bio->bi_status before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7595) 	 * this and loads of dio_bio->bi_status after this are fully ordered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7596) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7597) 	if (!refcount_dec_and_test(&dip->refs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7598) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7600) 	if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7601) 		__endio_write_update_ordered(BTRFS_I(dip->inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7602) 					     dip->logical_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7603) 					     dip->bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7604) 					     !dip->dio_bio->bi_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7605) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7606) 		unlock_extent(&BTRFS_I(dip->inode)->io_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7607) 			      dip->logical_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7608) 			      dip->logical_offset + dip->bytes - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7609) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7611) 	bio_endio(dip->dio_bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7612) 	kfree(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7615) static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7616) 					  int mirror_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7617) 					  unsigned long bio_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7618) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7619) 	struct btrfs_dio_private *dip = bio->bi_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7620) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7621) 	blk_status_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7623) 	BUG_ON(bio_op(bio) == REQ_OP_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7625) 	ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7626) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7627) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7629) 	refcount_inc(&dip->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7630) 	ret = btrfs_map_bio(fs_info, bio, mirror_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7631) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7632) 		refcount_dec(&dip->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7633) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7636) static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7637) 					     struct btrfs_io_bio *io_bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7638) 					     const bool uptodate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7639) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7640) 	struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7641) 	const u32 sectorsize = fs_info->sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7642) 	struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7643) 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7644) 	const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7645) 	struct bio_vec bvec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7646) 	struct bvec_iter iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7647) 	u64 start = io_bio->logical;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7648) 	int icsum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7649) 	blk_status_t err = BLK_STS_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7651) 	__bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7652) 		unsigned int i, nr_sectors, pgoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7654) 		nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7655) 		pgoff = bvec.bv_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7656) 		for (i = 0; i < nr_sectors; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7657) 			ASSERT(pgoff < PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7658) 			if (uptodate &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7659) 			    (!csum || !check_data_csum(inode, io_bio, icsum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7660) 						       bvec.bv_page, pgoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7661) 						       start, sectorsize))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7662) 				clean_io_failure(fs_info, failure_tree, io_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7663) 						 start, bvec.bv_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7664) 						 btrfs_ino(BTRFS_I(inode)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7665) 						 pgoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7666) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7667) 				blk_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7669) 				status = btrfs_submit_read_repair(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7670) 							&io_bio->bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7671) 							start - io_bio->logical,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7672) 							bvec.bv_page, pgoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7673) 							start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7674) 							start + sectorsize - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7675) 							io_bio->mirror_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7676) 							submit_dio_repair_bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7677) 				if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7678) 					err = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7679) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7680) 			start += sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7681) 			icsum++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7682) 			pgoff += sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7683) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7684) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7685) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7688) static void __endio_write_update_ordered(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7689) 					 const u64 offset, const u64 bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7690) 					 const bool uptodate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7691) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7692) 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7693) 	struct btrfs_ordered_extent *ordered = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7694) 	struct btrfs_workqueue *wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7695) 	u64 ordered_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7696) 	u64 ordered_bytes = bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7697) 	u64 last_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7699) 	if (btrfs_is_free_space_inode(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7700) 		wq = fs_info->endio_freespace_worker;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7701) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7702) 		wq = fs_info->endio_write_workers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7704) 	while (ordered_offset < offset + bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7705) 		last_offset = ordered_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7706) 		if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7707) 							 &ordered_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7708) 							 ordered_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7709) 							 uptodate)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7710) 			btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7711) 					NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7712) 			btrfs_queue_work(wq, &ordered->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7713) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7714) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7715) 		 * If btrfs_dec_test_ordered_pending does not find any ordered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7716) 		 * extent in the range, we can exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7717) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7718) 		if (ordered_offset == last_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7719) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7720) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7721) 		 * Our bio might span multiple ordered extents. In this case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7722) 		 * we keep going until we have accounted the whole dio.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7723) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7724) 		if (ordered_offset < offset + bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7725) 			ordered_bytes = offset + bytes - ordered_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7726) 			ordered = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7727) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7728) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7731) static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7732) 				    struct bio *bio, u64 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7734) 	struct inode *inode = private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7736) 	return btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7739) static void btrfs_end_dio_bio(struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7740) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7741) 	struct btrfs_dio_private *dip = bio->bi_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7742) 	blk_status_t err = bio->bi_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7744) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7745) 		btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7746) 			   "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7747) 			   btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7748) 			   bio->bi_opf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7749) 			   (unsigned long long)bio->bi_iter.bi_sector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7750) 			   bio->bi_iter.bi_size, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7752) 	if (bio_op(bio) == REQ_OP_READ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7753) 		err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7754) 					       !err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7755) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7757) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7758) 		dip->dio_bio->bi_status = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7760) 	bio_put(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7761) 	btrfs_dio_private_put(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7764) static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7765) 		struct inode *inode, u64 file_offset, int async_submit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7766) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7767) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7768) 	struct btrfs_dio_private *dip = bio->bi_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7769) 	bool write = bio_op(bio) == REQ_OP_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7770) 	blk_status_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7772) 	/* Check btrfs_submit_bio_hook() for rules about async submit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7773) 	if (async_submit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7774) 		async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7776) 	if (!write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7777) 		ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7778) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7779) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7780) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7782) 	if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7783) 		goto map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7785) 	if (write && async_submit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7786) 		ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7787) 					  file_offset, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7788) 					  btrfs_submit_bio_start_direct_io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7789) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7790) 	} else if (write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7791) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7792) 		 * If we aren't doing async submit, calculate the csum of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7793) 		 * bio now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7794) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7795) 		ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7796) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7797) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7798) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7799) 		u64 csum_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7801) 		csum_offset = file_offset - dip->logical_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7802) 		csum_offset >>= inode->i_sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7803) 		csum_offset *= btrfs_super_csum_size(fs_info->super_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7804) 		btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7805) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7806) map:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7807) 	ret = btrfs_map_bio(fs_info, bio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7808) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7809) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7812) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7813)  * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7814)  * or ordered extents whether or not we submit any bios.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7815)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7816) static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7817) 							  struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7818) 							  loff_t file_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7819) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7820) 	const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7821) 	const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7822) 	size_t dip_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7823) 	struct btrfs_dio_private *dip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7825) 	dip_size = sizeof(*dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7826) 	if (!write && csum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7827) 		struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7828) 		const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7829) 		size_t nblocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7831) 		nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7832) 		dip_size += csum_size * nblocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7833) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7835) 	dip = kzalloc(dip_size, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7836) 	if (!dip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7837) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7839) 	dip->inode = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7840) 	dip->logical_offset = file_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7841) 	dip->bytes = dio_bio->bi_iter.bi_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7842) 	dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7843) 	dip->dio_bio = dio_bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7844) 	refcount_set(&dip->refs, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7845) 	return dip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7848) static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7849) 		struct bio *dio_bio, loff_t file_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7850) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7851) 	const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7852) 	const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7853) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7854) 	const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7855) 			     BTRFS_BLOCK_GROUP_RAID56_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7856) 	struct btrfs_dio_private *dip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7857) 	struct bio *bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7858) 	u64 start_sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7859) 	int async_submit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7860) 	u64 submit_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7861) 	int clone_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7862) 	int clone_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7863) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7864) 	blk_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7865) 	struct btrfs_io_geometry geom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7866) 	struct btrfs_dio_data *dio_data = iomap->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7868) 	dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7869) 	if (!dip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7870) 		if (!write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7871) 			unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7872) 				file_offset + dio_bio->bi_iter.bi_size - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7873) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7874) 		dio_bio->bi_status = BLK_STS_RESOURCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7875) 		bio_endio(dio_bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7876) 		return BLK_QC_T_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7877) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7879) 	if (!write && csum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7880) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7881) 		 * Load the csums up front to reduce csum tree searches and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7882) 		 * contention when submitting bios.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7883) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7884) 		status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7885) 					       dip->csums);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7886) 		if (status != BLK_STS_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7887) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7888) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7890) 	start_sector = dio_bio->bi_iter.bi_sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7891) 	submit_len = dio_bio->bi_iter.bi_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7893) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7894) 		ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7895) 					    start_sector << 9, submit_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7896) 					    &geom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7897) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7898) 			status = errno_to_blk_status(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7899) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7900) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7901) 		ASSERT(geom.len <= INT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7903) 		clone_len = min_t(int, submit_len, geom.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7905) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7906) 		 * This will never fail as it's passing GPF_NOFS and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7907) 		 * the allocation is backed by btrfs_bioset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7908) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7909) 		bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7910) 		bio->bi_private = dip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7911) 		bio->bi_end_io = btrfs_end_dio_bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7912) 		btrfs_io_bio(bio)->logical = file_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7914) 		ASSERT(submit_len >= clone_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7915) 		submit_len -= clone_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7917) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7918) 		 * Increase the count before we submit the bio so we know
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7919) 		 * the end IO handler won't happen before we increase the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7920) 		 * count. Otherwise, the dip might get freed before we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7921) 		 * done setting it up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7922) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7923) 		 * We transfer the initial reference to the last bio, so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7924) 		 * don't need to increment the reference count for the last one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7925) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7926) 		if (submit_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7927) 			refcount_inc(&dip->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7928) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7929) 			 * If we are submitting more than one bio, submit them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7930) 			 * all asynchronously. The exception is RAID 5 or 6, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7931) 			 * asynchronous checksums make it difficult to collect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7932) 			 * full stripe writes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7933) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7934) 			if (!raid56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7935) 				async_submit = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7936) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7938) 		status = btrfs_submit_dio_bio(bio, inode, file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7939) 						async_submit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7940) 		if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7941) 			bio_put(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7942) 			if (submit_len > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7943) 				refcount_dec(&dip->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7944) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7945) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7947) 		dio_data->submitted += clone_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7948) 		clone_offset += clone_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7949) 		start_sector += clone_len >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7950) 		file_offset += clone_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7951) 	} while (submit_len > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7952) 	return BLK_QC_T_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7954) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7955) 	dip->dio_bio->bi_status = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7956) 	btrfs_dio_private_put(dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7957) 	return BLK_QC_T_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7958) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7960) static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7961) 			       const struct iov_iter *iter, loff_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7962) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7963) 	int seg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7964) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7965) 	unsigned int blocksize_mask = fs_info->sectorsize - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7966) 	ssize_t retval = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7968) 	if (offset & blocksize_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7969) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7971) 	if (iov_iter_alignment(iter) & blocksize_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7972) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7974) 	/* If this is a write we don't need to check anymore */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7975) 	if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7976) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7977) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7978) 	 * Check to make sure we don't have duplicate iov_base's in this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7979) 	 * iovec, if so return EINVAL, otherwise we'll get csum errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7980) 	 * when reading back.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7981) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7982) 	for (seg = 0; seg < iter->nr_segs; seg++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7983) 		for (i = seg + 1; i < iter->nr_segs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7984) 			if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7985) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7986) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7987) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7988) 	retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7989) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7990) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7991) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7993) static inline int btrfs_maybe_fsync_end_io(struct kiocb *iocb, ssize_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7994) 					   int error, unsigned flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7995) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7996) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7997) 	 * Now if we're still in the context of our submitter we know we can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7998) 	 * safely run generic_write_sync(), so clear our flag here so that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7999) 	 * caller knows to follow up with a sync.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8000) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8001) 	if (current->journal_info == BTRFS_DIO_SYNC_STUB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8002) 		current->journal_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8003) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8004) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8006) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8007) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8009) 	if (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8010) 		iocb->ki_flags |= IOCB_DSYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8011) 		return generic_write_sync(iocb, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8012) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8014) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8017) static const struct iomap_ops btrfs_dio_iomap_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8018) 	.iomap_begin            = btrfs_dio_iomap_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8019) 	.iomap_end              = btrfs_dio_iomap_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8020) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8022) static const struct iomap_dio_ops btrfs_dio_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8023) 	.submit_io		= btrfs_submit_direct,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8024) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8026) static const struct iomap_dio_ops btrfs_sync_dops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8027) 	.submit_io		= btrfs_submit_direct,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8028) 	.end_io			= btrfs_maybe_fsync_end_io,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8029) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8031) ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8032) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8033) 	struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8034) 	struct inode *inode = file->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8035) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8036) 	struct extent_changeset *data_reserved = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8037) 	loff_t offset = iocb->ki_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8038) 	size_t count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8039) 	bool relock = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8040) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8042) 	if (check_direct_IO(fs_info, iter, offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8043) 		ASSERT(current->journal_info == NULL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8044) 		       current->journal_info == BTRFS_DIO_SYNC_STUB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8045) 		current->journal_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8046) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8047) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8049) 	count = iov_iter_count(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8050) 	if (iov_iter_rw(iter) == WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8051) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8052) 		 * If the write DIO is beyond the EOF, we need update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8053) 		 * the isize, but it is protected by i_mutex. So we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8054) 		 * not unlock the i_mutex at this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8055) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8056) 		if (offset + count <= inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8057) 			inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8058) 			relock = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8059) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8060) 		down_read(&BTRFS_I(inode)->dio_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8061) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8063) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8064) 	 * We have are actually a sync iocb, so we need our fancy endio to know
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8065) 	 * if we need to sync.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8066) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8067) 	if (current->journal_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8068) 		ret = iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8069) 				   &btrfs_sync_dops, is_sync_kiocb(iocb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8070) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8071) 		ret = iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8072) 				   &btrfs_dio_ops, is_sync_kiocb(iocb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8074) 	if (ret == -ENOTBLK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8075) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8077) 	if (iov_iter_rw(iter) == WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8078) 		up_read(&BTRFS_I(inode)->dio_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8080) 	if (relock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8081) 		inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8083) 	extent_changeset_free(data_reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8084) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8087) static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8088) 			u64 start, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8089) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8090) 	int	ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8092) 	ret = fiemap_prep(inode, fieinfo, start, &len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8093) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8094) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8096) 	return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8097) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8099) int btrfs_readpage(struct file *file, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8101) 	struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8102) 	u64 start = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8103) 	u64 end = start + PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8104) 	unsigned long bio_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8105) 	struct bio *bio = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8106) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8108) 	btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8110) 	ret = btrfs_do_readpage(page, NULL, &bio, &bio_flags, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8111) 	if (bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8112) 		ret = submit_one_bio(bio, 0, bio_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8113) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8116) static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8118) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8119) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8121) 	if (current->flags & PF_MEMALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8122) 		redirty_page_for_writepage(wbc, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8123) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8124) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8127) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8128) 	 * If we are under memory pressure we will call this directly from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8129) 	 * VM, we need to make sure we have the inode referenced for the ordered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8130) 	 * extent.  If not just return like we didn't do anything.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8131) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8132) 	if (!igrab(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8133) 		redirty_page_for_writepage(wbc, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8134) 		return AOP_WRITEPAGE_ACTIVATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8136) 	ret = extent_write_full_page(page, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8137) 	btrfs_add_delayed_iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8138) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8141) static int btrfs_writepages(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8142) 			    struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8144) 	return extent_writepages(mapping, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8147) static void btrfs_readahead(struct readahead_control *rac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8149) 	extent_readahead(rac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8152) static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8154) 	int ret = try_release_extent_mapping(page, gfp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8155) 	if (ret == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8156) 		detach_page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8157) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8160) static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8162) 	if (PageWriteback(page) || PageDirty(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8163) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8164) 	return __btrfs_releasepage(page, gfp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8167) #ifdef CONFIG_MIGRATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8168) static int btrfs_migratepage(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8169) 			     struct page *newpage, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8170) 			     enum migrate_mode mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8172) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8174) 	ret = migrate_page_move_mapping(mapping, newpage, page, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8175) 	if (ret != MIGRATEPAGE_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8176) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8178) 	if (page_has_private(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8179) 		attach_page_private(newpage, detach_page_private(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8181) 	if (PagePrivate2(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8182) 		ClearPagePrivate2(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8183) 		SetPagePrivate2(newpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8186) 	if (mode != MIGRATE_SYNC_NO_COPY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8187) 		migrate_page_copy(newpage, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8188) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8189) 		migrate_page_states(newpage, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8190) 	return MIGRATEPAGE_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8192) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8194) static void btrfs_invalidatepage(struct page *page, unsigned int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8195) 				 unsigned int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8197) 	struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8198) 	struct extent_io_tree *tree = &inode->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8199) 	struct btrfs_ordered_extent *ordered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8200) 	struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8201) 	u64 page_start = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8202) 	u64 page_end = page_start + PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8203) 	u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8204) 	u64 end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8205) 	int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8207) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8208) 	 * we have the page locked, so new writeback can't start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8209) 	 * and the dirty bit won't be cleared while we are here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8210) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8211) 	 * Wait for IO on this page so that we can safely clear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8212) 	 * the PagePrivate2 bit and do ordered accounting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8213) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8214) 	wait_on_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8216) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8217) 	 * For subpage case, we have call sites like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8218) 	 * btrfs_punch_hole_lock_range() which passes range not aligned to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8219) 	 * sectorsize.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8220) 	 * If the range doesn't cover the full page, we don't need to and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8221) 	 * shouldn't clear page extent mapped, as page->private can still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8222) 	 * record subpage dirty bits for other part of the range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8223) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8224) 	 * For cases that can invalidate the full even the range doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8225) 	 * cover the full page, like invalidating the last page, we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8226) 	 * still safe to wait for ordered extent to finish.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8227) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8228) 	if (!(offset == 0 && length == PAGE_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8229) 		btrfs_releasepage(page, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8230) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8233) 	if (!inode_evicting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8234) 		lock_extent_bits(tree, page_start, page_end, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8236) 	start = page_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8237) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8238) 	ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8239) 	if (ordered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8240) 		end = min(page_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8241) 			  ordered->file_offset + ordered->num_bytes - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8242) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8243) 		 * IO on this page will never be started, so we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8244) 		 * to account for any ordered extents now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8245) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8246) 		if (!inode_evicting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8247) 			clear_extent_bit(tree, start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8248) 					 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8249) 					 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8250) 					 EXTENT_DEFRAG, 1, 0, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8251) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8252) 		 * whoever cleared the private bit is responsible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8253) 		 * for the finish_ordered_io
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8254) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8255) 		if (TestClearPagePrivate2(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8256) 			struct btrfs_ordered_inode_tree *tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8257) 			u64 new_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8259) 			tree = &inode->ordered_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8261) 			spin_lock_irq(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8262) 			set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8263) 			new_len = start - ordered->file_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8264) 			if (new_len < ordered->truncated_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8265) 				ordered->truncated_len = new_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8266) 			spin_unlock_irq(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8268) 			if (btrfs_dec_test_ordered_pending(inode, &ordered,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8269) 							   start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8270) 							   end - start + 1, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8271) 				btrfs_finish_ordered_io(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8272) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8273) 		btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8274) 		if (!inode_evicting) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8275) 			cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8276) 			lock_extent_bits(tree, start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8277) 					 &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8278) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8280) 		start = end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8281) 		if (start < page_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8282) 			goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8285) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8286) 	 * Qgroup reserved space handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8287) 	 * Page here will be either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8288) 	 * 1) Already written to disk or ordered extent already submitted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8289) 	 *    Then its QGROUP_RESERVED bit in io_tree is already cleaned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8290) 	 *    Qgroup will be handled by its qgroup_record then.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8291) 	 *    btrfs_qgroup_free_data() call will do nothing here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8292) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8293) 	 * 2) Not written to disk yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8294) 	 *    Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8295) 	 *    bit of its io_tree, and free the qgroup reserved data space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8296) 	 *    Since the IO will never happen for this page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8297) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8298) 	btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8299) 	if (!inode_evicting) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8300) 		clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8301) 				 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8302) 				 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8303) 				 &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8305) 		__btrfs_releasepage(page, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8306) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8308) 	ClearPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8309) 	detach_page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8312) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8313)  * btrfs_page_mkwrite() is not allowed to change the file size as it gets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8314)  * called from a page fault handler when a page is first dirtied. Hence we must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8315)  * be careful to check for EOF conditions here. We set the page up correctly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8316)  * for a written page which means we get ENOSPC checking when writing into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8317)  * holes and correct delalloc and unwritten extent mapping on filesystems that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8318)  * support these features.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8319)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8320)  * We are not allowed to take the i_mutex here so we have to play games to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8321)  * protect against truncate races as the page could now be beyond EOF.  Because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8322)  * truncate_setsize() writes the inode size before removing pages, once we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8323)  * the page lock we can determine safely if the page is beyond EOF. If it is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8324)  * beyond EOF, then the page is guaranteed safe against truncation until we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8325)  * unlock the page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8326)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8327) vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8329) 	struct page *page = vmf->page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8330) 	struct inode *inode = file_inode(vmf->vma->vm_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8331) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8332) 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8333) 	struct btrfs_ordered_extent *ordered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8334) 	struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8335) 	struct extent_changeset *data_reserved = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8336) 	char *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8337) 	unsigned long zero_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8338) 	loff_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8339) 	vm_fault_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8340) 	int ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8341) 	int reserved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8342) 	u64 reserved_space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8343) 	u64 page_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8344) 	u64 page_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8345) 	u64 end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8347) 	reserved_space = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8349) 	sb_start_pagefault(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8350) 	page_start = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8351) 	page_end = page_start + PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8352) 	end = page_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8354) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8355) 	 * Reserving delalloc space after obtaining the page lock can lead to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8356) 	 * deadlock. For example, if a dirty page is locked by this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8357) 	 * and the call to btrfs_delalloc_reserve_space() ends up triggering
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8358) 	 * dirty page write out, then the btrfs_writepage() function could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8359) 	 * end up waiting indefinitely to get a lock on the page currently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8360) 	 * being processed by btrfs_page_mkwrite() function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8361) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8362) 	ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8363) 					    page_start, reserved_space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8364) 	if (!ret2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8365) 		ret2 = file_update_time(vmf->vma->vm_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8366) 		reserved = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8367) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8368) 	if (ret2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8369) 		ret = vmf_error(ret2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8370) 		if (reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8371) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8372) 		goto out_noreserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8373) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8375) 	ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8376) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8377) 	lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8378) 	size = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8380) 	if ((page->mapping != inode->i_mapping) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8381) 	    (page_start >= size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8382) 		/* page got truncated out from underneath us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8383) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8384) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8385) 	wait_on_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8387) 	lock_extent_bits(io_tree, page_start, page_end, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8388) 	set_page_extent_mapped(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8390) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8391) 	 * we can't set the delalloc bits if there are pending ordered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8392) 	 * extents.  Drop our locks and wait for them to finish
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8393) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8394) 	ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8395) 			PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8396) 	if (ordered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8397) 		unlock_extent_cached(io_tree, page_start, page_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8398) 				     &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8399) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8400) 		btrfs_start_ordered_extent(ordered, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8401) 		btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8402) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8405) 	if (page->index == ((size - 1) >> PAGE_SHIFT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8406) 		reserved_space = round_up(size - page_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8407) 					  fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8408) 		if (reserved_space < PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8409) 			end = page_start + reserved_space - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8410) 			btrfs_delalloc_release_space(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8411) 					data_reserved, page_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8412) 					PAGE_SIZE - reserved_space, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8413) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8414) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8416) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8417) 	 * page_mkwrite gets called when the page is firstly dirtied after it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8418) 	 * faulted in, but write(2) could also dirty a page and set delalloc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8419) 	 * bits, thus in this case for space account reason, we still need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8420) 	 * clear any delalloc bits within this page range since we have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8421) 	 * reserve data&meta space before lock_page() (see above comments).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8422) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8423) 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8424) 			  EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8425) 			  EXTENT_DEFRAG, 0, 0, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8427) 	ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8428) 					&cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8429) 	if (ret2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8430) 		unlock_extent_cached(io_tree, page_start, page_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8431) 				     &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8432) 		ret = VM_FAULT_SIGBUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8433) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8434) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8436) 	/* page is wholly or partially inside EOF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8437) 	if (page_start + PAGE_SIZE > size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8438) 		zero_start = offset_in_page(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8439) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8440) 		zero_start = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8442) 	if (zero_start != PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8443) 		kaddr = kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8444) 		memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8445) 		flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8446) 		kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8447) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8448) 	ClearPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8449) 	set_page_dirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8450) 	SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8452) 	btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8454) 	unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8456) 	btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8457) 	sb_end_pagefault(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8458) 	extent_changeset_free(data_reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8459) 	return VM_FAULT_LOCKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8461) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8462) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8463) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8464) 	btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8465) 	btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8466) 				     reserved_space, (ret != 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8467) out_noreserve:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8468) 	sb_end_pagefault(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8469) 	extent_changeset_free(data_reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8470) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8473) static int btrfs_truncate(struct inode *inode, bool skip_writeback)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8475) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8476) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8477) 	struct btrfs_block_rsv *rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8478) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8479) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8480) 	u64 mask = fs_info->sectorsize - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8481) 	u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8483) 	if (!skip_writeback) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8484) 		ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8485) 					       (u64)-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8486) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8487) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8488) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8490) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8491) 	 * Yes ladies and gentlemen, this is indeed ugly.  We have a couple of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8492) 	 * things going on here:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8493) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8494) 	 * 1) We need to reserve space to update our inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8495) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8496) 	 * 2) We need to have something to cache all the space that is going to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8497) 	 * be free'd up by the truncate operation, but also have some slack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8498) 	 * space reserved in case it uses space during the truncate (thank you
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8499) 	 * very much snapshotting).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8500) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8501) 	 * And we need these to be separate.  The fact is we can use a lot of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8502) 	 * space doing the truncate, and we have no earthly idea how much space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8503) 	 * we will use, so we need the truncate reservation to be separate so it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8504) 	 * doesn't end up using space reserved for updating the inode.  We also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8505) 	 * need to be able to stop the transaction and start a new one, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8506) 	 * means we need to be able to update the inode several times, and we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8507) 	 * have no idea of knowing how many times that will be, so we can't just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8508) 	 * reserve 1 item for the entirety of the operation, so that has to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8509) 	 * done separately as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8510) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8511) 	 * So that leaves us with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8512) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8513) 	 * 1) rsv - for the truncate reservation, which we will steal from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8514) 	 * transaction reservation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8515) 	 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8516) 	 * updating the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8517) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8518) 	rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8519) 	if (!rsv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8520) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8521) 	rsv->size = min_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8522) 	rsv->failfast = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8524) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8525) 	 * 1 for the truncate slack space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8526) 	 * 1 for updating the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8527) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8528) 	trans = btrfs_start_transaction(root, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8529) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8530) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8531) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8532) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8534) 	/* Migrate the slack space for the truncate to our reserve */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8535) 	ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8536) 				      min_size, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8537) 	BUG_ON(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8539) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8540) 	 * So if we truncate and then write and fsync we normally would just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8541) 	 * write the extents that changed, which is a problem if we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8542) 	 * first truncate that entire inode.  So set this flag so we write out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8543) 	 * all of the extents in the inode to the sync log so we're completely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8544) 	 * safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8545) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8546) 	set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8547) 	trans->block_rsv = rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8549) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8550) 		ret = btrfs_truncate_inode_items(trans, root, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8551) 						 inode->i_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8552) 						 BTRFS_EXTENT_DATA_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8553) 		trans->block_rsv = &fs_info->trans_block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8554) 		if (ret != -ENOSPC && ret != -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8555) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8557) 		ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8558) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8559) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8561) 		btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8562) 		btrfs_btree_balance_dirty(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8564) 		trans = btrfs_start_transaction(root, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8565) 		if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8566) 			ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8567) 			trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8568) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8569) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8571) 		btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8572) 		ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8573) 					      rsv, min_size, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8574) 		BUG_ON(ret);	/* shouldn't happen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8575) 		trans->block_rsv = rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8576) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8578) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8579) 	 * We can't call btrfs_truncate_block inside a trans handle as we could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8580) 	 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8581) 	 * we've truncated everything except the last little bit, and can do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8582) 	 * btrfs_truncate_block and then update the disk_i_size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8583) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8584) 	if (ret == NEED_TRUNCATE_BLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8585) 		btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8586) 		btrfs_btree_balance_dirty(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8588) 		ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8589) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8590) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8591) 		trans = btrfs_start_transaction(root, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8592) 		if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8593) 			ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8594) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8595) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8596) 		btrfs_inode_safe_disk_i_size_write(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8597) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8599) 	if (trans) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8600) 		int ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8602) 		trans->block_rsv = &fs_info->trans_block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8603) 		ret2 = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8604) 		if (ret2 && !ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8605) 			ret = ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8607) 		ret2 = btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8608) 		if (ret2 && !ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8609) 			ret = ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8610) 		btrfs_btree_balance_dirty(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8611) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8612) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8613) 	btrfs_free_block_rsv(fs_info, rsv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8615) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8618) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8619)  * create a new subvolume directory/inode (helper for the ioctl).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8620)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8621) int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8622) 			     struct btrfs_root *new_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8623) 			     struct btrfs_root *parent_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8624) 			     u64 new_dirid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8626) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8627) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8628) 	u64 index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8630) 	inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8631) 				new_dirid, new_dirid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8632) 				S_IFDIR | (~current_umask() & S_IRWXUGO),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8633) 				&index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8634) 	if (IS_ERR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8635) 		return PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8636) 	inode->i_op = &btrfs_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8637) 	inode->i_fop = &btrfs_dir_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8639) 	set_nlink(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8640) 	btrfs_i_size_write(BTRFS_I(inode), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8641) 	unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8643) 	err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8644) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8645) 		btrfs_err(new_root->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8646) 			  "error inheriting subvolume %llu properties: %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8647) 			  new_root->root_key.objectid, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8649) 	err = btrfs_update_inode(trans, new_root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8651) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8652) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8655) struct inode *btrfs_alloc_inode(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8657) 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8658) 	struct btrfs_inode *ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8659) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8661) 	ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8662) 	if (!ei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8663) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8665) 	ei->root = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8666) 	ei->generation = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8667) 	ei->last_trans = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8668) 	ei->last_sub_trans = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8669) 	ei->logged_trans = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8670) 	ei->delalloc_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8671) 	ei->new_delalloc_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8672) 	ei->defrag_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8673) 	ei->disk_i_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8674) 	ei->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8675) 	ei->csum_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8676) 	ei->index_cnt = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8677) 	ei->dir_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8678) 	ei->last_unlink_trans = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8679) 	ei->last_reflink_trans = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8680) 	ei->last_log_commit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8682) 	spin_lock_init(&ei->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8683) 	ei->outstanding_extents = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8684) 	if (sb->s_magic != BTRFS_TEST_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8685) 		btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8686) 					      BTRFS_BLOCK_RSV_DELALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8687) 	ei->runtime_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8688) 	ei->prop_compress = BTRFS_COMPRESS_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8689) 	ei->defrag_compress = BTRFS_COMPRESS_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8691) 	ei->delayed_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8693) 	ei->i_otime.tv_sec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8694) 	ei->i_otime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8696) 	inode = &ei->vfs_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8697) 	extent_map_tree_init(&ei->extent_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8698) 	extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8699) 	extent_io_tree_init(fs_info, &ei->io_failure_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8700) 			    IO_TREE_INODE_IO_FAILURE, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8701) 	extent_io_tree_init(fs_info, &ei->file_extent_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8702) 			    IO_TREE_INODE_FILE_EXTENT, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8703) 	ei->io_tree.track_uptodate = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8704) 	ei->io_failure_tree.track_uptodate = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8705) 	atomic_set(&ei->sync_writers, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8706) 	mutex_init(&ei->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8707) 	btrfs_ordered_inode_tree_init(&ei->ordered_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8708) 	INIT_LIST_HEAD(&ei->delalloc_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8709) 	INIT_LIST_HEAD(&ei->delayed_iput);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8710) 	RB_CLEAR_NODE(&ei->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8711) 	init_rwsem(&ei->dio_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8713) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8716) #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8717) void btrfs_test_destroy_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8719) 	btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8720) 	kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8722) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8724) void btrfs_free_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8725) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8726) 	kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8729) void btrfs_destroy_inode(struct inode *vfs_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8730) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8731) 	struct btrfs_ordered_extent *ordered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8732) 	struct btrfs_inode *inode = BTRFS_I(vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8733) 	struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8735) 	WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8736) 	WARN_ON(vfs_inode->i_data.nrpages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8737) 	WARN_ON(inode->block_rsv.reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8738) 	WARN_ON(inode->block_rsv.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8739) 	WARN_ON(inode->outstanding_extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8740) 	WARN_ON(inode->delalloc_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8741) 	WARN_ON(inode->new_delalloc_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8742) 	WARN_ON(inode->csum_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8743) 	WARN_ON(inode->defrag_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8745) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8746) 	 * This can happen where we create an inode, but somebody else also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8747) 	 * created the same inode and we need to destroy the one we already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8748) 	 * created.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8749) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8750) 	if (!root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8751) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8753) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8754) 		ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8755) 		if (!ordered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8756) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8757) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8758) 			btrfs_err(root->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8759) 				  "found ordered extent %llu %llu on inode cleanup",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8760) 				  ordered->file_offset, ordered->num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8761) 			btrfs_remove_ordered_extent(inode, ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8762) 			btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8763) 			btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8764) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8765) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8766) 	btrfs_qgroup_check_reserved_leak(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8767) 	inode_tree_del(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8768) 	btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8769) 	btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8770) 	btrfs_put_root(inode->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8773) int btrfs_drop_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8774) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8775) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8777) 	if (root == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8778) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8780) 	/* the snap/subvol tree is on deleting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8781) 	if (btrfs_root_refs(&root->root_item) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8782) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8783) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8784) 		return generic_drop_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8787) static void init_once(void *foo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8788) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8789) 	struct btrfs_inode *ei = (struct btrfs_inode *) foo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8791) 	inode_init_once(&ei->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8794) void __cold btrfs_destroy_cachep(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8795) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8796) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8797) 	 * Make sure all delayed rcu free inodes are flushed before we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8798) 	 * destroy cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8799) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8800) 	rcu_barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8801) 	kmem_cache_destroy(btrfs_inode_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8802) 	kmem_cache_destroy(btrfs_trans_handle_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8803) 	kmem_cache_destroy(btrfs_path_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8804) 	kmem_cache_destroy(btrfs_free_space_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8805) 	kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8808) int __init btrfs_init_cachep(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8809) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8810) 	btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8811) 			sizeof(struct btrfs_inode), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8812) 			SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8813) 			init_once);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8814) 	if (!btrfs_inode_cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8815) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8817) 	btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8818) 			sizeof(struct btrfs_trans_handle), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8819) 			SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8820) 	if (!btrfs_trans_handle_cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8821) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8823) 	btrfs_path_cachep = kmem_cache_create("btrfs_path",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8824) 			sizeof(struct btrfs_path), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8825) 			SLAB_MEM_SPREAD, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8826) 	if (!btrfs_path_cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8827) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8829) 	btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8830) 			sizeof(struct btrfs_free_space), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8831) 			SLAB_MEM_SPREAD, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8832) 	if (!btrfs_free_space_cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8833) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8835) 	btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8836) 							PAGE_SIZE, PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8837) 							SLAB_MEM_SPREAD, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8838) 	if (!btrfs_free_space_bitmap_cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8839) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8841) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8842) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8843) 	btrfs_destroy_cachep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8844) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8847) static int btrfs_getattr(const struct path *path, struct kstat *stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8848) 			 u32 request_mask, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8849) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8850) 	u64 delalloc_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8851) 	struct inode *inode = d_inode(path->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8852) 	u32 blocksize = inode->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8853) 	u32 bi_flags = BTRFS_I(inode)->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8855) 	stat->result_mask |= STATX_BTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8856) 	stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8857) 	stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8858) 	if (bi_flags & BTRFS_INODE_APPEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8859) 		stat->attributes |= STATX_ATTR_APPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8860) 	if (bi_flags & BTRFS_INODE_COMPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8861) 		stat->attributes |= STATX_ATTR_COMPRESSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8862) 	if (bi_flags & BTRFS_INODE_IMMUTABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8863) 		stat->attributes |= STATX_ATTR_IMMUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8864) 	if (bi_flags & BTRFS_INODE_NODUMP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8865) 		stat->attributes |= STATX_ATTR_NODUMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8867) 	stat->attributes_mask |= (STATX_ATTR_APPEND |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8868) 				  STATX_ATTR_COMPRESSED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8869) 				  STATX_ATTR_IMMUTABLE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8870) 				  STATX_ATTR_NODUMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8872) 	generic_fillattr(inode, stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8873) 	stat->dev = BTRFS_I(inode)->root->anon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8875) 	spin_lock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8876) 	delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8877) 	spin_unlock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8878) 	stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8879) 			ALIGN(delalloc_bytes, blocksize)) >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8880) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8881) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8883) static int btrfs_rename_exchange(struct inode *old_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8884) 			      struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8885) 			      struct inode *new_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8886) 			      struct dentry *new_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8887) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8888) 	struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8889) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8890) 	struct btrfs_root *root = BTRFS_I(old_dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8891) 	struct btrfs_root *dest = BTRFS_I(new_dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8892) 	struct inode *new_inode = new_dentry->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8893) 	struct inode *old_inode = old_dentry->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8894) 	struct timespec64 ctime = current_time(old_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8895) 	u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8896) 	u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8897) 	u64 old_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8898) 	u64 new_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8899) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8900) 	int ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8901) 	bool root_log_pinned = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8902) 	bool dest_log_pinned = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8903) 	bool need_abort = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8905) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8906) 	 * For non-subvolumes allow exchange only within one subvolume, in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8907) 	 * same inode namespace. Two subvolumes (represented as directory) can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8908) 	 * be exchanged as they're a logical link and have a fixed inode number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8909) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8910) 	if (root != dest &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8911) 	    (old_ino != BTRFS_FIRST_FREE_OBJECTID ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8912) 	     new_ino != BTRFS_FIRST_FREE_OBJECTID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8913) 		return -EXDEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8915) 	/* close the race window with snapshot create/destroy ioctl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8916) 	if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8917) 	    new_ino == BTRFS_FIRST_FREE_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8918) 		down_read(&fs_info->subvol_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8920) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8921) 	 * We want to reserve the absolute worst case amount of items.  So if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8922) 	 * both inodes are subvols and we need to unlink them then that would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8923) 	 * require 4 item modifications, but if they are both normal inodes it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8924) 	 * would require 5 item modifications, so we'll assume their normal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8925) 	 * inodes.  So 5 * 2 is 10, plus 2 for the new links, so 12 total items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8926) 	 * should cover the worst case number of items we'll modify.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8927) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8928) 	trans = btrfs_start_transaction(root, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8929) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8930) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8931) 		goto out_notrans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8932) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8934) 	if (dest != root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8935) 		btrfs_record_root_in_trans(trans, dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8937) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8938) 	 * We need to find a free sequence number both in the source and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8939) 	 * in the destination directory for the exchange.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8940) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8941) 	ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8942) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8943) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8944) 	ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8945) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8946) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8948) 	BTRFS_I(old_inode)->dir_index = 0ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8949) 	BTRFS_I(new_inode)->dir_index = 0ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8951) 	/* Reference for the source. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8952) 	if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8953) 		/* force full log commit if subvolume involved. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8954) 		btrfs_set_log_full_commit(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8955) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8956) 		btrfs_pin_log_trans(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8957) 		root_log_pinned = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8958) 		ret = btrfs_insert_inode_ref(trans, dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8959) 					     new_dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8960) 					     new_dentry->d_name.len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8961) 					     old_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8962) 					     btrfs_ino(BTRFS_I(new_dir)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8963) 					     old_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8964) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8965) 			goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8966) 		need_abort = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8967) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8969) 	/* And now for the dest. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8970) 	if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8971) 		/* force full log commit if subvolume involved. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8972) 		btrfs_set_log_full_commit(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8973) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8974) 		btrfs_pin_log_trans(dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8975) 		dest_log_pinned = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8976) 		ret = btrfs_insert_inode_ref(trans, root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8977) 					     old_dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8978) 					     old_dentry->d_name.len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8979) 					     new_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8980) 					     btrfs_ino(BTRFS_I(old_dir)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8981) 					     new_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8982) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8983) 			if (need_abort)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8984) 				btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8985) 			goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8986) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8987) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8989) 	/* Update inode version and ctime/mtime. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8990) 	inode_inc_iversion(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8991) 	inode_inc_iversion(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8992) 	inode_inc_iversion(old_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8993) 	inode_inc_iversion(new_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8994) 	old_dir->i_ctime = old_dir->i_mtime = ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8995) 	new_dir->i_ctime = new_dir->i_mtime = ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8996) 	old_inode->i_ctime = ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8997) 	new_inode->i_ctime = ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8999) 	if (old_dentry->d_parent != new_dentry->d_parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9000) 		btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9001) 				BTRFS_I(old_inode), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9002) 		btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9003) 				BTRFS_I(new_inode), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9004) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9006) 	/* src is a subvolume */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9007) 	if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9008) 		ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9009) 	} else { /* src is an inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9010) 		ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9011) 					   BTRFS_I(old_dentry->d_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9012) 					   old_dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9013) 					   old_dentry->d_name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9014) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9015) 			ret = btrfs_update_inode(trans, root, old_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9016) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9017) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9018) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9019) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9020) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9022) 	/* dest is a subvolume */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9023) 	if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9024) 		ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9025) 	} else { /* dest is an inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9026) 		ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9027) 					   BTRFS_I(new_dentry->d_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9028) 					   new_dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9029) 					   new_dentry->d_name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9030) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9031) 			ret = btrfs_update_inode(trans, dest, new_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9032) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9033) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9034) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9035) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9036) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9038) 	ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9039) 			     new_dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9040) 			     new_dentry->d_name.len, 0, old_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9041) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9042) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9043) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9044) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9046) 	ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9047) 			     old_dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9048) 			     old_dentry->d_name.len, 0, new_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9049) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9050) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9051) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9052) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9054) 	if (old_inode->i_nlink == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9055) 		BTRFS_I(old_inode)->dir_index = old_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9056) 	if (new_inode->i_nlink == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9057) 		BTRFS_I(new_inode)->dir_index = new_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9059) 	if (root_log_pinned) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9060) 		btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9061) 				   new_dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9062) 		btrfs_end_log_trans(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9063) 		root_log_pinned = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9064) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9065) 	if (dest_log_pinned) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9066) 		btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9067) 				   old_dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9068) 		btrfs_end_log_trans(dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9069) 		dest_log_pinned = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9070) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9071) out_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9072) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9073) 	 * If we have pinned a log and an error happened, we unpin tasks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9074) 	 * trying to sync the log and force them to fallback to a transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9075) 	 * commit if the log currently contains any of the inodes involved in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9076) 	 * this rename operation (to ensure we do not persist a log with an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9077) 	 * inconsistent state for any of these inodes or leading to any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9078) 	 * inconsistencies when replayed). If the transaction was aborted, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9079) 	 * abortion reason is propagated to userspace when attempting to commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9080) 	 * the transaction. If the log does not contain any of these inodes, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9081) 	 * allow the tasks to sync it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9082) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9083) 	if (ret && (root_log_pinned || dest_log_pinned)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9084) 		if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9085) 		    btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9086) 		    btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9087) 		    (new_inode &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9088) 		     btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9089) 			btrfs_set_log_full_commit(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9091) 		if (root_log_pinned) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9092) 			btrfs_end_log_trans(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9093) 			root_log_pinned = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9094) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9095) 		if (dest_log_pinned) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9096) 			btrfs_end_log_trans(dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9097) 			dest_log_pinned = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9098) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9099) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9100) 	ret2 = btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9101) 	ret = ret ? ret : ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9102) out_notrans:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9103) 	if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9104) 	    old_ino == BTRFS_FIRST_FREE_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9105) 		up_read(&fs_info->subvol_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9107) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9110) static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9111) 				     struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9112) 				     struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9113) 				     struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9115) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9116) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9117) 	u64 objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9118) 	u64 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9120) 	ret = btrfs_find_free_ino(root, &objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9121) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9122) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9124) 	inode = btrfs_new_inode(trans, root, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9125) 				dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9126) 				dentry->d_name.len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9127) 				btrfs_ino(BTRFS_I(dir)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9128) 				objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9129) 				S_IFCHR | WHITEOUT_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9130) 				&index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9132) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9133) 		ret = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9134) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9137) 	inode->i_op = &btrfs_special_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9138) 	init_special_inode(inode, inode->i_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9139) 		WHITEOUT_DEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9141) 	ret = btrfs_init_inode_security(trans, inode, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9142) 				&dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9143) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9144) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9146) 	ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9147) 				BTRFS_I(inode), 0, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9148) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9149) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9151) 	ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9152) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9153) 	unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9154) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9155) 		inode_dec_link_count(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9156) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9158) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9161) static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9162) 			   struct inode *new_dir, struct dentry *new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9163) 			   unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9165) 	struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9166) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9167) 	unsigned int trans_num_items;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9168) 	struct btrfs_root *root = BTRFS_I(old_dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9169) 	struct btrfs_root *dest = BTRFS_I(new_dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9170) 	struct inode *new_inode = d_inode(new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9171) 	struct inode *old_inode = d_inode(old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9172) 	u64 index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9173) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9174) 	int ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9175) 	u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9176) 	bool log_pinned = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9178) 	if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9179) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9181) 	/* we only allow rename subvolume link between subvolumes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9182) 	if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9183) 		return -EXDEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9185) 	if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9186) 	    (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9187) 		return -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9189) 	if (S_ISDIR(old_inode->i_mode) && new_inode &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9190) 	    new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9191) 		return -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9194) 	/* check for collisions, even if the  name isn't there */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9195) 	ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9196) 			     new_dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9197) 			     new_dentry->d_name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9199) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9200) 		if (ret == -EEXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9201) 			/* we shouldn't get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9202) 			 * eexist without a new_inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9203) 			if (WARN_ON(!new_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9204) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9205) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9206) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9207) 			/* maybe -EOVERFLOW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9208) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9209) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9211) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9213) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9214) 	 * we're using rename to replace one file with another.  Start IO on it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9215) 	 * now so  we don't add too much work to the end of the transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9216) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9217) 	if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9218) 		filemap_flush(old_inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9220) 	/* close the racy window with snapshot create/destroy ioctl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9221) 	if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9222) 		down_read(&fs_info->subvol_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9223) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9224) 	 * We want to reserve the absolute worst case amount of items.  So if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9225) 	 * both inodes are subvols and we need to unlink them then that would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9226) 	 * require 4 item modifications, but if they are both normal inodes it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9227) 	 * would require 5 item modifications, so we'll assume they are normal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9228) 	 * inodes.  So 5 * 2 is 10, plus 1 for the new link, so 11 total items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9229) 	 * should cover the worst case number of items we'll modify.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9230) 	 * If our rename has the whiteout flag, we need more 5 units for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9231) 	 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9232) 	 * when selinux is enabled).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9233) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9234) 	trans_num_items = 11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9235) 	if (flags & RENAME_WHITEOUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9236) 		trans_num_items += 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9237) 	trans = btrfs_start_transaction(root, trans_num_items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9238) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9239) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9240) 		goto out_notrans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9241) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9243) 	if (dest != root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9244) 		btrfs_record_root_in_trans(trans, dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9246) 	ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9247) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9248) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9250) 	BTRFS_I(old_inode)->dir_index = 0ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9251) 	if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9252) 		/* force full log commit if subvolume involved. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9253) 		btrfs_set_log_full_commit(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9254) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9255) 		btrfs_pin_log_trans(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9256) 		log_pinned = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9257) 		ret = btrfs_insert_inode_ref(trans, dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9258) 					     new_dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9259) 					     new_dentry->d_name.len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9260) 					     old_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9261) 					     btrfs_ino(BTRFS_I(new_dir)), index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9262) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9263) 			goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9264) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9266) 	inode_inc_iversion(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9267) 	inode_inc_iversion(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9268) 	inode_inc_iversion(old_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9269) 	old_dir->i_ctime = old_dir->i_mtime =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9270) 	new_dir->i_ctime = new_dir->i_mtime =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9271) 	old_inode->i_ctime = current_time(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9273) 	if (old_dentry->d_parent != new_dentry->d_parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9274) 		btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9275) 				BTRFS_I(old_inode), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9277) 	if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9278) 		ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9279) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9280) 		ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9281) 					BTRFS_I(d_inode(old_dentry)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9282) 					old_dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9283) 					old_dentry->d_name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9284) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9285) 			ret = btrfs_update_inode(trans, root, old_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9287) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9288) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9289) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9290) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9292) 	if (new_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9293) 		inode_inc_iversion(new_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9294) 		new_inode->i_ctime = current_time(new_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9295) 		if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9296) 			     BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9297) 			ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9298) 			BUG_ON(new_inode->i_nlink == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9299) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9300) 			ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9301) 						 BTRFS_I(d_inode(new_dentry)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9302) 						 new_dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9303) 						 new_dentry->d_name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9304) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9305) 		if (!ret && new_inode->i_nlink == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9306) 			ret = btrfs_orphan_add(trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9307) 					BTRFS_I(d_inode(new_dentry)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9308) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9309) 			btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9310) 			goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9311) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9314) 	ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9315) 			     new_dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9316) 			     new_dentry->d_name.len, 0, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9317) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9318) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9319) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9322) 	if (old_inode->i_nlink == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9323) 		BTRFS_I(old_inode)->dir_index = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9325) 	if (log_pinned) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9326) 		btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9327) 				   new_dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9328) 		btrfs_end_log_trans(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9329) 		log_pinned = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9330) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9332) 	if (flags & RENAME_WHITEOUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9333) 		ret = btrfs_whiteout_for_rename(trans, root, old_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9334) 						old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9336) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9337) 			btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9338) 			goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9339) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9341) out_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9342) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9343) 	 * If we have pinned the log and an error happened, we unpin tasks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9344) 	 * trying to sync the log and force them to fallback to a transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9345) 	 * commit if the log currently contains any of the inodes involved in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9346) 	 * this rename operation (to ensure we do not persist a log with an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9347) 	 * inconsistent state for any of these inodes or leading to any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9348) 	 * inconsistencies when replayed). If the transaction was aborted, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9349) 	 * abortion reason is propagated to userspace when attempting to commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9350) 	 * the transaction. If the log does not contain any of these inodes, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9351) 	 * allow the tasks to sync it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9352) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9353) 	if (ret && log_pinned) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9354) 		if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9355) 		    btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9356) 		    btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9357) 		    (new_inode &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9358) 		     btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9359) 			btrfs_set_log_full_commit(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9361) 		btrfs_end_log_trans(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9362) 		log_pinned = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9364) 	ret2 = btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9365) 	ret = ret ? ret : ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9366) out_notrans:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9367) 	if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9368) 		up_read(&fs_info->subvol_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9370) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9373) static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9374) 			 struct inode *new_dir, struct dentry *new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9375) 			 unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9377) 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9378) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9380) 	if (flags & RENAME_EXCHANGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9381) 		return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9382) 					  new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9384) 	return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9387) struct btrfs_delalloc_work {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9388) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9389) 	struct completion completion;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9390) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9391) 	struct btrfs_work work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9392) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9394) static void btrfs_run_delalloc_work(struct btrfs_work *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9396) 	struct btrfs_delalloc_work *delalloc_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9397) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9399) 	delalloc_work = container_of(work, struct btrfs_delalloc_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9400) 				     work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9401) 	inode = delalloc_work->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9402) 	filemap_flush(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9403) 	if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9404) 				&BTRFS_I(inode)->runtime_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9405) 		filemap_flush(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9407) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9408) 	complete(&delalloc_work->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9411) static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9413) 	struct btrfs_delalloc_work *work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9415) 	work = kmalloc(sizeof(*work), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9416) 	if (!work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9417) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9419) 	init_completion(&work->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9420) 	INIT_LIST_HEAD(&work->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9421) 	work->inode = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9422) 	btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9424) 	return work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9427) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9428)  * some fairly slow code that needs optimization. This walks the list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9429)  * of all the inodes with pending delalloc and forces them to disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9430)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9431) static int start_delalloc_inodes(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9432) 				 struct writeback_control *wbc, bool snapshot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9433) 				 bool in_reclaim_context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9435) 	struct btrfs_inode *binode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9436) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9437) 	struct btrfs_delalloc_work *work, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9438) 	struct list_head works;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9439) 	struct list_head splice;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9440) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9441) 	bool full_flush = wbc->nr_to_write == LONG_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9443) 	INIT_LIST_HEAD(&works);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9444) 	INIT_LIST_HEAD(&splice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9446) 	mutex_lock(&root->delalloc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9447) 	spin_lock(&root->delalloc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9448) 	list_splice_init(&root->delalloc_inodes, &splice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9449) 	while (!list_empty(&splice)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9450) 		binode = list_entry(splice.next, struct btrfs_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9451) 				    delalloc_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9453) 		list_move_tail(&binode->delalloc_inodes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9454) 			       &root->delalloc_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9456) 		if (in_reclaim_context &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9457) 		    test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9458) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9460) 		inode = igrab(&binode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9461) 		if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9462) 			cond_resched_lock(&root->delalloc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9463) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9464) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9465) 		spin_unlock(&root->delalloc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9467) 		if (snapshot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9468) 			set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9469) 				&binode->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9470) 		if (full_flush) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9471) 			work = btrfs_alloc_delalloc_work(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9472) 			if (!work) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9473) 				iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9474) 				ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9475) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9476) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9477) 			list_add_tail(&work->list, &works);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9478) 			btrfs_queue_work(root->fs_info->flush_workers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9479) 					 &work->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9480) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9481) 			ret = sync_inode(inode, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9482) 			if (!ret &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9483) 			    test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9484) 				     &BTRFS_I(inode)->runtime_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9485) 				ret = sync_inode(inode, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9486) 			btrfs_add_delayed_iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9487) 			if (ret || wbc->nr_to_write <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9488) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9489) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9490) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9491) 		spin_lock(&root->delalloc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9492) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9493) 	spin_unlock(&root->delalloc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9495) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9496) 	list_for_each_entry_safe(work, next, &works, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9497) 		list_del_init(&work->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9498) 		wait_for_completion(&work->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9499) 		kfree(work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9500) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9502) 	if (!list_empty(&splice)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9503) 		spin_lock(&root->delalloc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9504) 		list_splice_tail(&splice, &root->delalloc_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9505) 		spin_unlock(&root->delalloc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9506) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9507) 	mutex_unlock(&root->delalloc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9508) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9511) int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9513) 	struct writeback_control wbc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9514) 		.nr_to_write = LONG_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9515) 		.sync_mode = WB_SYNC_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9516) 		.range_start = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9517) 		.range_end = LLONG_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9518) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9519) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9521) 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9522) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9524) 	return start_delalloc_inodes(root, &wbc, true, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9527) int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, u64 nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9528) 			       bool in_reclaim_context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9530) 	struct writeback_control wbc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9531) 		.nr_to_write = (nr == U64_MAX) ? LONG_MAX : (unsigned long)nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9532) 		.sync_mode = WB_SYNC_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9533) 		.range_start = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9534) 		.range_end = LLONG_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9535) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9536) 	struct btrfs_root *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9537) 	struct list_head splice;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9538) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9540) 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9541) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9543) 	INIT_LIST_HEAD(&splice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9545) 	mutex_lock(&fs_info->delalloc_root_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9546) 	spin_lock(&fs_info->delalloc_root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9547) 	list_splice_init(&fs_info->delalloc_roots, &splice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9548) 	while (!list_empty(&splice) && nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9549) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9550) 		 * Reset nr_to_write here so we know that we're doing a full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9551) 		 * flush.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9552) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9553) 		if (nr == U64_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9554) 			wbc.nr_to_write = LONG_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9556) 		root = list_first_entry(&splice, struct btrfs_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9557) 					delalloc_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9558) 		root = btrfs_grab_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9559) 		BUG_ON(!root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9560) 		list_move_tail(&root->delalloc_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9561) 			       &fs_info->delalloc_roots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9562) 		spin_unlock(&fs_info->delalloc_root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9564) 		ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9565) 		btrfs_put_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9566) 		if (ret < 0 || wbc.nr_to_write <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9567) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9568) 		spin_lock(&fs_info->delalloc_root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9569) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9570) 	spin_unlock(&fs_info->delalloc_root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9572) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9573) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9574) 	if (!list_empty(&splice)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9575) 		spin_lock(&fs_info->delalloc_root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9576) 		list_splice_tail(&splice, &fs_info->delalloc_roots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9577) 		spin_unlock(&fs_info->delalloc_root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9578) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9579) 	mutex_unlock(&fs_info->delalloc_root_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9580) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9583) static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9584) 			 const char *symname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9585) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9586) 	struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9587) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9588) 	struct btrfs_root *root = BTRFS_I(dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9589) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9590) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9591) 	struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9592) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9593) 	u64 objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9594) 	u64 index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9595) 	int name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9596) 	int datasize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9597) 	unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9598) 	struct btrfs_file_extent_item *ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9599) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9601) 	name_len = strlen(symname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9602) 	if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9603) 		return -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9605) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9606) 	 * 2 items for inode item and ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9607) 	 * 2 items for dir items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9608) 	 * 1 item for updating parent inode item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9609) 	 * 1 item for the inline extent item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9610) 	 * 1 item for xattr if selinux is on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9611) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9612) 	trans = btrfs_start_transaction(root, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9613) 	if (IS_ERR(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9614) 		return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9616) 	err = btrfs_find_free_ino(root, &objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9617) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9618) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9620) 	inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9621) 				dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9622) 				objectid, S_IFLNK|S_IRWXUGO, &index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9623) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9624) 		err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9625) 		inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9626) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9627) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9629) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9630) 	* If the active LSM wants to access the inode during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9631) 	* d_instantiate it needs these. Smack checks to see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9632) 	* if the filesystem supports xattrs by looking at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9633) 	* ops vector.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9634) 	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9635) 	inode->i_fop = &btrfs_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9636) 	inode->i_op = &btrfs_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9637) 	inode->i_mapping->a_ops = &btrfs_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9639) 	err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9640) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9641) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9643) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9644) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9645) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9646) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9647) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9648) 	key.objectid = btrfs_ino(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9649) 	key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9650) 	key.type = BTRFS_EXTENT_DATA_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9651) 	datasize = btrfs_file_extent_calc_inline_size(name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9652) 	err = btrfs_insert_empty_item(trans, root, path, &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9653) 				      datasize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9654) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9655) 		btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9656) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9657) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9658) 	leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9659) 	ei = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9660) 			    struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9661) 	btrfs_set_file_extent_generation(leaf, ei, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9662) 	btrfs_set_file_extent_type(leaf, ei,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9663) 				   BTRFS_FILE_EXTENT_INLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9664) 	btrfs_set_file_extent_encryption(leaf, ei, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9665) 	btrfs_set_file_extent_compression(leaf, ei, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9666) 	btrfs_set_file_extent_other_encoding(leaf, ei, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9667) 	btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9669) 	ptr = btrfs_file_extent_inline_start(ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9670) 	write_extent_buffer(leaf, symname, ptr, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9671) 	btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9672) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9674) 	inode->i_op = &btrfs_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9675) 	inode_nohighmem(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9676) 	inode_set_bytes(inode, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9677) 	btrfs_i_size_write(BTRFS_I(inode), name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9678) 	err = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9679) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9680) 	 * Last step, add directory indexes for our symlink inode. This is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9681) 	 * last step to avoid extra cleanup of these indexes if an error happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9682) 	 * elsewhere above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9683) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9684) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9685) 		err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9686) 				BTRFS_I(inode), 0, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9687) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9688) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9690) 	d_instantiate_new(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9692) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9693) 	btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9694) 	if (err && inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9695) 		inode_dec_link_count(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9696) 		discard_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9697) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9698) 	btrfs_btree_balance_dirty(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9699) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9702) static struct btrfs_trans_handle *insert_prealloc_file_extent(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9703) 				       struct btrfs_trans_handle *trans_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9704) 				       struct inode *inode, struct btrfs_key *ins,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9705) 				       u64 file_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9707) 	struct btrfs_file_extent_item stack_fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9708) 	struct btrfs_replace_extent_info extent_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9709) 	struct btrfs_trans_handle *trans = trans_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9710) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9711) 	u64 start = ins->objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9712) 	u64 len = ins->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9713) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9715) 	memset(&stack_fi, 0, sizeof(stack_fi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9717) 	btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9718) 	btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9719) 	btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9720) 	btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9721) 	btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9722) 	btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9723) 	/* Encryption and other encoding is reserved and all 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9725) 	ret = btrfs_qgroup_release_data(BTRFS_I(inode), file_offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9726) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9727) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9729) 	if (trans) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9730) 		ret = insert_reserved_file_extent(trans, BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9731) 						  file_offset, &stack_fi, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9732) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9733) 			return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9734) 		return trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9735) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9737) 	extent_info.disk_offset = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9738) 	extent_info.disk_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9739) 	extent_info.data_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9740) 	extent_info.data_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9741) 	extent_info.file_offset = file_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9742) 	extent_info.extent_buf = (char *)&stack_fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9743) 	extent_info.is_new_extent = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9744) 	extent_info.qgroup_reserved = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9745) 	extent_info.insertions = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9747) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9748) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9749) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9751) 	ret = btrfs_replace_file_extents(inode, path, file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9752) 				     file_offset + len - 1, &extent_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9753) 				     &trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9754) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9755) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9756) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9758) 	return trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9761) static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9762) 				       u64 start, u64 num_bytes, u64 min_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9763) 				       loff_t actual_len, u64 *alloc_hint,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9764) 				       struct btrfs_trans_handle *trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9765) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9766) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9767) 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9768) 	struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9769) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9770) 	struct btrfs_key ins;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9771) 	u64 cur_offset = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9772) 	u64 clear_offset = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9773) 	u64 i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9774) 	u64 cur_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9775) 	u64 last_alloc = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9776) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9777) 	bool own_trans = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9778) 	u64 end = start + num_bytes - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9780) 	if (trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9781) 		own_trans = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9782) 	while (num_bytes > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9783) 		cur_bytes = min_t(u64, num_bytes, SZ_256M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9784) 		cur_bytes = max(cur_bytes, min_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9785) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9786) 		 * If we are severely fragmented we could end up with really
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9787) 		 * small allocations, so if the allocator is returning small
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9788) 		 * chunks lets make its job easier by only searching for those
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9789) 		 * sized chunks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9790) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9791) 		cur_bytes = min(cur_bytes, last_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9792) 		ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9793) 				min_size, 0, *alloc_hint, &ins, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9794) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9795) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9797) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9798) 		 * We've reserved this space, and thus converted it from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9799) 		 * ->bytes_may_use to ->bytes_reserved.  Any error that happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9800) 		 * from here on out we will only need to clear our reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9801) 		 * for the remaining unreserved area, so advance our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9802) 		 * clear_offset by our extent size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9803) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9804) 		clear_offset += ins.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9806) 		last_alloc = ins.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9807) 		trans = insert_prealloc_file_extent(trans, inode, &ins, cur_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9808) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9809) 		 * Now that we inserted the prealloc extent we can finally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9810) 		 * decrement the number of reservations in the block group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9811) 		 * If we did it before, we could race with relocation and have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9812) 		 * relocation miss the reserved extent, making it fail later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9813) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9814) 		btrfs_dec_block_group_reservations(fs_info, ins.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9815) 		if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9816) 			ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9817) 			btrfs_free_reserved_extent(fs_info, ins.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9818) 						   ins.offset, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9819) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9820) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9822) 		btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9823) 					cur_offset + ins.offset -1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9825) 		em = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9826) 		if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9827) 			set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9828) 				&BTRFS_I(inode)->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9829) 			goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9830) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9832) 		em->start = cur_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9833) 		em->orig_start = cur_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9834) 		em->len = ins.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9835) 		em->block_start = ins.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9836) 		em->block_len = ins.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9837) 		em->orig_block_len = ins.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9838) 		em->ram_bytes = ins.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9839) 		set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9840) 		em->generation = trans->transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9842) 		while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9843) 			write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9844) 			ret = add_extent_mapping(em_tree, em, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9845) 			write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9846) 			if (ret != -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9847) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9848) 			btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9849) 						cur_offset + ins.offset - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9850) 						0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9851) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9852) 		free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9853) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9854) 		num_bytes -= ins.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9855) 		cur_offset += ins.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9856) 		*alloc_hint = ins.objectid + ins.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9858) 		inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9859) 		inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9860) 		BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9861) 		if (!(mode & FALLOC_FL_KEEP_SIZE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9862) 		    (actual_len > inode->i_size) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9863) 		    (cur_offset > inode->i_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9864) 			if (cur_offset > actual_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9865) 				i_size = actual_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9866) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9867) 				i_size = cur_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9868) 			i_size_write(inode, i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9869) 			btrfs_inode_safe_disk_i_size_write(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9870) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9872) 		ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9874) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9875) 			btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9876) 			if (own_trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9877) 				btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9878) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9879) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9881) 		if (own_trans) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9882) 			btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9883) 			trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9884) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9885) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9886) 	if (clear_offset < end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9887) 		btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9888) 			end - clear_offset + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9889) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9892) int btrfs_prealloc_file_range(struct inode *inode, int mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9893) 			      u64 start, u64 num_bytes, u64 min_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9894) 			      loff_t actual_len, u64 *alloc_hint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9895) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9896) 	return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9897) 					   min_size, actual_len, alloc_hint,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9898) 					   NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9901) int btrfs_prealloc_file_range_trans(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9902) 				    struct btrfs_trans_handle *trans, int mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9903) 				    u64 start, u64 num_bytes, u64 min_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9904) 				    loff_t actual_len, u64 *alloc_hint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9905) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9906) 	return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9907) 					   min_size, actual_len, alloc_hint, trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9908) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9910) static int btrfs_set_page_dirty(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9911) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9912) 	return __set_page_dirty_nobuffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9915) static int btrfs_permission(struct inode *inode, int mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9916) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9917) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9918) 	umode_t mode = inode->i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9920) 	if (mask & MAY_WRITE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9921) 	    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9922) 		if (btrfs_root_readonly(root))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9923) 			return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9924) 		if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9925) 			return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9926) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9927) 	return generic_permission(inode, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9928) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9930) static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9931) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9932) 	struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9933) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9934) 	struct btrfs_root *root = BTRFS_I(dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9935) 	struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9936) 	u64 objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9937) 	u64 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9938) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9940) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9941) 	 * 5 units required for adding orphan entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9942) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9943) 	trans = btrfs_start_transaction(root, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9944) 	if (IS_ERR(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9945) 		return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9947) 	ret = btrfs_find_free_ino(root, &objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9948) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9949) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9951) 	inode = btrfs_new_inode(trans, root, dir, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9952) 			btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9953) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9954) 		ret = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9955) 		inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9956) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9957) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9959) 	inode->i_fop = &btrfs_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9960) 	inode->i_op = &btrfs_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9962) 	inode->i_mapping->a_ops = &btrfs_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9964) 	ret = btrfs_init_inode_security(trans, inode, dir, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9965) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9966) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9968) 	ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9969) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9970) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9971) 	ret = btrfs_orphan_add(trans, BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9972) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9973) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9975) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9976) 	 * We set number of links to 0 in btrfs_new_inode(), and here we set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9977) 	 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9978) 	 * through:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9979) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9980) 	 *    d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9981) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9982) 	set_nlink(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9983) 	d_tmpfile(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9984) 	unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9985) 	mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9986) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9987) 	btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9988) 	if (ret && inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9989) 		discard_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9990) 	btrfs_btree_balance_dirty(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9991) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9992) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9994) void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9995) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9996) 	struct inode *inode = tree->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9997) 	unsigned long index = start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9998) 	unsigned long end_index = end >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9999) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10001) 	while (index <= end_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10002) 		page = find_get_page(inode->i_mapping, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10003) 		ASSERT(page); /* Pages should be in the extent_io_tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10004) 		set_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10005) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10006) 		index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10007) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10008) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10010) #ifdef CONFIG_SWAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10011) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10012)  * Add an entry indicating a block group or device which is pinned by a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10013)  * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10014)  * negative errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10015)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10016) static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10017) 				  bool is_block_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10018) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10019) 	struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10020) 	struct btrfs_swapfile_pin *sp, *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10021) 	struct rb_node **p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10022) 	struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10024) 	sp = kmalloc(sizeof(*sp), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10025) 	if (!sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10026) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10027) 	sp->ptr = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10028) 	sp->inode = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10029) 	sp->is_block_group = is_block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10030) 	sp->bg_extent_count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10032) 	spin_lock(&fs_info->swapfile_pins_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10033) 	p = &fs_info->swapfile_pins.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10034) 	while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10035) 		parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10036) 		entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10037) 		if (sp->ptr < entry->ptr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10038) 		    (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10039) 			p = &(*p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10040) 		} else if (sp->ptr > entry->ptr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10041) 			   (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10042) 			p = &(*p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10043) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10044) 			if (is_block_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10045) 				entry->bg_extent_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10046) 			spin_unlock(&fs_info->swapfile_pins_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10047) 			kfree(sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10048) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10049) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10050) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10051) 	rb_link_node(&sp->node, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10052) 	rb_insert_color(&sp->node, &fs_info->swapfile_pins);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10053) 	spin_unlock(&fs_info->swapfile_pins_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10054) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10055) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10057) /* Free all of the entries pinned by this swapfile. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10058) static void btrfs_free_swapfile_pins(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10059) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10060) 	struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10061) 	struct btrfs_swapfile_pin *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10062) 	struct rb_node *node, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10064) 	spin_lock(&fs_info->swapfile_pins_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10065) 	node = rb_first(&fs_info->swapfile_pins);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10066) 	while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10067) 		next = rb_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10068) 		sp = rb_entry(node, struct btrfs_swapfile_pin, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10069) 		if (sp->inode == inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10070) 			rb_erase(&sp->node, &fs_info->swapfile_pins);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10071) 			if (sp->is_block_group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10072) 				btrfs_dec_block_group_swap_extents(sp->ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10073) 							   sp->bg_extent_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10074) 				btrfs_put_block_group(sp->ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10075) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10076) 			kfree(sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10077) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10078) 		node = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10079) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10080) 	spin_unlock(&fs_info->swapfile_pins_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10081) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10083) struct btrfs_swap_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10084) 	u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10085) 	u64 block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10086) 	u64 block_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10087) 	u64 lowest_ppage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10088) 	u64 highest_ppage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10089) 	unsigned long nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10090) 	int nr_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10091) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10093) static int btrfs_add_swap_extent(struct swap_info_struct *sis,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10094) 				 struct btrfs_swap_info *bsi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10095) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10096) 	unsigned long nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10097) 	unsigned long max_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10098) 	u64 first_ppage, first_ppage_reported, next_ppage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10099) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10101) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10102) 	 * Our swapfile may have had its size extended after the swap header was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10103) 	 * written. In that case activating the swapfile should not go beyond
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10104) 	 * the max size set in the swap header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10105) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10106) 	if (bsi->nr_pages >= sis->max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10107) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10109) 	max_pages = sis->max - bsi->nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10110) 	first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10111) 	next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10112) 				PAGE_SIZE) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10114) 	if (first_ppage >= next_ppage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10115) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10116) 	nr_pages = next_ppage - first_ppage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10117) 	nr_pages = min(nr_pages, max_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10119) 	first_ppage_reported = first_ppage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10120) 	if (bsi->start == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10121) 		first_ppage_reported++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10122) 	if (bsi->lowest_ppage > first_ppage_reported)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10123) 		bsi->lowest_ppage = first_ppage_reported;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10124) 	if (bsi->highest_ppage < (next_ppage - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10125) 		bsi->highest_ppage = next_ppage - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10127) 	ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10128) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10129) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10130) 	bsi->nr_extents += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10131) 	bsi->nr_pages += nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10132) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10135) static void btrfs_swap_deactivate(struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10137) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10139) 	btrfs_free_swapfile_pins(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10140) 	atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10143) static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10144) 			       sector_t *span)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10146) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10147) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10148) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10149) 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10150) 	struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10151) 	struct extent_map *em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10152) 	struct btrfs_device *device = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10153) 	struct btrfs_swap_info bsi = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10154) 		.lowest_ppage = (sector_t)-1ULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10155) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10156) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10157) 	u64 isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10158) 	u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10160) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10161) 	 * If the swap file was just created, make sure delalloc is done. If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10162) 	 * file changes again after this, the user is doing something stupid and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10163) 	 * we don't really care.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10164) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10165) 	ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10166) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10167) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10169) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10170) 	 * The inode is locked, so these flags won't change after we check them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10171) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10172) 	if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10173) 		btrfs_warn(fs_info, "swapfile must not be compressed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10174) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10176) 	if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10177) 		btrfs_warn(fs_info, "swapfile must not be copy-on-write");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10178) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10179) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10180) 	if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10181) 		btrfs_warn(fs_info, "swapfile must not be checksummed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10182) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10185) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10186) 	 * Balance or device remove/replace/resize can move stuff around from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10187) 	 * under us. The exclop protection makes sure they aren't running/won't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10188) 	 * run concurrently while we are mapping the swap extents, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10189) 	 * fs_info->swapfile_pins prevents them from running while the swap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10190) 	 * file is active and moving the extents. Note that this also prevents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10191) 	 * a concurrent device add which isn't actually necessary, but it's not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10192) 	 * really worth the trouble to allow it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10193) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10194) 	if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10195) 		btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10196) 	   "cannot activate swapfile while exclusive operation is running");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10197) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10200) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10201) 	 * Prevent snapshot creation while we are activating the swap file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10202) 	 * We do not want to race with snapshot creation. If snapshot creation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10203) 	 * already started before we bumped nr_swapfiles from 0 to 1 and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10204) 	 * completes before the first write into the swap file after it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10205) 	 * activated, than that write would fallback to COW.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10206) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10207) 	if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10208) 		btrfs_exclop_finish(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10209) 		btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10210) 	   "cannot activate swapfile because snapshot creation is in progress");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10211) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10212) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10213) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10214) 	 * Snapshots can create extents which require COW even if NODATACOW is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10215) 	 * set. We use this counter to prevent snapshots. We must increment it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10216) 	 * before walking the extents because we don't want a concurrent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10217) 	 * snapshot to run after we've already checked the extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10218) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10219) 	atomic_inc(&root->nr_swapfiles);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10221) 	isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10223) 	lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10224) 	start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10225) 	while (start < isize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10226) 		u64 logical_block_start, physical_block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10227) 		struct btrfs_block_group *bg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10228) 		u64 len = isize - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10230) 		em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10231) 		if (IS_ERR(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10232) 			ret = PTR_ERR(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10233) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10234) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10236) 		if (em->block_start == EXTENT_MAP_HOLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10237) 			btrfs_warn(fs_info, "swapfile must not have holes");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10238) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10239) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10240) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10241) 		if (em->block_start == EXTENT_MAP_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10242) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10243) 			 * It's unlikely we'll ever actually find ourselves
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10244) 			 * here, as a file small enough to fit inline won't be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10245) 			 * big enough to store more than the swap header, but in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10246) 			 * case something changes in the future, let's catch it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10247) 			 * here rather than later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10248) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10249) 			btrfs_warn(fs_info, "swapfile must not be inline");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10250) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10251) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10252) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10253) 		if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10254) 			btrfs_warn(fs_info, "swapfile must not be compressed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10255) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10256) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10257) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10259) 		logical_block_start = em->block_start + (start - em->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10260) 		len = min(len, em->len - (start - em->start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10261) 		free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10262) 		em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10264) 		ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10265) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10266) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10267) 		} else if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10268) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10269) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10270) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10271) 				   "swapfile must not be copy-on-write");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10272) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10273) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10274) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10276) 		em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10277) 		if (IS_ERR(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10278) 			ret = PTR_ERR(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10279) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10280) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10282) 		if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10283) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10284) 				   "swapfile must have single data profile");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10285) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10286) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10287) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10289) 		if (device == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10290) 			device = em->map_lookup->stripes[0].dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10291) 			ret = btrfs_add_swapfile_pin(inode, device, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10292) 			if (ret == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10293) 				ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10294) 			else if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10295) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10296) 		} else if (device != em->map_lookup->stripes[0].dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10297) 			btrfs_warn(fs_info, "swapfile must be on one device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10298) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10299) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10300) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10302) 		physical_block_start = (em->map_lookup->stripes[0].physical +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10303) 					(logical_block_start - em->start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10304) 		len = min(len, em->len - (logical_block_start - em->start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10305) 		free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10306) 		em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10308) 		bg = btrfs_lookup_block_group(fs_info, logical_block_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10309) 		if (!bg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10310) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10311) 			   "could not find block group containing swapfile");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10312) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10313) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10314) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10316) 		if (!btrfs_inc_block_group_swap_extents(bg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10317) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10318) 			   "block group for swapfile at %llu is read-only%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10319) 			   bg->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10320) 			   atomic_read(&fs_info->scrubs_running) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10321) 				       " (scrub running)" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10322) 			btrfs_put_block_group(bg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10323) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10324) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10325) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10327) 		ret = btrfs_add_swapfile_pin(inode, bg, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10328) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10329) 			btrfs_put_block_group(bg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10330) 			if (ret == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10331) 				ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10332) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10333) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10334) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10336) 		if (bsi.block_len &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10337) 		    bsi.block_start + bsi.block_len == physical_block_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10338) 			bsi.block_len += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10339) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10340) 			if (bsi.block_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10341) 				ret = btrfs_add_swap_extent(sis, &bsi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10342) 				if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10343) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10344) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10345) 			bsi.start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10346) 			bsi.block_start = physical_block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10347) 			bsi.block_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10348) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10350) 		start += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10351) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10353) 	if (bsi.block_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10354) 		ret = btrfs_add_swap_extent(sis, &bsi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10356) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10357) 	if (!IS_ERR_OR_NULL(em))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10358) 		free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10360) 	unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10362) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10363) 		btrfs_swap_deactivate(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10365) 	btrfs_drew_write_unlock(&root->snapshot_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10367) 	btrfs_exclop_finish(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10369) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10370) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10372) 	if (device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10373) 		sis->bdev = device->bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10374) 	*span = bsi.highest_ppage - bsi.lowest_ppage + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10375) 	sis->max = bsi.nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10376) 	sis->pages = bsi.nr_pages - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10377) 	sis->highest_bit = bsi.nr_pages - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10378) 	return bsi.nr_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10380) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10381) static void btrfs_swap_deactivate(struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10385) static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10386) 			       sector_t *span)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10388) 	return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10390) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10392) static const struct inode_operations btrfs_dir_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10393) 	.getattr	= btrfs_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10394) 	.lookup		= btrfs_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10395) 	.create		= btrfs_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10396) 	.unlink		= btrfs_unlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10397) 	.link		= btrfs_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10398) 	.mkdir		= btrfs_mkdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10399) 	.rmdir		= btrfs_rmdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10400) 	.rename		= btrfs_rename2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10401) 	.symlink	= btrfs_symlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10402) 	.setattr	= btrfs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10403) 	.mknod		= btrfs_mknod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10404) 	.listxattr	= btrfs_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10405) 	.permission	= btrfs_permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10406) 	.get_acl	= btrfs_get_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10407) 	.set_acl	= btrfs_set_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10408) 	.update_time	= btrfs_update_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10409) 	.tmpfile        = btrfs_tmpfile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10410) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10412) static const struct file_operations btrfs_dir_file_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10413) 	.llseek		= generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10414) 	.read		= generic_read_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10415) 	.iterate_shared	= btrfs_real_readdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10416) 	.open		= btrfs_opendir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10417) 	.unlocked_ioctl	= btrfs_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10418) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10419) 	.compat_ioctl	= btrfs_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10420) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10421) 	.release        = btrfs_release_file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10422) 	.fsync		= btrfs_sync_file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10423) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10425) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10426)  * btrfs doesn't support the bmap operation because swapfiles
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10427)  * use bmap to make a mapping of extents in the file.  They assume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10428)  * these extents won't change over the life of the file and they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10429)  * use the bmap result to do IO directly to the drive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10430)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10431)  * the btrfs bmap call would return logical addresses that aren't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10432)  * suitable for IO and they also will change frequently as COW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10433)  * operations happen.  So, swapfile + btrfs == corruption.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10434)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10435)  * For now we're avoiding this by dropping bmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10436)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10437) static const struct address_space_operations btrfs_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10438) 	.readpage	= btrfs_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10439) 	.writepage	= btrfs_writepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10440) 	.writepages	= btrfs_writepages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10441) 	.readahead	= btrfs_readahead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10442) 	.direct_IO	= noop_direct_IO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10443) 	.invalidatepage = btrfs_invalidatepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10444) 	.releasepage	= btrfs_releasepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10445) #ifdef CONFIG_MIGRATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10446) 	.migratepage	= btrfs_migratepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10447) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10448) 	.set_page_dirty	= btrfs_set_page_dirty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10449) 	.error_remove_page = generic_error_remove_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10450) 	.swap_activate	= btrfs_swap_activate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10451) 	.swap_deactivate = btrfs_swap_deactivate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10452) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10454) static const struct inode_operations btrfs_file_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10455) 	.getattr	= btrfs_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10456) 	.setattr	= btrfs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10457) 	.listxattr      = btrfs_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10458) 	.permission	= btrfs_permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10459) 	.fiemap		= btrfs_fiemap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10460) 	.get_acl	= btrfs_get_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10461) 	.set_acl	= btrfs_set_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10462) 	.update_time	= btrfs_update_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10463) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10464) static const struct inode_operations btrfs_special_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10465) 	.getattr	= btrfs_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10466) 	.setattr	= btrfs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10467) 	.permission	= btrfs_permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10468) 	.listxattr	= btrfs_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10469) 	.get_acl	= btrfs_get_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10470) 	.set_acl	= btrfs_set_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10471) 	.update_time	= btrfs_update_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10472) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10473) static const struct inode_operations btrfs_symlink_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10474) 	.get_link	= page_get_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10475) 	.getattr	= btrfs_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10476) 	.setattr	= btrfs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10477) 	.permission	= btrfs_permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10478) 	.listxattr	= btrfs_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10479) 	.update_time	= btrfs_update_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10480) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10482) const struct dentry_operations btrfs_dentry_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10483) 	.d_delete	= btrfs_dentry_delete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10484) };