Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Copyright (C) 2007 Oracle.  All rights reserved.
0004  */
0005 
0006 #ifndef BTRFS_INODE_H
0007 #define BTRFS_INODE_H
0008 
0009 #include <linux/hash.h>
0010 #include <linux/refcount.h>
0011 #include "extent_map.h"
0012 #include "extent_io.h"
0013 #include "ordered-data.h"
0014 #include "delayed-inode.h"
0015 
0016 /*
0017  * Since we search a directory based on f_pos (struct dir_context::pos) we have
0018  * to start at 2 since '.' and '..' have f_pos of 0 and 1 respectively, so
0019  * everybody else has to start at 2 (see btrfs_real_readdir() and dir_emit_dots()).
0020  */
0021 #define BTRFS_DIR_START_INDEX 2
0022 
0023 /*
0024  * ordered_data_close is set by truncate when a file that used
0025  * to have good data has been truncated to zero.  When it is set
0026  * the btrfs file release call will add this inode to the
0027  * ordered operations list so that we make sure to flush out any
0028  * new data the application may have written before commit.
0029  */
0030 enum {
0031     BTRFS_INODE_FLUSH_ON_CLOSE,
0032     BTRFS_INODE_DUMMY,
0033     BTRFS_INODE_IN_DEFRAG,
0034     BTRFS_INODE_HAS_ASYNC_EXTENT,
0035      /*
0036       * Always set under the VFS' inode lock, otherwise it can cause races
0037       * during fsync (we start as a fast fsync and then end up in a full
0038       * fsync racing with ordered extent completion).
0039       */
0040     BTRFS_INODE_NEEDS_FULL_SYNC,
0041     BTRFS_INODE_COPY_EVERYTHING,
0042     BTRFS_INODE_IN_DELALLOC_LIST,
0043     BTRFS_INODE_HAS_PROPS,
0044     BTRFS_INODE_SNAPSHOT_FLUSH,
0045     /*
0046      * Set and used when logging an inode and it serves to signal that an
0047      * inode does not have xattrs, so subsequent fsyncs can avoid searching
0048      * for xattrs to log. This bit must be cleared whenever a xattr is added
0049      * to an inode.
0050      */
0051     BTRFS_INODE_NO_XATTRS,
0052     /*
0053      * Set when we are in a context where we need to start a transaction and
0054      * have dirty pages with the respective file range locked. This is to
0055      * ensure that when reserving space for the transaction, if we are low
0056      * on available space and need to flush delalloc, we will not flush
0057      * delalloc for this inode, because that could result in a deadlock (on
0058      * the file range, inode's io_tree).
0059      */
0060     BTRFS_INODE_NO_DELALLOC_FLUSH,
0061     /*
0062      * Set when we are working on enabling verity for a file. Computing and
0063      * writing the whole Merkle tree can take a while so we want to prevent
0064      * races where two separate tasks attempt to simultaneously start verity
0065      * on the same file.
0066      */
0067     BTRFS_INODE_VERITY_IN_PROGRESS,
0068 };
0069 
0070 /* in memory btrfs inode */
0071 struct btrfs_inode {
0072     /* which subvolume this inode belongs to */
0073     struct btrfs_root *root;
0074 
0075     /* key used to find this inode on disk.  This is used by the code
0076      * to read in roots of subvolumes
0077      */
0078     struct btrfs_key location;
0079 
0080     /*
0081      * Lock for counters and all fields used to determine if the inode is in
0082      * the log or not (last_trans, last_sub_trans, last_log_commit,
0083      * logged_trans), to access/update new_delalloc_bytes and to update the
0084      * VFS' inode number of bytes used.
0085      */
0086     spinlock_t lock;
0087 
0088     /* the extent_tree has caches of all the extent mappings to disk */
0089     struct extent_map_tree extent_tree;
0090 
0091     /* the io_tree does range state (DIRTY, LOCKED etc) */
0092     struct extent_io_tree io_tree;
0093 
0094     /* special utility tree used to record which mirrors have already been
0095      * tried when checksums fail for a given block
0096      */
0097     struct extent_io_tree io_failure_tree;
0098 
0099     /*
0100      * Keep track of where the inode has extent items mapped in order to
0101      * make sure the i_size adjustments are accurate
0102      */
0103     struct extent_io_tree file_extent_tree;
0104 
0105     /* held while logging the inode in tree-log.c */
0106     struct mutex log_mutex;
0107 
0108     /* used to order data wrt metadata */
0109     struct btrfs_ordered_inode_tree ordered_tree;
0110 
0111     /* list of all the delalloc inodes in the FS.  There are times we need
0112      * to write all the delalloc pages to disk, and this list is used
0113      * to walk them all.
0114      */
0115     struct list_head delalloc_inodes;
0116 
0117     /* node for the red-black tree that links inodes in subvolume root */
0118     struct rb_node rb_node;
0119 
0120     unsigned long runtime_flags;
0121 
0122     /* Keep track of who's O_SYNC/fsyncing currently */
0123     atomic_t sync_writers;
0124 
0125     /* full 64 bit generation number, struct vfs_inode doesn't have a big
0126      * enough field for this.
0127      */
0128     u64 generation;
0129 
0130     /*
0131      * transid of the trans_handle that last modified this inode
0132      */
0133     u64 last_trans;
0134 
0135     /*
0136      * transid that last logged this inode
0137      */
0138     u64 logged_trans;
0139 
0140     /*
0141      * log transid when this inode was last modified
0142      */
0143     int last_sub_trans;
0144 
0145     /* a local copy of root's last_log_commit */
0146     int last_log_commit;
0147 
0148     /*
0149      * Total number of bytes pending delalloc, used by stat to calculate the
0150      * real block usage of the file. This is used only for files.
0151      */
0152     u64 delalloc_bytes;
0153 
0154     union {
0155         /*
0156          * Total number of bytes pending delalloc that fall within a file
0157          * range that is either a hole or beyond EOF (and no prealloc extent
0158          * exists in the range). This is always <= delalloc_bytes and this
0159          * is used only for files.
0160          */
0161         u64 new_delalloc_bytes;
0162         /*
0163          * The offset of the last dir index key that was logged.
0164          * This is used only for directories.
0165          */
0166         u64 last_dir_index_offset;
0167     };
0168 
0169     /*
0170      * total number of bytes pending defrag, used by stat to check whether
0171      * it needs COW.
0172      */
0173     u64 defrag_bytes;
0174 
0175     /*
0176      * the size of the file stored in the metadata on disk.  data=ordered
0177      * means the in-memory i_size might be larger than the size on disk
0178      * because not all the blocks are written yet.
0179      */
0180     u64 disk_i_size;
0181 
0182     /*
0183      * If this is a directory then index_cnt is the counter for the index
0184      * number for new files that are created. For an empty directory, this
0185      * must be initialized to BTRFS_DIR_START_INDEX.
0186      */
0187     u64 index_cnt;
0188 
0189     /* Cache the directory index number to speed the dir/file remove */
0190     u64 dir_index;
0191 
0192     /* the fsync log has some corner cases that mean we have to check
0193      * directories to see if any unlinks have been done before
0194      * the directory was logged.  See tree-log.c for all the
0195      * details
0196      */
0197     u64 last_unlink_trans;
0198 
0199     /*
0200      * The id/generation of the last transaction where this inode was
0201      * either the source or the destination of a clone/dedupe operation.
0202      * Used when logging an inode to know if there are shared extents that
0203      * need special care when logging checksum items, to avoid duplicate
0204      * checksum items in a log (which can lead to a corruption where we end
0205      * up with missing checksum ranges after log replay).
0206      * Protected by the vfs inode lock.
0207      */
0208     u64 last_reflink_trans;
0209 
0210     /*
0211      * Number of bytes outstanding that are going to need csums.  This is
0212      * used in ENOSPC accounting.
0213      */
0214     u64 csum_bytes;
0215 
0216     /* Backwards incompatible flags, lower half of inode_item::flags  */
0217     u32 flags;
0218     /* Read-only compatibility flags, upper half of inode_item::flags */
0219     u32 ro_flags;
0220 
0221     /*
0222      * Counters to keep track of the number of extent item's we may use due
0223      * to delalloc and such.  outstanding_extents is the number of extent
0224      * items we think we'll end up using, and reserved_extents is the number
0225      * of extent items we've reserved metadata for.
0226      */
0227     unsigned outstanding_extents;
0228 
0229     struct btrfs_block_rsv block_rsv;
0230 
0231     /*
0232      * Cached values of inode properties
0233      */
0234     unsigned prop_compress;     /* per-file compression algorithm */
0235     /*
0236      * Force compression on the file using the defrag ioctl, could be
0237      * different from prop_compress and takes precedence if set
0238      */
0239     unsigned defrag_compress;
0240 
0241     struct btrfs_delayed_node *delayed_node;
0242 
0243     /* File creation time. */
0244     struct timespec64 i_otime;
0245 
0246     /* Hook into fs_info->delayed_iputs */
0247     struct list_head delayed_iput;
0248 
0249     struct rw_semaphore i_mmap_lock;
0250     struct inode vfs_inode;
0251 };
0252 
0253 static inline u32 btrfs_inode_sectorsize(const struct btrfs_inode *inode)
0254 {
0255     return inode->root->fs_info->sectorsize;
0256 }
0257 
0258 static inline struct btrfs_inode *BTRFS_I(const struct inode *inode)
0259 {
0260     return container_of(inode, struct btrfs_inode, vfs_inode);
0261 }
0262 
0263 static inline unsigned long btrfs_inode_hash(u64 objectid,
0264                          const struct btrfs_root *root)
0265 {
0266     u64 h = objectid ^ (root->root_key.objectid * GOLDEN_RATIO_PRIME);
0267 
0268 #if BITS_PER_LONG == 32
0269     h = (h >> 32) ^ (h & 0xffffffff);
0270 #endif
0271 
0272     return (unsigned long)h;
0273 }
0274 
0275 static inline void btrfs_insert_inode_hash(struct inode *inode)
0276 {
0277     unsigned long h = btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root);
0278 
0279     __insert_inode_hash(inode, h);
0280 }
0281 
0282 #if BITS_PER_LONG == 32
0283 
0284 /*
0285  * On 32 bit systems the i_ino of struct inode is 32 bits (unsigned long), so
0286  * we use the inode's location objectid which is a u64 to avoid truncation.
0287  */
0288 static inline u64 btrfs_ino(const struct btrfs_inode *inode)
0289 {
0290     u64 ino = inode->location.objectid;
0291 
0292     /* type == BTRFS_ROOT_ITEM_KEY: subvol dir */
0293     if (inode->location.type == BTRFS_ROOT_ITEM_KEY)
0294         ino = inode->vfs_inode.i_ino;
0295     return ino;
0296 }
0297 
0298 #else
0299 
0300 static inline u64 btrfs_ino(const struct btrfs_inode *inode)
0301 {
0302     return inode->vfs_inode.i_ino;
0303 }
0304 
0305 #endif
0306 
0307 static inline void btrfs_i_size_write(struct btrfs_inode *inode, u64 size)
0308 {
0309     i_size_write(&inode->vfs_inode, size);
0310     inode->disk_i_size = size;
0311 }
0312 
0313 static inline bool btrfs_is_free_space_inode(struct btrfs_inode *inode)
0314 {
0315     struct btrfs_root *root = inode->root;
0316 
0317     if (root == root->fs_info->tree_root &&
0318         btrfs_ino(inode) != BTRFS_BTREE_INODE_OBJECTID)
0319         return true;
0320 
0321     return false;
0322 }
0323 
0324 static inline bool is_data_inode(struct inode *inode)
0325 {
0326     return btrfs_ino(BTRFS_I(inode)) != BTRFS_BTREE_INODE_OBJECTID;
0327 }
0328 
0329 static inline void btrfs_mod_outstanding_extents(struct btrfs_inode *inode,
0330                          int mod)
0331 {
0332     lockdep_assert_held(&inode->lock);
0333     inode->outstanding_extents += mod;
0334     if (btrfs_is_free_space_inode(inode))
0335         return;
0336     trace_btrfs_inode_mod_outstanding_extents(inode->root, btrfs_ino(inode),
0337                           mod);
0338 }
0339 
0340 /*
0341  * Called every time after doing a buffered, direct IO or memory mapped write.
0342  *
0343  * This is to ensure that if we write to a file that was previously fsynced in
0344  * the current transaction, then try to fsync it again in the same transaction,
0345  * we will know that there were changes in the file and that it needs to be
0346  * logged.
0347  */
0348 static inline void btrfs_set_inode_last_sub_trans(struct btrfs_inode *inode)
0349 {
0350     spin_lock(&inode->lock);
0351     inode->last_sub_trans = inode->root->log_transid;
0352     spin_unlock(&inode->lock);
0353 }
0354 
0355 /*
0356  * Should be called while holding the inode's VFS lock in exclusive mode or in a
0357  * context where no one else can access the inode concurrently (during inode
0358  * creation or when loading an inode from disk).
0359  */
0360 static inline void btrfs_set_inode_full_sync(struct btrfs_inode *inode)
0361 {
0362     set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
0363     /*
0364      * The inode may have been part of a reflink operation in the last
0365      * transaction that modified it, and then a fsync has reset the
0366      * last_reflink_trans to avoid subsequent fsyncs in the same
0367      * transaction to do unnecessary work. So update last_reflink_trans
0368      * to the last_trans value (we have to be pessimistic and assume a
0369      * reflink happened).
0370      *
0371      * The ->last_trans is protected by the inode's spinlock and we can
0372      * have a concurrent ordered extent completion update it. Also set
0373      * last_reflink_trans to ->last_trans only if the former is less than
0374      * the later, because we can be called in a context where
0375      * last_reflink_trans was set to the current transaction generation
0376      * while ->last_trans was not yet updated in the current transaction,
0377      * and therefore has a lower value.
0378      */
0379     spin_lock(&inode->lock);
0380     if (inode->last_reflink_trans < inode->last_trans)
0381         inode->last_reflink_trans = inode->last_trans;
0382     spin_unlock(&inode->lock);
0383 }
0384 
0385 static inline bool btrfs_inode_in_log(struct btrfs_inode *inode, u64 generation)
0386 {
0387     bool ret = false;
0388 
0389     spin_lock(&inode->lock);
0390     if (inode->logged_trans == generation &&
0391         inode->last_sub_trans <= inode->last_log_commit &&
0392         inode->last_sub_trans <= inode->root->last_log_commit)
0393         ret = true;
0394     spin_unlock(&inode->lock);
0395     return ret;
0396 }
0397 
0398 /*
0399  * Check if the inode has flags compatible with compression
0400  */
0401 static inline bool btrfs_inode_can_compress(const struct btrfs_inode *inode)
0402 {
0403     if (inode->flags & BTRFS_INODE_NODATACOW ||
0404         inode->flags & BTRFS_INODE_NODATASUM)
0405         return false;
0406     return true;
0407 }
0408 
0409 /*
0410  * btrfs_inode_item stores flags in a u64, btrfs_inode stores them in two
0411  * separate u32s. These two functions convert between the two representations.
0412  */
0413 static inline u64 btrfs_inode_combine_flags(u32 flags, u32 ro_flags)
0414 {
0415     return (flags | ((u64)ro_flags << 32));
0416 }
0417 
0418 static inline void btrfs_inode_split_flags(u64 inode_item_flags,
0419                        u32 *flags, u32 *ro_flags)
0420 {
0421     *flags = (u32)inode_item_flags;
0422     *ro_flags = (u32)(inode_item_flags >> 32);
0423 }
0424 
0425 /* Array of bytes with variable length, hexadecimal format 0x1234 */
0426 #define CSUM_FMT                "0x%*phN"
0427 #define CSUM_FMT_VALUE(size, bytes)     size, bytes
0428 
0429 static inline void btrfs_print_data_csum_error(struct btrfs_inode *inode,
0430         u64 logical_start, u8 *csum, u8 *csum_expected, int mirror_num)
0431 {
0432     struct btrfs_root *root = inode->root;
0433     const u32 csum_size = root->fs_info->csum_size;
0434 
0435     /* Output minus objectid, which is more meaningful */
0436     if (root->root_key.objectid >= BTRFS_LAST_FREE_OBJECTID)
0437         btrfs_warn_rl(root->fs_info,
0438 "csum failed root %lld ino %lld off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d",
0439             root->root_key.objectid, btrfs_ino(inode),
0440             logical_start,
0441             CSUM_FMT_VALUE(csum_size, csum),
0442             CSUM_FMT_VALUE(csum_size, csum_expected),
0443             mirror_num);
0444     else
0445         btrfs_warn_rl(root->fs_info,
0446 "csum failed root %llu ino %llu off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d",
0447             root->root_key.objectid, btrfs_ino(inode),
0448             logical_start,
0449             CSUM_FMT_VALUE(csum_size, csum),
0450             CSUM_FMT_VALUE(csum_size, csum_expected),
0451             mirror_num);
0452 }
0453 
0454 #endif