xref: /openbmc/linux/fs/btrfs/btrfs_inode.h (revision c56cbe90)
19888c340SDavid Sterba /* SPDX-License-Identifier: GPL-2.0 */
26cbd5570SChris Mason /*
36cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
46cbd5570SChris Mason  */
56cbd5570SChris Mason 
69888c340SDavid Sterba #ifndef BTRFS_INODE_H
79888c340SDavid Sterba #define BTRFS_INODE_H
82c90e5d6SChris Mason 
9778ba82bSFilipe David Borba Manana #include <linux/hash.h>
10e3b318d1SOmar Sandoval #include <linux/refcount.h>
11a52d9a80SChris Mason #include "extent_map.h"
12d1310b2eSChris Mason #include "extent_io.h"
13e6dcd2dcSChris Mason #include "ordered-data.h"
1416cdcec7SMiao Xie #include "delayed-inode.h"
15a52d9a80SChris Mason 
1672ac3c0dSJosef Bacik /*
17528ee697SFilipe Manana  * Since we search a directory based on f_pos (struct dir_context::pos) we have
18528ee697SFilipe Manana  * to start at 2 since '.' and '..' have f_pos of 0 and 1 respectively, so
19528ee697SFilipe Manana  * everybody else has to start at 2 (see btrfs_real_readdir() and dir_emit_dots()).
20528ee697SFilipe Manana  */
21528ee697SFilipe Manana #define BTRFS_DIR_START_INDEX 2
22528ee697SFilipe Manana 
23528ee697SFilipe Manana /*
2472ac3c0dSJosef Bacik  * ordered_data_close is set by truncate when a file that used
2572ac3c0dSJosef Bacik  * to have good data has been truncated to zero.  When it is set
2672ac3c0dSJosef Bacik  * the btrfs file release call will add this inode to the
2772ac3c0dSJosef Bacik  * ordered operations list so that we make sure to flush out any
2872ac3c0dSJosef Bacik  * new data the application may have written before commit.
2972ac3c0dSJosef Bacik  */
307efc3e34SOmar Sandoval enum {
311fd4033dSNikolay Borisov 	BTRFS_INODE_FLUSH_ON_CLOSE,
327efc3e34SOmar Sandoval 	BTRFS_INODE_DUMMY,
337efc3e34SOmar Sandoval 	BTRFS_INODE_IN_DEFRAG,
347efc3e34SOmar Sandoval 	BTRFS_INODE_HAS_ASYNC_EXTENT,
3548778179SFilipe Manana 	 /*
3648778179SFilipe Manana 	  * Always set under the VFS' inode lock, otherwise it can cause races
3748778179SFilipe Manana 	  * during fsync (we start as a fast fsync and then end up in a full
3848778179SFilipe Manana 	  * fsync racing with ordered extent completion).
3948778179SFilipe Manana 	  */
407efc3e34SOmar Sandoval 	BTRFS_INODE_NEEDS_FULL_SYNC,
417efc3e34SOmar Sandoval 	BTRFS_INODE_COPY_EVERYTHING,
427efc3e34SOmar Sandoval 	BTRFS_INODE_IN_DELALLOC_LIST,
437efc3e34SOmar Sandoval 	BTRFS_INODE_HAS_PROPS,
443cd24c69SEthan Lien 	BTRFS_INODE_SNAPSHOT_FLUSH,
45f2f121abSFilipe Manana 	/*
46f2f121abSFilipe Manana 	 * Set and used when logging an inode and it serves to signal that an
47f2f121abSFilipe Manana 	 * inode does not have xattrs, so subsequent fsyncs can avoid searching
48f2f121abSFilipe Manana 	 * for xattrs to log. This bit must be cleared whenever a xattr is added
49f2f121abSFilipe Manana 	 * to an inode.
50f2f121abSFilipe Manana 	 */
51f2f121abSFilipe Manana 	BTRFS_INODE_NO_XATTRS,
523d45f221SFilipe Manana 	/*
533d45f221SFilipe Manana 	 * Set when we are in a context where we need to start a transaction and
543d45f221SFilipe Manana 	 * have dirty pages with the respective file range locked. This is to
553d45f221SFilipe Manana 	 * ensure that when reserving space for the transaction, if we are low
563d45f221SFilipe Manana 	 * on available space and need to flush delalloc, we will not flush
573d45f221SFilipe Manana 	 * delalloc for this inode, because that could result in a deadlock (on
583d45f221SFilipe Manana 	 * the file range, inode's io_tree).
593d45f221SFilipe Manana 	 */
603d45f221SFilipe Manana 	BTRFS_INODE_NO_DELALLOC_FLUSH,
6114605409SBoris Burkov 	/*
6214605409SBoris Burkov 	 * Set when we are working on enabling verity for a file. Computing and
6314605409SBoris Burkov 	 * writing the whole Merkle tree can take a while so we want to prevent
6414605409SBoris Burkov 	 * races where two separate tasks attempt to simultaneously start verity
6514605409SBoris Burkov 	 * on the same file.
6614605409SBoris Burkov 	 */
6714605409SBoris Burkov 	BTRFS_INODE_VERITY_IN_PROGRESS,
689b9b8854SJosef Bacik 	/* Set when this inode is a free space inode. */
699b9b8854SJosef Bacik 	BTRFS_INODE_FREE_SPACE_INODE,
707efc3e34SOmar Sandoval };
7172ac3c0dSJosef Bacik 
72f1ace244SAneesh /* in memory btrfs inode */
732c90e5d6SChris Mason struct btrfs_inode {
74d352ac68SChris Mason 	/* which subvolume this inode belongs to */
75d6e4a428SChris Mason 	struct btrfs_root *root;
76d352ac68SChris Mason 
77d352ac68SChris Mason 	/* key used to find this inode on disk.  This is used by the code
78d352ac68SChris Mason 	 * to read in roots of subvolumes
79d352ac68SChris Mason 	 */
80d6e4a428SChris Mason 	struct btrfs_key location;
81d352ac68SChris Mason 
822f2ff0eeSFilipe Manana 	/*
832f2ff0eeSFilipe Manana 	 * Lock for counters and all fields used to determine if the inode is in
842f2ff0eeSFilipe Manana 	 * the log or not (last_trans, last_sub_trans, last_log_commit,
852766ff61SFilipe Manana 	 * logged_trans), to access/update new_delalloc_bytes and to update the
862766ff61SFilipe Manana 	 * VFS' inode number of bytes used.
872f2ff0eeSFilipe Manana 	 */
889e0baf60SJosef Bacik 	spinlock_t lock;
899e0baf60SJosef Bacik 
90d352ac68SChris Mason 	/* the extent_tree has caches of all the extent mappings to disk */
91a52d9a80SChris Mason 	struct extent_map_tree extent_tree;
92d352ac68SChris Mason 
93d352ac68SChris Mason 	/* the io_tree does range state (DIRTY, LOCKED etc) */
94d1310b2eSChris Mason 	struct extent_io_tree io_tree;
95d352ac68SChris Mason 
9641a2ee75SJosef Bacik 	/*
9741a2ee75SJosef Bacik 	 * Keep track of where the inode has extent items mapped in order to
9841a2ee75SJosef Bacik 	 * make sure the i_size adjustments are accurate
9941a2ee75SJosef Bacik 	 */
10041a2ee75SJosef Bacik 	struct extent_io_tree file_extent_tree;
10141a2ee75SJosef Bacik 
102d352ac68SChris Mason 	/* held while logging the inode in tree-log.c */
103e02119d5SChris Mason 	struct mutex log_mutex;
104d352ac68SChris Mason 
105d352ac68SChris Mason 	/* used to order data wrt metadata */
106e6dcd2dcSChris Mason 	struct btrfs_ordered_inode_tree ordered_tree;
10715ee9bc7SJosef Bacik 
108d352ac68SChris Mason 	/* list of all the delalloc inodes in the FS.  There are times we need
109d352ac68SChris Mason 	 * to write all the delalloc pages to disk, and this list is used
110d352ac68SChris Mason 	 * to walk them all.
111d352ac68SChris Mason 	 */
112ea8c2819SChris Mason 	struct list_head delalloc_inodes;
113ea8c2819SChris Mason 
1145d4f98a2SYan Zheng 	/* node for the red-black tree that links inodes in subvolume root */
1155d4f98a2SYan Zheng 	struct rb_node rb_node;
1165d4f98a2SYan Zheng 
11772ac3c0dSJosef Bacik 	unsigned long runtime_flags;
11872ac3c0dSJosef Bacik 
119d352ac68SChris Mason 	/* full 64 bit generation number, struct vfs_inode doesn't have a big
120d352ac68SChris Mason 	 * enough field for this.
121d352ac68SChris Mason 	 */
122e02119d5SChris Mason 	u64 generation;
123e02119d5SChris Mason 
12415ee9bc7SJosef Bacik 	/*
12515ee9bc7SJosef Bacik 	 * transid of the trans_handle that last modified this inode
12615ee9bc7SJosef Bacik 	 */
12715ee9bc7SJosef Bacik 	u64 last_trans;
128257c62e1SChris Mason 
129257c62e1SChris Mason 	/*
130e02119d5SChris Mason 	 * transid that last logged this inode
131e02119d5SChris Mason 	 */
132e02119d5SChris Mason 	u64 logged_trans;
13349eb7e46SChris Mason 
134bb14a59bSMiao Xie 	/*
135bb14a59bSMiao Xie 	 * log transid when this inode was last modified
136bb14a59bSMiao Xie 	 */
137bb14a59bSMiao Xie 	int last_sub_trans;
138bb14a59bSMiao Xie 
139bb14a59bSMiao Xie 	/* a local copy of root's last_log_commit */
140bb14a59bSMiao Xie 	int last_log_commit;
141bb14a59bSMiao Xie 
142fa4b8cb1SFilipe Manana 	union {
143dc287224SFilipe Manana 		/*
144fa4b8cb1SFilipe Manana 		 * Total number of bytes pending delalloc, used by stat to
145fa4b8cb1SFilipe Manana 		 * calculate the real block usage of the file. This is used
146fa4b8cb1SFilipe Manana 		 * only for files.
147d352ac68SChris Mason 		 */
1489069218dSChris Mason 		u64 delalloc_bytes;
149fa4b8cb1SFilipe Manana 		/*
150fa4b8cb1SFilipe Manana 		 * The lowest possible index of the next dir index key which
151fa4b8cb1SFilipe Manana 		 * points to an inode that needs to be logged.
152fa4b8cb1SFilipe Manana 		 * This is used only for directories.
153fa4b8cb1SFilipe Manana 		 * Use the helpers btrfs_get_first_dir_index_to_log() and
154fa4b8cb1SFilipe Manana 		 * btrfs_set_first_dir_index_to_log() to access this field.
155fa4b8cb1SFilipe Manana 		 */
156fa4b8cb1SFilipe Manana 		u64 first_dir_index_to_log;
157fa4b8cb1SFilipe Manana 	};
158d352ac68SChris Mason 
159dc287224SFilipe Manana 	union {
160d352ac68SChris Mason 		/*
161a7e3b975SFilipe Manana 		 * Total number of bytes pending delalloc that fall within a file
162a7e3b975SFilipe Manana 		 * range that is either a hole or beyond EOF (and no prealloc extent
163dc287224SFilipe Manana 		 * exists in the range). This is always <= delalloc_bytes and this
164dc287224SFilipe Manana 		 * is used only for files.
165a7e3b975SFilipe Manana 		 */
166a7e3b975SFilipe Manana 		u64 new_delalloc_bytes;
167dc287224SFilipe Manana 		/*
168dc287224SFilipe Manana 		 * The offset of the last dir index key that was logged.
169dc287224SFilipe Manana 		 * This is used only for directories.
170dc287224SFilipe Manana 		 */
171dc287224SFilipe Manana 		u64 last_dir_index_offset;
172dc287224SFilipe Manana 	};
173a7e3b975SFilipe Manana 
174a7e3b975SFilipe Manana 	/*
17547059d93SWang Shilong 	 * total number of bytes pending defrag, used by stat to check whether
17647059d93SWang Shilong 	 * it needs COW.
17747059d93SWang Shilong 	 */
17847059d93SWang Shilong 	u64 defrag_bytes;
17947059d93SWang Shilong 
18047059d93SWang Shilong 	/*
181d352ac68SChris Mason 	 * the size of the file stored in the metadata on disk.  data=ordered
182d352ac68SChris Mason 	 * means the in-memory i_size might be larger than the size on disk
183d352ac68SChris Mason 	 * because not all the blocks are written yet.
184d352ac68SChris Mason 	 */
185dbe674a9SChris Mason 	u64 disk_i_size;
186d352ac68SChris Mason 
187aec7477bSJosef Bacik 	/*
188528ee697SFilipe Manana 	 * If this is a directory then index_cnt is the counter for the index
189528ee697SFilipe Manana 	 * number for new files that are created. For an empty directory, this
190528ee697SFilipe Manana 	 * must be initialized to BTRFS_DIR_START_INDEX.
191aec7477bSJosef Bacik 	 */
192aec7477bSJosef Bacik 	u64 index_cnt;
193d352ac68SChris Mason 
19467de1176SMiao Xie 	/* Cache the directory index number to speed the dir/file remove */
19567de1176SMiao Xie 	u64 dir_index;
19667de1176SMiao Xie 
19712fcfd22SChris Mason 	/* the fsync log has some corner cases that mean we have to check
19812fcfd22SChris Mason 	 * directories to see if any unlinks have been done before
19912fcfd22SChris Mason 	 * the directory was logged.  See tree-log.c for all the
20012fcfd22SChris Mason 	 * details
20112fcfd22SChris Mason 	 */
20212fcfd22SChris Mason 	u64 last_unlink_trans;
20312fcfd22SChris Mason 
2047709cde3SJosef Bacik 	/*
2053ebac17cSFilipe Manana 	 * The id/generation of the last transaction where this inode was
2063ebac17cSFilipe Manana 	 * either the source or the destination of a clone/dedupe operation.
2073ebac17cSFilipe Manana 	 * Used when logging an inode to know if there are shared extents that
2083ebac17cSFilipe Manana 	 * need special care when logging checksum items, to avoid duplicate
2093ebac17cSFilipe Manana 	 * checksum items in a log (which can lead to a corruption where we end
2103ebac17cSFilipe Manana 	 * up with missing checksum ranges after log replay).
2113ebac17cSFilipe Manana 	 * Protected by the vfs inode lock.
2123ebac17cSFilipe Manana 	 */
2133ebac17cSFilipe Manana 	u64 last_reflink_trans;
2143ebac17cSFilipe Manana 
2153ebac17cSFilipe Manana 	/*
2167709cde3SJosef Bacik 	 * Number of bytes outstanding that are going to need csums.  This is
2177709cde3SJosef Bacik 	 * used in ENOSPC accounting.
2187709cde3SJosef Bacik 	 */
2197709cde3SJosef Bacik 	u64 csum_bytes;
2207709cde3SJosef Bacik 
22177eea05eSBoris Burkov 	/* Backwards incompatible flags, lower half of inode_item::flags  */
222f1bdcc0aSJosef Bacik 	u32 flags;
22377eea05eSBoris Burkov 	/* Read-only compatibility flags, upper half of inode_item::flags */
22477eea05eSBoris Burkov 	u32 ro_flags;
225f1bdcc0aSJosef Bacik 
2265a3f23d5SChris Mason 	/*
22732c00affSJosef Bacik 	 * Counters to keep track of the number of extent item's we may use due
22832c00affSJosef Bacik 	 * to delalloc and such.  outstanding_extents is the number of extent
22932c00affSJosef Bacik 	 * items we think we'll end up using, and reserved_extents is the number
23032c00affSJosef Bacik 	 * of extent items we've reserved metadata for.
2319ed74f2dSJosef Bacik 	 */
2329e0baf60SJosef Bacik 	unsigned outstanding_extents;
23369fe2d75SJosef Bacik 
23469fe2d75SJosef Bacik 	struct btrfs_block_rsv block_rsv;
2359ed74f2dSJosef Bacik 
2369ed74f2dSJosef Bacik 	/*
237b52aa8c9SDavid Sterba 	 * Cached values of inode properties
2381e701a32SChris Mason 	 */
239b52aa8c9SDavid Sterba 	unsigned prop_compress;		/* per-file compression algorithm */
240eec63c65SDavid Sterba 	/*
241eec63c65SDavid Sterba 	 * Force compression on the file using the defrag ioctl, could be
242eec63c65SDavid Sterba 	 * different from prop_compress and takes precedence if set
243eec63c65SDavid Sterba 	 */
244eec63c65SDavid Sterba 	unsigned defrag_compress;
2451e701a32SChris Mason 
24616cdcec7SMiao Xie 	struct btrfs_delayed_node *delayed_node;
24716cdcec7SMiao Xie 
2489cc97d64Schandan r 	/* File creation time. */
249d3c6be6fSArnd Bergmann 	struct timespec64 i_otime;
2509cc97d64Schandan r 
2518089fe62SDavid Sterba 	/* Hook into fs_info->delayed_iputs */
2528089fe62SDavid Sterba 	struct list_head delayed_iput;
2538089fe62SDavid Sterba 
2548318ba79SJosef Bacik 	struct rw_semaphore i_mmap_lock;
255d352ac68SChris Mason 	struct inode vfs_inode;
2562c90e5d6SChris Mason };
257dbe674a9SChris Mason 
btrfs_get_first_dir_index_to_log(const struct btrfs_inode * inode)258fa4b8cb1SFilipe Manana static inline u64 btrfs_get_first_dir_index_to_log(const struct btrfs_inode *inode)
259fa4b8cb1SFilipe Manana {
260fa4b8cb1SFilipe Manana 	return READ_ONCE(inode->first_dir_index_to_log);
261fa4b8cb1SFilipe Manana }
262fa4b8cb1SFilipe Manana 
btrfs_set_first_dir_index_to_log(struct btrfs_inode * inode,u64 index)263fa4b8cb1SFilipe Manana static inline void btrfs_set_first_dir_index_to_log(struct btrfs_inode *inode,
264fa4b8cb1SFilipe Manana 						    u64 index)
265fa4b8cb1SFilipe Manana {
266fa4b8cb1SFilipe Manana 	WRITE_ONCE(inode->first_dir_index_to_log, index);
267fa4b8cb1SFilipe Manana }
268fa4b8cb1SFilipe Manana 
BTRFS_I(const struct inode * inode)2699a35b637SJeff Mahoney static inline struct btrfs_inode *BTRFS_I(const struct inode *inode)
2702c90e5d6SChris Mason {
2712c90e5d6SChris Mason 	return container_of(inode, struct btrfs_inode, vfs_inode);
2722c90e5d6SChris Mason }
2732c90e5d6SChris Mason 
btrfs_inode_hash(u64 objectid,const struct btrfs_root * root)274778ba82bSFilipe David Borba Manana static inline unsigned long btrfs_inode_hash(u64 objectid,
275778ba82bSFilipe David Borba Manana 					     const struct btrfs_root *root)
276778ba82bSFilipe David Borba Manana {
2774fd786e6SMisono Tomohiro 	u64 h = objectid ^ (root->root_key.objectid * GOLDEN_RATIO_PRIME);
278778ba82bSFilipe David Borba Manana 
279778ba82bSFilipe David Borba Manana #if BITS_PER_LONG == 32
280778ba82bSFilipe David Borba Manana 	h = (h >> 32) ^ (h & 0xffffffff);
281778ba82bSFilipe David Borba Manana #endif
282778ba82bSFilipe David Borba Manana 
283778ba82bSFilipe David Borba Manana 	return (unsigned long)h;
284778ba82bSFilipe David Borba Manana }
285778ba82bSFilipe David Borba Manana 
286cf2404a9SFilipe Manana #if BITS_PER_LONG == 32
287cf2404a9SFilipe Manana 
288cf2404a9SFilipe Manana /*
289cf2404a9SFilipe Manana  * On 32 bit systems the i_ino of struct inode is 32 bits (unsigned long), so
290cf2404a9SFilipe Manana  * we use the inode's location objectid which is a u64 to avoid truncation.
291cf2404a9SFilipe Manana  */
btrfs_ino(const struct btrfs_inode * inode)2929a35b637SJeff Mahoney static inline u64 btrfs_ino(const struct btrfs_inode *inode)
29333345d01SLi Zefan {
2944a0cc7caSNikolay Borisov 	u64 ino = inode->location.objectid;
29533345d01SLi Zefan 
296adac5584SFilipe Manana 	/* type == BTRFS_ROOT_ITEM_KEY: subvol dir */
297adac5584SFilipe Manana 	if (inode->location.type == BTRFS_ROOT_ITEM_KEY)
2984a0cc7caSNikolay Borisov 		ino = inode->vfs_inode.i_ino;
29933345d01SLi Zefan 	return ino;
30033345d01SLi Zefan }
30133345d01SLi Zefan 
302cf2404a9SFilipe Manana #else
303cf2404a9SFilipe Manana 
btrfs_ino(const struct btrfs_inode * inode)304cf2404a9SFilipe Manana static inline u64 btrfs_ino(const struct btrfs_inode *inode)
305cf2404a9SFilipe Manana {
306cf2404a9SFilipe Manana 	return inode->vfs_inode.i_ino;
307cf2404a9SFilipe Manana }
308cf2404a9SFilipe Manana 
309cf2404a9SFilipe Manana #endif
310cf2404a9SFilipe Manana 
btrfs_i_size_write(struct btrfs_inode * inode,u64 size)3116ef06d27SNikolay Borisov static inline void btrfs_i_size_write(struct btrfs_inode *inode, u64 size)
312dbe674a9SChris Mason {
3136ef06d27SNikolay Borisov 	i_size_write(&inode->vfs_inode, size);
3146ef06d27SNikolay Borisov 	inode->disk_i_size = size;
315dbe674a9SChris Mason }
316dbe674a9SChris Mason 
btrfs_is_free_space_inode(struct btrfs_inode * inode)31770ddc553SNikolay Borisov static inline bool btrfs_is_free_space_inode(struct btrfs_inode *inode)
3182cf8572dSChris Mason {
3199b9b8854SJosef Bacik 	return test_bit(BTRFS_INODE_FREE_SPACE_INODE, &inode->runtime_flags);
3202cf8572dSChris Mason }
3212cf8572dSChris Mason 
is_data_inode(struct inode * inode)32206f2548fSNikolay Borisov static inline bool is_data_inode(struct inode *inode)
32306f2548fSNikolay Borisov {
32406f2548fSNikolay Borisov 	return btrfs_ino(BTRFS_I(inode)) != BTRFS_BTREE_INODE_OBJECTID;
32506f2548fSNikolay Borisov }
32606f2548fSNikolay Borisov 
btrfs_mod_outstanding_extents(struct btrfs_inode * inode,int mod)3278b62f87bSJosef Bacik static inline void btrfs_mod_outstanding_extents(struct btrfs_inode *inode,
3288b62f87bSJosef Bacik 						 int mod)
3298b62f87bSJosef Bacik {
3308b62f87bSJosef Bacik 	lockdep_assert_held(&inode->lock);
3318b62f87bSJosef Bacik 	inode->outstanding_extents += mod;
3328b62f87bSJosef Bacik 	if (btrfs_is_free_space_inode(inode))
3338b62f87bSJosef Bacik 		return;
334dd48d407SJosef Bacik 	trace_btrfs_inode_mod_outstanding_extents(inode->root, btrfs_ino(inode),
33564425500SNaohiro Aota 						  mod, inode->outstanding_extents);
3368b62f87bSJosef Bacik }
3378b62f87bSJosef Bacik 
338bc0939fcSFilipe Manana /*
339bc0939fcSFilipe Manana  * Called every time after doing a buffered, direct IO or memory mapped write.
340bc0939fcSFilipe Manana  *
341bc0939fcSFilipe Manana  * This is to ensure that if we write to a file that was previously fsynced in
342bc0939fcSFilipe Manana  * the current transaction, then try to fsync it again in the same transaction,
343bc0939fcSFilipe Manana  * we will know that there were changes in the file and that it needs to be
344bc0939fcSFilipe Manana  * logged.
345bc0939fcSFilipe Manana  */
btrfs_set_inode_last_sub_trans(struct btrfs_inode * inode)346bc0939fcSFilipe Manana static inline void btrfs_set_inode_last_sub_trans(struct btrfs_inode *inode)
347bc0939fcSFilipe Manana {
348bc0939fcSFilipe Manana 	spin_lock(&inode->lock);
349bc0939fcSFilipe Manana 	inode->last_sub_trans = inode->root->log_transid;
350bc0939fcSFilipe Manana 	spin_unlock(&inode->lock);
351bc0939fcSFilipe Manana }
352bc0939fcSFilipe Manana 
35323e3337fSFilipe Manana /*
35423e3337fSFilipe Manana  * Should be called while holding the inode's VFS lock in exclusive mode or in a
35523e3337fSFilipe Manana  * context where no one else can access the inode concurrently (during inode
35623e3337fSFilipe Manana  * creation or when loading an inode from disk).
35723e3337fSFilipe Manana  */
btrfs_set_inode_full_sync(struct btrfs_inode * inode)35823e3337fSFilipe Manana static inline void btrfs_set_inode_full_sync(struct btrfs_inode *inode)
35923e3337fSFilipe Manana {
36023e3337fSFilipe Manana 	set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
36123e3337fSFilipe Manana 	/*
36223e3337fSFilipe Manana 	 * The inode may have been part of a reflink operation in the last
36323e3337fSFilipe Manana 	 * transaction that modified it, and then a fsync has reset the
36423e3337fSFilipe Manana 	 * last_reflink_trans to avoid subsequent fsyncs in the same
36523e3337fSFilipe Manana 	 * transaction to do unnecessary work. So update last_reflink_trans
36623e3337fSFilipe Manana 	 * to the last_trans value (we have to be pessimistic and assume a
36723e3337fSFilipe Manana 	 * reflink happened).
36823e3337fSFilipe Manana 	 *
36923e3337fSFilipe Manana 	 * The ->last_trans is protected by the inode's spinlock and we can
37023e3337fSFilipe Manana 	 * have a concurrent ordered extent completion update it. Also set
37123e3337fSFilipe Manana 	 * last_reflink_trans to ->last_trans only if the former is less than
37223e3337fSFilipe Manana 	 * the later, because we can be called in a context where
37323e3337fSFilipe Manana 	 * last_reflink_trans was set to the current transaction generation
37423e3337fSFilipe Manana 	 * while ->last_trans was not yet updated in the current transaction,
37523e3337fSFilipe Manana 	 * and therefore has a lower value.
37623e3337fSFilipe Manana 	 */
37723e3337fSFilipe Manana 	spin_lock(&inode->lock);
37823e3337fSFilipe Manana 	if (inode->last_reflink_trans < inode->last_trans)
37923e3337fSFilipe Manana 		inode->last_reflink_trans = inode->last_trans;
38023e3337fSFilipe Manana 	spin_unlock(&inode->lock);
38123e3337fSFilipe Manana }
38223e3337fSFilipe Manana 
btrfs_inode_in_log(struct btrfs_inode * inode,u64 generation)383209ecbb8SFilipe Manana static inline bool btrfs_inode_in_log(struct btrfs_inode *inode, u64 generation)
38422ee6985SJosef Bacik {
385209ecbb8SFilipe Manana 	bool ret = false;
3862f2ff0eeSFilipe Manana 
3870f8939b8SNikolay Borisov 	spin_lock(&inode->lock);
3880f8939b8SNikolay Borisov 	if (inode->logged_trans == generation &&
3890f8939b8SNikolay Borisov 	    inode->last_sub_trans <= inode->last_log_commit &&
390209ecbb8SFilipe Manana 	    inode->last_sub_trans <= inode->root->last_log_commit)
391209ecbb8SFilipe Manana 		ret = true;
3920f8939b8SNikolay Borisov 	spin_unlock(&inode->lock);
3932f2ff0eeSFilipe Manana 	return ret;
39422ee6985SJosef Bacik }
39522ee6985SJosef Bacik 
396e6f9d696SChung-Chiang Cheng /*
397e6f9d696SChung-Chiang Cheng  * Check if the inode has flags compatible with compression
398e6f9d696SChung-Chiang Cheng  */
btrfs_inode_can_compress(const struct btrfs_inode * inode)399e6f9d696SChung-Chiang Cheng static inline bool btrfs_inode_can_compress(const struct btrfs_inode *inode)
400e6f9d696SChung-Chiang Cheng {
401e6f9d696SChung-Chiang Cheng 	if (inode->flags & BTRFS_INODE_NODATACOW ||
402e6f9d696SChung-Chiang Cheng 	    inode->flags & BTRFS_INODE_NODATASUM)
403e6f9d696SChung-Chiang Cheng 		return false;
404e6f9d696SChung-Chiang Cheng 	return true;
405e6f9d696SChung-Chiang Cheng }
406e6f9d696SChung-Chiang Cheng 
4077ebc7e5fSJohannes Thumshirn /* Array of bytes with variable length, hexadecimal format 0x1234 */
4087ebc7e5fSJohannes Thumshirn #define CSUM_FMT				"0x%*phN"
4097ebc7e5fSJohannes Thumshirn #define CSUM_FMT_VALUE(size, bytes)		size, bytes
4102885fd63SJosef Bacik 
4112885fd63SJosef Bacik int btrfs_check_sector_csum(struct btrfs_fs_info *fs_info, struct page *page,
4122885fd63SJosef Bacik 			    u32 pgoff, u8 *csum, const u8 * const csum_expected);
413e5219044SChristoph Hellwig bool btrfs_data_csum_ok(struct btrfs_bio *bbio, struct btrfs_device *dev,
414e5219044SChristoph Hellwig 			u32 bio_offset, struct bio_vec *bv);
4152885fd63SJosef Bacik noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
4162885fd63SJosef Bacik 			      u64 *orig_start, u64 *orig_block_len,
4172885fd63SJosef Bacik 			      u64 *ram_bytes, bool nowait, bool strict);
4182885fd63SJosef Bacik 
4192885fd63SJosef Bacik void __btrfs_del_delalloc_inode(struct btrfs_root *root, struct btrfs_inode *inode);
4202885fd63SJosef Bacik struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry);
4212885fd63SJosef Bacik int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index);
4222885fd63SJosef Bacik int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4232885fd63SJosef Bacik 		       struct btrfs_inode *dir, struct btrfs_inode *inode,
4242885fd63SJosef Bacik 		       const struct fscrypt_str *name);
4252885fd63SJosef Bacik int btrfs_add_link(struct btrfs_trans_handle *trans,
4262885fd63SJosef Bacik 		   struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
4272885fd63SJosef Bacik 		   const struct fscrypt_str *name, int add_backref, u64 index);
4283c4f91e2SDavid Sterba int btrfs_delete_subvolume(struct btrfs_inode *dir, struct dentry *dentry);
4292885fd63SJosef Bacik int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
4302885fd63SJosef Bacik 			 int front);
4312885fd63SJosef Bacik 
4322885fd63SJosef Bacik int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context);
4332885fd63SJosef Bacik int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
4342885fd63SJosef Bacik 			       bool in_reclaim_context);
4352885fd63SJosef Bacik int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
4362885fd63SJosef Bacik 			      unsigned int extra_bits,
4372885fd63SJosef Bacik 			      struct extent_state **cached_state);
4382885fd63SJosef Bacik 
4392885fd63SJosef Bacik struct btrfs_new_inode_args {
4402885fd63SJosef Bacik 	/* Input */
4412885fd63SJosef Bacik 	struct inode *dir;
4422885fd63SJosef Bacik 	struct dentry *dentry;
4432885fd63SJosef Bacik 	struct inode *inode;
4442885fd63SJosef Bacik 	bool orphan;
4452885fd63SJosef Bacik 	bool subvol;
4462885fd63SJosef Bacik 
4472885fd63SJosef Bacik 	/* Output from btrfs_new_inode_prepare(), input to btrfs_create_new_inode(). */
4482885fd63SJosef Bacik 	struct posix_acl *default_acl;
4492885fd63SJosef Bacik 	struct posix_acl *acl;
4502885fd63SJosef Bacik 	struct fscrypt_name fname;
4512885fd63SJosef Bacik };
4522885fd63SJosef Bacik 
4532885fd63SJosef Bacik int btrfs_new_inode_prepare(struct btrfs_new_inode_args *args,
4542885fd63SJosef Bacik 			    unsigned int *trans_num_items);
4552885fd63SJosef Bacik int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
4562885fd63SJosef Bacik 			   struct btrfs_new_inode_args *args);
4572885fd63SJosef Bacik void btrfs_new_inode_args_destroy(struct btrfs_new_inode_args *args);
458f2d40141SChristian Brauner struct inode *btrfs_new_subvol_inode(struct mnt_idmap *idmap,
4592885fd63SJosef Bacik 				     struct inode *dir);
4604c5d166fSDavid Sterba  void btrfs_set_delalloc_extent(struct btrfs_inode *inode, struct extent_state *state,
4612885fd63SJosef Bacik 			        u32 bits);
462bd54766eSDavid Sterba void btrfs_clear_delalloc_extent(struct btrfs_inode *inode,
4632885fd63SJosef Bacik 				 struct extent_state *state, u32 bits);
4642454151cSDavid Sterba void btrfs_merge_delalloc_extent(struct btrfs_inode *inode, struct extent_state *new,
4652885fd63SJosef Bacik 				 struct extent_state *other);
46662798a49SDavid Sterba void btrfs_split_delalloc_extent(struct btrfs_inode *inode,
4672885fd63SJosef Bacik 				 struct extent_state *orig, u64 split);
4682885fd63SJosef Bacik void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end);
4692885fd63SJosef Bacik vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf);
4702885fd63SJosef Bacik void btrfs_evict_inode(struct inode *inode);
4712885fd63SJosef Bacik struct inode *btrfs_alloc_inode(struct super_block *sb);
4722885fd63SJosef Bacik void btrfs_destroy_inode(struct inode *inode);
4732885fd63SJosef Bacik void btrfs_free_inode(struct inode *inode);
4742885fd63SJosef Bacik int btrfs_drop_inode(struct inode *inode);
4752885fd63SJosef Bacik int __init btrfs_init_cachep(void);
4762885fd63SJosef Bacik void __cold btrfs_destroy_cachep(void);
4772885fd63SJosef Bacik struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
4782885fd63SJosef Bacik 			      struct btrfs_root *root, struct btrfs_path *path);
4792885fd63SJosef Bacik struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root);
4802885fd63SJosef Bacik struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
4812885fd63SJosef Bacik 				    struct page *page, size_t pg_offset,
4822885fd63SJosef Bacik 				    u64 start, u64 end);
4832885fd63SJosef Bacik int btrfs_update_inode(struct btrfs_trans_handle *trans,
4842885fd63SJosef Bacik 		       struct btrfs_root *root, struct btrfs_inode *inode);
4852885fd63SJosef Bacik int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4862885fd63SJosef Bacik 				struct btrfs_root *root, struct btrfs_inode *inode);
4872885fd63SJosef Bacik int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct btrfs_inode *inode);
4882885fd63SJosef Bacik int btrfs_orphan_cleanup(struct btrfs_root *root);
4892885fd63SJosef Bacik int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size);
490e55cf7caSDavid Sterba void btrfs_add_delayed_iput(struct btrfs_inode *inode);
4912885fd63SJosef Bacik void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info);
4922885fd63SJosef Bacik int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info);
4932885fd63SJosef Bacik int btrfs_prealloc_file_range(struct inode *inode, int mode,
4942885fd63SJosef Bacik 			      u64 start, u64 num_bytes, u64 min_size,
4952885fd63SJosef Bacik 			      loff_t actual_len, u64 *alloc_hint);
4962885fd63SJosef Bacik int btrfs_prealloc_file_range_trans(struct inode *inode,
4972885fd63SJosef Bacik 				    struct btrfs_trans_handle *trans, int mode,
4982885fd63SJosef Bacik 				    u64 start, u64 num_bytes, u64 min_size,
4992885fd63SJosef Bacik 				    loff_t actual_len, u64 *alloc_hint);
5002885fd63SJosef Bacik int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
501*c56cbe90SChristoph Hellwig 			     u64 start, u64 end, struct writeback_control *wbc);
5022885fd63SJosef Bacik int btrfs_writepage_cow_fixup(struct page *page);
5032885fd63SJosef Bacik int btrfs_encoded_io_compression_from_extent(struct btrfs_fs_info *fs_info,
5042885fd63SJosef Bacik 					     int compress_type);
5052885fd63SJosef Bacik int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode,
5062885fd63SJosef Bacik 					  u64 file_offset, u64 disk_bytenr,
5072885fd63SJosef Bacik 					  u64 disk_io_size,
5082885fd63SJosef Bacik 					  struct page **pages);
5092885fd63SJosef Bacik ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter,
5102885fd63SJosef Bacik 			   struct btrfs_ioctl_encoded_io_args *encoded);
5112885fd63SJosef Bacik ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from,
5122885fd63SJosef Bacik 			       const struct btrfs_ioctl_encoded_io_args *encoded);
5132885fd63SJosef Bacik 
5142885fd63SJosef Bacik ssize_t btrfs_dio_read(struct kiocb *iocb, struct iov_iter *iter,
5152885fd63SJosef Bacik 		       size_t done_before);
5162885fd63SJosef Bacik struct iomap_dio *btrfs_dio_write(struct kiocb *iocb, struct iov_iter *iter,
5172885fd63SJosef Bacik 				  size_t done_before);
5182885fd63SJosef Bacik 
5192885fd63SJosef Bacik extern const struct dentry_operations btrfs_dentry_operations;
5202885fd63SJosef Bacik 
5212885fd63SJosef Bacik /* Inode locking type flags, by default the exclusive lock is taken. */
5222885fd63SJosef Bacik enum btrfs_ilock_type {
5232885fd63SJosef Bacik 	ENUM_BIT(BTRFS_ILOCK_SHARED),
5242885fd63SJosef Bacik 	ENUM_BIT(BTRFS_ILOCK_TRY),
5252885fd63SJosef Bacik 	ENUM_BIT(BTRFS_ILOCK_MMAP),
5262885fd63SJosef Bacik };
5272885fd63SJosef Bacik 
52829b6352bSDavid Sterba int btrfs_inode_lock(struct btrfs_inode *inode, unsigned int ilock_flags);
529e5d4d75bSDavid Sterba void btrfs_inode_unlock(struct btrfs_inode *inode, unsigned int ilock_flags);
5302885fd63SJosef Bacik void btrfs_update_inode_bytes(struct btrfs_inode *inode, const u64 add_bytes,
5312885fd63SJosef Bacik 			      const u64 del_bytes);
5322885fd63SJosef Bacik void btrfs_assert_inode_range_clean(struct btrfs_inode *inode, u64 start, u64 end);
5332885fd63SJosef Bacik 
5342c90e5d6SChris Mason #endif
535