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 { 24bbe339ccSDavid Sterba BTRFS_INODE_ORDERED_DATA_CLOSE, 257efc3e34SOmar Sandoval BTRFS_INODE_DUMMY, 267efc3e34SOmar Sandoval BTRFS_INODE_IN_DEFRAG, 277efc3e34SOmar Sandoval BTRFS_INODE_HAS_ASYNC_EXTENT, 28*48778179SFilipe Manana /* 29*48778179SFilipe Manana * Always set under the VFS' inode lock, otherwise it can cause races 30*48778179SFilipe Manana * during fsync (we start as a fast fsync and then end up in a full 31*48778179SFilipe Manana * fsync racing with ordered extent completion). 32*48778179SFilipe Manana */ 337efc3e34SOmar Sandoval BTRFS_INODE_NEEDS_FULL_SYNC, 347efc3e34SOmar Sandoval BTRFS_INODE_COPY_EVERYTHING, 357efc3e34SOmar Sandoval BTRFS_INODE_IN_DELALLOC_LIST, 368e0fa5d7SDavid Sterba BTRFS_INODE_READDIO_NEED_LOCK, 377efc3e34SOmar Sandoval BTRFS_INODE_HAS_PROPS, 383cd24c69SEthan Lien BTRFS_INODE_SNAPSHOT_FLUSH, 397efc3e34SOmar Sandoval }; 4072ac3c0dSJosef Bacik 41f1ace244SAneesh /* in memory btrfs inode */ 422c90e5d6SChris Mason struct btrfs_inode { 43d352ac68SChris Mason /* which subvolume this inode belongs to */ 44d6e4a428SChris Mason struct btrfs_root *root; 45d352ac68SChris Mason 46d352ac68SChris Mason /* key used to find this inode on disk. This is used by the code 47d352ac68SChris Mason * to read in roots of subvolumes 48d352ac68SChris Mason */ 49d6e4a428SChris Mason struct btrfs_key location; 50d352ac68SChris Mason 512f2ff0eeSFilipe Manana /* 522f2ff0eeSFilipe Manana * Lock for counters and all fields used to determine if the inode is in 532f2ff0eeSFilipe Manana * the log or not (last_trans, last_sub_trans, last_log_commit, 542f2ff0eeSFilipe Manana * logged_trans). 552f2ff0eeSFilipe Manana */ 569e0baf60SJosef Bacik spinlock_t lock; 579e0baf60SJosef Bacik 58d352ac68SChris Mason /* the extent_tree has caches of all the extent mappings to disk */ 59a52d9a80SChris Mason struct extent_map_tree extent_tree; 60d352ac68SChris Mason 61d352ac68SChris Mason /* the io_tree does range state (DIRTY, LOCKED etc) */ 62d1310b2eSChris Mason struct extent_io_tree io_tree; 63d352ac68SChris Mason 64d352ac68SChris Mason /* special utility tree used to record which mirrors have already been 65d352ac68SChris Mason * tried when checksums fail for a given block 66d352ac68SChris Mason */ 677e38326fSChris Mason struct extent_io_tree io_failure_tree; 68d352ac68SChris Mason 6941a2ee75SJosef Bacik /* 7041a2ee75SJosef Bacik * Keep track of where the inode has extent items mapped in order to 7141a2ee75SJosef Bacik * make sure the i_size adjustments are accurate 7241a2ee75SJosef Bacik */ 7341a2ee75SJosef Bacik struct extent_io_tree file_extent_tree; 7441a2ee75SJosef Bacik 75d352ac68SChris Mason /* held while logging the inode in tree-log.c */ 76e02119d5SChris Mason struct mutex log_mutex; 77d352ac68SChris Mason 78d352ac68SChris Mason /* used to order data wrt metadata */ 79e6dcd2dcSChris Mason struct btrfs_ordered_inode_tree ordered_tree; 8015ee9bc7SJosef Bacik 81d352ac68SChris Mason /* list of all the delalloc inodes in the FS. There are times we need 82d352ac68SChris Mason * to write all the delalloc pages to disk, and this list is used 83d352ac68SChris Mason * to walk them all. 84d352ac68SChris Mason */ 85ea8c2819SChris Mason struct list_head delalloc_inodes; 86ea8c2819SChris Mason 875d4f98a2SYan Zheng /* node for the red-black tree that links inodes in subvolume root */ 885d4f98a2SYan Zheng struct rb_node rb_node; 895d4f98a2SYan Zheng 9072ac3c0dSJosef Bacik unsigned long runtime_flags; 9172ac3c0dSJosef Bacik 929c931c5aSNathaniel Yazdani /* Keep track of who's O_SYNC/fsyncing currently */ 93b812ce28SJosef Bacik atomic_t sync_writers; 94b812ce28SJosef Bacik 95d352ac68SChris Mason /* full 64 bit generation number, struct vfs_inode doesn't have a big 96d352ac68SChris Mason * enough field for this. 97d352ac68SChris Mason */ 98e02119d5SChris Mason u64 generation; 99e02119d5SChris Mason 10015ee9bc7SJosef Bacik /* 10115ee9bc7SJosef Bacik * transid of the trans_handle that last modified this inode 10215ee9bc7SJosef Bacik */ 10315ee9bc7SJosef Bacik u64 last_trans; 104257c62e1SChris Mason 105257c62e1SChris Mason /* 106e02119d5SChris Mason * transid that last logged this inode 107e02119d5SChris Mason */ 108e02119d5SChris Mason u64 logged_trans; 10949eb7e46SChris Mason 110bb14a59bSMiao Xie /* 111bb14a59bSMiao Xie * log transid when this inode was last modified 112bb14a59bSMiao Xie */ 113bb14a59bSMiao Xie int last_sub_trans; 114bb14a59bSMiao Xie 115bb14a59bSMiao Xie /* a local copy of root's last_log_commit */ 116bb14a59bSMiao Xie int last_log_commit; 117bb14a59bSMiao Xie 118d352ac68SChris Mason /* total number of bytes pending delalloc, used by stat to calc the 119d352ac68SChris Mason * real block usage of the file 120d352ac68SChris Mason */ 1219069218dSChris Mason u64 delalloc_bytes; 122d352ac68SChris Mason 123d352ac68SChris Mason /* 124a7e3b975SFilipe Manana * Total number of bytes pending delalloc that fall within a file 125a7e3b975SFilipe Manana * range that is either a hole or beyond EOF (and no prealloc extent 126a7e3b975SFilipe Manana * exists in the range). This is always <= delalloc_bytes. 127a7e3b975SFilipe Manana */ 128a7e3b975SFilipe Manana u64 new_delalloc_bytes; 129a7e3b975SFilipe Manana 130a7e3b975SFilipe Manana /* 13147059d93SWang Shilong * total number of bytes pending defrag, used by stat to check whether 13247059d93SWang Shilong * it needs COW. 13347059d93SWang Shilong */ 13447059d93SWang Shilong u64 defrag_bytes; 13547059d93SWang Shilong 13647059d93SWang Shilong /* 137d352ac68SChris Mason * the size of the file stored in the metadata on disk. data=ordered 138d352ac68SChris Mason * means the in-memory i_size might be larger than the size on disk 139d352ac68SChris Mason * because not all the blocks are written yet. 140d352ac68SChris Mason */ 141dbe674a9SChris Mason u64 disk_i_size; 142d352ac68SChris Mason 143aec7477bSJosef Bacik /* 144aec7477bSJosef Bacik * if this is a directory then index_cnt is the counter for the index 145aec7477bSJosef Bacik * number for new files that are created 146aec7477bSJosef Bacik */ 147aec7477bSJosef Bacik u64 index_cnt; 148d352ac68SChris Mason 14967de1176SMiao Xie /* Cache the directory index number to speed the dir/file remove */ 15067de1176SMiao Xie u64 dir_index; 15167de1176SMiao Xie 15212fcfd22SChris Mason /* the fsync log has some corner cases that mean we have to check 15312fcfd22SChris Mason * directories to see if any unlinks have been done before 15412fcfd22SChris Mason * the directory was logged. See tree-log.c for all the 15512fcfd22SChris Mason * details 15612fcfd22SChris Mason */ 15712fcfd22SChris Mason u64 last_unlink_trans; 15812fcfd22SChris Mason 1597709cde3SJosef Bacik /* 1603ebac17cSFilipe Manana * The id/generation of the last transaction where this inode was 1613ebac17cSFilipe Manana * either the source or the destination of a clone/dedupe operation. 1623ebac17cSFilipe Manana * Used when logging an inode to know if there are shared extents that 1633ebac17cSFilipe Manana * need special care when logging checksum items, to avoid duplicate 1643ebac17cSFilipe Manana * checksum items in a log (which can lead to a corruption where we end 1653ebac17cSFilipe Manana * up with missing checksum ranges after log replay). 1663ebac17cSFilipe Manana * Protected by the vfs inode lock. 1673ebac17cSFilipe Manana */ 1683ebac17cSFilipe Manana u64 last_reflink_trans; 1693ebac17cSFilipe Manana 1703ebac17cSFilipe Manana /* 1717709cde3SJosef Bacik * Number of bytes outstanding that are going to need csums. This is 1727709cde3SJosef Bacik * used in ENOSPC accounting. 1737709cde3SJosef Bacik */ 1747709cde3SJosef Bacik u64 csum_bytes; 1757709cde3SJosef Bacik 176f1bdcc0aSJosef Bacik /* flags field from the on disk inode */ 177f1bdcc0aSJosef Bacik u32 flags; 178f1bdcc0aSJosef Bacik 1795a3f23d5SChris Mason /* 18032c00affSJosef Bacik * Counters to keep track of the number of extent item's we may use due 18132c00affSJosef Bacik * to delalloc and such. outstanding_extents is the number of extent 18232c00affSJosef Bacik * items we think we'll end up using, and reserved_extents is the number 18332c00affSJosef Bacik * of extent items we've reserved metadata for. 1849ed74f2dSJosef Bacik */ 1859e0baf60SJosef Bacik unsigned outstanding_extents; 18669fe2d75SJosef Bacik 18769fe2d75SJosef Bacik struct btrfs_block_rsv block_rsv; 1889ed74f2dSJosef Bacik 1899ed74f2dSJosef Bacik /* 190b52aa8c9SDavid Sterba * Cached values of inode properties 1911e701a32SChris Mason */ 192b52aa8c9SDavid Sterba unsigned prop_compress; /* per-file compression algorithm */ 193eec63c65SDavid Sterba /* 194eec63c65SDavid Sterba * Force compression on the file using the defrag ioctl, could be 195eec63c65SDavid Sterba * different from prop_compress and takes precedence if set 196eec63c65SDavid Sterba */ 197eec63c65SDavid Sterba unsigned defrag_compress; 1981e701a32SChris Mason 19916cdcec7SMiao Xie struct btrfs_delayed_node *delayed_node; 20016cdcec7SMiao Xie 2019cc97d64Schandan r /* File creation time. */ 202d3c6be6fSArnd Bergmann struct timespec64 i_otime; 2039cc97d64Schandan r 2048089fe62SDavid Sterba /* Hook into fs_info->delayed_iputs */ 2058089fe62SDavid Sterba struct list_head delayed_iput; 2068089fe62SDavid Sterba 2075f9a8a51SFilipe Manana /* 2085f9a8a51SFilipe Manana * To avoid races between lockless (i_mutex not held) direct IO writes 2095f9a8a51SFilipe Manana * and concurrent fsync requests. Direct IO writes must acquire read 2105f9a8a51SFilipe Manana * access on this semaphore for creating an extent map and its 2115f9a8a51SFilipe Manana * corresponding ordered extent. The fast fsync path must acquire write 2125f9a8a51SFilipe Manana * access on this semaphore before it collects ordered extents and 2135f9a8a51SFilipe Manana * extent maps. 2145f9a8a51SFilipe Manana */ 2155f9a8a51SFilipe Manana struct rw_semaphore dio_sem; 2165f9a8a51SFilipe Manana 217d352ac68SChris Mason struct inode vfs_inode; 2182c90e5d6SChris Mason }; 219dbe674a9SChris Mason 2209a35b637SJeff Mahoney static inline struct btrfs_inode *BTRFS_I(const struct inode *inode) 2212c90e5d6SChris Mason { 2222c90e5d6SChris Mason return container_of(inode, struct btrfs_inode, vfs_inode); 2232c90e5d6SChris Mason } 2242c90e5d6SChris Mason 225778ba82bSFilipe David Borba Manana static inline unsigned long btrfs_inode_hash(u64 objectid, 226778ba82bSFilipe David Borba Manana const struct btrfs_root *root) 227778ba82bSFilipe David Borba Manana { 2284fd786e6SMisono Tomohiro u64 h = objectid ^ (root->root_key.objectid * GOLDEN_RATIO_PRIME); 229778ba82bSFilipe David Borba Manana 230778ba82bSFilipe David Borba Manana #if BITS_PER_LONG == 32 231778ba82bSFilipe David Borba Manana h = (h >> 32) ^ (h & 0xffffffff); 232778ba82bSFilipe David Borba Manana #endif 233778ba82bSFilipe David Borba Manana 234778ba82bSFilipe David Borba Manana return (unsigned long)h; 235778ba82bSFilipe David Borba Manana } 236778ba82bSFilipe David Borba Manana 237778ba82bSFilipe David Borba Manana static inline void btrfs_insert_inode_hash(struct inode *inode) 238778ba82bSFilipe David Borba Manana { 239778ba82bSFilipe David Borba Manana unsigned long h = btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root); 240778ba82bSFilipe David Borba Manana 241778ba82bSFilipe David Borba Manana __insert_inode_hash(inode, h); 242778ba82bSFilipe David Borba Manana } 243778ba82bSFilipe David Borba Manana 2449a35b637SJeff Mahoney static inline u64 btrfs_ino(const struct btrfs_inode *inode) 24533345d01SLi Zefan { 2464a0cc7caSNikolay Borisov u64 ino = inode->location.objectid; 24733345d01SLi Zefan 24814c7cca7SLiu Bo /* 24914c7cca7SLiu Bo * !ino: btree_inode 25014c7cca7SLiu Bo * type == BTRFS_ROOT_ITEM_KEY: subvol dir 25114c7cca7SLiu Bo */ 2524a0cc7caSNikolay Borisov if (!ino || inode->location.type == BTRFS_ROOT_ITEM_KEY) 2534a0cc7caSNikolay Borisov ino = inode->vfs_inode.i_ino; 25433345d01SLi Zefan return ino; 25533345d01SLi Zefan } 25633345d01SLi Zefan 2576ef06d27SNikolay Borisov static inline void btrfs_i_size_write(struct btrfs_inode *inode, u64 size) 258dbe674a9SChris Mason { 2596ef06d27SNikolay Borisov i_size_write(&inode->vfs_inode, size); 2606ef06d27SNikolay Borisov inode->disk_i_size = size; 261dbe674a9SChris Mason } 262dbe674a9SChris Mason 26370ddc553SNikolay Borisov static inline bool btrfs_is_free_space_inode(struct btrfs_inode *inode) 2642cf8572dSChris Mason { 26570ddc553SNikolay Borisov struct btrfs_root *root = inode->root; 26683eea1f1SLiu Bo 26751a8cf9dSLiu Bo if (root == root->fs_info->tree_root && 26870ddc553SNikolay Borisov btrfs_ino(inode) != BTRFS_BTREE_INODE_OBJECTID) 26951a8cf9dSLiu Bo return true; 27070ddc553SNikolay Borisov if (inode->location.objectid == BTRFS_FREE_INO_OBJECTID) 2712cf8572dSChris Mason return true; 2722cf8572dSChris Mason return false; 2732cf8572dSChris Mason } 2742cf8572dSChris Mason 27506f2548fSNikolay Borisov static inline bool is_data_inode(struct inode *inode) 27606f2548fSNikolay Borisov { 27706f2548fSNikolay Borisov return btrfs_ino(BTRFS_I(inode)) != BTRFS_BTREE_INODE_OBJECTID; 27806f2548fSNikolay Borisov } 27906f2548fSNikolay Borisov 2808b62f87bSJosef Bacik static inline void btrfs_mod_outstanding_extents(struct btrfs_inode *inode, 2818b62f87bSJosef Bacik int mod) 2828b62f87bSJosef Bacik { 2838b62f87bSJosef Bacik lockdep_assert_held(&inode->lock); 2848b62f87bSJosef Bacik inode->outstanding_extents += mod; 2858b62f87bSJosef Bacik if (btrfs_is_free_space_inode(inode)) 2868b62f87bSJosef Bacik return; 287dd48d407SJosef Bacik trace_btrfs_inode_mod_outstanding_extents(inode->root, btrfs_ino(inode), 288dd48d407SJosef Bacik mod); 2898b62f87bSJosef Bacik } 2908b62f87bSJosef Bacik 2910f8939b8SNikolay Borisov static inline int btrfs_inode_in_log(struct btrfs_inode *inode, u64 generation) 29222ee6985SJosef Bacik { 2932f2ff0eeSFilipe Manana int ret = 0; 2942f2ff0eeSFilipe Manana 2950f8939b8SNikolay Borisov spin_lock(&inode->lock); 2960f8939b8SNikolay Borisov if (inode->logged_trans == generation && 2970f8939b8SNikolay Borisov inode->last_sub_trans <= inode->last_log_commit && 2980f8939b8SNikolay Borisov inode->last_sub_trans <= inode->root->last_log_commit) { 299125c4cf9SFilipe Manana /* 300125c4cf9SFilipe Manana * After a ranged fsync we might have left some extent maps 301125c4cf9SFilipe Manana * (that fall outside the fsync's range). So return false 302125c4cf9SFilipe Manana * here if the list isn't empty, to make sure btrfs_log_inode() 303125c4cf9SFilipe Manana * will be called and process those extent maps. 304125c4cf9SFilipe Manana */ 305125c4cf9SFilipe Manana smp_mb(); 3060f8939b8SNikolay Borisov if (list_empty(&inode->extent_tree.modified_extents)) 3072f2ff0eeSFilipe Manana ret = 1; 308125c4cf9SFilipe Manana } 3090f8939b8SNikolay Borisov spin_unlock(&inode->lock); 3102f2ff0eeSFilipe Manana return ret; 31122ee6985SJosef Bacik } 31222ee6985SJosef Bacik 313facc8a22SMiao Xie struct btrfs_dio_private { 314facc8a22SMiao Xie struct inode *inode; 315facc8a22SMiao Xie u64 logical_offset; 316facc8a22SMiao Xie u64 disk_bytenr; 317facc8a22SMiao Xie u64 bytes; 318facc8a22SMiao Xie 319e3b318d1SOmar Sandoval /* 320e3b318d1SOmar Sandoval * References to this structure. There is one reference per in-flight 321e3b318d1SOmar Sandoval * bio plus one while we're still setting up. 322e3b318d1SOmar Sandoval */ 323e3b318d1SOmar Sandoval refcount_t refs; 324facc8a22SMiao Xie 325facc8a22SMiao Xie /* dio_bio came from fs/direct-io.c */ 326facc8a22SMiao Xie struct bio *dio_bio; 327c1dc0896SMiao Xie 32885879573SOmar Sandoval /* Array of checksums */ 32985879573SOmar Sandoval u8 csums[]; 330facc8a22SMiao Xie }; 331facc8a22SMiao Xie 3328e0fa5d7SDavid Sterba /* 3338e0fa5d7SDavid Sterba * Disable DIO read nolock optimization, so new dio readers will be forced 3348e0fa5d7SDavid Sterba * to grab i_mutex. It is used to avoid the endless truncate due to 3358e0fa5d7SDavid Sterba * nonlocked dio read. 3368e0fa5d7SDavid Sterba */ 3378e0fa5d7SDavid Sterba static inline void btrfs_inode_block_unlocked_dio(struct btrfs_inode *inode) 3388e0fa5d7SDavid Sterba { 3398e0fa5d7SDavid Sterba set_bit(BTRFS_INODE_READDIO_NEED_LOCK, &inode->runtime_flags); 3408e0fa5d7SDavid Sterba smp_mb(); 3418e0fa5d7SDavid Sterba } 3428e0fa5d7SDavid Sterba 3438e0fa5d7SDavid Sterba static inline void btrfs_inode_resume_unlocked_dio(struct btrfs_inode *inode) 3448e0fa5d7SDavid Sterba { 3458e0fa5d7SDavid Sterba smp_mb__before_atomic(); 3468e0fa5d7SDavid Sterba clear_bit(BTRFS_INODE_READDIO_NEED_LOCK, &inode->runtime_flags); 3478e0fa5d7SDavid Sterba } 3488e0fa5d7SDavid Sterba 3497ebc7e5fSJohannes Thumshirn /* Array of bytes with variable length, hexadecimal format 0x1234 */ 3507ebc7e5fSJohannes Thumshirn #define CSUM_FMT "0x%*phN" 3517ebc7e5fSJohannes Thumshirn #define CSUM_FMT_VALUE(size, bytes) size, bytes 3527ebc7e5fSJohannes Thumshirn 3530970a22eSNikolay Borisov static inline void btrfs_print_data_csum_error(struct btrfs_inode *inode, 354ea41d6b2SJohannes Thumshirn u64 logical_start, u8 *csum, u8 *csum_expected, int mirror_num) 3556f6b643eSQu Wenruo { 3560970a22eSNikolay Borisov struct btrfs_root *root = inode->root; 3577ebc7e5fSJohannes Thumshirn struct btrfs_super_block *sb = root->fs_info->super_copy; 3587ebc7e5fSJohannes Thumshirn const u16 csum_size = btrfs_super_csum_size(sb); 3596f6b643eSQu Wenruo 3606f6b643eSQu Wenruo /* Output minus objectid, which is more meaningful */ 3614fd786e6SMisono Tomohiro if (root->root_key.objectid >= BTRFS_LAST_FREE_OBJECTID) 3626f6b643eSQu Wenruo btrfs_warn_rl(root->fs_info, 3637ebc7e5fSJohannes Thumshirn "csum failed root %lld ino %lld off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d", 3644fd786e6SMisono Tomohiro root->root_key.objectid, btrfs_ino(inode), 3657ebc7e5fSJohannes Thumshirn logical_start, 366ea41d6b2SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, csum), 367ea41d6b2SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, csum_expected), 3687ebc7e5fSJohannes Thumshirn mirror_num); 3696f6b643eSQu Wenruo else 3706f6b643eSQu Wenruo btrfs_warn_rl(root->fs_info, 3717ebc7e5fSJohannes Thumshirn "csum failed root %llu ino %llu off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d", 3724fd786e6SMisono Tomohiro root->root_key.objectid, btrfs_ino(inode), 3737ebc7e5fSJohannes Thumshirn logical_start, 374ea41d6b2SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, csum), 375ea41d6b2SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, csum_expected), 3767ebc7e5fSJohannes Thumshirn mirror_num); 3776f6b643eSQu Wenruo } 3786f6b643eSQu Wenruo 3792c90e5d6SChris Mason #endif 380