xref: /openbmc/linux/fs/btrfs/btrfs_inode.h (revision 77eea05e7851d910b7992c8c237a6b5d462050da)
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 /*
1772ac3c0dSJosef Bacik  * ordered_data_close is set by truncate when a file that used
1872ac3c0dSJosef Bacik  * to have good data has been truncated to zero.  When it is set
1972ac3c0dSJosef Bacik  * the btrfs file release call will add this inode to the
2072ac3c0dSJosef Bacik  * ordered operations list so that we make sure to flush out any
2172ac3c0dSJosef Bacik  * new data the application may have written before commit.
2272ac3c0dSJosef Bacik  */
237efc3e34SOmar Sandoval enum {
241fd4033dSNikolay Borisov 	BTRFS_INODE_FLUSH_ON_CLOSE,
257efc3e34SOmar Sandoval 	BTRFS_INODE_DUMMY,
267efc3e34SOmar Sandoval 	BTRFS_INODE_IN_DEFRAG,
277efc3e34SOmar Sandoval 	BTRFS_INODE_HAS_ASYNC_EXTENT,
2848778179SFilipe Manana 	 /*
2948778179SFilipe Manana 	  * Always set under the VFS' inode lock, otherwise it can cause races
3048778179SFilipe Manana 	  * during fsync (we start as a fast fsync and then end up in a full
3148778179SFilipe Manana 	  * fsync racing with ordered extent completion).
3248778179SFilipe Manana 	  */
337efc3e34SOmar Sandoval 	BTRFS_INODE_NEEDS_FULL_SYNC,
347efc3e34SOmar Sandoval 	BTRFS_INODE_COPY_EVERYTHING,
357efc3e34SOmar Sandoval 	BTRFS_INODE_IN_DELALLOC_LIST,
367efc3e34SOmar Sandoval 	BTRFS_INODE_HAS_PROPS,
373cd24c69SEthan Lien 	BTRFS_INODE_SNAPSHOT_FLUSH,
38f2f121abSFilipe Manana 	/*
39f2f121abSFilipe Manana 	 * Set and used when logging an inode and it serves to signal that an
40f2f121abSFilipe Manana 	 * inode does not have xattrs, so subsequent fsyncs can avoid searching
41f2f121abSFilipe Manana 	 * for xattrs to log. This bit must be cleared whenever a xattr is added
42f2f121abSFilipe Manana 	 * to an inode.
43f2f121abSFilipe Manana 	 */
44f2f121abSFilipe Manana 	BTRFS_INODE_NO_XATTRS,
453d45f221SFilipe Manana 	/*
463d45f221SFilipe Manana 	 * Set when we are in a context where we need to start a transaction and
473d45f221SFilipe Manana 	 * have dirty pages with the respective file range locked. This is to
483d45f221SFilipe Manana 	 * ensure that when reserving space for the transaction, if we are low
493d45f221SFilipe Manana 	 * on available space and need to flush delalloc, we will not flush
503d45f221SFilipe Manana 	 * delalloc for this inode, because that could result in a deadlock (on
513d45f221SFilipe Manana 	 * the file range, inode's io_tree).
523d45f221SFilipe Manana 	 */
533d45f221SFilipe Manana 	BTRFS_INODE_NO_DELALLOC_FLUSH,
547efc3e34SOmar Sandoval };
5572ac3c0dSJosef Bacik 
56f1ace244SAneesh /* in memory btrfs inode */
572c90e5d6SChris Mason struct btrfs_inode {
58d352ac68SChris Mason 	/* which subvolume this inode belongs to */
59d6e4a428SChris Mason 	struct btrfs_root *root;
60d352ac68SChris Mason 
61d352ac68SChris Mason 	/* key used to find this inode on disk.  This is used by the code
62d352ac68SChris Mason 	 * to read in roots of subvolumes
63d352ac68SChris Mason 	 */
64d6e4a428SChris Mason 	struct btrfs_key location;
65d352ac68SChris Mason 
662f2ff0eeSFilipe Manana 	/*
672f2ff0eeSFilipe Manana 	 * Lock for counters and all fields used to determine if the inode is in
682f2ff0eeSFilipe Manana 	 * the log or not (last_trans, last_sub_trans, last_log_commit,
692766ff61SFilipe Manana 	 * logged_trans), to access/update new_delalloc_bytes and to update the
702766ff61SFilipe Manana 	 * VFS' inode number of bytes used.
712f2ff0eeSFilipe Manana 	 */
729e0baf60SJosef Bacik 	spinlock_t lock;
739e0baf60SJosef Bacik 
74d352ac68SChris Mason 	/* the extent_tree has caches of all the extent mappings to disk */
75a52d9a80SChris Mason 	struct extent_map_tree extent_tree;
76d352ac68SChris Mason 
77d352ac68SChris Mason 	/* the io_tree does range state (DIRTY, LOCKED etc) */
78d1310b2eSChris Mason 	struct extent_io_tree io_tree;
79d352ac68SChris Mason 
80d352ac68SChris Mason 	/* special utility tree used to record which mirrors have already been
81d352ac68SChris Mason 	 * tried when checksums fail for a given block
82d352ac68SChris Mason 	 */
837e38326fSChris Mason 	struct extent_io_tree io_failure_tree;
84d352ac68SChris Mason 
8541a2ee75SJosef Bacik 	/*
8641a2ee75SJosef Bacik 	 * Keep track of where the inode has extent items mapped in order to
8741a2ee75SJosef Bacik 	 * make sure the i_size adjustments are accurate
8841a2ee75SJosef Bacik 	 */
8941a2ee75SJosef Bacik 	struct extent_io_tree file_extent_tree;
9041a2ee75SJosef Bacik 
91d352ac68SChris Mason 	/* held while logging the inode in tree-log.c */
92e02119d5SChris Mason 	struct mutex log_mutex;
93d352ac68SChris Mason 
94d352ac68SChris Mason 	/* used to order data wrt metadata */
95e6dcd2dcSChris Mason 	struct btrfs_ordered_inode_tree ordered_tree;
9615ee9bc7SJosef Bacik 
97d352ac68SChris Mason 	/* list of all the delalloc inodes in the FS.  There are times we need
98d352ac68SChris Mason 	 * to write all the delalloc pages to disk, and this list is used
99d352ac68SChris Mason 	 * to walk them all.
100d352ac68SChris Mason 	 */
101ea8c2819SChris Mason 	struct list_head delalloc_inodes;
102ea8c2819SChris Mason 
1035d4f98a2SYan Zheng 	/* node for the red-black tree that links inodes in subvolume root */
1045d4f98a2SYan Zheng 	struct rb_node rb_node;
1055d4f98a2SYan Zheng 
10672ac3c0dSJosef Bacik 	unsigned long runtime_flags;
10772ac3c0dSJosef Bacik 
1089c931c5aSNathaniel Yazdani 	/* Keep track of who's O_SYNC/fsyncing currently */
109b812ce28SJosef Bacik 	atomic_t sync_writers;
110b812ce28SJosef Bacik 
111d352ac68SChris Mason 	/* full 64 bit generation number, struct vfs_inode doesn't have a big
112d352ac68SChris Mason 	 * enough field for this.
113d352ac68SChris Mason 	 */
114e02119d5SChris Mason 	u64 generation;
115e02119d5SChris Mason 
11615ee9bc7SJosef Bacik 	/*
11715ee9bc7SJosef Bacik 	 * transid of the trans_handle that last modified this inode
11815ee9bc7SJosef Bacik 	 */
11915ee9bc7SJosef Bacik 	u64 last_trans;
120257c62e1SChris Mason 
121257c62e1SChris Mason 	/*
122e02119d5SChris Mason 	 * transid that last logged this inode
123e02119d5SChris Mason 	 */
124e02119d5SChris Mason 	u64 logged_trans;
12549eb7e46SChris Mason 
126bb14a59bSMiao Xie 	/*
127bb14a59bSMiao Xie 	 * log transid when this inode was last modified
128bb14a59bSMiao Xie 	 */
129bb14a59bSMiao Xie 	int last_sub_trans;
130bb14a59bSMiao Xie 
131bb14a59bSMiao Xie 	/* a local copy of root's last_log_commit */
132bb14a59bSMiao Xie 	int last_log_commit;
133bb14a59bSMiao Xie 
134d352ac68SChris Mason 	/* total number of bytes pending delalloc, used by stat to calc the
135d352ac68SChris Mason 	 * real block usage of the file
136d352ac68SChris Mason 	 */
1379069218dSChris Mason 	u64 delalloc_bytes;
138d352ac68SChris Mason 
139d352ac68SChris Mason 	/*
140a7e3b975SFilipe Manana 	 * Total number of bytes pending delalloc that fall within a file
141a7e3b975SFilipe Manana 	 * range that is either a hole or beyond EOF (and no prealloc extent
142a7e3b975SFilipe Manana 	 * exists in the range). This is always <= delalloc_bytes.
143a7e3b975SFilipe Manana 	 */
144a7e3b975SFilipe Manana 	u64 new_delalloc_bytes;
145a7e3b975SFilipe Manana 
146a7e3b975SFilipe Manana 	/*
14747059d93SWang Shilong 	 * total number of bytes pending defrag, used by stat to check whether
14847059d93SWang Shilong 	 * it needs COW.
14947059d93SWang Shilong 	 */
15047059d93SWang Shilong 	u64 defrag_bytes;
15147059d93SWang Shilong 
15247059d93SWang Shilong 	/*
153d352ac68SChris Mason 	 * the size of the file stored in the metadata on disk.  data=ordered
154d352ac68SChris Mason 	 * means the in-memory i_size might be larger than the size on disk
155d352ac68SChris Mason 	 * because not all the blocks are written yet.
156d352ac68SChris Mason 	 */
157dbe674a9SChris Mason 	u64 disk_i_size;
158d352ac68SChris Mason 
159aec7477bSJosef Bacik 	/*
160aec7477bSJosef Bacik 	 * if this is a directory then index_cnt is the counter for the index
161aec7477bSJosef Bacik 	 * number for new files that are created
162aec7477bSJosef Bacik 	 */
163aec7477bSJosef Bacik 	u64 index_cnt;
164d352ac68SChris Mason 
16567de1176SMiao Xie 	/* Cache the directory index number to speed the dir/file remove */
16667de1176SMiao Xie 	u64 dir_index;
16767de1176SMiao Xie 
16812fcfd22SChris Mason 	/* the fsync log has some corner cases that mean we have to check
16912fcfd22SChris Mason 	 * directories to see if any unlinks have been done before
17012fcfd22SChris Mason 	 * the directory was logged.  See tree-log.c for all the
17112fcfd22SChris Mason 	 * details
17212fcfd22SChris Mason 	 */
17312fcfd22SChris Mason 	u64 last_unlink_trans;
17412fcfd22SChris Mason 
1757709cde3SJosef Bacik 	/*
1763ebac17cSFilipe Manana 	 * The id/generation of the last transaction where this inode was
1773ebac17cSFilipe Manana 	 * either the source or the destination of a clone/dedupe operation.
1783ebac17cSFilipe Manana 	 * Used when logging an inode to know if there are shared extents that
1793ebac17cSFilipe Manana 	 * need special care when logging checksum items, to avoid duplicate
1803ebac17cSFilipe Manana 	 * checksum items in a log (which can lead to a corruption where we end
1813ebac17cSFilipe Manana 	 * up with missing checksum ranges after log replay).
1823ebac17cSFilipe Manana 	 * Protected by the vfs inode lock.
1833ebac17cSFilipe Manana 	 */
1843ebac17cSFilipe Manana 	u64 last_reflink_trans;
1853ebac17cSFilipe Manana 
1863ebac17cSFilipe Manana 	/*
1877709cde3SJosef Bacik 	 * Number of bytes outstanding that are going to need csums.  This is
1887709cde3SJosef Bacik 	 * used in ENOSPC accounting.
1897709cde3SJosef Bacik 	 */
1907709cde3SJosef Bacik 	u64 csum_bytes;
1917709cde3SJosef Bacik 
192*77eea05eSBoris Burkov 	/* Backwards incompatible flags, lower half of inode_item::flags  */
193f1bdcc0aSJosef Bacik 	u32 flags;
194*77eea05eSBoris Burkov 	/* Read-only compatibility flags, upper half of inode_item::flags */
195*77eea05eSBoris Burkov 	u32 ro_flags;
196f1bdcc0aSJosef Bacik 
1975a3f23d5SChris Mason 	/*
19832c00affSJosef Bacik 	 * Counters to keep track of the number of extent item's we may use due
19932c00affSJosef Bacik 	 * to delalloc and such.  outstanding_extents is the number of extent
20032c00affSJosef Bacik 	 * items we think we'll end up using, and reserved_extents is the number
20132c00affSJosef Bacik 	 * of extent items we've reserved metadata for.
2029ed74f2dSJosef Bacik 	 */
2039e0baf60SJosef Bacik 	unsigned outstanding_extents;
20469fe2d75SJosef Bacik 
20569fe2d75SJosef Bacik 	struct btrfs_block_rsv block_rsv;
2069ed74f2dSJosef Bacik 
2079ed74f2dSJosef Bacik 	/*
208b52aa8c9SDavid Sterba 	 * Cached values of inode properties
2091e701a32SChris Mason 	 */
210b52aa8c9SDavid Sterba 	unsigned prop_compress;		/* per-file compression algorithm */
211eec63c65SDavid Sterba 	/*
212eec63c65SDavid Sterba 	 * Force compression on the file using the defrag ioctl, could be
213eec63c65SDavid Sterba 	 * different from prop_compress and takes precedence if set
214eec63c65SDavid Sterba 	 */
215eec63c65SDavid Sterba 	unsigned defrag_compress;
2161e701a32SChris Mason 
21716cdcec7SMiao Xie 	struct btrfs_delayed_node *delayed_node;
21816cdcec7SMiao Xie 
2199cc97d64Schandan r 	/* File creation time. */
220d3c6be6fSArnd Bergmann 	struct timespec64 i_otime;
2219cc97d64Schandan r 
2228089fe62SDavid Sterba 	/* Hook into fs_info->delayed_iputs */
2238089fe62SDavid Sterba 	struct list_head delayed_iput;
2248089fe62SDavid Sterba 
2258318ba79SJosef Bacik 	struct rw_semaphore i_mmap_lock;
226d352ac68SChris Mason 	struct inode vfs_inode;
2272c90e5d6SChris Mason };
228dbe674a9SChris Mason 
2296fee248dSNikolay Borisov static inline u32 btrfs_inode_sectorsize(const struct btrfs_inode *inode)
2306fee248dSNikolay Borisov {
2316fee248dSNikolay Borisov 	return inode->root->fs_info->sectorsize;
2326fee248dSNikolay Borisov }
2336fee248dSNikolay Borisov 
2349a35b637SJeff Mahoney static inline struct btrfs_inode *BTRFS_I(const struct inode *inode)
2352c90e5d6SChris Mason {
2362c90e5d6SChris Mason 	return container_of(inode, struct btrfs_inode, vfs_inode);
2372c90e5d6SChris Mason }
2382c90e5d6SChris Mason 
239778ba82bSFilipe David Borba Manana static inline unsigned long btrfs_inode_hash(u64 objectid,
240778ba82bSFilipe David Borba Manana 					     const struct btrfs_root *root)
241778ba82bSFilipe David Borba Manana {
2424fd786e6SMisono Tomohiro 	u64 h = objectid ^ (root->root_key.objectid * GOLDEN_RATIO_PRIME);
243778ba82bSFilipe David Borba Manana 
244778ba82bSFilipe David Borba Manana #if BITS_PER_LONG == 32
245778ba82bSFilipe David Borba Manana 	h = (h >> 32) ^ (h & 0xffffffff);
246778ba82bSFilipe David Borba Manana #endif
247778ba82bSFilipe David Borba Manana 
248778ba82bSFilipe David Borba Manana 	return (unsigned long)h;
249778ba82bSFilipe David Borba Manana }
250778ba82bSFilipe David Borba Manana 
251778ba82bSFilipe David Borba Manana static inline void btrfs_insert_inode_hash(struct inode *inode)
252778ba82bSFilipe David Borba Manana {
253778ba82bSFilipe David Borba Manana 	unsigned long h = btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root);
254778ba82bSFilipe David Borba Manana 
255778ba82bSFilipe David Borba Manana 	__insert_inode_hash(inode, h);
256778ba82bSFilipe David Borba Manana }
257778ba82bSFilipe David Borba Manana 
2589a35b637SJeff Mahoney static inline u64 btrfs_ino(const struct btrfs_inode *inode)
25933345d01SLi Zefan {
2604a0cc7caSNikolay Borisov 	u64 ino = inode->location.objectid;
26133345d01SLi Zefan 
26214c7cca7SLiu Bo 	/*
26314c7cca7SLiu Bo 	 * !ino: btree_inode
26414c7cca7SLiu Bo 	 * type == BTRFS_ROOT_ITEM_KEY: subvol dir
26514c7cca7SLiu Bo 	 */
2664a0cc7caSNikolay Borisov 	if (!ino || inode->location.type == BTRFS_ROOT_ITEM_KEY)
2674a0cc7caSNikolay Borisov 		ino = inode->vfs_inode.i_ino;
26833345d01SLi Zefan 	return ino;
26933345d01SLi Zefan }
27033345d01SLi Zefan 
2716ef06d27SNikolay Borisov static inline void btrfs_i_size_write(struct btrfs_inode *inode, u64 size)
272dbe674a9SChris Mason {
2736ef06d27SNikolay Borisov 	i_size_write(&inode->vfs_inode, size);
2746ef06d27SNikolay Borisov 	inode->disk_i_size = size;
275dbe674a9SChris Mason }
276dbe674a9SChris Mason 
27770ddc553SNikolay Borisov static inline bool btrfs_is_free_space_inode(struct btrfs_inode *inode)
2782cf8572dSChris Mason {
27970ddc553SNikolay Borisov 	struct btrfs_root *root = inode->root;
28083eea1f1SLiu Bo 
28151a8cf9dSLiu Bo 	if (root == root->fs_info->tree_root &&
28270ddc553SNikolay Borisov 	    btrfs_ino(inode) != BTRFS_BTREE_INODE_OBJECTID)
28351a8cf9dSLiu Bo 		return true;
28470ddc553SNikolay Borisov 	if (inode->location.objectid == BTRFS_FREE_INO_OBJECTID)
2852cf8572dSChris Mason 		return true;
2862cf8572dSChris Mason 	return false;
2872cf8572dSChris Mason }
2882cf8572dSChris Mason 
28906f2548fSNikolay Borisov static inline bool is_data_inode(struct inode *inode)
29006f2548fSNikolay Borisov {
29106f2548fSNikolay Borisov 	return btrfs_ino(BTRFS_I(inode)) != BTRFS_BTREE_INODE_OBJECTID;
29206f2548fSNikolay Borisov }
29306f2548fSNikolay Borisov 
2948b62f87bSJosef Bacik static inline void btrfs_mod_outstanding_extents(struct btrfs_inode *inode,
2958b62f87bSJosef Bacik 						 int mod)
2968b62f87bSJosef Bacik {
2978b62f87bSJosef Bacik 	lockdep_assert_held(&inode->lock);
2988b62f87bSJosef Bacik 	inode->outstanding_extents += mod;
2998b62f87bSJosef Bacik 	if (btrfs_is_free_space_inode(inode))
3008b62f87bSJosef Bacik 		return;
301dd48d407SJosef Bacik 	trace_btrfs_inode_mod_outstanding_extents(inode->root, btrfs_ino(inode),
302dd48d407SJosef Bacik 						  mod);
3038b62f87bSJosef Bacik }
3048b62f87bSJosef Bacik 
305bc0939fcSFilipe Manana /*
306bc0939fcSFilipe Manana  * Called every time after doing a buffered, direct IO or memory mapped write.
307bc0939fcSFilipe Manana  *
308bc0939fcSFilipe Manana  * This is to ensure that if we write to a file that was previously fsynced in
309bc0939fcSFilipe Manana  * the current transaction, then try to fsync it again in the same transaction,
310bc0939fcSFilipe Manana  * we will know that there were changes in the file and that it needs to be
311bc0939fcSFilipe Manana  * logged.
312bc0939fcSFilipe Manana  */
313bc0939fcSFilipe Manana static inline void btrfs_set_inode_last_sub_trans(struct btrfs_inode *inode)
314bc0939fcSFilipe Manana {
315bc0939fcSFilipe Manana 	spin_lock(&inode->lock);
316bc0939fcSFilipe Manana 	inode->last_sub_trans = inode->root->log_transid;
317bc0939fcSFilipe Manana 	spin_unlock(&inode->lock);
318bc0939fcSFilipe Manana }
319bc0939fcSFilipe Manana 
320209ecbb8SFilipe Manana static inline bool btrfs_inode_in_log(struct btrfs_inode *inode, u64 generation)
32122ee6985SJosef Bacik {
322209ecbb8SFilipe Manana 	bool ret = false;
3232f2ff0eeSFilipe Manana 
3240f8939b8SNikolay Borisov 	spin_lock(&inode->lock);
3250f8939b8SNikolay Borisov 	if (inode->logged_trans == generation &&
3260f8939b8SNikolay Borisov 	    inode->last_sub_trans <= inode->last_log_commit &&
327209ecbb8SFilipe Manana 	    inode->last_sub_trans <= inode->root->last_log_commit)
328209ecbb8SFilipe Manana 		ret = true;
3290f8939b8SNikolay Borisov 	spin_unlock(&inode->lock);
3302f2ff0eeSFilipe Manana 	return ret;
33122ee6985SJosef Bacik }
33222ee6985SJosef Bacik 
333facc8a22SMiao Xie struct btrfs_dio_private {
334facc8a22SMiao Xie 	struct inode *inode;
335facc8a22SMiao Xie 	u64 logical_offset;
336facc8a22SMiao Xie 	u64 disk_bytenr;
337523929f1SQu Wenruo 	/* Used for bio::bi_size */
338523929f1SQu Wenruo 	u32 bytes;
339facc8a22SMiao Xie 
340e3b318d1SOmar Sandoval 	/*
341e3b318d1SOmar Sandoval 	 * References to this structure. There is one reference per in-flight
342e3b318d1SOmar Sandoval 	 * bio plus one while we're still setting up.
343e3b318d1SOmar Sandoval 	 */
344e3b318d1SOmar Sandoval 	refcount_t refs;
345facc8a22SMiao Xie 
346facc8a22SMiao Xie 	/* dio_bio came from fs/direct-io.c */
347facc8a22SMiao Xie 	struct bio *dio_bio;
348c1dc0896SMiao Xie 
34985879573SOmar Sandoval 	/* Array of checksums */
35085879573SOmar Sandoval 	u8 csums[];
351facc8a22SMiao Xie };
352facc8a22SMiao Xie 
353*77eea05eSBoris Burkov /*
354*77eea05eSBoris Burkov  * btrfs_inode_item stores flags in a u64, btrfs_inode stores them in two
355*77eea05eSBoris Burkov  * separate u32s. These two functions convert between the two representations.
356*77eea05eSBoris Burkov  */
357*77eea05eSBoris Burkov static inline u64 btrfs_inode_combine_flags(u32 flags, u32 ro_flags)
358*77eea05eSBoris Burkov {
359*77eea05eSBoris Burkov 	return (flags | ((u64)ro_flags << 32));
360*77eea05eSBoris Burkov }
361*77eea05eSBoris Burkov 
362*77eea05eSBoris Burkov static inline void btrfs_inode_split_flags(u64 inode_item_flags,
363*77eea05eSBoris Burkov 					   u32 *flags, u32 *ro_flags)
364*77eea05eSBoris Burkov {
365*77eea05eSBoris Burkov 	*flags = (u32)inode_item_flags;
366*77eea05eSBoris Burkov 	*ro_flags = (u32)(inode_item_flags >> 32);
367*77eea05eSBoris Burkov }
368*77eea05eSBoris Burkov 
3697ebc7e5fSJohannes Thumshirn /* Array of bytes with variable length, hexadecimal format 0x1234 */
3707ebc7e5fSJohannes Thumshirn #define CSUM_FMT				"0x%*phN"
3717ebc7e5fSJohannes Thumshirn #define CSUM_FMT_VALUE(size, bytes)		size, bytes
3727ebc7e5fSJohannes Thumshirn 
3730970a22eSNikolay Borisov static inline void btrfs_print_data_csum_error(struct btrfs_inode *inode,
374ea41d6b2SJohannes Thumshirn 		u64 logical_start, u8 *csum, u8 *csum_expected, int mirror_num)
3756f6b643eSQu Wenruo {
3760970a22eSNikolay Borisov 	struct btrfs_root *root = inode->root;
377223486c2SDavid Sterba 	const u32 csum_size = root->fs_info->csum_size;
3786f6b643eSQu Wenruo 
3796f6b643eSQu Wenruo 	/* Output minus objectid, which is more meaningful */
3804fd786e6SMisono Tomohiro 	if (root->root_key.objectid >= BTRFS_LAST_FREE_OBJECTID)
3816f6b643eSQu Wenruo 		btrfs_warn_rl(root->fs_info,
3827ebc7e5fSJohannes Thumshirn "csum failed root %lld ino %lld off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d",
3834fd786e6SMisono Tomohiro 			root->root_key.objectid, btrfs_ino(inode),
3847ebc7e5fSJohannes Thumshirn 			logical_start,
385ea41d6b2SJohannes Thumshirn 			CSUM_FMT_VALUE(csum_size, csum),
386ea41d6b2SJohannes Thumshirn 			CSUM_FMT_VALUE(csum_size, csum_expected),
3877ebc7e5fSJohannes Thumshirn 			mirror_num);
3886f6b643eSQu Wenruo 	else
3896f6b643eSQu Wenruo 		btrfs_warn_rl(root->fs_info,
3907ebc7e5fSJohannes Thumshirn "csum failed root %llu ino %llu off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d",
3914fd786e6SMisono Tomohiro 			root->root_key.objectid, btrfs_ino(inode),
3927ebc7e5fSJohannes Thumshirn 			logical_start,
393ea41d6b2SJohannes Thumshirn 			CSUM_FMT_VALUE(csum_size, csum),
394ea41d6b2SJohannes Thumshirn 			CSUM_FMT_VALUE(csum_size, csum_expected),
3957ebc7e5fSJohannes Thumshirn 			mirror_num);
3966f6b643eSQu Wenruo }
3976f6b643eSQu Wenruo 
3982c90e5d6SChris Mason #endif
399