xref: /openbmc/linux/fs/btrfs/btrfs_inode.h (revision 23e3337faf73e5bb2610697977e175313d48acb0)
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,
687efc3e34SOmar Sandoval };
6972ac3c0dSJosef Bacik 
70f1ace244SAneesh /* in memory btrfs inode */
712c90e5d6SChris Mason struct btrfs_inode {
72d352ac68SChris Mason 	/* which subvolume this inode belongs to */
73d6e4a428SChris Mason 	struct btrfs_root *root;
74d352ac68SChris Mason 
75d352ac68SChris Mason 	/* key used to find this inode on disk.  This is used by the code
76d352ac68SChris Mason 	 * to read in roots of subvolumes
77d352ac68SChris Mason 	 */
78d6e4a428SChris Mason 	struct btrfs_key location;
79d352ac68SChris Mason 
802f2ff0eeSFilipe Manana 	/*
812f2ff0eeSFilipe Manana 	 * Lock for counters and all fields used to determine if the inode is in
822f2ff0eeSFilipe Manana 	 * the log or not (last_trans, last_sub_trans, last_log_commit,
832766ff61SFilipe Manana 	 * logged_trans), to access/update new_delalloc_bytes and to update the
842766ff61SFilipe Manana 	 * VFS' inode number of bytes used.
852f2ff0eeSFilipe Manana 	 */
869e0baf60SJosef Bacik 	spinlock_t lock;
879e0baf60SJosef Bacik 
88d352ac68SChris Mason 	/* the extent_tree has caches of all the extent mappings to disk */
89a52d9a80SChris Mason 	struct extent_map_tree extent_tree;
90d352ac68SChris Mason 
91d352ac68SChris Mason 	/* the io_tree does range state (DIRTY, LOCKED etc) */
92d1310b2eSChris Mason 	struct extent_io_tree io_tree;
93d352ac68SChris Mason 
94d352ac68SChris Mason 	/* special utility tree used to record which mirrors have already been
95d352ac68SChris Mason 	 * tried when checksums fail for a given block
96d352ac68SChris Mason 	 */
977e38326fSChris Mason 	struct extent_io_tree io_failure_tree;
98d352ac68SChris Mason 
9941a2ee75SJosef Bacik 	/*
10041a2ee75SJosef Bacik 	 * Keep track of where the inode has extent items mapped in order to
10141a2ee75SJosef Bacik 	 * make sure the i_size adjustments are accurate
10241a2ee75SJosef Bacik 	 */
10341a2ee75SJosef Bacik 	struct extent_io_tree file_extent_tree;
10441a2ee75SJosef Bacik 
105d352ac68SChris Mason 	/* held while logging the inode in tree-log.c */
106e02119d5SChris Mason 	struct mutex log_mutex;
107d352ac68SChris Mason 
108d352ac68SChris Mason 	/* used to order data wrt metadata */
109e6dcd2dcSChris Mason 	struct btrfs_ordered_inode_tree ordered_tree;
11015ee9bc7SJosef Bacik 
111d352ac68SChris Mason 	/* list of all the delalloc inodes in the FS.  There are times we need
112d352ac68SChris Mason 	 * to write all the delalloc pages to disk, and this list is used
113d352ac68SChris Mason 	 * to walk them all.
114d352ac68SChris Mason 	 */
115ea8c2819SChris Mason 	struct list_head delalloc_inodes;
116ea8c2819SChris Mason 
1175d4f98a2SYan Zheng 	/* node for the red-black tree that links inodes in subvolume root */
1185d4f98a2SYan Zheng 	struct rb_node rb_node;
1195d4f98a2SYan Zheng 
12072ac3c0dSJosef Bacik 	unsigned long runtime_flags;
12172ac3c0dSJosef Bacik 
1229c931c5aSNathaniel Yazdani 	/* Keep track of who's O_SYNC/fsyncing currently */
123b812ce28SJosef Bacik 	atomic_t sync_writers;
124b812ce28SJosef Bacik 
125d352ac68SChris Mason 	/* full 64 bit generation number, struct vfs_inode doesn't have a big
126d352ac68SChris Mason 	 * enough field for this.
127d352ac68SChris Mason 	 */
128e02119d5SChris Mason 	u64 generation;
129e02119d5SChris Mason 
13015ee9bc7SJosef Bacik 	/*
13115ee9bc7SJosef Bacik 	 * transid of the trans_handle that last modified this inode
13215ee9bc7SJosef Bacik 	 */
13315ee9bc7SJosef Bacik 	u64 last_trans;
134257c62e1SChris Mason 
135257c62e1SChris Mason 	/*
136e02119d5SChris Mason 	 * transid that last logged this inode
137e02119d5SChris Mason 	 */
138e02119d5SChris Mason 	u64 logged_trans;
13949eb7e46SChris Mason 
140bb14a59bSMiao Xie 	/*
141bb14a59bSMiao Xie 	 * log transid when this inode was last modified
142bb14a59bSMiao Xie 	 */
143bb14a59bSMiao Xie 	int last_sub_trans;
144bb14a59bSMiao Xie 
145bb14a59bSMiao Xie 	/* a local copy of root's last_log_commit */
146bb14a59bSMiao Xie 	int last_log_commit;
147bb14a59bSMiao Xie 
148dc287224SFilipe Manana 	/*
149339d0354SFilipe Manana 	 * Total number of bytes pending delalloc, used by stat to calculate the
150339d0354SFilipe Manana 	 * real block usage of the file. This is used only for files.
151d352ac68SChris Mason 	 */
1529069218dSChris Mason 	u64 delalloc_bytes;
153d352ac68SChris Mason 
154dc287224SFilipe Manana 	union {
155d352ac68SChris Mason 		/*
156a7e3b975SFilipe Manana 		 * Total number of bytes pending delalloc that fall within a file
157a7e3b975SFilipe Manana 		 * range that is either a hole or beyond EOF (and no prealloc extent
158dc287224SFilipe Manana 		 * exists in the range). This is always <= delalloc_bytes and this
159dc287224SFilipe Manana 		 * is used only for files.
160a7e3b975SFilipe Manana 		 */
161a7e3b975SFilipe Manana 		u64 new_delalloc_bytes;
162dc287224SFilipe Manana 		/*
163dc287224SFilipe Manana 		 * The offset of the last dir index key that was logged.
164dc287224SFilipe Manana 		 * This is used only for directories.
165dc287224SFilipe Manana 		 */
166dc287224SFilipe Manana 		u64 last_dir_index_offset;
167dc287224SFilipe Manana 	};
168a7e3b975SFilipe Manana 
169a7e3b975SFilipe Manana 	/*
17047059d93SWang Shilong 	 * total number of bytes pending defrag, used by stat to check whether
17147059d93SWang Shilong 	 * it needs COW.
17247059d93SWang Shilong 	 */
17347059d93SWang Shilong 	u64 defrag_bytes;
17447059d93SWang Shilong 
17547059d93SWang Shilong 	/*
176d352ac68SChris Mason 	 * the size of the file stored in the metadata on disk.  data=ordered
177d352ac68SChris Mason 	 * means the in-memory i_size might be larger than the size on disk
178d352ac68SChris Mason 	 * because not all the blocks are written yet.
179d352ac68SChris Mason 	 */
180dbe674a9SChris Mason 	u64 disk_i_size;
181d352ac68SChris Mason 
182aec7477bSJosef Bacik 	/*
183528ee697SFilipe Manana 	 * If this is a directory then index_cnt is the counter for the index
184528ee697SFilipe Manana 	 * number for new files that are created. For an empty directory, this
185528ee697SFilipe Manana 	 * must be initialized to BTRFS_DIR_START_INDEX.
186aec7477bSJosef Bacik 	 */
187aec7477bSJosef Bacik 	u64 index_cnt;
188d352ac68SChris Mason 
18967de1176SMiao Xie 	/* Cache the directory index number to speed the dir/file remove */
19067de1176SMiao Xie 	u64 dir_index;
19167de1176SMiao Xie 
19212fcfd22SChris Mason 	/* the fsync log has some corner cases that mean we have to check
19312fcfd22SChris Mason 	 * directories to see if any unlinks have been done before
19412fcfd22SChris Mason 	 * the directory was logged.  See tree-log.c for all the
19512fcfd22SChris Mason 	 * details
19612fcfd22SChris Mason 	 */
19712fcfd22SChris Mason 	u64 last_unlink_trans;
19812fcfd22SChris Mason 
1997709cde3SJosef Bacik 	/*
2003ebac17cSFilipe Manana 	 * The id/generation of the last transaction where this inode was
2013ebac17cSFilipe Manana 	 * either the source or the destination of a clone/dedupe operation.
2023ebac17cSFilipe Manana 	 * Used when logging an inode to know if there are shared extents that
2033ebac17cSFilipe Manana 	 * need special care when logging checksum items, to avoid duplicate
2043ebac17cSFilipe Manana 	 * checksum items in a log (which can lead to a corruption where we end
2053ebac17cSFilipe Manana 	 * up with missing checksum ranges after log replay).
2063ebac17cSFilipe Manana 	 * Protected by the vfs inode lock.
2073ebac17cSFilipe Manana 	 */
2083ebac17cSFilipe Manana 	u64 last_reflink_trans;
2093ebac17cSFilipe Manana 
2103ebac17cSFilipe Manana 	/*
2117709cde3SJosef Bacik 	 * Number of bytes outstanding that are going to need csums.  This is
2127709cde3SJosef Bacik 	 * used in ENOSPC accounting.
2137709cde3SJosef Bacik 	 */
2147709cde3SJosef Bacik 	u64 csum_bytes;
2157709cde3SJosef Bacik 
21677eea05eSBoris Burkov 	/* Backwards incompatible flags, lower half of inode_item::flags  */
217f1bdcc0aSJosef Bacik 	u32 flags;
21877eea05eSBoris Burkov 	/* Read-only compatibility flags, upper half of inode_item::flags */
21977eea05eSBoris Burkov 	u32 ro_flags;
220f1bdcc0aSJosef Bacik 
2215a3f23d5SChris Mason 	/*
22232c00affSJosef Bacik 	 * Counters to keep track of the number of extent item's we may use due
22332c00affSJosef Bacik 	 * to delalloc and such.  outstanding_extents is the number of extent
22432c00affSJosef Bacik 	 * items we think we'll end up using, and reserved_extents is the number
22532c00affSJosef Bacik 	 * of extent items we've reserved metadata for.
2269ed74f2dSJosef Bacik 	 */
2279e0baf60SJosef Bacik 	unsigned outstanding_extents;
22869fe2d75SJosef Bacik 
22969fe2d75SJosef Bacik 	struct btrfs_block_rsv block_rsv;
2309ed74f2dSJosef Bacik 
2319ed74f2dSJosef Bacik 	/*
232b52aa8c9SDavid Sterba 	 * Cached values of inode properties
2331e701a32SChris Mason 	 */
234b52aa8c9SDavid Sterba 	unsigned prop_compress;		/* per-file compression algorithm */
235eec63c65SDavid Sterba 	/*
236eec63c65SDavid Sterba 	 * Force compression on the file using the defrag ioctl, could be
237eec63c65SDavid Sterba 	 * different from prop_compress and takes precedence if set
238eec63c65SDavid Sterba 	 */
239eec63c65SDavid Sterba 	unsigned defrag_compress;
2401e701a32SChris Mason 
24116cdcec7SMiao Xie 	struct btrfs_delayed_node *delayed_node;
24216cdcec7SMiao Xie 
2439cc97d64Schandan r 	/* File creation time. */
244d3c6be6fSArnd Bergmann 	struct timespec64 i_otime;
2459cc97d64Schandan r 
2468089fe62SDavid Sterba 	/* Hook into fs_info->delayed_iputs */
2478089fe62SDavid Sterba 	struct list_head delayed_iput;
2488089fe62SDavid Sterba 
2498318ba79SJosef Bacik 	struct rw_semaphore i_mmap_lock;
250d352ac68SChris Mason 	struct inode vfs_inode;
2512c90e5d6SChris Mason };
252dbe674a9SChris Mason 
2536fee248dSNikolay Borisov static inline u32 btrfs_inode_sectorsize(const struct btrfs_inode *inode)
2546fee248dSNikolay Borisov {
2556fee248dSNikolay Borisov 	return inode->root->fs_info->sectorsize;
2566fee248dSNikolay Borisov }
2576fee248dSNikolay Borisov 
2589a35b637SJeff Mahoney static inline struct btrfs_inode *BTRFS_I(const struct inode *inode)
2592c90e5d6SChris Mason {
2602c90e5d6SChris Mason 	return container_of(inode, struct btrfs_inode, vfs_inode);
2612c90e5d6SChris Mason }
2622c90e5d6SChris Mason 
263778ba82bSFilipe David Borba Manana static inline unsigned long btrfs_inode_hash(u64 objectid,
264778ba82bSFilipe David Borba Manana 					     const struct btrfs_root *root)
265778ba82bSFilipe David Borba Manana {
2664fd786e6SMisono Tomohiro 	u64 h = objectid ^ (root->root_key.objectid * GOLDEN_RATIO_PRIME);
267778ba82bSFilipe David Borba Manana 
268778ba82bSFilipe David Borba Manana #if BITS_PER_LONG == 32
269778ba82bSFilipe David Borba Manana 	h = (h >> 32) ^ (h & 0xffffffff);
270778ba82bSFilipe David Borba Manana #endif
271778ba82bSFilipe David Borba Manana 
272778ba82bSFilipe David Borba Manana 	return (unsigned long)h;
273778ba82bSFilipe David Borba Manana }
274778ba82bSFilipe David Borba Manana 
275778ba82bSFilipe David Borba Manana static inline void btrfs_insert_inode_hash(struct inode *inode)
276778ba82bSFilipe David Borba Manana {
277778ba82bSFilipe David Borba Manana 	unsigned long h = btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root);
278778ba82bSFilipe David Borba Manana 
279778ba82bSFilipe David Borba Manana 	__insert_inode_hash(inode, h);
280778ba82bSFilipe David Borba Manana }
281778ba82bSFilipe David Borba Manana 
2829a35b637SJeff Mahoney static inline u64 btrfs_ino(const struct btrfs_inode *inode)
28333345d01SLi Zefan {
2844a0cc7caSNikolay Borisov 	u64 ino = inode->location.objectid;
28533345d01SLi Zefan 
28614c7cca7SLiu Bo 	/*
28714c7cca7SLiu Bo 	 * !ino: btree_inode
28814c7cca7SLiu Bo 	 * type == BTRFS_ROOT_ITEM_KEY: subvol dir
28914c7cca7SLiu Bo 	 */
2904a0cc7caSNikolay Borisov 	if (!ino || inode->location.type == BTRFS_ROOT_ITEM_KEY)
2914a0cc7caSNikolay Borisov 		ino = inode->vfs_inode.i_ino;
29233345d01SLi Zefan 	return ino;
29333345d01SLi Zefan }
29433345d01SLi Zefan 
2956ef06d27SNikolay Borisov static inline void btrfs_i_size_write(struct btrfs_inode *inode, u64 size)
296dbe674a9SChris Mason {
2976ef06d27SNikolay Borisov 	i_size_write(&inode->vfs_inode, size);
2986ef06d27SNikolay Borisov 	inode->disk_i_size = size;
299dbe674a9SChris Mason }
300dbe674a9SChris Mason 
30170ddc553SNikolay Borisov static inline bool btrfs_is_free_space_inode(struct btrfs_inode *inode)
3022cf8572dSChris Mason {
30370ddc553SNikolay Borisov 	struct btrfs_root *root = inode->root;
30483eea1f1SLiu Bo 
30551a8cf9dSLiu Bo 	if (root == root->fs_info->tree_root &&
30670ddc553SNikolay Borisov 	    btrfs_ino(inode) != BTRFS_BTREE_INODE_OBJECTID)
30751a8cf9dSLiu Bo 		return true;
30870ddc553SNikolay Borisov 	if (inode->location.objectid == BTRFS_FREE_INO_OBJECTID)
3092cf8572dSChris Mason 		return true;
3102cf8572dSChris Mason 	return false;
3112cf8572dSChris Mason }
3122cf8572dSChris Mason 
31306f2548fSNikolay Borisov static inline bool is_data_inode(struct inode *inode)
31406f2548fSNikolay Borisov {
31506f2548fSNikolay Borisov 	return btrfs_ino(BTRFS_I(inode)) != BTRFS_BTREE_INODE_OBJECTID;
31606f2548fSNikolay Borisov }
31706f2548fSNikolay Borisov 
3188b62f87bSJosef Bacik static inline void btrfs_mod_outstanding_extents(struct btrfs_inode *inode,
3198b62f87bSJosef Bacik 						 int mod)
3208b62f87bSJosef Bacik {
3218b62f87bSJosef Bacik 	lockdep_assert_held(&inode->lock);
3228b62f87bSJosef Bacik 	inode->outstanding_extents += mod;
3238b62f87bSJosef Bacik 	if (btrfs_is_free_space_inode(inode))
3248b62f87bSJosef Bacik 		return;
325dd48d407SJosef Bacik 	trace_btrfs_inode_mod_outstanding_extents(inode->root, btrfs_ino(inode),
326dd48d407SJosef Bacik 						  mod);
3278b62f87bSJosef Bacik }
3288b62f87bSJosef Bacik 
329bc0939fcSFilipe Manana /*
330bc0939fcSFilipe Manana  * Called every time after doing a buffered, direct IO or memory mapped write.
331bc0939fcSFilipe Manana  *
332bc0939fcSFilipe Manana  * This is to ensure that if we write to a file that was previously fsynced in
333bc0939fcSFilipe Manana  * the current transaction, then try to fsync it again in the same transaction,
334bc0939fcSFilipe Manana  * we will know that there were changes in the file and that it needs to be
335bc0939fcSFilipe Manana  * logged.
336bc0939fcSFilipe Manana  */
337bc0939fcSFilipe Manana static inline void btrfs_set_inode_last_sub_trans(struct btrfs_inode *inode)
338bc0939fcSFilipe Manana {
339bc0939fcSFilipe Manana 	spin_lock(&inode->lock);
340bc0939fcSFilipe Manana 	inode->last_sub_trans = inode->root->log_transid;
341bc0939fcSFilipe Manana 	spin_unlock(&inode->lock);
342bc0939fcSFilipe Manana }
343bc0939fcSFilipe Manana 
344*23e3337fSFilipe Manana /*
345*23e3337fSFilipe Manana  * Should be called while holding the inode's VFS lock in exclusive mode or in a
346*23e3337fSFilipe Manana  * context where no one else can access the inode concurrently (during inode
347*23e3337fSFilipe Manana  * creation or when loading an inode from disk).
348*23e3337fSFilipe Manana  */
349*23e3337fSFilipe Manana static inline void btrfs_set_inode_full_sync(struct btrfs_inode *inode)
350*23e3337fSFilipe Manana {
351*23e3337fSFilipe Manana 	set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
352*23e3337fSFilipe Manana 	/*
353*23e3337fSFilipe Manana 	 * The inode may have been part of a reflink operation in the last
354*23e3337fSFilipe Manana 	 * transaction that modified it, and then a fsync has reset the
355*23e3337fSFilipe Manana 	 * last_reflink_trans to avoid subsequent fsyncs in the same
356*23e3337fSFilipe Manana 	 * transaction to do unnecessary work. So update last_reflink_trans
357*23e3337fSFilipe Manana 	 * to the last_trans value (we have to be pessimistic and assume a
358*23e3337fSFilipe Manana 	 * reflink happened).
359*23e3337fSFilipe Manana 	 *
360*23e3337fSFilipe Manana 	 * The ->last_trans is protected by the inode's spinlock and we can
361*23e3337fSFilipe Manana 	 * have a concurrent ordered extent completion update it. Also set
362*23e3337fSFilipe Manana 	 * last_reflink_trans to ->last_trans only if the former is less than
363*23e3337fSFilipe Manana 	 * the later, because we can be called in a context where
364*23e3337fSFilipe Manana 	 * last_reflink_trans was set to the current transaction generation
365*23e3337fSFilipe Manana 	 * while ->last_trans was not yet updated in the current transaction,
366*23e3337fSFilipe Manana 	 * and therefore has a lower value.
367*23e3337fSFilipe Manana 	 */
368*23e3337fSFilipe Manana 	spin_lock(&inode->lock);
369*23e3337fSFilipe Manana 	if (inode->last_reflink_trans < inode->last_trans)
370*23e3337fSFilipe Manana 		inode->last_reflink_trans = inode->last_trans;
371*23e3337fSFilipe Manana 	spin_unlock(&inode->lock);
372*23e3337fSFilipe Manana }
373*23e3337fSFilipe Manana 
374209ecbb8SFilipe Manana static inline bool btrfs_inode_in_log(struct btrfs_inode *inode, u64 generation)
37522ee6985SJosef Bacik {
376209ecbb8SFilipe Manana 	bool ret = false;
3772f2ff0eeSFilipe Manana 
3780f8939b8SNikolay Borisov 	spin_lock(&inode->lock);
3790f8939b8SNikolay Borisov 	if (inode->logged_trans == generation &&
3800f8939b8SNikolay Borisov 	    inode->last_sub_trans <= inode->last_log_commit &&
381209ecbb8SFilipe Manana 	    inode->last_sub_trans <= inode->root->last_log_commit)
382209ecbb8SFilipe Manana 		ret = true;
3830f8939b8SNikolay Borisov 	spin_unlock(&inode->lock);
3842f2ff0eeSFilipe Manana 	return ret;
38522ee6985SJosef Bacik }
38622ee6985SJosef Bacik 
387facc8a22SMiao Xie struct btrfs_dio_private {
388facc8a22SMiao Xie 	struct inode *inode;
38947926ab5SQu Wenruo 
39047926ab5SQu Wenruo 	/*
39147926ab5SQu Wenruo 	 * Since DIO can use anonymous page, we cannot use page_offset() to
39247926ab5SQu Wenruo 	 * grab the file offset, thus need a dedicated member for file offset.
39347926ab5SQu Wenruo 	 */
39447926ab5SQu Wenruo 	u64 file_offset;
395facc8a22SMiao Xie 	u64 disk_bytenr;
396523929f1SQu Wenruo 	/* Used for bio::bi_size */
397523929f1SQu Wenruo 	u32 bytes;
398facc8a22SMiao Xie 
399e3b318d1SOmar Sandoval 	/*
400e3b318d1SOmar Sandoval 	 * References to this structure. There is one reference per in-flight
401e3b318d1SOmar Sandoval 	 * bio plus one while we're still setting up.
402e3b318d1SOmar Sandoval 	 */
403e3b318d1SOmar Sandoval 	refcount_t refs;
404facc8a22SMiao Xie 
405facc8a22SMiao Xie 	/* dio_bio came from fs/direct-io.c */
406facc8a22SMiao Xie 	struct bio *dio_bio;
407c1dc0896SMiao Xie 
40885879573SOmar Sandoval 	/* Array of checksums */
40985879573SOmar Sandoval 	u8 csums[];
410facc8a22SMiao Xie };
411facc8a22SMiao Xie 
41277eea05eSBoris Burkov /*
41377eea05eSBoris Burkov  * btrfs_inode_item stores flags in a u64, btrfs_inode stores them in two
41477eea05eSBoris Burkov  * separate u32s. These two functions convert between the two representations.
41577eea05eSBoris Burkov  */
41677eea05eSBoris Burkov static inline u64 btrfs_inode_combine_flags(u32 flags, u32 ro_flags)
41777eea05eSBoris Burkov {
41877eea05eSBoris Burkov 	return (flags | ((u64)ro_flags << 32));
41977eea05eSBoris Burkov }
42077eea05eSBoris Burkov 
42177eea05eSBoris Burkov static inline void btrfs_inode_split_flags(u64 inode_item_flags,
42277eea05eSBoris Burkov 					   u32 *flags, u32 *ro_flags)
42377eea05eSBoris Burkov {
42477eea05eSBoris Burkov 	*flags = (u32)inode_item_flags;
42577eea05eSBoris Burkov 	*ro_flags = (u32)(inode_item_flags >> 32);
42677eea05eSBoris Burkov }
42777eea05eSBoris Burkov 
4287ebc7e5fSJohannes Thumshirn /* Array of bytes with variable length, hexadecimal format 0x1234 */
4297ebc7e5fSJohannes Thumshirn #define CSUM_FMT				"0x%*phN"
4307ebc7e5fSJohannes Thumshirn #define CSUM_FMT_VALUE(size, bytes)		size, bytes
4317ebc7e5fSJohannes Thumshirn 
4320970a22eSNikolay Borisov static inline void btrfs_print_data_csum_error(struct btrfs_inode *inode,
433ea41d6b2SJohannes Thumshirn 		u64 logical_start, u8 *csum, u8 *csum_expected, int mirror_num)
4346f6b643eSQu Wenruo {
4350970a22eSNikolay Borisov 	struct btrfs_root *root = inode->root;
436223486c2SDavid Sterba 	const u32 csum_size = root->fs_info->csum_size;
4376f6b643eSQu Wenruo 
4386f6b643eSQu Wenruo 	/* Output minus objectid, which is more meaningful */
4394fd786e6SMisono Tomohiro 	if (root->root_key.objectid >= BTRFS_LAST_FREE_OBJECTID)
4406f6b643eSQu Wenruo 		btrfs_warn_rl(root->fs_info,
4417ebc7e5fSJohannes Thumshirn "csum failed root %lld ino %lld off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d",
4424fd786e6SMisono Tomohiro 			root->root_key.objectid, btrfs_ino(inode),
4437ebc7e5fSJohannes Thumshirn 			logical_start,
444ea41d6b2SJohannes Thumshirn 			CSUM_FMT_VALUE(csum_size, csum),
445ea41d6b2SJohannes Thumshirn 			CSUM_FMT_VALUE(csum_size, csum_expected),
4467ebc7e5fSJohannes Thumshirn 			mirror_num);
4476f6b643eSQu Wenruo 	else
4486f6b643eSQu Wenruo 		btrfs_warn_rl(root->fs_info,
4497ebc7e5fSJohannes Thumshirn "csum failed root %llu ino %llu off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d",
4504fd786e6SMisono Tomohiro 			root->root_key.objectid, btrfs_ino(inode),
4517ebc7e5fSJohannes Thumshirn 			logical_start,
452ea41d6b2SJohannes Thumshirn 			CSUM_FMT_VALUE(csum_size, csum),
453ea41d6b2SJohannes Thumshirn 			CSUM_FMT_VALUE(csum_size, csum_expected),
4547ebc7e5fSJohannes Thumshirn 			mirror_num);
4556f6b643eSQu Wenruo }
4566f6b643eSQu Wenruo 
4572c90e5d6SChris Mason #endif
458