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>
115693a128SDavid Sterba #include <linux/spinlock.h>
125693a128SDavid Sterba #include <linux/mutex.h>
135693a128SDavid Sterba #include <linux/rwsem.h>
145693a128SDavid Sterba #include <linux/fs.h>
155693a128SDavid Sterba #include <linux/mm.h>
165693a128SDavid Sterba #include <linux/compiler.h>
1704cc63d1SJosef Bacik #include <linux/fscrypt.h>
185693a128SDavid Sterba #include <linux/lockdep.h>
195693a128SDavid Sterba #include <uapi/linux/btrfs_tree.h>
205335f437SJosef Bacik #include <trace/events/btrfs.h>
215693a128SDavid Sterba #include "block-rsv.h"
225693a128SDavid Sterba #include "btrfs_inode.h"
23a52d9a80SChris Mason #include "extent_map.h"
24d1310b2eSChris Mason #include "extent_io.h"
255693a128SDavid Sterba #include "extent-io-tree.h"
26e6dcd2dcSChris Mason #include "ordered-data.h"
2716cdcec7SMiao Xie #include "delayed-inode.h"
28a52d9a80SChris Mason
295693a128SDavid Sterba struct extent_state;
305693a128SDavid Sterba struct posix_acl;
315693a128SDavid Sterba struct iov_iter;
325693a128SDavid Sterba struct writeback_control;
335693a128SDavid Sterba struct btrfs_root;
345693a128SDavid Sterba struct btrfs_fs_info;
355693a128SDavid Sterba struct btrfs_trans_handle;
365693a128SDavid Sterba
3772ac3c0dSJosef Bacik /*
38528ee697SFilipe Manana * Since we search a directory based on f_pos (struct dir_context::pos) we have
39528ee697SFilipe Manana * to start at 2 since '.' and '..' have f_pos of 0 and 1 respectively, so
40528ee697SFilipe Manana * everybody else has to start at 2 (see btrfs_real_readdir() and dir_emit_dots()).
41528ee697SFilipe Manana */
42528ee697SFilipe Manana #define BTRFS_DIR_START_INDEX 2
43528ee697SFilipe Manana
44528ee697SFilipe Manana /*
4572ac3c0dSJosef Bacik * ordered_data_close is set by truncate when a file that used
4672ac3c0dSJosef Bacik * to have good data has been truncated to zero. When it is set
4772ac3c0dSJosef Bacik * the btrfs file release call will add this inode to the
4872ac3c0dSJosef Bacik * ordered operations list so that we make sure to flush out any
4972ac3c0dSJosef Bacik * new data the application may have written before commit.
5072ac3c0dSJosef Bacik */
517efc3e34SOmar Sandoval enum {
521fd4033dSNikolay Borisov BTRFS_INODE_FLUSH_ON_CLOSE,
537efc3e34SOmar Sandoval BTRFS_INODE_DUMMY,
547efc3e34SOmar Sandoval BTRFS_INODE_IN_DEFRAG,
557efc3e34SOmar Sandoval BTRFS_INODE_HAS_ASYNC_EXTENT,
5648778179SFilipe Manana /*
5748778179SFilipe Manana * Always set under the VFS' inode lock, otherwise it can cause races
5848778179SFilipe Manana * during fsync (we start as a fast fsync and then end up in a full
5948778179SFilipe Manana * fsync racing with ordered extent completion).
6048778179SFilipe Manana */
617efc3e34SOmar Sandoval BTRFS_INODE_NEEDS_FULL_SYNC,
627efc3e34SOmar Sandoval BTRFS_INODE_COPY_EVERYTHING,
637efc3e34SOmar Sandoval BTRFS_INODE_HAS_PROPS,
643cd24c69SEthan Lien BTRFS_INODE_SNAPSHOT_FLUSH,
65f2f121abSFilipe Manana /*
66f2f121abSFilipe Manana * Set and used when logging an inode and it serves to signal that an
67f2f121abSFilipe Manana * inode does not have xattrs, so subsequent fsyncs can avoid searching
68f2f121abSFilipe Manana * for xattrs to log. This bit must be cleared whenever a xattr is added
69f2f121abSFilipe Manana * to an inode.
70f2f121abSFilipe Manana */
71f2f121abSFilipe Manana BTRFS_INODE_NO_XATTRS,
723d45f221SFilipe Manana /*
733d45f221SFilipe Manana * Set when we are in a context where we need to start a transaction and
743d45f221SFilipe Manana * have dirty pages with the respective file range locked. This is to
753d45f221SFilipe Manana * ensure that when reserving space for the transaction, if we are low
763d45f221SFilipe Manana * on available space and need to flush delalloc, we will not flush
773d45f221SFilipe Manana * delalloc for this inode, because that could result in a deadlock (on
783d45f221SFilipe Manana * the file range, inode's io_tree).
793d45f221SFilipe Manana */
803d45f221SFilipe Manana BTRFS_INODE_NO_DELALLOC_FLUSH,
8114605409SBoris Burkov /*
8214605409SBoris Burkov * Set when we are working on enabling verity for a file. Computing and
8314605409SBoris Burkov * writing the whole Merkle tree can take a while so we want to prevent
8414605409SBoris Burkov * races where two separate tasks attempt to simultaneously start verity
8514605409SBoris Burkov * on the same file.
8614605409SBoris Burkov */
8714605409SBoris Burkov BTRFS_INODE_VERITY_IN_PROGRESS,
889b9b8854SJosef Bacik /* Set when this inode is a free space inode. */
899b9b8854SJosef Bacik BTRFS_INODE_FREE_SPACE_INODE,
90ed9b50a1SJosef Bacik /* Set when there are no capabilities in XATTs for the inode. */
91ed9b50a1SJosef Bacik BTRFS_INODE_NO_CAP_XATTR,
92*f13e01b8SFilipe Manana /*
93*f13e01b8SFilipe Manana * Set if an error happened when doing a COW write before submitting a
94*f13e01b8SFilipe Manana * bio or during writeback. Used for both buffered writes and direct IO
95*f13e01b8SFilipe Manana * writes. This is to signal a fast fsync that it has to wait for
96*f13e01b8SFilipe Manana * ordered extents to complete and therefore not log extent maps that
97*f13e01b8SFilipe Manana * point to unwritten extents (when an ordered extent completes and it
98*f13e01b8SFilipe Manana * has the BTRFS_ORDERED_IOERR flag set, it drops extent maps in its
99*f13e01b8SFilipe Manana * range).
100*f13e01b8SFilipe Manana */
101*f13e01b8SFilipe Manana BTRFS_INODE_COW_WRITE_ERROR,
1027efc3e34SOmar Sandoval };
10372ac3c0dSJosef Bacik
104f1ace244SAneesh /* in memory btrfs inode */
1052c90e5d6SChris Mason struct btrfs_inode {
106d352ac68SChris Mason /* which subvolume this inode belongs to */
107d6e4a428SChris Mason struct btrfs_root *root;
108d352ac68SChris Mason
109d352ac68SChris Mason /* key used to find this inode on disk. This is used by the code
110d352ac68SChris Mason * to read in roots of subvolumes
111d352ac68SChris Mason */
112d6e4a428SChris Mason struct btrfs_key location;
113d352ac68SChris Mason
114e41570d3SDavid Sterba /* Cached value of inode property 'compression'. */
115e41570d3SDavid Sterba u8 prop_compress;
116e41570d3SDavid Sterba
117e41570d3SDavid Sterba /*
118e41570d3SDavid Sterba * Force compression on the file using the defrag ioctl, could be
119e41570d3SDavid Sterba * different from prop_compress and takes precedence if set.
120e41570d3SDavid Sterba */
121e41570d3SDavid Sterba u8 defrag_compress;
122e41570d3SDavid Sterba
1232f2ff0eeSFilipe Manana /*
1242f2ff0eeSFilipe Manana * Lock for counters and all fields used to determine if the inode is in
1252f2ff0eeSFilipe Manana * the log or not (last_trans, last_sub_trans, last_log_commit,
12668539bd0SFilipe Manana * logged_trans), to access/update delalloc_bytes, new_delalloc_bytes,
12768539bd0SFilipe Manana * defrag_bytes, disk_i_size, outstanding_extents, csum_bytes and to
12868539bd0SFilipe Manana * update the VFS' inode number of bytes used.
1292f2ff0eeSFilipe Manana */
1309e0baf60SJosef Bacik spinlock_t lock;
1319e0baf60SJosef Bacik
132d352ac68SChris Mason /* the extent_tree has caches of all the extent mappings to disk */
133a52d9a80SChris Mason struct extent_map_tree extent_tree;
134d352ac68SChris Mason
135d352ac68SChris Mason /* the io_tree does range state (DIRTY, LOCKED etc) */
136d1310b2eSChris Mason struct extent_io_tree io_tree;
137d352ac68SChris Mason
13841a2ee75SJosef Bacik /*
13941a2ee75SJosef Bacik * Keep track of where the inode has extent items mapped in order to
140637e6e0fSDavid Sterba * make sure the i_size adjustments are accurate. Not required when the
141637e6e0fSDavid Sterba * filesystem is NO_HOLES, the status can't be set while mounted as
142637e6e0fSDavid Sterba * it's a mkfs-time feature.
14341a2ee75SJosef Bacik */
144637e6e0fSDavid Sterba struct extent_io_tree *file_extent_tree;
14541a2ee75SJosef Bacik
146d352ac68SChris Mason /* held while logging the inode in tree-log.c */
147e02119d5SChris Mason struct mutex log_mutex;
148d352ac68SChris Mason
149398fb913SDavid Sterba /*
150398fb913SDavid Sterba * Counters to keep track of the number of extent item's we may use due
151398fb913SDavid Sterba * to delalloc and such. outstanding_extents is the number of extent
152398fb913SDavid Sterba * items we think we'll end up using, and reserved_extents is the number
15368539bd0SFilipe Manana * of extent items we've reserved metadata for. Protected by 'lock'.
154398fb913SDavid Sterba */
155398fb913SDavid Sterba unsigned outstanding_extents;
156398fb913SDavid Sterba
157d352ac68SChris Mason /* used to order data wrt metadata */
15854c65371SDavid Sterba spinlock_t ordered_tree_lock;
15954c65371SDavid Sterba struct rb_root ordered_tree;
16054c65371SDavid Sterba struct rb_node *ordered_tree_last;
16115ee9bc7SJosef Bacik
162d352ac68SChris Mason /* list of all the delalloc inodes in the FS. There are times we need
163d352ac68SChris Mason * to write all the delalloc pages to disk, and this list is used
164d352ac68SChris Mason * to walk them all.
165d352ac68SChris Mason */
166ea8c2819SChris Mason struct list_head delalloc_inodes;
167ea8c2819SChris Mason
1685d4f98a2SYan Zheng /* node for the red-black tree that links inodes in subvolume root */
1695d4f98a2SYan Zheng struct rb_node rb_node;
1705d4f98a2SYan Zheng
17172ac3c0dSJosef Bacik unsigned long runtime_flags;
17272ac3c0dSJosef Bacik
173d352ac68SChris Mason /* full 64 bit generation number, struct vfs_inode doesn't have a big
174d352ac68SChris Mason * enough field for this.
175d352ac68SChris Mason */
176e02119d5SChris Mason u64 generation;
177e02119d5SChris Mason
17815ee9bc7SJosef Bacik /*
17968539bd0SFilipe Manana * ID of the transaction handle that last modified this inode.
18068539bd0SFilipe Manana * Protected by 'lock'.
18115ee9bc7SJosef Bacik */
18215ee9bc7SJosef Bacik u64 last_trans;
183257c62e1SChris Mason
184257c62e1SChris Mason /*
18568539bd0SFilipe Manana * ID of the transaction that last logged this inode.
18668539bd0SFilipe Manana * Protected by 'lock'.
187e02119d5SChris Mason */
188e02119d5SChris Mason u64 logged_trans;
18949eb7e46SChris Mason
190bb14a59bSMiao Xie /*
19168539bd0SFilipe Manana * Log transaction ID when this inode was last modified.
19268539bd0SFilipe Manana * Protected by 'lock'.
193bb14a59bSMiao Xie */
194bb14a59bSMiao Xie int last_sub_trans;
195bb14a59bSMiao Xie
19668539bd0SFilipe Manana /* A local copy of root's last_log_commit. Protected by 'lock'. */
197bb14a59bSMiao Xie int last_log_commit;
198bb14a59bSMiao Xie
199fa4b8cb1SFilipe Manana union {
200dc287224SFilipe Manana /*
201fa4b8cb1SFilipe Manana * Total number of bytes pending delalloc, used by stat to
202fa4b8cb1SFilipe Manana * calculate the real block usage of the file. This is used
20368539bd0SFilipe Manana * only for files. Protected by 'lock'.
204d352ac68SChris Mason */
2059069218dSChris Mason u64 delalloc_bytes;
206fa4b8cb1SFilipe Manana /*
207fa4b8cb1SFilipe Manana * The lowest possible index of the next dir index key which
208fa4b8cb1SFilipe Manana * points to an inode that needs to be logged.
209fa4b8cb1SFilipe Manana * This is used only for directories.
210fa4b8cb1SFilipe Manana * Use the helpers btrfs_get_first_dir_index_to_log() and
211fa4b8cb1SFilipe Manana * btrfs_set_first_dir_index_to_log() to access this field.
212fa4b8cb1SFilipe Manana */
213fa4b8cb1SFilipe Manana u64 first_dir_index_to_log;
214fa4b8cb1SFilipe Manana };
215d352ac68SChris Mason
216dc287224SFilipe Manana union {
217d352ac68SChris Mason /*
218a7e3b975SFilipe Manana * Total number of bytes pending delalloc that fall within a file
219a7e3b975SFilipe Manana * range that is either a hole or beyond EOF (and no prealloc extent
220dc287224SFilipe Manana * exists in the range). This is always <= delalloc_bytes and this
22168539bd0SFilipe Manana * is used only for files. Protected by 'lock'.
222a7e3b975SFilipe Manana */
223a7e3b975SFilipe Manana u64 new_delalloc_bytes;
224dc287224SFilipe Manana /*
225dc287224SFilipe Manana * The offset of the last dir index key that was logged.
226dc287224SFilipe Manana * This is used only for directories.
227dc287224SFilipe Manana */
228dc287224SFilipe Manana u64 last_dir_index_offset;
229dc287224SFilipe Manana };
230a7e3b975SFilipe Manana
231a7e3b975SFilipe Manana /*
23268539bd0SFilipe Manana * Total number of bytes pending defrag, used by stat to check whether
23368539bd0SFilipe Manana * it needs COW. Protected by 'lock'.
23447059d93SWang Shilong */
23547059d93SWang Shilong u64 defrag_bytes;
23647059d93SWang Shilong
23747059d93SWang Shilong /*
23868539bd0SFilipe Manana * The size of the file stored in the metadata on disk. data=ordered
239d352ac68SChris Mason * means the in-memory i_size might be larger than the size on disk
24068539bd0SFilipe Manana * because not all the blocks are written yet. Protected by 'lock'.
241d352ac68SChris Mason */
242dbe674a9SChris Mason u64 disk_i_size;
243d352ac68SChris Mason
244aec7477bSJosef Bacik /*
245528ee697SFilipe Manana * If this is a directory then index_cnt is the counter for the index
246528ee697SFilipe Manana * number for new files that are created. For an empty directory, this
247528ee697SFilipe Manana * must be initialized to BTRFS_DIR_START_INDEX.
248aec7477bSJosef Bacik */
249aec7477bSJosef Bacik u64 index_cnt;
250d352ac68SChris Mason
25167de1176SMiao Xie /* Cache the directory index number to speed the dir/file remove */
25267de1176SMiao Xie u64 dir_index;
25367de1176SMiao Xie
25412fcfd22SChris Mason /* the fsync log has some corner cases that mean we have to check
25512fcfd22SChris Mason * directories to see if any unlinks have been done before
25612fcfd22SChris Mason * the directory was logged. See tree-log.c for all the
25712fcfd22SChris Mason * details
25812fcfd22SChris Mason */
25912fcfd22SChris Mason u64 last_unlink_trans;
26012fcfd22SChris Mason
2617709cde3SJosef Bacik /*
2623ebac17cSFilipe Manana * The id/generation of the last transaction where this inode was
2633ebac17cSFilipe Manana * either the source or the destination of a clone/dedupe operation.
2643ebac17cSFilipe Manana * Used when logging an inode to know if there are shared extents that
2653ebac17cSFilipe Manana * need special care when logging checksum items, to avoid duplicate
2663ebac17cSFilipe Manana * checksum items in a log (which can lead to a corruption where we end
2673ebac17cSFilipe Manana * up with missing checksum ranges after log replay).
2683ebac17cSFilipe Manana * Protected by the vfs inode lock.
2693ebac17cSFilipe Manana */
2703ebac17cSFilipe Manana u64 last_reflink_trans;
2713ebac17cSFilipe Manana
2723ebac17cSFilipe Manana /*
2737709cde3SJosef Bacik * Number of bytes outstanding that are going to need csums. This is
27468539bd0SFilipe Manana * used in ENOSPC accounting. Protected by 'lock'.
2757709cde3SJosef Bacik */
2767709cde3SJosef Bacik u64 csum_bytes;
2777709cde3SJosef Bacik
27877eea05eSBoris Burkov /* Backwards incompatible flags, lower half of inode_item::flags */
279f1bdcc0aSJosef Bacik u32 flags;
28077eea05eSBoris Burkov /* Read-only compatibility flags, upper half of inode_item::flags */
28177eea05eSBoris Burkov u32 ro_flags;
282f1bdcc0aSJosef Bacik
28369fe2d75SJosef Bacik struct btrfs_block_rsv block_rsv;
2849ed74f2dSJosef Bacik
28516cdcec7SMiao Xie struct btrfs_delayed_node *delayed_node;
28616cdcec7SMiao Xie
2879cc97d64Schandan r /* File creation time. */
288c6e8f898SDavid Sterba u64 i_otime_sec;
289c6e8f898SDavid Sterba u32 i_otime_nsec;
2909cc97d64Schandan r
2918089fe62SDavid Sterba /* Hook into fs_info->delayed_iputs */
2928089fe62SDavid Sterba struct list_head delayed_iput;
2938089fe62SDavid Sterba
2948318ba79SJosef Bacik struct rw_semaphore i_mmap_lock;
295d352ac68SChris Mason struct inode vfs_inode;
2962c90e5d6SChris Mason };
297dbe674a9SChris Mason
btrfs_get_first_dir_index_to_log(const struct btrfs_inode * inode)298fa4b8cb1SFilipe Manana static inline u64 btrfs_get_first_dir_index_to_log(const struct btrfs_inode *inode)
299fa4b8cb1SFilipe Manana {
300fa4b8cb1SFilipe Manana return READ_ONCE(inode->first_dir_index_to_log);
301fa4b8cb1SFilipe Manana }
302fa4b8cb1SFilipe Manana
btrfs_set_first_dir_index_to_log(struct btrfs_inode * inode,u64 index)303fa4b8cb1SFilipe Manana static inline void btrfs_set_first_dir_index_to_log(struct btrfs_inode *inode,
304fa4b8cb1SFilipe Manana u64 index)
305fa4b8cb1SFilipe Manana {
306fa4b8cb1SFilipe Manana WRITE_ONCE(inode->first_dir_index_to_log, index);
307fa4b8cb1SFilipe Manana }
308fa4b8cb1SFilipe Manana
BTRFS_I(const struct inode * inode)3099a35b637SJeff Mahoney static inline struct btrfs_inode *BTRFS_I(const struct inode *inode)
3102c90e5d6SChris Mason {
3112c90e5d6SChris Mason return container_of(inode, struct btrfs_inode, vfs_inode);
3122c90e5d6SChris Mason }
3132c90e5d6SChris Mason
btrfs_inode_hash(u64 objectid,const struct btrfs_root * root)314778ba82bSFilipe David Borba Manana static inline unsigned long btrfs_inode_hash(u64 objectid,
315778ba82bSFilipe David Borba Manana const struct btrfs_root *root)
316778ba82bSFilipe David Borba Manana {
3174fd786e6SMisono Tomohiro u64 h = objectid ^ (root->root_key.objectid * GOLDEN_RATIO_PRIME);
318778ba82bSFilipe David Borba Manana
319778ba82bSFilipe David Borba Manana #if BITS_PER_LONG == 32
320778ba82bSFilipe David Borba Manana h = (h >> 32) ^ (h & 0xffffffff);
321778ba82bSFilipe David Borba Manana #endif
322778ba82bSFilipe David Borba Manana
323778ba82bSFilipe David Borba Manana return (unsigned long)h;
324778ba82bSFilipe David Borba Manana }
325778ba82bSFilipe David Borba Manana
326cf2404a9SFilipe Manana #if BITS_PER_LONG == 32
327cf2404a9SFilipe Manana
328cf2404a9SFilipe Manana /*
329cf2404a9SFilipe Manana * On 32 bit systems the i_ino of struct inode is 32 bits (unsigned long), so
330cf2404a9SFilipe Manana * we use the inode's location objectid which is a u64 to avoid truncation.
331cf2404a9SFilipe Manana */
btrfs_ino(const struct btrfs_inode * inode)3329a35b637SJeff Mahoney static inline u64 btrfs_ino(const struct btrfs_inode *inode)
33333345d01SLi Zefan {
3344a0cc7caSNikolay Borisov u64 ino = inode->location.objectid;
33533345d01SLi Zefan
336adac5584SFilipe Manana /* type == BTRFS_ROOT_ITEM_KEY: subvol dir */
337adac5584SFilipe Manana if (inode->location.type == BTRFS_ROOT_ITEM_KEY)
3384a0cc7caSNikolay Borisov ino = inode->vfs_inode.i_ino;
33933345d01SLi Zefan return ino;
34033345d01SLi Zefan }
34133345d01SLi Zefan
342cf2404a9SFilipe Manana #else
343cf2404a9SFilipe Manana
btrfs_ino(const struct btrfs_inode * inode)344cf2404a9SFilipe Manana static inline u64 btrfs_ino(const struct btrfs_inode *inode)
345cf2404a9SFilipe Manana {
346cf2404a9SFilipe Manana return inode->vfs_inode.i_ino;
347cf2404a9SFilipe Manana }
348cf2404a9SFilipe Manana
349cf2404a9SFilipe Manana #endif
350cf2404a9SFilipe Manana
btrfs_i_size_write(struct btrfs_inode * inode,u64 size)3516ef06d27SNikolay Borisov static inline void btrfs_i_size_write(struct btrfs_inode *inode, u64 size)
352dbe674a9SChris Mason {
3536ef06d27SNikolay Borisov i_size_write(&inode->vfs_inode, size);
3546ef06d27SNikolay Borisov inode->disk_i_size = size;
355dbe674a9SChris Mason }
356dbe674a9SChris Mason
btrfs_is_free_space_inode(struct btrfs_inode * inode)35770ddc553SNikolay Borisov static inline bool btrfs_is_free_space_inode(struct btrfs_inode *inode)
3582cf8572dSChris Mason {
3599b9b8854SJosef Bacik return test_bit(BTRFS_INODE_FREE_SPACE_INODE, &inode->runtime_flags);
3602cf8572dSChris Mason }
3612cf8572dSChris Mason
is_data_inode(struct inode * inode)36206f2548fSNikolay Borisov static inline bool is_data_inode(struct inode *inode)
36306f2548fSNikolay Borisov {
36406f2548fSNikolay Borisov return btrfs_ino(BTRFS_I(inode)) != BTRFS_BTREE_INODE_OBJECTID;
36506f2548fSNikolay Borisov }
36606f2548fSNikolay Borisov
btrfs_mod_outstanding_extents(struct btrfs_inode * inode,int mod)3678b62f87bSJosef Bacik static inline void btrfs_mod_outstanding_extents(struct btrfs_inode *inode,
3688b62f87bSJosef Bacik int mod)
3698b62f87bSJosef Bacik {
3708b62f87bSJosef Bacik lockdep_assert_held(&inode->lock);
3718b62f87bSJosef Bacik inode->outstanding_extents += mod;
3728b62f87bSJosef Bacik if (btrfs_is_free_space_inode(inode))
3738b62f87bSJosef Bacik return;
374dd48d407SJosef Bacik trace_btrfs_inode_mod_outstanding_extents(inode->root, btrfs_ino(inode),
37564425500SNaohiro Aota mod, inode->outstanding_extents);
3768b62f87bSJosef Bacik }
3778b62f87bSJosef Bacik
378bc0939fcSFilipe Manana /*
379bc0939fcSFilipe Manana * Called every time after doing a buffered, direct IO or memory mapped write.
380bc0939fcSFilipe Manana *
381bc0939fcSFilipe Manana * This is to ensure that if we write to a file that was previously fsynced in
382bc0939fcSFilipe Manana * the current transaction, then try to fsync it again in the same transaction,
383bc0939fcSFilipe Manana * we will know that there were changes in the file and that it needs to be
384bc0939fcSFilipe Manana * logged.
385bc0939fcSFilipe Manana */
btrfs_set_inode_last_sub_trans(struct btrfs_inode * inode)386bc0939fcSFilipe Manana static inline void btrfs_set_inode_last_sub_trans(struct btrfs_inode *inode)
387bc0939fcSFilipe Manana {
388bc0939fcSFilipe Manana spin_lock(&inode->lock);
389bc0939fcSFilipe Manana inode->last_sub_trans = inode->root->log_transid;
390bc0939fcSFilipe Manana spin_unlock(&inode->lock);
391bc0939fcSFilipe Manana }
392bc0939fcSFilipe Manana
39323e3337fSFilipe Manana /*
39465bb9fb0SFilipe Manana * Should be called while holding the inode's VFS lock in exclusive mode, or
39565bb9fb0SFilipe Manana * while holding the inode's mmap lock (struct btrfs_inode::i_mmap_lock) in
39665bb9fb0SFilipe Manana * either shared or exclusive mode, or in a context where no one else can access
39765bb9fb0SFilipe Manana * the inode concurrently (during inode creation or when loading an inode from
39865bb9fb0SFilipe Manana * disk).
39923e3337fSFilipe Manana */
btrfs_set_inode_full_sync(struct btrfs_inode * inode)40023e3337fSFilipe Manana static inline void btrfs_set_inode_full_sync(struct btrfs_inode *inode)
40123e3337fSFilipe Manana {
40223e3337fSFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
40323e3337fSFilipe Manana /*
40423e3337fSFilipe Manana * The inode may have been part of a reflink operation in the last
40523e3337fSFilipe Manana * transaction that modified it, and then a fsync has reset the
40623e3337fSFilipe Manana * last_reflink_trans to avoid subsequent fsyncs in the same
40723e3337fSFilipe Manana * transaction to do unnecessary work. So update last_reflink_trans
40823e3337fSFilipe Manana * to the last_trans value (we have to be pessimistic and assume a
40923e3337fSFilipe Manana * reflink happened).
41023e3337fSFilipe Manana *
41123e3337fSFilipe Manana * The ->last_trans is protected by the inode's spinlock and we can
41223e3337fSFilipe Manana * have a concurrent ordered extent completion update it. Also set
41323e3337fSFilipe Manana * last_reflink_trans to ->last_trans only if the former is less than
41423e3337fSFilipe Manana * the later, because we can be called in a context where
41523e3337fSFilipe Manana * last_reflink_trans was set to the current transaction generation
41623e3337fSFilipe Manana * while ->last_trans was not yet updated in the current transaction,
41723e3337fSFilipe Manana * and therefore has a lower value.
41823e3337fSFilipe Manana */
41923e3337fSFilipe Manana spin_lock(&inode->lock);
42023e3337fSFilipe Manana if (inode->last_reflink_trans < inode->last_trans)
42123e3337fSFilipe Manana inode->last_reflink_trans = inode->last_trans;
42223e3337fSFilipe Manana spin_unlock(&inode->lock);
42323e3337fSFilipe Manana }
42423e3337fSFilipe Manana
btrfs_inode_in_log(struct btrfs_inode * inode,u64 generation)425209ecbb8SFilipe Manana static inline bool btrfs_inode_in_log(struct btrfs_inode *inode, u64 generation)
42622ee6985SJosef Bacik {
427209ecbb8SFilipe Manana bool ret = false;
4282f2ff0eeSFilipe Manana
4290f8939b8SNikolay Borisov spin_lock(&inode->lock);
4300f8939b8SNikolay Borisov if (inode->logged_trans == generation &&
4310f8939b8SNikolay Borisov inode->last_sub_trans <= inode->last_log_commit &&
432f9850787SFilipe Manana inode->last_sub_trans <= btrfs_get_root_last_log_commit(inode->root))
433209ecbb8SFilipe Manana ret = true;
4340f8939b8SNikolay Borisov spin_unlock(&inode->lock);
4352f2ff0eeSFilipe Manana return ret;
43622ee6985SJosef Bacik }
43722ee6985SJosef Bacik
438e6f9d696SChung-Chiang Cheng /*
439e6f9d696SChung-Chiang Cheng * Check if the inode has flags compatible with compression
440e6f9d696SChung-Chiang Cheng */
btrfs_inode_can_compress(const struct btrfs_inode * inode)441e6f9d696SChung-Chiang Cheng static inline bool btrfs_inode_can_compress(const struct btrfs_inode *inode)
442e6f9d696SChung-Chiang Cheng {
443e6f9d696SChung-Chiang Cheng if (inode->flags & BTRFS_INODE_NODATACOW ||
444e6f9d696SChung-Chiang Cheng inode->flags & BTRFS_INODE_NODATASUM)
445e6f9d696SChung-Chiang Cheng return false;
446e6f9d696SChung-Chiang Cheng return true;
447e6f9d696SChung-Chiang Cheng }
448e6f9d696SChung-Chiang Cheng
4497ebc7e5fSJohannes Thumshirn /* Array of bytes with variable length, hexadecimal format 0x1234 */
4507ebc7e5fSJohannes Thumshirn #define CSUM_FMT "0x%*phN"
4517ebc7e5fSJohannes Thumshirn #define CSUM_FMT_VALUE(size, bytes) size, bytes
4522885fd63SJosef Bacik
4532885fd63SJosef Bacik int btrfs_check_sector_csum(struct btrfs_fs_info *fs_info, struct page *page,
4542885fd63SJosef Bacik u32 pgoff, u8 *csum, const u8 * const csum_expected);
455e5219044SChristoph Hellwig bool btrfs_data_csum_ok(struct btrfs_bio *bbio, struct btrfs_device *dev,
456e5219044SChristoph Hellwig u32 bio_offset, struct bio_vec *bv);
4572885fd63SJosef Bacik noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
4582885fd63SJosef Bacik u64 *orig_start, u64 *orig_block_len,
4592885fd63SJosef Bacik u64 *ram_bytes, bool nowait, bool strict);
4602885fd63SJosef Bacik
4615a8a57f9SDavid Sterba void btrfs_del_delalloc_inode(struct btrfs_inode *inode);
4622885fd63SJosef Bacik struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry);
4632885fd63SJosef Bacik int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index);
4642885fd63SJosef Bacik int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4652885fd63SJosef Bacik struct btrfs_inode *dir, struct btrfs_inode *inode,
4662885fd63SJosef Bacik const struct fscrypt_str *name);
4672885fd63SJosef Bacik int btrfs_add_link(struct btrfs_trans_handle *trans,
4682885fd63SJosef Bacik struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
4692885fd63SJosef Bacik const struct fscrypt_str *name, int add_backref, u64 index);
4703c4f91e2SDavid Sterba int btrfs_delete_subvolume(struct btrfs_inode *dir, struct dentry *dentry);
4712885fd63SJosef Bacik int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
4722885fd63SJosef Bacik int front);
4732885fd63SJosef Bacik
4742885fd63SJosef Bacik int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context);
4752885fd63SJosef Bacik int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
4762885fd63SJosef Bacik bool in_reclaim_context);
4772885fd63SJosef Bacik int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
4782885fd63SJosef Bacik unsigned int extra_bits,
4792885fd63SJosef Bacik struct extent_state **cached_state);
4802885fd63SJosef Bacik
4812885fd63SJosef Bacik struct btrfs_new_inode_args {
4822885fd63SJosef Bacik /* Input */
4832885fd63SJosef Bacik struct inode *dir;
4842885fd63SJosef Bacik struct dentry *dentry;
4852885fd63SJosef Bacik struct inode *inode;
4862885fd63SJosef Bacik bool orphan;
4872885fd63SJosef Bacik bool subvol;
4882885fd63SJosef Bacik
4892885fd63SJosef Bacik /* Output from btrfs_new_inode_prepare(), input to btrfs_create_new_inode(). */
4902885fd63SJosef Bacik struct posix_acl *default_acl;
4912885fd63SJosef Bacik struct posix_acl *acl;
4922885fd63SJosef Bacik struct fscrypt_name fname;
4932885fd63SJosef Bacik };
4942885fd63SJosef Bacik
4952885fd63SJosef Bacik int btrfs_new_inode_prepare(struct btrfs_new_inode_args *args,
4962885fd63SJosef Bacik unsigned int *trans_num_items);
4972885fd63SJosef Bacik int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
4982885fd63SJosef Bacik struct btrfs_new_inode_args *args);
4992885fd63SJosef Bacik void btrfs_new_inode_args_destroy(struct btrfs_new_inode_args *args);
500f2d40141SChristian Brauner struct inode *btrfs_new_subvol_inode(struct mnt_idmap *idmap,
5012885fd63SJosef Bacik struct inode *dir);
5024c5d166fSDavid Sterba void btrfs_set_delalloc_extent(struct btrfs_inode *inode, struct extent_state *state,
5032885fd63SJosef Bacik u32 bits);
504bd54766eSDavid Sterba void btrfs_clear_delalloc_extent(struct btrfs_inode *inode,
5052885fd63SJosef Bacik struct extent_state *state, u32 bits);
5062454151cSDavid Sterba void btrfs_merge_delalloc_extent(struct btrfs_inode *inode, struct extent_state *new,
5072885fd63SJosef Bacik struct extent_state *other);
50862798a49SDavid Sterba void btrfs_split_delalloc_extent(struct btrfs_inode *inode,
5092885fd63SJosef Bacik struct extent_state *orig, u64 split);
5102885fd63SJosef Bacik void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end);
5112885fd63SJosef Bacik void btrfs_evict_inode(struct inode *inode);
5122885fd63SJosef Bacik struct inode *btrfs_alloc_inode(struct super_block *sb);
5132885fd63SJosef Bacik void btrfs_destroy_inode(struct inode *inode);
5142885fd63SJosef Bacik void btrfs_free_inode(struct inode *inode);
5152885fd63SJosef Bacik int btrfs_drop_inode(struct inode *inode);
5162885fd63SJosef Bacik int __init btrfs_init_cachep(void);
5172885fd63SJosef Bacik void __cold btrfs_destroy_cachep(void);
5182885fd63SJosef Bacik struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
5192885fd63SJosef Bacik struct btrfs_root *root, struct btrfs_path *path);
5202885fd63SJosef Bacik struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root);
5212885fd63SJosef Bacik struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
5228bab0a30SQu Wenruo struct page *page, u64 start, u64 len);
5232885fd63SJosef Bacik int btrfs_update_inode(struct btrfs_trans_handle *trans,
5248b9d0322SFilipe Manana struct btrfs_inode *inode);
5252885fd63SJosef Bacik int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
5260a5d0dc5SFilipe Manana struct btrfs_inode *inode);
5272885fd63SJosef Bacik int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct btrfs_inode *inode);
5282885fd63SJosef Bacik int btrfs_orphan_cleanup(struct btrfs_root *root);
5292885fd63SJosef Bacik int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size);
530e55cf7caSDavid Sterba void btrfs_add_delayed_iput(struct btrfs_inode *inode);
5312885fd63SJosef Bacik void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info);
5322885fd63SJosef Bacik int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info);
5332885fd63SJosef Bacik int btrfs_prealloc_file_range(struct inode *inode, int mode,
5342885fd63SJosef Bacik u64 start, u64 num_bytes, u64 min_size,
5352885fd63SJosef Bacik loff_t actual_len, u64 *alloc_hint);
5362885fd63SJosef Bacik int btrfs_prealloc_file_range_trans(struct inode *inode,
5372885fd63SJosef Bacik struct btrfs_trans_handle *trans, int mode,
5382885fd63SJosef Bacik u64 start, u64 num_bytes, u64 min_size,
5392885fd63SJosef Bacik loff_t actual_len, u64 *alloc_hint);
5402885fd63SJosef Bacik int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
541c56cbe90SChristoph Hellwig u64 start, u64 end, struct writeback_control *wbc);
5422885fd63SJosef Bacik int btrfs_writepage_cow_fixup(struct page *page);
5432885fd63SJosef Bacik int btrfs_encoded_io_compression_from_extent(struct btrfs_fs_info *fs_info,
5442885fd63SJosef Bacik int compress_type);
5452885fd63SJosef Bacik int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode,
5462885fd63SJosef Bacik u64 file_offset, u64 disk_bytenr,
5472885fd63SJosef Bacik u64 disk_io_size,
5482885fd63SJosef Bacik struct page **pages);
5492885fd63SJosef Bacik ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter,
5502885fd63SJosef Bacik struct btrfs_ioctl_encoded_io_args *encoded);
5512885fd63SJosef Bacik ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from,
5522885fd63SJosef Bacik const struct btrfs_ioctl_encoded_io_args *encoded);
5532885fd63SJosef Bacik
5542885fd63SJosef Bacik ssize_t btrfs_dio_read(struct kiocb *iocb, struct iov_iter *iter,
5552885fd63SJosef Bacik size_t done_before);
5562885fd63SJosef Bacik struct iomap_dio *btrfs_dio_write(struct kiocb *iocb, struct iov_iter *iter,
5572885fd63SJosef Bacik size_t done_before);
5585e485ac6SFilipe Manana struct btrfs_inode *btrfs_find_first_inode(struct btrfs_root *root, u64 min_ino);
5592885fd63SJosef Bacik
5602885fd63SJosef Bacik extern const struct dentry_operations btrfs_dentry_operations;
5612885fd63SJosef Bacik
5622885fd63SJosef Bacik /* Inode locking type flags, by default the exclusive lock is taken. */
5632885fd63SJosef Bacik enum btrfs_ilock_type {
5642885fd63SJosef Bacik ENUM_BIT(BTRFS_ILOCK_SHARED),
5652885fd63SJosef Bacik ENUM_BIT(BTRFS_ILOCK_TRY),
5662885fd63SJosef Bacik ENUM_BIT(BTRFS_ILOCK_MMAP),
5672885fd63SJosef Bacik };
5682885fd63SJosef Bacik
56929b6352bSDavid Sterba int btrfs_inode_lock(struct btrfs_inode *inode, unsigned int ilock_flags);
570e5d4d75bSDavid Sterba void btrfs_inode_unlock(struct btrfs_inode *inode, unsigned int ilock_flags);
5712885fd63SJosef Bacik void btrfs_update_inode_bytes(struct btrfs_inode *inode, const u64 add_bytes,
5722885fd63SJosef Bacik const u64 del_bytes);
5732885fd63SJosef Bacik void btrfs_assert_inode_range_clean(struct btrfs_inode *inode, u64 start, u64 end);
5742885fd63SJosef Bacik
5752c90e5d6SChris Mason #endif
576