162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2007 Oracle.  All rights reserved.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#ifndef BTRFS_INODE_H
762306a36Sopenharmony_ci#define BTRFS_INODE_H
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/hash.h>
1062306a36Sopenharmony_ci#include <linux/refcount.h>
1162306a36Sopenharmony_ci#include "extent_map.h"
1262306a36Sopenharmony_ci#include "extent_io.h"
1362306a36Sopenharmony_ci#include "ordered-data.h"
1462306a36Sopenharmony_ci#include "delayed-inode.h"
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/*
1762306a36Sopenharmony_ci * Since we search a directory based on f_pos (struct dir_context::pos) we have
1862306a36Sopenharmony_ci * to start at 2 since '.' and '..' have f_pos of 0 and 1 respectively, so
1962306a36Sopenharmony_ci * everybody else has to start at 2 (see btrfs_real_readdir() and dir_emit_dots()).
2062306a36Sopenharmony_ci */
2162306a36Sopenharmony_ci#define BTRFS_DIR_START_INDEX 2
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci/*
2462306a36Sopenharmony_ci * ordered_data_close is set by truncate when a file that used
2562306a36Sopenharmony_ci * to have good data has been truncated to zero.  When it is set
2662306a36Sopenharmony_ci * the btrfs file release call will add this inode to the
2762306a36Sopenharmony_ci * ordered operations list so that we make sure to flush out any
2862306a36Sopenharmony_ci * new data the application may have written before commit.
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_cienum {
3162306a36Sopenharmony_ci	BTRFS_INODE_FLUSH_ON_CLOSE,
3262306a36Sopenharmony_ci	BTRFS_INODE_DUMMY,
3362306a36Sopenharmony_ci	BTRFS_INODE_IN_DEFRAG,
3462306a36Sopenharmony_ci	BTRFS_INODE_HAS_ASYNC_EXTENT,
3562306a36Sopenharmony_ci	 /*
3662306a36Sopenharmony_ci	  * Always set under the VFS' inode lock, otherwise it can cause races
3762306a36Sopenharmony_ci	  * during fsync (we start as a fast fsync and then end up in a full
3862306a36Sopenharmony_ci	  * fsync racing with ordered extent completion).
3962306a36Sopenharmony_ci	  */
4062306a36Sopenharmony_ci	BTRFS_INODE_NEEDS_FULL_SYNC,
4162306a36Sopenharmony_ci	BTRFS_INODE_COPY_EVERYTHING,
4262306a36Sopenharmony_ci	BTRFS_INODE_IN_DELALLOC_LIST,
4362306a36Sopenharmony_ci	BTRFS_INODE_HAS_PROPS,
4462306a36Sopenharmony_ci	BTRFS_INODE_SNAPSHOT_FLUSH,
4562306a36Sopenharmony_ci	/*
4662306a36Sopenharmony_ci	 * Set and used when logging an inode and it serves to signal that an
4762306a36Sopenharmony_ci	 * inode does not have xattrs, so subsequent fsyncs can avoid searching
4862306a36Sopenharmony_ci	 * for xattrs to log. This bit must be cleared whenever a xattr is added
4962306a36Sopenharmony_ci	 * to an inode.
5062306a36Sopenharmony_ci	 */
5162306a36Sopenharmony_ci	BTRFS_INODE_NO_XATTRS,
5262306a36Sopenharmony_ci	/*
5362306a36Sopenharmony_ci	 * Set when we are in a context where we need to start a transaction and
5462306a36Sopenharmony_ci	 * have dirty pages with the respective file range locked. This is to
5562306a36Sopenharmony_ci	 * ensure that when reserving space for the transaction, if we are low
5662306a36Sopenharmony_ci	 * on available space and need to flush delalloc, we will not flush
5762306a36Sopenharmony_ci	 * delalloc for this inode, because that could result in a deadlock (on
5862306a36Sopenharmony_ci	 * the file range, inode's io_tree).
5962306a36Sopenharmony_ci	 */
6062306a36Sopenharmony_ci	BTRFS_INODE_NO_DELALLOC_FLUSH,
6162306a36Sopenharmony_ci	/*
6262306a36Sopenharmony_ci	 * Set when we are working on enabling verity for a file. Computing and
6362306a36Sopenharmony_ci	 * writing the whole Merkle tree can take a while so we want to prevent
6462306a36Sopenharmony_ci	 * races where two separate tasks attempt to simultaneously start verity
6562306a36Sopenharmony_ci	 * on the same file.
6662306a36Sopenharmony_ci	 */
6762306a36Sopenharmony_ci	BTRFS_INODE_VERITY_IN_PROGRESS,
6862306a36Sopenharmony_ci	/* Set when this inode is a free space inode. */
6962306a36Sopenharmony_ci	BTRFS_INODE_FREE_SPACE_INODE,
7062306a36Sopenharmony_ci};
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci/* in memory btrfs inode */
7362306a36Sopenharmony_cistruct btrfs_inode {
7462306a36Sopenharmony_ci	/* which subvolume this inode belongs to */
7562306a36Sopenharmony_ci	struct btrfs_root *root;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	/* key used to find this inode on disk.  This is used by the code
7862306a36Sopenharmony_ci	 * to read in roots of subvolumes
7962306a36Sopenharmony_ci	 */
8062306a36Sopenharmony_ci	struct btrfs_key location;
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	/*
8362306a36Sopenharmony_ci	 * Lock for counters and all fields used to determine if the inode is in
8462306a36Sopenharmony_ci	 * the log or not (last_trans, last_sub_trans, last_log_commit,
8562306a36Sopenharmony_ci	 * logged_trans), to access/update new_delalloc_bytes and to update the
8662306a36Sopenharmony_ci	 * VFS' inode number of bytes used.
8762306a36Sopenharmony_ci	 */
8862306a36Sopenharmony_ci	spinlock_t lock;
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	/* the extent_tree has caches of all the extent mappings to disk */
9162306a36Sopenharmony_ci	struct extent_map_tree extent_tree;
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	/* the io_tree does range state (DIRTY, LOCKED etc) */
9462306a36Sopenharmony_ci	struct extent_io_tree io_tree;
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	/*
9762306a36Sopenharmony_ci	 * Keep track of where the inode has extent items mapped in order to
9862306a36Sopenharmony_ci	 * make sure the i_size adjustments are accurate
9962306a36Sopenharmony_ci	 */
10062306a36Sopenharmony_ci	struct extent_io_tree file_extent_tree;
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	/* held while logging the inode in tree-log.c */
10362306a36Sopenharmony_ci	struct mutex log_mutex;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	/* used to order data wrt metadata */
10662306a36Sopenharmony_ci	struct btrfs_ordered_inode_tree ordered_tree;
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	/* list of all the delalloc inodes in the FS.  There are times we need
10962306a36Sopenharmony_ci	 * to write all the delalloc pages to disk, and this list is used
11062306a36Sopenharmony_ci	 * to walk them all.
11162306a36Sopenharmony_ci	 */
11262306a36Sopenharmony_ci	struct list_head delalloc_inodes;
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	/* node for the red-black tree that links inodes in subvolume root */
11562306a36Sopenharmony_ci	struct rb_node rb_node;
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	unsigned long runtime_flags;
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	/* full 64 bit generation number, struct vfs_inode doesn't have a big
12062306a36Sopenharmony_ci	 * enough field for this.
12162306a36Sopenharmony_ci	 */
12262306a36Sopenharmony_ci	u64 generation;
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	/*
12562306a36Sopenharmony_ci	 * transid of the trans_handle that last modified this inode
12662306a36Sopenharmony_ci	 */
12762306a36Sopenharmony_ci	u64 last_trans;
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	/*
13062306a36Sopenharmony_ci	 * transid that last logged this inode
13162306a36Sopenharmony_ci	 */
13262306a36Sopenharmony_ci	u64 logged_trans;
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	/*
13562306a36Sopenharmony_ci	 * log transid when this inode was last modified
13662306a36Sopenharmony_ci	 */
13762306a36Sopenharmony_ci	int last_sub_trans;
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	/* a local copy of root's last_log_commit */
14062306a36Sopenharmony_ci	int last_log_commit;
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	union {
14362306a36Sopenharmony_ci		/*
14462306a36Sopenharmony_ci		 * Total number of bytes pending delalloc, used by stat to
14562306a36Sopenharmony_ci		 * calculate the real block usage of the file. This is used
14662306a36Sopenharmony_ci		 * only for files.
14762306a36Sopenharmony_ci		 */
14862306a36Sopenharmony_ci		u64 delalloc_bytes;
14962306a36Sopenharmony_ci		/*
15062306a36Sopenharmony_ci		 * The lowest possible index of the next dir index key which
15162306a36Sopenharmony_ci		 * points to an inode that needs to be logged.
15262306a36Sopenharmony_ci		 * This is used only for directories.
15362306a36Sopenharmony_ci		 * Use the helpers btrfs_get_first_dir_index_to_log() and
15462306a36Sopenharmony_ci		 * btrfs_set_first_dir_index_to_log() to access this field.
15562306a36Sopenharmony_ci		 */
15662306a36Sopenharmony_ci		u64 first_dir_index_to_log;
15762306a36Sopenharmony_ci	};
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	union {
16062306a36Sopenharmony_ci		/*
16162306a36Sopenharmony_ci		 * Total number of bytes pending delalloc that fall within a file
16262306a36Sopenharmony_ci		 * range that is either a hole or beyond EOF (and no prealloc extent
16362306a36Sopenharmony_ci		 * exists in the range). This is always <= delalloc_bytes and this
16462306a36Sopenharmony_ci		 * is used only for files.
16562306a36Sopenharmony_ci		 */
16662306a36Sopenharmony_ci		u64 new_delalloc_bytes;
16762306a36Sopenharmony_ci		/*
16862306a36Sopenharmony_ci		 * The offset of the last dir index key that was logged.
16962306a36Sopenharmony_ci		 * This is used only for directories.
17062306a36Sopenharmony_ci		 */
17162306a36Sopenharmony_ci		u64 last_dir_index_offset;
17262306a36Sopenharmony_ci	};
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	/*
17562306a36Sopenharmony_ci	 * total number of bytes pending defrag, used by stat to check whether
17662306a36Sopenharmony_ci	 * it needs COW.
17762306a36Sopenharmony_ci	 */
17862306a36Sopenharmony_ci	u64 defrag_bytes;
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	/*
18162306a36Sopenharmony_ci	 * the size of the file stored in the metadata on disk.  data=ordered
18262306a36Sopenharmony_ci	 * means the in-memory i_size might be larger than the size on disk
18362306a36Sopenharmony_ci	 * because not all the blocks are written yet.
18462306a36Sopenharmony_ci	 */
18562306a36Sopenharmony_ci	u64 disk_i_size;
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	/*
18862306a36Sopenharmony_ci	 * If this is a directory then index_cnt is the counter for the index
18962306a36Sopenharmony_ci	 * number for new files that are created. For an empty directory, this
19062306a36Sopenharmony_ci	 * must be initialized to BTRFS_DIR_START_INDEX.
19162306a36Sopenharmony_ci	 */
19262306a36Sopenharmony_ci	u64 index_cnt;
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci	/* Cache the directory index number to speed the dir/file remove */
19562306a36Sopenharmony_ci	u64 dir_index;
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci	/* the fsync log has some corner cases that mean we have to check
19862306a36Sopenharmony_ci	 * directories to see if any unlinks have been done before
19962306a36Sopenharmony_ci	 * the directory was logged.  See tree-log.c for all the
20062306a36Sopenharmony_ci	 * details
20162306a36Sopenharmony_ci	 */
20262306a36Sopenharmony_ci	u64 last_unlink_trans;
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci	/*
20562306a36Sopenharmony_ci	 * The id/generation of the last transaction where this inode was
20662306a36Sopenharmony_ci	 * either the source or the destination of a clone/dedupe operation.
20762306a36Sopenharmony_ci	 * Used when logging an inode to know if there are shared extents that
20862306a36Sopenharmony_ci	 * need special care when logging checksum items, to avoid duplicate
20962306a36Sopenharmony_ci	 * checksum items in a log (which can lead to a corruption where we end
21062306a36Sopenharmony_ci	 * up with missing checksum ranges after log replay).
21162306a36Sopenharmony_ci	 * Protected by the vfs inode lock.
21262306a36Sopenharmony_ci	 */
21362306a36Sopenharmony_ci	u64 last_reflink_trans;
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	/*
21662306a36Sopenharmony_ci	 * Number of bytes outstanding that are going to need csums.  This is
21762306a36Sopenharmony_ci	 * used in ENOSPC accounting.
21862306a36Sopenharmony_ci	 */
21962306a36Sopenharmony_ci	u64 csum_bytes;
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	/* Backwards incompatible flags, lower half of inode_item::flags  */
22262306a36Sopenharmony_ci	u32 flags;
22362306a36Sopenharmony_ci	/* Read-only compatibility flags, upper half of inode_item::flags */
22462306a36Sopenharmony_ci	u32 ro_flags;
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci	/*
22762306a36Sopenharmony_ci	 * Counters to keep track of the number of extent item's we may use due
22862306a36Sopenharmony_ci	 * to delalloc and such.  outstanding_extents is the number of extent
22962306a36Sopenharmony_ci	 * items we think we'll end up using, and reserved_extents is the number
23062306a36Sopenharmony_ci	 * of extent items we've reserved metadata for.
23162306a36Sopenharmony_ci	 */
23262306a36Sopenharmony_ci	unsigned outstanding_extents;
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci	struct btrfs_block_rsv block_rsv;
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci	/*
23762306a36Sopenharmony_ci	 * Cached values of inode properties
23862306a36Sopenharmony_ci	 */
23962306a36Sopenharmony_ci	unsigned prop_compress;		/* per-file compression algorithm */
24062306a36Sopenharmony_ci	/*
24162306a36Sopenharmony_ci	 * Force compression on the file using the defrag ioctl, could be
24262306a36Sopenharmony_ci	 * different from prop_compress and takes precedence if set
24362306a36Sopenharmony_ci	 */
24462306a36Sopenharmony_ci	unsigned defrag_compress;
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci	struct btrfs_delayed_node *delayed_node;
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci	/* File creation time. */
24962306a36Sopenharmony_ci	struct timespec64 i_otime;
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	/* Hook into fs_info->delayed_iputs */
25262306a36Sopenharmony_ci	struct list_head delayed_iput;
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci	struct rw_semaphore i_mmap_lock;
25562306a36Sopenharmony_ci	struct inode vfs_inode;
25662306a36Sopenharmony_ci};
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_cistatic inline u64 btrfs_get_first_dir_index_to_log(const struct btrfs_inode *inode)
25962306a36Sopenharmony_ci{
26062306a36Sopenharmony_ci	return READ_ONCE(inode->first_dir_index_to_log);
26162306a36Sopenharmony_ci}
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_cistatic inline void btrfs_set_first_dir_index_to_log(struct btrfs_inode *inode,
26462306a36Sopenharmony_ci						    u64 index)
26562306a36Sopenharmony_ci{
26662306a36Sopenharmony_ci	WRITE_ONCE(inode->first_dir_index_to_log, index);
26762306a36Sopenharmony_ci}
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_cistatic inline struct btrfs_inode *BTRFS_I(const struct inode *inode)
27062306a36Sopenharmony_ci{
27162306a36Sopenharmony_ci	return container_of(inode, struct btrfs_inode, vfs_inode);
27262306a36Sopenharmony_ci}
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_cistatic inline unsigned long btrfs_inode_hash(u64 objectid,
27562306a36Sopenharmony_ci					     const struct btrfs_root *root)
27662306a36Sopenharmony_ci{
27762306a36Sopenharmony_ci	u64 h = objectid ^ (root->root_key.objectid * GOLDEN_RATIO_PRIME);
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci#if BITS_PER_LONG == 32
28062306a36Sopenharmony_ci	h = (h >> 32) ^ (h & 0xffffffff);
28162306a36Sopenharmony_ci#endif
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci	return (unsigned long)h;
28462306a36Sopenharmony_ci}
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci#if BITS_PER_LONG == 32
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci/*
28962306a36Sopenharmony_ci * On 32 bit systems the i_ino of struct inode is 32 bits (unsigned long), so
29062306a36Sopenharmony_ci * we use the inode's location objectid which is a u64 to avoid truncation.
29162306a36Sopenharmony_ci */
29262306a36Sopenharmony_cistatic inline u64 btrfs_ino(const struct btrfs_inode *inode)
29362306a36Sopenharmony_ci{
29462306a36Sopenharmony_ci	u64 ino = inode->location.objectid;
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci	/* type == BTRFS_ROOT_ITEM_KEY: subvol dir */
29762306a36Sopenharmony_ci	if (inode->location.type == BTRFS_ROOT_ITEM_KEY)
29862306a36Sopenharmony_ci		ino = inode->vfs_inode.i_ino;
29962306a36Sopenharmony_ci	return ino;
30062306a36Sopenharmony_ci}
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci#else
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_cistatic inline u64 btrfs_ino(const struct btrfs_inode *inode)
30562306a36Sopenharmony_ci{
30662306a36Sopenharmony_ci	return inode->vfs_inode.i_ino;
30762306a36Sopenharmony_ci}
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_ci#endif
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_cistatic inline void btrfs_i_size_write(struct btrfs_inode *inode, u64 size)
31262306a36Sopenharmony_ci{
31362306a36Sopenharmony_ci	i_size_write(&inode->vfs_inode, size);
31462306a36Sopenharmony_ci	inode->disk_i_size = size;
31562306a36Sopenharmony_ci}
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_cistatic inline bool btrfs_is_free_space_inode(struct btrfs_inode *inode)
31862306a36Sopenharmony_ci{
31962306a36Sopenharmony_ci	return test_bit(BTRFS_INODE_FREE_SPACE_INODE, &inode->runtime_flags);
32062306a36Sopenharmony_ci}
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_cistatic inline bool is_data_inode(struct inode *inode)
32362306a36Sopenharmony_ci{
32462306a36Sopenharmony_ci	return btrfs_ino(BTRFS_I(inode)) != BTRFS_BTREE_INODE_OBJECTID;
32562306a36Sopenharmony_ci}
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_cistatic inline void btrfs_mod_outstanding_extents(struct btrfs_inode *inode,
32862306a36Sopenharmony_ci						 int mod)
32962306a36Sopenharmony_ci{
33062306a36Sopenharmony_ci	lockdep_assert_held(&inode->lock);
33162306a36Sopenharmony_ci	inode->outstanding_extents += mod;
33262306a36Sopenharmony_ci	if (btrfs_is_free_space_inode(inode))
33362306a36Sopenharmony_ci		return;
33462306a36Sopenharmony_ci	trace_btrfs_inode_mod_outstanding_extents(inode->root, btrfs_ino(inode),
33562306a36Sopenharmony_ci						  mod, inode->outstanding_extents);
33662306a36Sopenharmony_ci}
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_ci/*
33962306a36Sopenharmony_ci * Called every time after doing a buffered, direct IO or memory mapped write.
34062306a36Sopenharmony_ci *
34162306a36Sopenharmony_ci * This is to ensure that if we write to a file that was previously fsynced in
34262306a36Sopenharmony_ci * the current transaction, then try to fsync it again in the same transaction,
34362306a36Sopenharmony_ci * we will know that there were changes in the file and that it needs to be
34462306a36Sopenharmony_ci * logged.
34562306a36Sopenharmony_ci */
34662306a36Sopenharmony_cistatic inline void btrfs_set_inode_last_sub_trans(struct btrfs_inode *inode)
34762306a36Sopenharmony_ci{
34862306a36Sopenharmony_ci	spin_lock(&inode->lock);
34962306a36Sopenharmony_ci	inode->last_sub_trans = inode->root->log_transid;
35062306a36Sopenharmony_ci	spin_unlock(&inode->lock);
35162306a36Sopenharmony_ci}
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci/*
35462306a36Sopenharmony_ci * Should be called while holding the inode's VFS lock in exclusive mode or in a
35562306a36Sopenharmony_ci * context where no one else can access the inode concurrently (during inode
35662306a36Sopenharmony_ci * creation or when loading an inode from disk).
35762306a36Sopenharmony_ci */
35862306a36Sopenharmony_cistatic inline void btrfs_set_inode_full_sync(struct btrfs_inode *inode)
35962306a36Sopenharmony_ci{
36062306a36Sopenharmony_ci	set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
36162306a36Sopenharmony_ci	/*
36262306a36Sopenharmony_ci	 * The inode may have been part of a reflink operation in the last
36362306a36Sopenharmony_ci	 * transaction that modified it, and then a fsync has reset the
36462306a36Sopenharmony_ci	 * last_reflink_trans to avoid subsequent fsyncs in the same
36562306a36Sopenharmony_ci	 * transaction to do unnecessary work. So update last_reflink_trans
36662306a36Sopenharmony_ci	 * to the last_trans value (we have to be pessimistic and assume a
36762306a36Sopenharmony_ci	 * reflink happened).
36862306a36Sopenharmony_ci	 *
36962306a36Sopenharmony_ci	 * The ->last_trans is protected by the inode's spinlock and we can
37062306a36Sopenharmony_ci	 * have a concurrent ordered extent completion update it. Also set
37162306a36Sopenharmony_ci	 * last_reflink_trans to ->last_trans only if the former is less than
37262306a36Sopenharmony_ci	 * the later, because we can be called in a context where
37362306a36Sopenharmony_ci	 * last_reflink_trans was set to the current transaction generation
37462306a36Sopenharmony_ci	 * while ->last_trans was not yet updated in the current transaction,
37562306a36Sopenharmony_ci	 * and therefore has a lower value.
37662306a36Sopenharmony_ci	 */
37762306a36Sopenharmony_ci	spin_lock(&inode->lock);
37862306a36Sopenharmony_ci	if (inode->last_reflink_trans < inode->last_trans)
37962306a36Sopenharmony_ci		inode->last_reflink_trans = inode->last_trans;
38062306a36Sopenharmony_ci	spin_unlock(&inode->lock);
38162306a36Sopenharmony_ci}
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_cistatic inline bool btrfs_inode_in_log(struct btrfs_inode *inode, u64 generation)
38462306a36Sopenharmony_ci{
38562306a36Sopenharmony_ci	bool ret = false;
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci	spin_lock(&inode->lock);
38862306a36Sopenharmony_ci	if (inode->logged_trans == generation &&
38962306a36Sopenharmony_ci	    inode->last_sub_trans <= inode->last_log_commit &&
39062306a36Sopenharmony_ci	    inode->last_sub_trans <= inode->root->last_log_commit)
39162306a36Sopenharmony_ci		ret = true;
39262306a36Sopenharmony_ci	spin_unlock(&inode->lock);
39362306a36Sopenharmony_ci	return ret;
39462306a36Sopenharmony_ci}
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci/*
39762306a36Sopenharmony_ci * Check if the inode has flags compatible with compression
39862306a36Sopenharmony_ci */
39962306a36Sopenharmony_cistatic inline bool btrfs_inode_can_compress(const struct btrfs_inode *inode)
40062306a36Sopenharmony_ci{
40162306a36Sopenharmony_ci	if (inode->flags & BTRFS_INODE_NODATACOW ||
40262306a36Sopenharmony_ci	    inode->flags & BTRFS_INODE_NODATASUM)
40362306a36Sopenharmony_ci		return false;
40462306a36Sopenharmony_ci	return true;
40562306a36Sopenharmony_ci}
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_ci/* Array of bytes with variable length, hexadecimal format 0x1234 */
40862306a36Sopenharmony_ci#define CSUM_FMT				"0x%*phN"
40962306a36Sopenharmony_ci#define CSUM_FMT_VALUE(size, bytes)		size, bytes
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ciint btrfs_check_sector_csum(struct btrfs_fs_info *fs_info, struct page *page,
41262306a36Sopenharmony_ci			    u32 pgoff, u8 *csum, const u8 * const csum_expected);
41362306a36Sopenharmony_cibool btrfs_data_csum_ok(struct btrfs_bio *bbio, struct btrfs_device *dev,
41462306a36Sopenharmony_ci			u32 bio_offset, struct bio_vec *bv);
41562306a36Sopenharmony_cinoinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
41662306a36Sopenharmony_ci			      u64 *orig_start, u64 *orig_block_len,
41762306a36Sopenharmony_ci			      u64 *ram_bytes, bool nowait, bool strict);
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_civoid __btrfs_del_delalloc_inode(struct btrfs_root *root, struct btrfs_inode *inode);
42062306a36Sopenharmony_cistruct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry);
42162306a36Sopenharmony_ciint btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index);
42262306a36Sopenharmony_ciint btrfs_unlink_inode(struct btrfs_trans_handle *trans,
42362306a36Sopenharmony_ci		       struct btrfs_inode *dir, struct btrfs_inode *inode,
42462306a36Sopenharmony_ci		       const struct fscrypt_str *name);
42562306a36Sopenharmony_ciint btrfs_add_link(struct btrfs_trans_handle *trans,
42662306a36Sopenharmony_ci		   struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
42762306a36Sopenharmony_ci		   const struct fscrypt_str *name, int add_backref, u64 index);
42862306a36Sopenharmony_ciint btrfs_delete_subvolume(struct btrfs_inode *dir, struct dentry *dentry);
42962306a36Sopenharmony_ciint btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
43062306a36Sopenharmony_ci			 int front);
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_ciint btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context);
43362306a36Sopenharmony_ciint btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
43462306a36Sopenharmony_ci			       bool in_reclaim_context);
43562306a36Sopenharmony_ciint btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
43662306a36Sopenharmony_ci			      unsigned int extra_bits,
43762306a36Sopenharmony_ci			      struct extent_state **cached_state);
43862306a36Sopenharmony_ci
43962306a36Sopenharmony_cistruct btrfs_new_inode_args {
44062306a36Sopenharmony_ci	/* Input */
44162306a36Sopenharmony_ci	struct inode *dir;
44262306a36Sopenharmony_ci	struct dentry *dentry;
44362306a36Sopenharmony_ci	struct inode *inode;
44462306a36Sopenharmony_ci	bool orphan;
44562306a36Sopenharmony_ci	bool subvol;
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ci	/* Output from btrfs_new_inode_prepare(), input to btrfs_create_new_inode(). */
44862306a36Sopenharmony_ci	struct posix_acl *default_acl;
44962306a36Sopenharmony_ci	struct posix_acl *acl;
45062306a36Sopenharmony_ci	struct fscrypt_name fname;
45162306a36Sopenharmony_ci};
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ciint btrfs_new_inode_prepare(struct btrfs_new_inode_args *args,
45462306a36Sopenharmony_ci			    unsigned int *trans_num_items);
45562306a36Sopenharmony_ciint btrfs_create_new_inode(struct btrfs_trans_handle *trans,
45662306a36Sopenharmony_ci			   struct btrfs_new_inode_args *args);
45762306a36Sopenharmony_civoid btrfs_new_inode_args_destroy(struct btrfs_new_inode_args *args);
45862306a36Sopenharmony_cistruct inode *btrfs_new_subvol_inode(struct mnt_idmap *idmap,
45962306a36Sopenharmony_ci				     struct inode *dir);
46062306a36Sopenharmony_ci void btrfs_set_delalloc_extent(struct btrfs_inode *inode, struct extent_state *state,
46162306a36Sopenharmony_ci			        u32 bits);
46262306a36Sopenharmony_civoid btrfs_clear_delalloc_extent(struct btrfs_inode *inode,
46362306a36Sopenharmony_ci				 struct extent_state *state, u32 bits);
46462306a36Sopenharmony_civoid btrfs_merge_delalloc_extent(struct btrfs_inode *inode, struct extent_state *new,
46562306a36Sopenharmony_ci				 struct extent_state *other);
46662306a36Sopenharmony_civoid btrfs_split_delalloc_extent(struct btrfs_inode *inode,
46762306a36Sopenharmony_ci				 struct extent_state *orig, u64 split);
46862306a36Sopenharmony_civoid btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end);
46962306a36Sopenharmony_civm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf);
47062306a36Sopenharmony_civoid btrfs_evict_inode(struct inode *inode);
47162306a36Sopenharmony_cistruct inode *btrfs_alloc_inode(struct super_block *sb);
47262306a36Sopenharmony_civoid btrfs_destroy_inode(struct inode *inode);
47362306a36Sopenharmony_civoid btrfs_free_inode(struct inode *inode);
47462306a36Sopenharmony_ciint btrfs_drop_inode(struct inode *inode);
47562306a36Sopenharmony_ciint __init btrfs_init_cachep(void);
47662306a36Sopenharmony_civoid __cold btrfs_destroy_cachep(void);
47762306a36Sopenharmony_cistruct inode *btrfs_iget_path(struct super_block *s, u64 ino,
47862306a36Sopenharmony_ci			      struct btrfs_root *root, struct btrfs_path *path);
47962306a36Sopenharmony_cistruct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root);
48062306a36Sopenharmony_cistruct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
48162306a36Sopenharmony_ci				    struct page *page, size_t pg_offset,
48262306a36Sopenharmony_ci				    u64 start, u64 end);
48362306a36Sopenharmony_ciint btrfs_update_inode(struct btrfs_trans_handle *trans,
48462306a36Sopenharmony_ci		       struct btrfs_root *root, struct btrfs_inode *inode);
48562306a36Sopenharmony_ciint btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
48662306a36Sopenharmony_ci				struct btrfs_root *root, struct btrfs_inode *inode);
48762306a36Sopenharmony_ciint btrfs_orphan_add(struct btrfs_trans_handle *trans, struct btrfs_inode *inode);
48862306a36Sopenharmony_ciint btrfs_orphan_cleanup(struct btrfs_root *root);
48962306a36Sopenharmony_ciint btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size);
49062306a36Sopenharmony_civoid btrfs_add_delayed_iput(struct btrfs_inode *inode);
49162306a36Sopenharmony_civoid btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info);
49262306a36Sopenharmony_ciint btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info);
49362306a36Sopenharmony_ciint btrfs_prealloc_file_range(struct inode *inode, int mode,
49462306a36Sopenharmony_ci			      u64 start, u64 num_bytes, u64 min_size,
49562306a36Sopenharmony_ci			      loff_t actual_len, u64 *alloc_hint);
49662306a36Sopenharmony_ciint btrfs_prealloc_file_range_trans(struct inode *inode,
49762306a36Sopenharmony_ci				    struct btrfs_trans_handle *trans, int mode,
49862306a36Sopenharmony_ci				    u64 start, u64 num_bytes, u64 min_size,
49962306a36Sopenharmony_ci				    loff_t actual_len, u64 *alloc_hint);
50062306a36Sopenharmony_ciint btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
50162306a36Sopenharmony_ci			     u64 start, u64 end, struct writeback_control *wbc);
50262306a36Sopenharmony_ciint btrfs_writepage_cow_fixup(struct page *page);
50362306a36Sopenharmony_ciint btrfs_encoded_io_compression_from_extent(struct btrfs_fs_info *fs_info,
50462306a36Sopenharmony_ci					     int compress_type);
50562306a36Sopenharmony_ciint btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode,
50662306a36Sopenharmony_ci					  u64 file_offset, u64 disk_bytenr,
50762306a36Sopenharmony_ci					  u64 disk_io_size,
50862306a36Sopenharmony_ci					  struct page **pages);
50962306a36Sopenharmony_cissize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter,
51062306a36Sopenharmony_ci			   struct btrfs_ioctl_encoded_io_args *encoded);
51162306a36Sopenharmony_cissize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from,
51262306a36Sopenharmony_ci			       const struct btrfs_ioctl_encoded_io_args *encoded);
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_cissize_t btrfs_dio_read(struct kiocb *iocb, struct iov_iter *iter,
51562306a36Sopenharmony_ci		       size_t done_before);
51662306a36Sopenharmony_cistruct iomap_dio *btrfs_dio_write(struct kiocb *iocb, struct iov_iter *iter,
51762306a36Sopenharmony_ci				  size_t done_before);
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ciextern const struct dentry_operations btrfs_dentry_operations;
52062306a36Sopenharmony_ci
52162306a36Sopenharmony_ci/* Inode locking type flags, by default the exclusive lock is taken. */
52262306a36Sopenharmony_cienum btrfs_ilock_type {
52362306a36Sopenharmony_ci	ENUM_BIT(BTRFS_ILOCK_SHARED),
52462306a36Sopenharmony_ci	ENUM_BIT(BTRFS_ILOCK_TRY),
52562306a36Sopenharmony_ci	ENUM_BIT(BTRFS_ILOCK_MMAP),
52662306a36Sopenharmony_ci};
52762306a36Sopenharmony_ci
52862306a36Sopenharmony_ciint btrfs_inode_lock(struct btrfs_inode *inode, unsigned int ilock_flags);
52962306a36Sopenharmony_civoid btrfs_inode_unlock(struct btrfs_inode *inode, unsigned int ilock_flags);
53062306a36Sopenharmony_civoid btrfs_update_inode_bytes(struct btrfs_inode *inode, const u64 add_bytes,
53162306a36Sopenharmony_ci			      const u64 del_bytes);
53262306a36Sopenharmony_civoid btrfs_assert_inode_range_clean(struct btrfs_inode *inode, u64 start, u64 end);
53362306a36Sopenharmony_ci
53462306a36Sopenharmony_ci#endif
535