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> 10a52d9a80SChris Mason #include "extent_map.h" 11d1310b2eSChris Mason #include "extent_io.h" 12e6dcd2dcSChris Mason #include "ordered-data.h" 1316cdcec7SMiao Xie #include "delayed-inode.h" 14a52d9a80SChris Mason 1572ac3c0dSJosef Bacik /* 1672ac3c0dSJosef Bacik * ordered_data_close is set by truncate when a file that used 1772ac3c0dSJosef Bacik * to have good data has been truncated to zero. When it is set 1872ac3c0dSJosef Bacik * the btrfs file release call will add this inode to the 1972ac3c0dSJosef Bacik * ordered operations list so that we make sure to flush out any 2072ac3c0dSJosef Bacik * new data the application may have written before commit. 2172ac3c0dSJosef Bacik */ 227efc3e34SOmar Sandoval enum { 23bbe339ccSDavid Sterba BTRFS_INODE_ORDERED_DATA_CLOSE, 247efc3e34SOmar Sandoval BTRFS_INODE_DUMMY, 257efc3e34SOmar Sandoval BTRFS_INODE_IN_DEFRAG, 267efc3e34SOmar Sandoval BTRFS_INODE_HAS_ASYNC_EXTENT, 277efc3e34SOmar Sandoval BTRFS_INODE_NEEDS_FULL_SYNC, 287efc3e34SOmar Sandoval BTRFS_INODE_COPY_EVERYTHING, 297efc3e34SOmar Sandoval BTRFS_INODE_IN_DELALLOC_LIST, 307efc3e34SOmar Sandoval BTRFS_INODE_READDIO_NEED_LOCK, 317efc3e34SOmar Sandoval BTRFS_INODE_HAS_PROPS, 323cd24c69SEthan Lien BTRFS_INODE_SNAPSHOT_FLUSH, 337efc3e34SOmar Sandoval }; 3472ac3c0dSJosef Bacik 35f1ace244SAneesh /* in memory btrfs inode */ 362c90e5d6SChris Mason struct btrfs_inode { 37d352ac68SChris Mason /* which subvolume this inode belongs to */ 38d6e4a428SChris Mason struct btrfs_root *root; 39d352ac68SChris Mason 40d352ac68SChris Mason /* key used to find this inode on disk. This is used by the code 41d352ac68SChris Mason * to read in roots of subvolumes 42d352ac68SChris Mason */ 43d6e4a428SChris Mason struct btrfs_key location; 44d352ac68SChris Mason 452f2ff0eeSFilipe Manana /* 462f2ff0eeSFilipe Manana * Lock for counters and all fields used to determine if the inode is in 472f2ff0eeSFilipe Manana * the log or not (last_trans, last_sub_trans, last_log_commit, 482f2ff0eeSFilipe Manana * logged_trans). 492f2ff0eeSFilipe Manana */ 509e0baf60SJosef Bacik spinlock_t lock; 519e0baf60SJosef Bacik 52d352ac68SChris Mason /* the extent_tree has caches of all the extent mappings to disk */ 53a52d9a80SChris Mason struct extent_map_tree extent_tree; 54d352ac68SChris Mason 55d352ac68SChris Mason /* the io_tree does range state (DIRTY, LOCKED etc) */ 56d1310b2eSChris Mason struct extent_io_tree io_tree; 57d352ac68SChris Mason 58d352ac68SChris Mason /* special utility tree used to record which mirrors have already been 59d352ac68SChris Mason * tried when checksums fail for a given block 60d352ac68SChris Mason */ 617e38326fSChris Mason struct extent_io_tree io_failure_tree; 62d352ac68SChris Mason 63*41a2ee75SJosef Bacik /* 64*41a2ee75SJosef Bacik * Keep track of where the inode has extent items mapped in order to 65*41a2ee75SJosef Bacik * make sure the i_size adjustments are accurate 66*41a2ee75SJosef Bacik */ 67*41a2ee75SJosef Bacik struct extent_io_tree file_extent_tree; 68*41a2ee75SJosef Bacik 69d352ac68SChris Mason /* held while logging the inode in tree-log.c */ 70e02119d5SChris Mason struct mutex log_mutex; 71d352ac68SChris Mason 72d352ac68SChris Mason /* used to order data wrt metadata */ 73e6dcd2dcSChris Mason struct btrfs_ordered_inode_tree ordered_tree; 7415ee9bc7SJosef Bacik 75d352ac68SChris Mason /* list of all the delalloc inodes in the FS. There are times we need 76d352ac68SChris Mason * to write all the delalloc pages to disk, and this list is used 77d352ac68SChris Mason * to walk them all. 78d352ac68SChris Mason */ 79ea8c2819SChris Mason struct list_head delalloc_inodes; 80ea8c2819SChris Mason 815d4f98a2SYan Zheng /* node for the red-black tree that links inodes in subvolume root */ 825d4f98a2SYan Zheng struct rb_node rb_node; 835d4f98a2SYan Zheng 8472ac3c0dSJosef Bacik unsigned long runtime_flags; 8572ac3c0dSJosef Bacik 869c931c5aSNathaniel Yazdani /* Keep track of who's O_SYNC/fsyncing currently */ 87b812ce28SJosef Bacik atomic_t sync_writers; 88b812ce28SJosef Bacik 89d352ac68SChris Mason /* full 64 bit generation number, struct vfs_inode doesn't have a big 90d352ac68SChris Mason * enough field for this. 91d352ac68SChris Mason */ 92e02119d5SChris Mason u64 generation; 93e02119d5SChris Mason 9415ee9bc7SJosef Bacik /* 9515ee9bc7SJosef Bacik * transid of the trans_handle that last modified this inode 9615ee9bc7SJosef Bacik */ 9715ee9bc7SJosef Bacik u64 last_trans; 98257c62e1SChris Mason 99257c62e1SChris Mason /* 100e02119d5SChris Mason * transid that last logged this inode 101e02119d5SChris Mason */ 102e02119d5SChris Mason u64 logged_trans; 10349eb7e46SChris Mason 104bb14a59bSMiao Xie /* 105bb14a59bSMiao Xie * log transid when this inode was last modified 106bb14a59bSMiao Xie */ 107bb14a59bSMiao Xie int last_sub_trans; 108bb14a59bSMiao Xie 109bb14a59bSMiao Xie /* a local copy of root's last_log_commit */ 110bb14a59bSMiao Xie int last_log_commit; 111bb14a59bSMiao Xie 112d352ac68SChris Mason /* total number of bytes pending delalloc, used by stat to calc the 113d352ac68SChris Mason * real block usage of the file 114d352ac68SChris Mason */ 1159069218dSChris Mason u64 delalloc_bytes; 116d352ac68SChris Mason 117d352ac68SChris Mason /* 118a7e3b975SFilipe Manana * Total number of bytes pending delalloc that fall within a file 119a7e3b975SFilipe Manana * range that is either a hole or beyond EOF (and no prealloc extent 120a7e3b975SFilipe Manana * exists in the range). This is always <= delalloc_bytes. 121a7e3b975SFilipe Manana */ 122a7e3b975SFilipe Manana u64 new_delalloc_bytes; 123a7e3b975SFilipe Manana 124a7e3b975SFilipe Manana /* 12547059d93SWang Shilong * total number of bytes pending defrag, used by stat to check whether 12647059d93SWang Shilong * it needs COW. 12747059d93SWang Shilong */ 12847059d93SWang Shilong u64 defrag_bytes; 12947059d93SWang Shilong 13047059d93SWang Shilong /* 131d352ac68SChris Mason * the size of the file stored in the metadata on disk. data=ordered 132d352ac68SChris Mason * means the in-memory i_size might be larger than the size on disk 133d352ac68SChris Mason * because not all the blocks are written yet. 134d352ac68SChris Mason */ 135dbe674a9SChris Mason u64 disk_i_size; 136d352ac68SChris Mason 137aec7477bSJosef Bacik /* 138aec7477bSJosef Bacik * if this is a directory then index_cnt is the counter for the index 139aec7477bSJosef Bacik * number for new files that are created 140aec7477bSJosef Bacik */ 141aec7477bSJosef Bacik u64 index_cnt; 142d352ac68SChris Mason 14367de1176SMiao Xie /* Cache the directory index number to speed the dir/file remove */ 14467de1176SMiao Xie u64 dir_index; 14567de1176SMiao Xie 14612fcfd22SChris Mason /* the fsync log has some corner cases that mean we have to check 14712fcfd22SChris Mason * directories to see if any unlinks have been done before 14812fcfd22SChris Mason * the directory was logged. See tree-log.c for all the 14912fcfd22SChris Mason * details 15012fcfd22SChris Mason */ 15112fcfd22SChris Mason u64 last_unlink_trans; 15212fcfd22SChris Mason 1537709cde3SJosef Bacik /* 1547709cde3SJosef Bacik * Number of bytes outstanding that are going to need csums. This is 1557709cde3SJosef Bacik * used in ENOSPC accounting. 1567709cde3SJosef Bacik */ 1577709cde3SJosef Bacik u64 csum_bytes; 1587709cde3SJosef Bacik 159f1bdcc0aSJosef Bacik /* flags field from the on disk inode */ 160f1bdcc0aSJosef Bacik u32 flags; 161f1bdcc0aSJosef Bacik 1625a3f23d5SChris Mason /* 16332c00affSJosef Bacik * Counters to keep track of the number of extent item's we may use due 16432c00affSJosef Bacik * to delalloc and such. outstanding_extents is the number of extent 16532c00affSJosef Bacik * items we think we'll end up using, and reserved_extents is the number 16632c00affSJosef Bacik * of extent items we've reserved metadata for. 1679ed74f2dSJosef Bacik */ 1689e0baf60SJosef Bacik unsigned outstanding_extents; 16969fe2d75SJosef Bacik 17069fe2d75SJosef Bacik struct btrfs_block_rsv block_rsv; 1719ed74f2dSJosef Bacik 1729ed74f2dSJosef Bacik /* 173b52aa8c9SDavid Sterba * Cached values of inode properties 1741e701a32SChris Mason */ 175b52aa8c9SDavid Sterba unsigned prop_compress; /* per-file compression algorithm */ 176eec63c65SDavid Sterba /* 177eec63c65SDavid Sterba * Force compression on the file using the defrag ioctl, could be 178eec63c65SDavid Sterba * different from prop_compress and takes precedence if set 179eec63c65SDavid Sterba */ 180eec63c65SDavid Sterba unsigned defrag_compress; 1811e701a32SChris Mason 18216cdcec7SMiao Xie struct btrfs_delayed_node *delayed_node; 18316cdcec7SMiao Xie 1849cc97d64Schandan r /* File creation time. */ 185d3c6be6fSArnd Bergmann struct timespec64 i_otime; 1869cc97d64Schandan r 1878089fe62SDavid Sterba /* Hook into fs_info->delayed_iputs */ 1888089fe62SDavid Sterba struct list_head delayed_iput; 1898089fe62SDavid Sterba 1905f9a8a51SFilipe Manana /* 1915f9a8a51SFilipe Manana * To avoid races between lockless (i_mutex not held) direct IO writes 1925f9a8a51SFilipe Manana * and concurrent fsync requests. Direct IO writes must acquire read 1935f9a8a51SFilipe Manana * access on this semaphore for creating an extent map and its 1945f9a8a51SFilipe Manana * corresponding ordered extent. The fast fsync path must acquire write 1955f9a8a51SFilipe Manana * access on this semaphore before it collects ordered extents and 1965f9a8a51SFilipe Manana * extent maps. 1975f9a8a51SFilipe Manana */ 1985f9a8a51SFilipe Manana struct rw_semaphore dio_sem; 1995f9a8a51SFilipe Manana 200d352ac68SChris Mason struct inode vfs_inode; 2012c90e5d6SChris Mason }; 202dbe674a9SChris Mason 2039a35b637SJeff Mahoney static inline struct btrfs_inode *BTRFS_I(const struct inode *inode) 2042c90e5d6SChris Mason { 2052c90e5d6SChris Mason return container_of(inode, struct btrfs_inode, vfs_inode); 2062c90e5d6SChris Mason } 2072c90e5d6SChris Mason 208778ba82bSFilipe David Borba Manana static inline unsigned long btrfs_inode_hash(u64 objectid, 209778ba82bSFilipe David Borba Manana const struct btrfs_root *root) 210778ba82bSFilipe David Borba Manana { 2114fd786e6SMisono Tomohiro u64 h = objectid ^ (root->root_key.objectid * GOLDEN_RATIO_PRIME); 212778ba82bSFilipe David Borba Manana 213778ba82bSFilipe David Borba Manana #if BITS_PER_LONG == 32 214778ba82bSFilipe David Borba Manana h = (h >> 32) ^ (h & 0xffffffff); 215778ba82bSFilipe David Borba Manana #endif 216778ba82bSFilipe David Borba Manana 217778ba82bSFilipe David Borba Manana return (unsigned long)h; 218778ba82bSFilipe David Borba Manana } 219778ba82bSFilipe David Borba Manana 220778ba82bSFilipe David Borba Manana static inline void btrfs_insert_inode_hash(struct inode *inode) 221778ba82bSFilipe David Borba Manana { 222778ba82bSFilipe David Borba Manana unsigned long h = btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root); 223778ba82bSFilipe David Borba Manana 224778ba82bSFilipe David Borba Manana __insert_inode_hash(inode, h); 225778ba82bSFilipe David Borba Manana } 226778ba82bSFilipe David Borba Manana 2279a35b637SJeff Mahoney static inline u64 btrfs_ino(const struct btrfs_inode *inode) 22833345d01SLi Zefan { 2294a0cc7caSNikolay Borisov u64 ino = inode->location.objectid; 23033345d01SLi Zefan 23114c7cca7SLiu Bo /* 23214c7cca7SLiu Bo * !ino: btree_inode 23314c7cca7SLiu Bo * type == BTRFS_ROOT_ITEM_KEY: subvol dir 23414c7cca7SLiu Bo */ 2354a0cc7caSNikolay Borisov if (!ino || inode->location.type == BTRFS_ROOT_ITEM_KEY) 2364a0cc7caSNikolay Borisov ino = inode->vfs_inode.i_ino; 23733345d01SLi Zefan return ino; 23833345d01SLi Zefan } 23933345d01SLi Zefan 2406ef06d27SNikolay Borisov static inline void btrfs_i_size_write(struct btrfs_inode *inode, u64 size) 241dbe674a9SChris Mason { 2426ef06d27SNikolay Borisov i_size_write(&inode->vfs_inode, size); 2436ef06d27SNikolay Borisov inode->disk_i_size = size; 244dbe674a9SChris Mason } 245dbe674a9SChris Mason 24670ddc553SNikolay Borisov static inline bool btrfs_is_free_space_inode(struct btrfs_inode *inode) 2472cf8572dSChris Mason { 24870ddc553SNikolay Borisov struct btrfs_root *root = inode->root; 24983eea1f1SLiu Bo 25051a8cf9dSLiu Bo if (root == root->fs_info->tree_root && 25170ddc553SNikolay Borisov btrfs_ino(inode) != BTRFS_BTREE_INODE_OBJECTID) 25251a8cf9dSLiu Bo return true; 25370ddc553SNikolay Borisov if (inode->location.objectid == BTRFS_FREE_INO_OBJECTID) 2542cf8572dSChris Mason return true; 2552cf8572dSChris Mason return false; 2562cf8572dSChris Mason } 2572cf8572dSChris Mason 25806f2548fSNikolay Borisov static inline bool is_data_inode(struct inode *inode) 25906f2548fSNikolay Borisov { 26006f2548fSNikolay Borisov return btrfs_ino(BTRFS_I(inode)) != BTRFS_BTREE_INODE_OBJECTID; 26106f2548fSNikolay Borisov } 26206f2548fSNikolay Borisov 2638b62f87bSJosef Bacik static inline void btrfs_mod_outstanding_extents(struct btrfs_inode *inode, 2648b62f87bSJosef Bacik int mod) 2658b62f87bSJosef Bacik { 2668b62f87bSJosef Bacik lockdep_assert_held(&inode->lock); 2678b62f87bSJosef Bacik inode->outstanding_extents += mod; 2688b62f87bSJosef Bacik if (btrfs_is_free_space_inode(inode)) 2698b62f87bSJosef Bacik return; 270dd48d407SJosef Bacik trace_btrfs_inode_mod_outstanding_extents(inode->root, btrfs_ino(inode), 271dd48d407SJosef Bacik mod); 2728b62f87bSJosef Bacik } 2738b62f87bSJosef Bacik 2740f8939b8SNikolay Borisov static inline int btrfs_inode_in_log(struct btrfs_inode *inode, u64 generation) 27522ee6985SJosef Bacik { 2762f2ff0eeSFilipe Manana int ret = 0; 2772f2ff0eeSFilipe Manana 2780f8939b8SNikolay Borisov spin_lock(&inode->lock); 2790f8939b8SNikolay Borisov if (inode->logged_trans == generation && 2800f8939b8SNikolay Borisov inode->last_sub_trans <= inode->last_log_commit && 2810f8939b8SNikolay Borisov inode->last_sub_trans <= inode->root->last_log_commit) { 282125c4cf9SFilipe Manana /* 283125c4cf9SFilipe Manana * After a ranged fsync we might have left some extent maps 284125c4cf9SFilipe Manana * (that fall outside the fsync's range). So return false 285125c4cf9SFilipe Manana * here if the list isn't empty, to make sure btrfs_log_inode() 286125c4cf9SFilipe Manana * will be called and process those extent maps. 287125c4cf9SFilipe Manana */ 288125c4cf9SFilipe Manana smp_mb(); 2890f8939b8SNikolay Borisov if (list_empty(&inode->extent_tree.modified_extents)) 2902f2ff0eeSFilipe Manana ret = 1; 291125c4cf9SFilipe Manana } 2920f8939b8SNikolay Borisov spin_unlock(&inode->lock); 2932f2ff0eeSFilipe Manana return ret; 29422ee6985SJosef Bacik } 29522ee6985SJosef Bacik 296c1dc0896SMiao Xie #define BTRFS_DIO_ORIG_BIO_SUBMITTED 0x1 297c1dc0896SMiao Xie 298facc8a22SMiao Xie struct btrfs_dio_private { 299facc8a22SMiao Xie struct inode *inode; 300c1dc0896SMiao Xie unsigned long flags; 301facc8a22SMiao Xie u64 logical_offset; 302facc8a22SMiao Xie u64 disk_bytenr; 303facc8a22SMiao Xie u64 bytes; 304facc8a22SMiao Xie void *private; 305facc8a22SMiao Xie 306facc8a22SMiao Xie /* number of bios pending for this dio */ 307facc8a22SMiao Xie atomic_t pending_bios; 308facc8a22SMiao Xie 309facc8a22SMiao Xie /* IO errors */ 310facc8a22SMiao Xie int errors; 311facc8a22SMiao Xie 312facc8a22SMiao Xie /* orig_bio is our btrfs_io_bio */ 313facc8a22SMiao Xie struct bio *orig_bio; 314facc8a22SMiao Xie 315facc8a22SMiao Xie /* dio_bio came from fs/direct-io.c */ 316facc8a22SMiao Xie struct bio *dio_bio; 317c1dc0896SMiao Xie 318c1dc0896SMiao Xie /* 31901327610SNicholas D Steeves * The original bio may be split to several sub-bios, this is 320c1dc0896SMiao Xie * done during endio of sub-bios 321c1dc0896SMiao Xie */ 3224e4cbee9SChristoph Hellwig blk_status_t (*subio_endio)(struct inode *, struct btrfs_io_bio *, 3234e4cbee9SChristoph Hellwig blk_status_t); 324facc8a22SMiao Xie }; 325facc8a22SMiao Xie 3262e60a51eSMiao Xie /* 3272e60a51eSMiao Xie * Disable DIO read nolock optimization, so new dio readers will be forced 3282e60a51eSMiao Xie * to grab i_mutex. It is used to avoid the endless truncate due to 3292e60a51eSMiao Xie * nonlocked dio read. 3302e60a51eSMiao Xie */ 331abcefb1eSNikolay Borisov static inline void btrfs_inode_block_unlocked_dio(struct btrfs_inode *inode) 3322e60a51eSMiao Xie { 333abcefb1eSNikolay Borisov set_bit(BTRFS_INODE_READDIO_NEED_LOCK, &inode->runtime_flags); 3342e60a51eSMiao Xie smp_mb(); 3352e60a51eSMiao Xie } 3362e60a51eSMiao Xie 3370b581701SNikolay Borisov static inline void btrfs_inode_resume_unlocked_dio(struct btrfs_inode *inode) 3382e60a51eSMiao Xie { 3394e857c58SPeter Zijlstra smp_mb__before_atomic(); 3400b581701SNikolay Borisov clear_bit(BTRFS_INODE_READDIO_NEED_LOCK, &inode->runtime_flags); 3412e60a51eSMiao Xie } 3422e60a51eSMiao Xie 3437ebc7e5fSJohannes Thumshirn /* Array of bytes with variable length, hexadecimal format 0x1234 */ 3447ebc7e5fSJohannes Thumshirn #define CSUM_FMT "0x%*phN" 3457ebc7e5fSJohannes Thumshirn #define CSUM_FMT_VALUE(size, bytes) size, bytes 3467ebc7e5fSJohannes Thumshirn 3470970a22eSNikolay Borisov static inline void btrfs_print_data_csum_error(struct btrfs_inode *inode, 348ea41d6b2SJohannes Thumshirn u64 logical_start, u8 *csum, u8 *csum_expected, int mirror_num) 3496f6b643eSQu Wenruo { 3500970a22eSNikolay Borisov struct btrfs_root *root = inode->root; 3517ebc7e5fSJohannes Thumshirn struct btrfs_super_block *sb = root->fs_info->super_copy; 3527ebc7e5fSJohannes Thumshirn const u16 csum_size = btrfs_super_csum_size(sb); 3536f6b643eSQu Wenruo 3546f6b643eSQu Wenruo /* Output minus objectid, which is more meaningful */ 3554fd786e6SMisono Tomohiro if (root->root_key.objectid >= BTRFS_LAST_FREE_OBJECTID) 3566f6b643eSQu Wenruo btrfs_warn_rl(root->fs_info, 3577ebc7e5fSJohannes Thumshirn "csum failed root %lld ino %lld off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d", 3584fd786e6SMisono Tomohiro root->root_key.objectid, btrfs_ino(inode), 3597ebc7e5fSJohannes Thumshirn logical_start, 360ea41d6b2SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, csum), 361ea41d6b2SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, csum_expected), 3627ebc7e5fSJohannes Thumshirn mirror_num); 3636f6b643eSQu Wenruo else 3646f6b643eSQu Wenruo btrfs_warn_rl(root->fs_info, 3657ebc7e5fSJohannes Thumshirn "csum failed root %llu ino %llu off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d", 3664fd786e6SMisono Tomohiro root->root_key.objectid, btrfs_ino(inode), 3677ebc7e5fSJohannes Thumshirn logical_start, 368ea41d6b2SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, csum), 369ea41d6b2SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, csum_expected), 3707ebc7e5fSJohannes Thumshirn mirror_num); 3716f6b643eSQu Wenruo } 3726f6b643eSQu Wenruo 3732c90e5d6SChris Mason #endif 374