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> 10*e3b318d1SOmar 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, 287efc3e34SOmar Sandoval BTRFS_INODE_NEEDS_FULL_SYNC, 297efc3e34SOmar Sandoval BTRFS_INODE_COPY_EVERYTHING, 307efc3e34SOmar Sandoval BTRFS_INODE_IN_DELALLOC_LIST, 317efc3e34SOmar Sandoval BTRFS_INODE_READDIO_NEED_LOCK, 327efc3e34SOmar Sandoval BTRFS_INODE_HAS_PROPS, 333cd24c69SEthan Lien BTRFS_INODE_SNAPSHOT_FLUSH, 347efc3e34SOmar Sandoval }; 3572ac3c0dSJosef Bacik 36f1ace244SAneesh /* in memory btrfs inode */ 372c90e5d6SChris Mason struct btrfs_inode { 38d352ac68SChris Mason /* which subvolume this inode belongs to */ 39d6e4a428SChris Mason struct btrfs_root *root; 40d352ac68SChris Mason 41d352ac68SChris Mason /* key used to find this inode on disk. This is used by the code 42d352ac68SChris Mason * to read in roots of subvolumes 43d352ac68SChris Mason */ 44d6e4a428SChris Mason struct btrfs_key location; 45d352ac68SChris Mason 462f2ff0eeSFilipe Manana /* 472f2ff0eeSFilipe Manana * Lock for counters and all fields used to determine if the inode is in 482f2ff0eeSFilipe Manana * the log or not (last_trans, last_sub_trans, last_log_commit, 492f2ff0eeSFilipe Manana * logged_trans). 502f2ff0eeSFilipe Manana */ 519e0baf60SJosef Bacik spinlock_t lock; 529e0baf60SJosef Bacik 53d352ac68SChris Mason /* the extent_tree has caches of all the extent mappings to disk */ 54a52d9a80SChris Mason struct extent_map_tree extent_tree; 55d352ac68SChris Mason 56d352ac68SChris Mason /* the io_tree does range state (DIRTY, LOCKED etc) */ 57d1310b2eSChris Mason struct extent_io_tree io_tree; 58d352ac68SChris Mason 59d352ac68SChris Mason /* special utility tree used to record which mirrors have already been 60d352ac68SChris Mason * tried when checksums fail for a given block 61d352ac68SChris Mason */ 627e38326fSChris Mason struct extent_io_tree io_failure_tree; 63d352ac68SChris Mason 6441a2ee75SJosef Bacik /* 6541a2ee75SJosef Bacik * Keep track of where the inode has extent items mapped in order to 6641a2ee75SJosef Bacik * make sure the i_size adjustments are accurate 6741a2ee75SJosef Bacik */ 6841a2ee75SJosef Bacik struct extent_io_tree file_extent_tree; 6941a2ee75SJosef Bacik 70d352ac68SChris Mason /* held while logging the inode in tree-log.c */ 71e02119d5SChris Mason struct mutex log_mutex; 72d352ac68SChris Mason 73d352ac68SChris Mason /* used to order data wrt metadata */ 74e6dcd2dcSChris Mason struct btrfs_ordered_inode_tree ordered_tree; 7515ee9bc7SJosef Bacik 76d352ac68SChris Mason /* list of all the delalloc inodes in the FS. There are times we need 77d352ac68SChris Mason * to write all the delalloc pages to disk, and this list is used 78d352ac68SChris Mason * to walk them all. 79d352ac68SChris Mason */ 80ea8c2819SChris Mason struct list_head delalloc_inodes; 81ea8c2819SChris Mason 825d4f98a2SYan Zheng /* node for the red-black tree that links inodes in subvolume root */ 835d4f98a2SYan Zheng struct rb_node rb_node; 845d4f98a2SYan Zheng 8572ac3c0dSJosef Bacik unsigned long runtime_flags; 8672ac3c0dSJosef Bacik 879c931c5aSNathaniel Yazdani /* Keep track of who's O_SYNC/fsyncing currently */ 88b812ce28SJosef Bacik atomic_t sync_writers; 89b812ce28SJosef Bacik 90d352ac68SChris Mason /* full 64 bit generation number, struct vfs_inode doesn't have a big 91d352ac68SChris Mason * enough field for this. 92d352ac68SChris Mason */ 93e02119d5SChris Mason u64 generation; 94e02119d5SChris Mason 9515ee9bc7SJosef Bacik /* 9615ee9bc7SJosef Bacik * transid of the trans_handle that last modified this inode 9715ee9bc7SJosef Bacik */ 9815ee9bc7SJosef Bacik u64 last_trans; 99257c62e1SChris Mason 100257c62e1SChris Mason /* 101e02119d5SChris Mason * transid that last logged this inode 102e02119d5SChris Mason */ 103e02119d5SChris Mason u64 logged_trans; 10449eb7e46SChris Mason 105bb14a59bSMiao Xie /* 106bb14a59bSMiao Xie * log transid when this inode was last modified 107bb14a59bSMiao Xie */ 108bb14a59bSMiao Xie int last_sub_trans; 109bb14a59bSMiao Xie 110bb14a59bSMiao Xie /* a local copy of root's last_log_commit */ 111bb14a59bSMiao Xie int last_log_commit; 112bb14a59bSMiao Xie 113d352ac68SChris Mason /* total number of bytes pending delalloc, used by stat to calc the 114d352ac68SChris Mason * real block usage of the file 115d352ac68SChris Mason */ 1169069218dSChris Mason u64 delalloc_bytes; 117d352ac68SChris Mason 118d352ac68SChris Mason /* 119a7e3b975SFilipe Manana * Total number of bytes pending delalloc that fall within a file 120a7e3b975SFilipe Manana * range that is either a hole or beyond EOF (and no prealloc extent 121a7e3b975SFilipe Manana * exists in the range). This is always <= delalloc_bytes. 122a7e3b975SFilipe Manana */ 123a7e3b975SFilipe Manana u64 new_delalloc_bytes; 124a7e3b975SFilipe Manana 125a7e3b975SFilipe Manana /* 12647059d93SWang Shilong * total number of bytes pending defrag, used by stat to check whether 12747059d93SWang Shilong * it needs COW. 12847059d93SWang Shilong */ 12947059d93SWang Shilong u64 defrag_bytes; 13047059d93SWang Shilong 13147059d93SWang Shilong /* 132d352ac68SChris Mason * the size of the file stored in the metadata on disk. data=ordered 133d352ac68SChris Mason * means the in-memory i_size might be larger than the size on disk 134d352ac68SChris Mason * because not all the blocks are written yet. 135d352ac68SChris Mason */ 136dbe674a9SChris Mason u64 disk_i_size; 137d352ac68SChris Mason 138aec7477bSJosef Bacik /* 139aec7477bSJosef Bacik * if this is a directory then index_cnt is the counter for the index 140aec7477bSJosef Bacik * number for new files that are created 141aec7477bSJosef Bacik */ 142aec7477bSJosef Bacik u64 index_cnt; 143d352ac68SChris Mason 14467de1176SMiao Xie /* Cache the directory index number to speed the dir/file remove */ 14567de1176SMiao Xie u64 dir_index; 14667de1176SMiao Xie 14712fcfd22SChris Mason /* the fsync log has some corner cases that mean we have to check 14812fcfd22SChris Mason * directories to see if any unlinks have been done before 14912fcfd22SChris Mason * the directory was logged. See tree-log.c for all the 15012fcfd22SChris Mason * details 15112fcfd22SChris Mason */ 15212fcfd22SChris Mason u64 last_unlink_trans; 15312fcfd22SChris Mason 1547709cde3SJosef Bacik /* 1557709cde3SJosef Bacik * Number of bytes outstanding that are going to need csums. This is 1567709cde3SJosef Bacik * used in ENOSPC accounting. 1577709cde3SJosef Bacik */ 1587709cde3SJosef Bacik u64 csum_bytes; 1597709cde3SJosef Bacik 160f1bdcc0aSJosef Bacik /* flags field from the on disk inode */ 161f1bdcc0aSJosef Bacik u32 flags; 162f1bdcc0aSJosef Bacik 1635a3f23d5SChris Mason /* 16432c00affSJosef Bacik * Counters to keep track of the number of extent item's we may use due 16532c00affSJosef Bacik * to delalloc and such. outstanding_extents is the number of extent 16632c00affSJosef Bacik * items we think we'll end up using, and reserved_extents is the number 16732c00affSJosef Bacik * of extent items we've reserved metadata for. 1689ed74f2dSJosef Bacik */ 1699e0baf60SJosef Bacik unsigned outstanding_extents; 17069fe2d75SJosef Bacik 17169fe2d75SJosef Bacik struct btrfs_block_rsv block_rsv; 1729ed74f2dSJosef Bacik 1739ed74f2dSJosef Bacik /* 174b52aa8c9SDavid Sterba * Cached values of inode properties 1751e701a32SChris Mason */ 176b52aa8c9SDavid Sterba unsigned prop_compress; /* per-file compression algorithm */ 177eec63c65SDavid Sterba /* 178eec63c65SDavid Sterba * Force compression on the file using the defrag ioctl, could be 179eec63c65SDavid Sterba * different from prop_compress and takes precedence if set 180eec63c65SDavid Sterba */ 181eec63c65SDavid Sterba unsigned defrag_compress; 1821e701a32SChris Mason 18316cdcec7SMiao Xie struct btrfs_delayed_node *delayed_node; 18416cdcec7SMiao Xie 1859cc97d64Schandan r /* File creation time. */ 186d3c6be6fSArnd Bergmann struct timespec64 i_otime; 1879cc97d64Schandan r 1888089fe62SDavid Sterba /* Hook into fs_info->delayed_iputs */ 1898089fe62SDavid Sterba struct list_head delayed_iput; 1908089fe62SDavid Sterba 1915f9a8a51SFilipe Manana /* 1925f9a8a51SFilipe Manana * To avoid races between lockless (i_mutex not held) direct IO writes 1935f9a8a51SFilipe Manana * and concurrent fsync requests. Direct IO writes must acquire read 1945f9a8a51SFilipe Manana * access on this semaphore for creating an extent map and its 1955f9a8a51SFilipe Manana * corresponding ordered extent. The fast fsync path must acquire write 1965f9a8a51SFilipe Manana * access on this semaphore before it collects ordered extents and 1975f9a8a51SFilipe Manana * extent maps. 1985f9a8a51SFilipe Manana */ 1995f9a8a51SFilipe Manana struct rw_semaphore dio_sem; 2005f9a8a51SFilipe Manana 201d352ac68SChris Mason struct inode vfs_inode; 2022c90e5d6SChris Mason }; 203dbe674a9SChris Mason 2049a35b637SJeff Mahoney static inline struct btrfs_inode *BTRFS_I(const struct inode *inode) 2052c90e5d6SChris Mason { 2062c90e5d6SChris Mason return container_of(inode, struct btrfs_inode, vfs_inode); 2072c90e5d6SChris Mason } 2082c90e5d6SChris Mason 209778ba82bSFilipe David Borba Manana static inline unsigned long btrfs_inode_hash(u64 objectid, 210778ba82bSFilipe David Borba Manana const struct btrfs_root *root) 211778ba82bSFilipe David Borba Manana { 2124fd786e6SMisono Tomohiro u64 h = objectid ^ (root->root_key.objectid * GOLDEN_RATIO_PRIME); 213778ba82bSFilipe David Borba Manana 214778ba82bSFilipe David Borba Manana #if BITS_PER_LONG == 32 215778ba82bSFilipe David Borba Manana h = (h >> 32) ^ (h & 0xffffffff); 216778ba82bSFilipe David Borba Manana #endif 217778ba82bSFilipe David Borba Manana 218778ba82bSFilipe David Borba Manana return (unsigned long)h; 219778ba82bSFilipe David Borba Manana } 220778ba82bSFilipe David Borba Manana 221778ba82bSFilipe David Borba Manana static inline void btrfs_insert_inode_hash(struct inode *inode) 222778ba82bSFilipe David Borba Manana { 223778ba82bSFilipe David Borba Manana unsigned long h = btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root); 224778ba82bSFilipe David Borba Manana 225778ba82bSFilipe David Borba Manana __insert_inode_hash(inode, h); 226778ba82bSFilipe David Borba Manana } 227778ba82bSFilipe David Borba Manana 2289a35b637SJeff Mahoney static inline u64 btrfs_ino(const struct btrfs_inode *inode) 22933345d01SLi Zefan { 2304a0cc7caSNikolay Borisov u64 ino = inode->location.objectid; 23133345d01SLi Zefan 23214c7cca7SLiu Bo /* 23314c7cca7SLiu Bo * !ino: btree_inode 23414c7cca7SLiu Bo * type == BTRFS_ROOT_ITEM_KEY: subvol dir 23514c7cca7SLiu Bo */ 2364a0cc7caSNikolay Borisov if (!ino || inode->location.type == BTRFS_ROOT_ITEM_KEY) 2374a0cc7caSNikolay Borisov ino = inode->vfs_inode.i_ino; 23833345d01SLi Zefan return ino; 23933345d01SLi Zefan } 24033345d01SLi Zefan 2416ef06d27SNikolay Borisov static inline void btrfs_i_size_write(struct btrfs_inode *inode, u64 size) 242dbe674a9SChris Mason { 2436ef06d27SNikolay Borisov i_size_write(&inode->vfs_inode, size); 2446ef06d27SNikolay Borisov inode->disk_i_size = size; 245dbe674a9SChris Mason } 246dbe674a9SChris Mason 24770ddc553SNikolay Borisov static inline bool btrfs_is_free_space_inode(struct btrfs_inode *inode) 2482cf8572dSChris Mason { 24970ddc553SNikolay Borisov struct btrfs_root *root = inode->root; 25083eea1f1SLiu Bo 25151a8cf9dSLiu Bo if (root == root->fs_info->tree_root && 25270ddc553SNikolay Borisov btrfs_ino(inode) != BTRFS_BTREE_INODE_OBJECTID) 25351a8cf9dSLiu Bo return true; 25470ddc553SNikolay Borisov if (inode->location.objectid == BTRFS_FREE_INO_OBJECTID) 2552cf8572dSChris Mason return true; 2562cf8572dSChris Mason return false; 2572cf8572dSChris Mason } 2582cf8572dSChris Mason 25906f2548fSNikolay Borisov static inline bool is_data_inode(struct inode *inode) 26006f2548fSNikolay Borisov { 26106f2548fSNikolay Borisov return btrfs_ino(BTRFS_I(inode)) != BTRFS_BTREE_INODE_OBJECTID; 26206f2548fSNikolay Borisov } 26306f2548fSNikolay Borisov 2648b62f87bSJosef Bacik static inline void btrfs_mod_outstanding_extents(struct btrfs_inode *inode, 2658b62f87bSJosef Bacik int mod) 2668b62f87bSJosef Bacik { 2678b62f87bSJosef Bacik lockdep_assert_held(&inode->lock); 2688b62f87bSJosef Bacik inode->outstanding_extents += mod; 2698b62f87bSJosef Bacik if (btrfs_is_free_space_inode(inode)) 2708b62f87bSJosef Bacik return; 271dd48d407SJosef Bacik trace_btrfs_inode_mod_outstanding_extents(inode->root, btrfs_ino(inode), 272dd48d407SJosef Bacik mod); 2738b62f87bSJosef Bacik } 2748b62f87bSJosef Bacik 2750f8939b8SNikolay Borisov static inline int btrfs_inode_in_log(struct btrfs_inode *inode, u64 generation) 27622ee6985SJosef Bacik { 2772f2ff0eeSFilipe Manana int ret = 0; 2782f2ff0eeSFilipe Manana 2790f8939b8SNikolay Borisov spin_lock(&inode->lock); 2800f8939b8SNikolay Borisov if (inode->logged_trans == generation && 2810f8939b8SNikolay Borisov inode->last_sub_trans <= inode->last_log_commit && 2820f8939b8SNikolay Borisov inode->last_sub_trans <= inode->root->last_log_commit) { 283125c4cf9SFilipe Manana /* 284125c4cf9SFilipe Manana * After a ranged fsync we might have left some extent maps 285125c4cf9SFilipe Manana * (that fall outside the fsync's range). So return false 286125c4cf9SFilipe Manana * here if the list isn't empty, to make sure btrfs_log_inode() 287125c4cf9SFilipe Manana * will be called and process those extent maps. 288125c4cf9SFilipe Manana */ 289125c4cf9SFilipe Manana smp_mb(); 2900f8939b8SNikolay Borisov if (list_empty(&inode->extent_tree.modified_extents)) 2912f2ff0eeSFilipe Manana ret = 1; 292125c4cf9SFilipe Manana } 2930f8939b8SNikolay Borisov spin_unlock(&inode->lock); 2942f2ff0eeSFilipe Manana return ret; 29522ee6985SJosef Bacik } 29622ee6985SJosef Bacik 297c1dc0896SMiao Xie #define BTRFS_DIO_ORIG_BIO_SUBMITTED 0x1 298c1dc0896SMiao Xie 299facc8a22SMiao Xie struct btrfs_dio_private { 300facc8a22SMiao Xie struct inode *inode; 301c1dc0896SMiao Xie unsigned long flags; 302facc8a22SMiao Xie u64 logical_offset; 303facc8a22SMiao Xie u64 disk_bytenr; 304facc8a22SMiao Xie u64 bytes; 305facc8a22SMiao Xie 306*e3b318d1SOmar Sandoval /* 307*e3b318d1SOmar Sandoval * References to this structure. There is one reference per in-flight 308*e3b318d1SOmar Sandoval * bio plus one while we're still setting up. 309*e3b318d1SOmar Sandoval */ 310*e3b318d1SOmar Sandoval refcount_t refs; 311facc8a22SMiao Xie 312facc8a22SMiao Xie /* IO errors */ 313facc8a22SMiao Xie int errors; 314facc8a22SMiao Xie 315facc8a22SMiao Xie /* orig_bio is our btrfs_io_bio */ 316facc8a22SMiao Xie struct bio *orig_bio; 317facc8a22SMiao Xie 318facc8a22SMiao Xie /* dio_bio came from fs/direct-io.c */ 319facc8a22SMiao Xie struct bio *dio_bio; 320c1dc0896SMiao Xie 321c1dc0896SMiao Xie /* 32201327610SNicholas D Steeves * The original bio may be split to several sub-bios, this is 323c1dc0896SMiao Xie * done during endio of sub-bios 324c1dc0896SMiao Xie */ 3254e4cbee9SChristoph Hellwig blk_status_t (*subio_endio)(struct inode *, struct btrfs_io_bio *, 3264e4cbee9SChristoph Hellwig blk_status_t); 327facc8a22SMiao Xie }; 328facc8a22SMiao Xie 3292e60a51eSMiao Xie /* 3302e60a51eSMiao Xie * Disable DIO read nolock optimization, so new dio readers will be forced 3312e60a51eSMiao Xie * to grab i_mutex. It is used to avoid the endless truncate due to 3322e60a51eSMiao Xie * nonlocked dio read. 3332e60a51eSMiao Xie */ 334abcefb1eSNikolay Borisov static inline void btrfs_inode_block_unlocked_dio(struct btrfs_inode *inode) 3352e60a51eSMiao Xie { 336abcefb1eSNikolay Borisov set_bit(BTRFS_INODE_READDIO_NEED_LOCK, &inode->runtime_flags); 3372e60a51eSMiao Xie smp_mb(); 3382e60a51eSMiao Xie } 3392e60a51eSMiao Xie 3400b581701SNikolay Borisov static inline void btrfs_inode_resume_unlocked_dio(struct btrfs_inode *inode) 3412e60a51eSMiao Xie { 3424e857c58SPeter Zijlstra smp_mb__before_atomic(); 3430b581701SNikolay Borisov clear_bit(BTRFS_INODE_READDIO_NEED_LOCK, &inode->runtime_flags); 3442e60a51eSMiao Xie } 3452e60a51eSMiao Xie 3467ebc7e5fSJohannes Thumshirn /* Array of bytes with variable length, hexadecimal format 0x1234 */ 3477ebc7e5fSJohannes Thumshirn #define CSUM_FMT "0x%*phN" 3487ebc7e5fSJohannes Thumshirn #define CSUM_FMT_VALUE(size, bytes) size, bytes 3497ebc7e5fSJohannes Thumshirn 3500970a22eSNikolay Borisov static inline void btrfs_print_data_csum_error(struct btrfs_inode *inode, 351ea41d6b2SJohannes Thumshirn u64 logical_start, u8 *csum, u8 *csum_expected, int mirror_num) 3526f6b643eSQu Wenruo { 3530970a22eSNikolay Borisov struct btrfs_root *root = inode->root; 3547ebc7e5fSJohannes Thumshirn struct btrfs_super_block *sb = root->fs_info->super_copy; 3557ebc7e5fSJohannes Thumshirn const u16 csum_size = btrfs_super_csum_size(sb); 3566f6b643eSQu Wenruo 3576f6b643eSQu Wenruo /* Output minus objectid, which is more meaningful */ 3584fd786e6SMisono Tomohiro if (root->root_key.objectid >= BTRFS_LAST_FREE_OBJECTID) 3596f6b643eSQu Wenruo btrfs_warn_rl(root->fs_info, 3607ebc7e5fSJohannes Thumshirn "csum failed root %lld ino %lld off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d", 3614fd786e6SMisono Tomohiro root->root_key.objectid, btrfs_ino(inode), 3627ebc7e5fSJohannes Thumshirn logical_start, 363ea41d6b2SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, csum), 364ea41d6b2SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, csum_expected), 3657ebc7e5fSJohannes Thumshirn mirror_num); 3666f6b643eSQu Wenruo else 3676f6b643eSQu Wenruo btrfs_warn_rl(root->fs_info, 3687ebc7e5fSJohannes Thumshirn "csum failed root %llu ino %llu off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d", 3694fd786e6SMisono Tomohiro root->root_key.objectid, btrfs_ino(inode), 3707ebc7e5fSJohannes Thumshirn logical_start, 371ea41d6b2SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, csum), 372ea41d6b2SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, csum_expected), 3737ebc7e5fSJohannes Thumshirn mirror_num); 3746f6b643eSQu Wenruo } 3756f6b643eSQu Wenruo 3762c90e5d6SChris Mason #endif 377