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,
85971d03cdSFilipe Manana * logged_trans), to access/update delalloc_bytes, new_delalloc_bytes,
86971d03cdSFilipe Manana * defrag_bytes, disk_i_size, outstanding_extents, csum_bytes and to
87971d03cdSFilipe Manana * update the VFS' inode number of bytes used.
88*f56a6d9cSFilipe Manana * Also protects setting struct file::private_data.
892f2ff0eeSFilipe Manana */
909e0baf60SJosef Bacik spinlock_t lock;
919e0baf60SJosef Bacik
92d352ac68SChris Mason /* the extent_tree has caches of all the extent mappings to disk */
93a52d9a80SChris Mason struct extent_map_tree extent_tree;
94d352ac68SChris Mason
95d352ac68SChris Mason /* the io_tree does range state (DIRTY, LOCKED etc) */
96d1310b2eSChris Mason struct extent_io_tree io_tree;
97d352ac68SChris Mason
9841a2ee75SJosef Bacik /*
9941a2ee75SJosef Bacik * Keep track of where the inode has extent items mapped in order to
10041a2ee75SJosef Bacik * make sure the i_size adjustments are accurate
10141a2ee75SJosef Bacik */
10241a2ee75SJosef Bacik struct extent_io_tree file_extent_tree;
10341a2ee75SJosef Bacik
104d352ac68SChris Mason /* held while logging the inode in tree-log.c */
105e02119d5SChris Mason struct mutex log_mutex;
106d352ac68SChris Mason
107a0cc053bSDavid Sterba /*
108a0cc053bSDavid Sterba * Counters to keep track of the number of extent item's we may use due
109a0cc053bSDavid Sterba * to delalloc and such. outstanding_extents is the number of extent
110a0cc053bSDavid Sterba * items we think we'll end up using, and reserved_extents is the number
111971d03cdSFilipe Manana * of extent items we've reserved metadata for. Protected by 'lock'.
112a0cc053bSDavid Sterba */
113a0cc053bSDavid Sterba unsigned outstanding_extents;
114a0cc053bSDavid Sterba
115d352ac68SChris Mason /* used to order data wrt metadata */
116e6dcd2dcSChris Mason struct btrfs_ordered_inode_tree ordered_tree;
11715ee9bc7SJosef Bacik
118d352ac68SChris Mason /* list of all the delalloc inodes in the FS. There are times we need
119d352ac68SChris Mason * to write all the delalloc pages to disk, and this list is used
120d352ac68SChris Mason * to walk them all.
121d352ac68SChris Mason */
122ea8c2819SChris Mason struct list_head delalloc_inodes;
123ea8c2819SChris Mason
1245d4f98a2SYan Zheng /* node for the red-black tree that links inodes in subvolume root */
1255d4f98a2SYan Zheng struct rb_node rb_node;
1265d4f98a2SYan Zheng
12772ac3c0dSJosef Bacik unsigned long runtime_flags;
12872ac3c0dSJosef Bacik
129d352ac68SChris Mason /* full 64 bit generation number, struct vfs_inode doesn't have a big
130d352ac68SChris Mason * enough field for this.
131d352ac68SChris Mason */
132e02119d5SChris Mason u64 generation;
133e02119d5SChris Mason
13415ee9bc7SJosef Bacik /*
135971d03cdSFilipe Manana * ID of the transaction handle that last modified this inode.
136971d03cdSFilipe Manana * Protected by 'lock'.
13715ee9bc7SJosef Bacik */
13815ee9bc7SJosef Bacik u64 last_trans;
139257c62e1SChris Mason
140257c62e1SChris Mason /*
141971d03cdSFilipe Manana * ID of the transaction that last logged this inode.
142971d03cdSFilipe Manana * Protected by 'lock'.
143e02119d5SChris Mason */
144e02119d5SChris Mason u64 logged_trans;
14549eb7e46SChris Mason
146bb14a59bSMiao Xie /*
147971d03cdSFilipe Manana * Log transaction ID when this inode was last modified.
148971d03cdSFilipe Manana * Protected by 'lock'.
149bb14a59bSMiao Xie */
150bb14a59bSMiao Xie int last_sub_trans;
151bb14a59bSMiao Xie
152971d03cdSFilipe Manana /* A local copy of root's last_log_commit. Protected by 'lock'. */
153bb14a59bSMiao Xie int last_log_commit;
154bb14a59bSMiao Xie
155fa4b8cb1SFilipe Manana union {
156dc287224SFilipe Manana /*
157fa4b8cb1SFilipe Manana * Total number of bytes pending delalloc, used by stat to
158fa4b8cb1SFilipe Manana * calculate the real block usage of the file. This is used
159971d03cdSFilipe Manana * only for files. Protected by 'lock'.
160d352ac68SChris Mason */
1619069218dSChris Mason u64 delalloc_bytes;
162fa4b8cb1SFilipe Manana /*
163fa4b8cb1SFilipe Manana * The lowest possible index of the next dir index key which
164fa4b8cb1SFilipe Manana * points to an inode that needs to be logged.
165fa4b8cb1SFilipe Manana * This is used only for directories.
166fa4b8cb1SFilipe Manana * Use the helpers btrfs_get_first_dir_index_to_log() and
167fa4b8cb1SFilipe Manana * btrfs_set_first_dir_index_to_log() to access this field.
168fa4b8cb1SFilipe Manana */
169fa4b8cb1SFilipe Manana u64 first_dir_index_to_log;
170fa4b8cb1SFilipe Manana };
171d352ac68SChris Mason
172dc287224SFilipe Manana union {
173d352ac68SChris Mason /*
174a7e3b975SFilipe Manana * Total number of bytes pending delalloc that fall within a file
175a7e3b975SFilipe Manana * range that is either a hole or beyond EOF (and no prealloc extent
176dc287224SFilipe Manana * exists in the range). This is always <= delalloc_bytes and this
177971d03cdSFilipe Manana * is used only for files. Protected by 'lock'.
178a7e3b975SFilipe Manana */
179a7e3b975SFilipe Manana u64 new_delalloc_bytes;
180dc287224SFilipe Manana /*
181dc287224SFilipe Manana * The offset of the last dir index key that was logged.
182dc287224SFilipe Manana * This is used only for directories.
183dc287224SFilipe Manana */
184dc287224SFilipe Manana u64 last_dir_index_offset;
185dc287224SFilipe Manana };
186a7e3b975SFilipe Manana
187a7e3b975SFilipe Manana /*
188971d03cdSFilipe Manana * Total number of bytes pending defrag, used by stat to check whether
189971d03cdSFilipe Manana * it needs COW. Protected by 'lock'.
19047059d93SWang Shilong */
19147059d93SWang Shilong u64 defrag_bytes;
19247059d93SWang Shilong
19347059d93SWang Shilong /*
194971d03cdSFilipe Manana * The size of the file stored in the metadata on disk. data=ordered
195d352ac68SChris Mason * means the in-memory i_size might be larger than the size on disk
196971d03cdSFilipe Manana * because not all the blocks are written yet. Protected by 'lock'.
197d352ac68SChris Mason */
198dbe674a9SChris Mason u64 disk_i_size;
199d352ac68SChris Mason
200aec7477bSJosef Bacik /*
201528ee697SFilipe Manana * If this is a directory then index_cnt is the counter for the index
202528ee697SFilipe Manana * number for new files that are created. For an empty directory, this
203528ee697SFilipe Manana * must be initialized to BTRFS_DIR_START_INDEX.
204aec7477bSJosef Bacik */
205aec7477bSJosef Bacik u64 index_cnt;
206d352ac68SChris Mason
20767de1176SMiao Xie /* Cache the directory index number to speed the dir/file remove */
20867de1176SMiao Xie u64 dir_index;
20967de1176SMiao Xie
21012fcfd22SChris Mason /* the fsync log has some corner cases that mean we have to check
21112fcfd22SChris Mason * directories to see if any unlinks have been done before
21212fcfd22SChris Mason * the directory was logged. See tree-log.c for all the
21312fcfd22SChris Mason * details
21412fcfd22SChris Mason */
21512fcfd22SChris Mason u64 last_unlink_trans;
21612fcfd22SChris Mason
2177709cde3SJosef Bacik /*
2183ebac17cSFilipe Manana * The id/generation of the last transaction where this inode was
2193ebac17cSFilipe Manana * either the source or the destination of a clone/dedupe operation.
2203ebac17cSFilipe Manana * Used when logging an inode to know if there are shared extents that
2213ebac17cSFilipe Manana * need special care when logging checksum items, to avoid duplicate
2223ebac17cSFilipe Manana * checksum items in a log (which can lead to a corruption where we end
2233ebac17cSFilipe Manana * up with missing checksum ranges after log replay).
2243ebac17cSFilipe Manana * Protected by the vfs inode lock.
2253ebac17cSFilipe Manana */
2263ebac17cSFilipe Manana u64 last_reflink_trans;
2273ebac17cSFilipe Manana
2283ebac17cSFilipe Manana /*
2297709cde3SJosef Bacik * Number of bytes outstanding that are going to need csums. This is
230971d03cdSFilipe Manana * used in ENOSPC accounting. Protected by 'lock'.
2317709cde3SJosef Bacik */
2327709cde3SJosef Bacik u64 csum_bytes;
2337709cde3SJosef Bacik
23477eea05eSBoris Burkov /* Backwards incompatible flags, lower half of inode_item::flags */
235f1bdcc0aSJosef Bacik u32 flags;
23677eea05eSBoris Burkov /* Read-only compatibility flags, upper half of inode_item::flags */
23777eea05eSBoris Burkov u32 ro_flags;
238f1bdcc0aSJosef Bacik
23969fe2d75SJosef Bacik struct btrfs_block_rsv block_rsv;
2409ed74f2dSJosef Bacik
2419ed74f2dSJosef Bacik /*
242b52aa8c9SDavid Sterba * Cached values of inode properties
2431e701a32SChris Mason */
244b52aa8c9SDavid Sterba unsigned prop_compress; /* per-file compression algorithm */
245eec63c65SDavid Sterba /*
246eec63c65SDavid Sterba * Force compression on the file using the defrag ioctl, could be
247eec63c65SDavid Sterba * different from prop_compress and takes precedence if set
248eec63c65SDavid Sterba */
249eec63c65SDavid Sterba unsigned defrag_compress;
2501e701a32SChris Mason
25116cdcec7SMiao Xie struct btrfs_delayed_node *delayed_node;
25216cdcec7SMiao Xie
2539cc97d64Schandan r /* File creation time. */
254d3c6be6fSArnd Bergmann struct timespec64 i_otime;
2559cc97d64Schandan r
2568089fe62SDavid Sterba /* Hook into fs_info->delayed_iputs */
2578089fe62SDavid Sterba struct list_head delayed_iput;
2588089fe62SDavid Sterba
2598318ba79SJosef Bacik struct rw_semaphore i_mmap_lock;
260d352ac68SChris Mason struct inode vfs_inode;
2612c90e5d6SChris Mason };
262dbe674a9SChris Mason
btrfs_get_first_dir_index_to_log(const struct btrfs_inode * inode)263fa4b8cb1SFilipe Manana static inline u64 btrfs_get_first_dir_index_to_log(const struct btrfs_inode *inode)
264fa4b8cb1SFilipe Manana {
265fa4b8cb1SFilipe Manana return READ_ONCE(inode->first_dir_index_to_log);
266fa4b8cb1SFilipe Manana }
267fa4b8cb1SFilipe Manana
btrfs_set_first_dir_index_to_log(struct btrfs_inode * inode,u64 index)268fa4b8cb1SFilipe Manana static inline void btrfs_set_first_dir_index_to_log(struct btrfs_inode *inode,
269fa4b8cb1SFilipe Manana u64 index)
270fa4b8cb1SFilipe Manana {
271fa4b8cb1SFilipe Manana WRITE_ONCE(inode->first_dir_index_to_log, index);
272fa4b8cb1SFilipe Manana }
273fa4b8cb1SFilipe Manana
BTRFS_I(const struct inode * inode)2749a35b637SJeff Mahoney static inline struct btrfs_inode *BTRFS_I(const struct inode *inode)
2752c90e5d6SChris Mason {
2762c90e5d6SChris Mason return container_of(inode, struct btrfs_inode, vfs_inode);
2772c90e5d6SChris Mason }
2782c90e5d6SChris Mason
btrfs_inode_hash(u64 objectid,const struct btrfs_root * root)279778ba82bSFilipe David Borba Manana static inline unsigned long btrfs_inode_hash(u64 objectid,
280778ba82bSFilipe David Borba Manana const struct btrfs_root *root)
281778ba82bSFilipe David Borba Manana {
2824fd786e6SMisono Tomohiro u64 h = objectid ^ (root->root_key.objectid * GOLDEN_RATIO_PRIME);
283778ba82bSFilipe David Borba Manana
284778ba82bSFilipe David Borba Manana #if BITS_PER_LONG == 32
285778ba82bSFilipe David Borba Manana h = (h >> 32) ^ (h & 0xffffffff);
286778ba82bSFilipe David Borba Manana #endif
287778ba82bSFilipe David Borba Manana
288778ba82bSFilipe David Borba Manana return (unsigned long)h;
289778ba82bSFilipe David Borba Manana }
290778ba82bSFilipe David Borba Manana
291cf2404a9SFilipe Manana #if BITS_PER_LONG == 32
292cf2404a9SFilipe Manana
293cf2404a9SFilipe Manana /*
294cf2404a9SFilipe Manana * On 32 bit systems the i_ino of struct inode is 32 bits (unsigned long), so
295cf2404a9SFilipe Manana * we use the inode's location objectid which is a u64 to avoid truncation.
296cf2404a9SFilipe Manana */
btrfs_ino(const struct btrfs_inode * inode)2979a35b637SJeff Mahoney static inline u64 btrfs_ino(const struct btrfs_inode *inode)
29833345d01SLi Zefan {
2994a0cc7caSNikolay Borisov u64 ino = inode->location.objectid;
30033345d01SLi Zefan
301adac5584SFilipe Manana /* type == BTRFS_ROOT_ITEM_KEY: subvol dir */
302adac5584SFilipe Manana if (inode->location.type == BTRFS_ROOT_ITEM_KEY)
3034a0cc7caSNikolay Borisov ino = inode->vfs_inode.i_ino;
30433345d01SLi Zefan return ino;
30533345d01SLi Zefan }
30633345d01SLi Zefan
307cf2404a9SFilipe Manana #else
308cf2404a9SFilipe Manana
btrfs_ino(const struct btrfs_inode * inode)309cf2404a9SFilipe Manana static inline u64 btrfs_ino(const struct btrfs_inode *inode)
310cf2404a9SFilipe Manana {
311cf2404a9SFilipe Manana return inode->vfs_inode.i_ino;
312cf2404a9SFilipe Manana }
313cf2404a9SFilipe Manana
314cf2404a9SFilipe Manana #endif
315cf2404a9SFilipe Manana
btrfs_i_size_write(struct btrfs_inode * inode,u64 size)3166ef06d27SNikolay Borisov static inline void btrfs_i_size_write(struct btrfs_inode *inode, u64 size)
317dbe674a9SChris Mason {
3186ef06d27SNikolay Borisov i_size_write(&inode->vfs_inode, size);
3196ef06d27SNikolay Borisov inode->disk_i_size = size;
320dbe674a9SChris Mason }
321dbe674a9SChris Mason
btrfs_is_free_space_inode(struct btrfs_inode * inode)32270ddc553SNikolay Borisov static inline bool btrfs_is_free_space_inode(struct btrfs_inode *inode)
3232cf8572dSChris Mason {
3249b9b8854SJosef Bacik return test_bit(BTRFS_INODE_FREE_SPACE_INODE, &inode->runtime_flags);
3252cf8572dSChris Mason }
3262cf8572dSChris Mason
is_data_inode(struct inode * inode)32706f2548fSNikolay Borisov static inline bool is_data_inode(struct inode *inode)
32806f2548fSNikolay Borisov {
32906f2548fSNikolay Borisov return btrfs_ino(BTRFS_I(inode)) != BTRFS_BTREE_INODE_OBJECTID;
33006f2548fSNikolay Borisov }
33106f2548fSNikolay Borisov
btrfs_mod_outstanding_extents(struct btrfs_inode * inode,int mod)3328b62f87bSJosef Bacik static inline void btrfs_mod_outstanding_extents(struct btrfs_inode *inode,
3338b62f87bSJosef Bacik int mod)
3348b62f87bSJosef Bacik {
3358b62f87bSJosef Bacik lockdep_assert_held(&inode->lock);
3368b62f87bSJosef Bacik inode->outstanding_extents += mod;
3378b62f87bSJosef Bacik if (btrfs_is_free_space_inode(inode))
3388b62f87bSJosef Bacik return;
339dd48d407SJosef Bacik trace_btrfs_inode_mod_outstanding_extents(inode->root, btrfs_ino(inode),
34064425500SNaohiro Aota mod, inode->outstanding_extents);
3418b62f87bSJosef Bacik }
3428b62f87bSJosef Bacik
343bc0939fcSFilipe Manana /*
344bc0939fcSFilipe Manana * Called every time after doing a buffered, direct IO or memory mapped write.
345bc0939fcSFilipe Manana *
346bc0939fcSFilipe Manana * This is to ensure that if we write to a file that was previously fsynced in
347bc0939fcSFilipe Manana * the current transaction, then try to fsync it again in the same transaction,
348bc0939fcSFilipe Manana * we will know that there were changes in the file and that it needs to be
349bc0939fcSFilipe Manana * logged.
350bc0939fcSFilipe Manana */
btrfs_set_inode_last_sub_trans(struct btrfs_inode * inode)351bc0939fcSFilipe Manana static inline void btrfs_set_inode_last_sub_trans(struct btrfs_inode *inode)
352bc0939fcSFilipe Manana {
353bc0939fcSFilipe Manana spin_lock(&inode->lock);
354bc0939fcSFilipe Manana inode->last_sub_trans = inode->root->log_transid;
355bc0939fcSFilipe Manana spin_unlock(&inode->lock);
356bc0939fcSFilipe Manana }
357bc0939fcSFilipe Manana
35823e3337fSFilipe Manana /*
35923e3337fSFilipe Manana * Should be called while holding the inode's VFS lock in exclusive mode or in a
36023e3337fSFilipe Manana * context where no one else can access the inode concurrently (during inode
36123e3337fSFilipe Manana * creation or when loading an inode from disk).
36223e3337fSFilipe Manana */
btrfs_set_inode_full_sync(struct btrfs_inode * inode)36323e3337fSFilipe Manana static inline void btrfs_set_inode_full_sync(struct btrfs_inode *inode)
36423e3337fSFilipe Manana {
36523e3337fSFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
36623e3337fSFilipe Manana /*
36723e3337fSFilipe Manana * The inode may have been part of a reflink operation in the last
36823e3337fSFilipe Manana * transaction that modified it, and then a fsync has reset the
36923e3337fSFilipe Manana * last_reflink_trans to avoid subsequent fsyncs in the same
37023e3337fSFilipe Manana * transaction to do unnecessary work. So update last_reflink_trans
37123e3337fSFilipe Manana * to the last_trans value (we have to be pessimistic and assume a
37223e3337fSFilipe Manana * reflink happened).
37323e3337fSFilipe Manana *
37423e3337fSFilipe Manana * The ->last_trans is protected by the inode's spinlock and we can
37523e3337fSFilipe Manana * have a concurrent ordered extent completion update it. Also set
37623e3337fSFilipe Manana * last_reflink_trans to ->last_trans only if the former is less than
37723e3337fSFilipe Manana * the later, because we can be called in a context where
37823e3337fSFilipe Manana * last_reflink_trans was set to the current transaction generation
37923e3337fSFilipe Manana * while ->last_trans was not yet updated in the current transaction,
38023e3337fSFilipe Manana * and therefore has a lower value.
38123e3337fSFilipe Manana */
38223e3337fSFilipe Manana spin_lock(&inode->lock);
38323e3337fSFilipe Manana if (inode->last_reflink_trans < inode->last_trans)
38423e3337fSFilipe Manana inode->last_reflink_trans = inode->last_trans;
38523e3337fSFilipe Manana spin_unlock(&inode->lock);
38623e3337fSFilipe Manana }
38723e3337fSFilipe Manana
btrfs_inode_in_log(struct btrfs_inode * inode,u64 generation)388209ecbb8SFilipe Manana static inline bool btrfs_inode_in_log(struct btrfs_inode *inode, u64 generation)
38922ee6985SJosef Bacik {
390209ecbb8SFilipe Manana bool ret = false;
3912f2ff0eeSFilipe Manana
3920f8939b8SNikolay Borisov spin_lock(&inode->lock);
3930f8939b8SNikolay Borisov if (inode->logged_trans == generation &&
3940f8939b8SNikolay Borisov inode->last_sub_trans <= inode->last_log_commit &&
395209ecbb8SFilipe Manana inode->last_sub_trans <= inode->root->last_log_commit)
396209ecbb8SFilipe Manana ret = true;
3970f8939b8SNikolay Borisov spin_unlock(&inode->lock);
3982f2ff0eeSFilipe Manana return ret;
39922ee6985SJosef Bacik }
40022ee6985SJosef Bacik
401e6f9d696SChung-Chiang Cheng /*
402e6f9d696SChung-Chiang Cheng * Check if the inode has flags compatible with compression
403e6f9d696SChung-Chiang Cheng */
btrfs_inode_can_compress(const struct btrfs_inode * inode)404e6f9d696SChung-Chiang Cheng static inline bool btrfs_inode_can_compress(const struct btrfs_inode *inode)
405e6f9d696SChung-Chiang Cheng {
406e6f9d696SChung-Chiang Cheng if (inode->flags & BTRFS_INODE_NODATACOW ||
407e6f9d696SChung-Chiang Cheng inode->flags & BTRFS_INODE_NODATASUM)
408e6f9d696SChung-Chiang Cheng return false;
409e6f9d696SChung-Chiang Cheng return true;
410e6f9d696SChung-Chiang Cheng }
411e6f9d696SChung-Chiang Cheng
4127ebc7e5fSJohannes Thumshirn /* Array of bytes with variable length, hexadecimal format 0x1234 */
4137ebc7e5fSJohannes Thumshirn #define CSUM_FMT "0x%*phN"
4147ebc7e5fSJohannes Thumshirn #define CSUM_FMT_VALUE(size, bytes) size, bytes
4152885fd63SJosef Bacik
4162885fd63SJosef Bacik int btrfs_check_sector_csum(struct btrfs_fs_info *fs_info, struct page *page,
4172885fd63SJosef Bacik u32 pgoff, u8 *csum, const u8 * const csum_expected);
418e5219044SChristoph Hellwig bool btrfs_data_csum_ok(struct btrfs_bio *bbio, struct btrfs_device *dev,
419e5219044SChristoph Hellwig u32 bio_offset, struct bio_vec *bv);
4202885fd63SJosef Bacik noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
4212885fd63SJosef Bacik u64 *orig_start, u64 *orig_block_len,
4222885fd63SJosef Bacik u64 *ram_bytes, bool nowait, bool strict);
4232885fd63SJosef Bacik
4242885fd63SJosef Bacik void __btrfs_del_delalloc_inode(struct btrfs_root *root, struct btrfs_inode *inode);
4252885fd63SJosef Bacik struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry);
4262885fd63SJosef Bacik int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index);
4272885fd63SJosef Bacik int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4282885fd63SJosef Bacik struct btrfs_inode *dir, struct btrfs_inode *inode,
4292885fd63SJosef Bacik const struct fscrypt_str *name);
4302885fd63SJosef Bacik int btrfs_add_link(struct btrfs_trans_handle *trans,
4312885fd63SJosef Bacik struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
4322885fd63SJosef Bacik const struct fscrypt_str *name, int add_backref, u64 index);
4333c4f91e2SDavid Sterba int btrfs_delete_subvolume(struct btrfs_inode *dir, struct dentry *dentry);
4342885fd63SJosef Bacik int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
4352885fd63SJosef Bacik int front);
4362885fd63SJosef Bacik
4372885fd63SJosef Bacik int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context);
4382885fd63SJosef Bacik int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
4392885fd63SJosef Bacik bool in_reclaim_context);
4402885fd63SJosef Bacik int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
4412885fd63SJosef Bacik unsigned int extra_bits,
4422885fd63SJosef Bacik struct extent_state **cached_state);
4432885fd63SJosef Bacik
4442885fd63SJosef Bacik struct btrfs_new_inode_args {
4452885fd63SJosef Bacik /* Input */
4462885fd63SJosef Bacik struct inode *dir;
4472885fd63SJosef Bacik struct dentry *dentry;
4482885fd63SJosef Bacik struct inode *inode;
4492885fd63SJosef Bacik bool orphan;
4502885fd63SJosef Bacik bool subvol;
4512885fd63SJosef Bacik
4522885fd63SJosef Bacik /* Output from btrfs_new_inode_prepare(), input to btrfs_create_new_inode(). */
4532885fd63SJosef Bacik struct posix_acl *default_acl;
4542885fd63SJosef Bacik struct posix_acl *acl;
4552885fd63SJosef Bacik struct fscrypt_name fname;
4562885fd63SJosef Bacik };
4572885fd63SJosef Bacik
4582885fd63SJosef Bacik int btrfs_new_inode_prepare(struct btrfs_new_inode_args *args,
4592885fd63SJosef Bacik unsigned int *trans_num_items);
4602885fd63SJosef Bacik int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
4612885fd63SJosef Bacik struct btrfs_new_inode_args *args);
4622885fd63SJosef Bacik void btrfs_new_inode_args_destroy(struct btrfs_new_inode_args *args);
463f2d40141SChristian Brauner struct inode *btrfs_new_subvol_inode(struct mnt_idmap *idmap,
4642885fd63SJosef Bacik struct inode *dir);
4654c5d166fSDavid Sterba void btrfs_set_delalloc_extent(struct btrfs_inode *inode, struct extent_state *state,
4662885fd63SJosef Bacik u32 bits);
467bd54766eSDavid Sterba void btrfs_clear_delalloc_extent(struct btrfs_inode *inode,
4682885fd63SJosef Bacik struct extent_state *state, u32 bits);
4692454151cSDavid Sterba void btrfs_merge_delalloc_extent(struct btrfs_inode *inode, struct extent_state *new,
4702885fd63SJosef Bacik struct extent_state *other);
47162798a49SDavid Sterba void btrfs_split_delalloc_extent(struct btrfs_inode *inode,
4722885fd63SJosef Bacik struct extent_state *orig, u64 split);
4732885fd63SJosef Bacik void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end);
4742885fd63SJosef Bacik vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf);
4752885fd63SJosef Bacik void btrfs_evict_inode(struct inode *inode);
4762885fd63SJosef Bacik struct inode *btrfs_alloc_inode(struct super_block *sb);
4772885fd63SJosef Bacik void btrfs_destroy_inode(struct inode *inode);
4782885fd63SJosef Bacik void btrfs_free_inode(struct inode *inode);
4792885fd63SJosef Bacik int btrfs_drop_inode(struct inode *inode);
4802885fd63SJosef Bacik int __init btrfs_init_cachep(void);
4812885fd63SJosef Bacik void __cold btrfs_destroy_cachep(void);
4822885fd63SJosef Bacik struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
4832885fd63SJosef Bacik struct btrfs_root *root, struct btrfs_path *path);
4842885fd63SJosef Bacik struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root);
4852885fd63SJosef Bacik struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
4862885fd63SJosef Bacik struct page *page, size_t pg_offset,
4872885fd63SJosef Bacik u64 start, u64 end);
4882885fd63SJosef Bacik int btrfs_update_inode(struct btrfs_trans_handle *trans,
4892885fd63SJosef Bacik struct btrfs_root *root, struct btrfs_inode *inode);
4902885fd63SJosef Bacik int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4912885fd63SJosef Bacik struct btrfs_root *root, struct btrfs_inode *inode);
4922885fd63SJosef Bacik int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct btrfs_inode *inode);
4932885fd63SJosef Bacik int btrfs_orphan_cleanup(struct btrfs_root *root);
4942885fd63SJosef Bacik int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size);
495e55cf7caSDavid Sterba void btrfs_add_delayed_iput(struct btrfs_inode *inode);
4962885fd63SJosef Bacik void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info);
4972885fd63SJosef Bacik int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info);
4982885fd63SJosef Bacik int btrfs_prealloc_file_range(struct inode *inode, int mode,
4992885fd63SJosef Bacik u64 start, u64 num_bytes, u64 min_size,
5002885fd63SJosef Bacik loff_t actual_len, u64 *alloc_hint);
5012885fd63SJosef Bacik int btrfs_prealloc_file_range_trans(struct inode *inode,
5022885fd63SJosef Bacik struct btrfs_trans_handle *trans, int mode,
5032885fd63SJosef Bacik u64 start, u64 num_bytes, u64 min_size,
5042885fd63SJosef Bacik loff_t actual_len, u64 *alloc_hint);
5052885fd63SJosef Bacik int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
506c56cbe90SChristoph Hellwig u64 start, u64 end, struct writeback_control *wbc);
5072885fd63SJosef Bacik int btrfs_writepage_cow_fixup(struct page *page);
5082885fd63SJosef Bacik int btrfs_encoded_io_compression_from_extent(struct btrfs_fs_info *fs_info,
5092885fd63SJosef Bacik int compress_type);
5102885fd63SJosef Bacik int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode,
5112885fd63SJosef Bacik u64 file_offset, u64 disk_bytenr,
5122885fd63SJosef Bacik u64 disk_io_size,
5132885fd63SJosef Bacik struct page **pages);
5142885fd63SJosef Bacik ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter,
5152885fd63SJosef Bacik struct btrfs_ioctl_encoded_io_args *encoded);
5162885fd63SJosef Bacik ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from,
5172885fd63SJosef Bacik const struct btrfs_ioctl_encoded_io_args *encoded);
5182885fd63SJosef Bacik
5192885fd63SJosef Bacik ssize_t btrfs_dio_read(struct kiocb *iocb, struct iov_iter *iter,
5202885fd63SJosef Bacik size_t done_before);
5212885fd63SJosef Bacik struct iomap_dio *btrfs_dio_write(struct kiocb *iocb, struct iov_iter *iter,
5222885fd63SJosef Bacik size_t done_before);
5232885fd63SJosef Bacik
5242885fd63SJosef Bacik extern const struct dentry_operations btrfs_dentry_operations;
5252885fd63SJosef Bacik
5262885fd63SJosef Bacik /* Inode locking type flags, by default the exclusive lock is taken. */
5272885fd63SJosef Bacik enum btrfs_ilock_type {
5282885fd63SJosef Bacik ENUM_BIT(BTRFS_ILOCK_SHARED),
5292885fd63SJosef Bacik ENUM_BIT(BTRFS_ILOCK_TRY),
5302885fd63SJosef Bacik ENUM_BIT(BTRFS_ILOCK_MMAP),
5312885fd63SJosef Bacik };
5322885fd63SJosef Bacik
53329b6352bSDavid Sterba int btrfs_inode_lock(struct btrfs_inode *inode, unsigned int ilock_flags);
534e5d4d75bSDavid Sterba void btrfs_inode_unlock(struct btrfs_inode *inode, unsigned int ilock_flags);
5352885fd63SJosef Bacik void btrfs_update_inode_bytes(struct btrfs_inode *inode, const u64 add_bytes,
5362885fd63SJosef Bacik const u64 del_bytes);
5372885fd63SJosef Bacik void btrfs_assert_inode_range_clean(struct btrfs_inode *inode, u64 start, u64 end);
5382885fd63SJosef Bacik
5392c90e5d6SChris Mason #endif
540