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_ORDERED_DATA_H
0007 #define BTRFS_ORDERED_DATA_H
0008 
0009 /* one of these per inode */
0010 struct btrfs_ordered_inode_tree {
0011     spinlock_t lock;
0012     struct rb_root tree;
0013     struct rb_node *last;
0014 };
0015 
0016 struct btrfs_ordered_sum {
0017     /* bytenr is the start of this extent on disk */
0018     u64 bytenr;
0019 
0020     /*
0021      * this is the length in bytes covered by the sums array below.
0022      */
0023     int len;
0024     struct list_head list;
0025     /* last field is a variable length array of csums */
0026     u8 sums[];
0027 };
0028 
0029 /*
0030  * Bits for btrfs_ordered_extent::flags.
0031  *
0032  * BTRFS_ORDERED_IO_DONE is set when all of the blocks are written.
0033  * It is used to make sure metadata is inserted into the tree only once
0034  * per extent.
0035  *
0036  * BTRFS_ORDERED_COMPLETE is set when the extent is removed from the
0037  * rbtree, just before waking any waiters.  It is used to indicate the
0038  * IO is done and any metadata is inserted into the tree.
0039  */
0040 enum {
0041     /*
0042      * Different types for ordered extents, one and only one of the 4 types
0043      * need to be set when creating ordered extent.
0044      *
0045      * REGULAR: For regular non-compressed COW write
0046      * NOCOW:   For NOCOW write into existing non-hole extent
0047      * PREALLOC:    For NOCOW write into preallocated extent
0048      * COMPRESSED:  For compressed COW write
0049      */
0050     BTRFS_ORDERED_REGULAR,
0051     BTRFS_ORDERED_NOCOW,
0052     BTRFS_ORDERED_PREALLOC,
0053     BTRFS_ORDERED_COMPRESSED,
0054 
0055     /*
0056      * Extra bit for direct io, can only be set for
0057      * REGULAR/NOCOW/PREALLOC. No direct io for compressed extent.
0058      */
0059     BTRFS_ORDERED_DIRECT,
0060 
0061     /* Extra status bits for ordered extents */
0062 
0063     /* set when all the pages are written */
0064     BTRFS_ORDERED_IO_DONE,
0065     /* set when removed from the tree */
0066     BTRFS_ORDERED_COMPLETE,
0067     /* We had an io error when writing this out */
0068     BTRFS_ORDERED_IOERR,
0069     /* Set when we have to truncate an extent */
0070     BTRFS_ORDERED_TRUNCATED,
0071     /* Used during fsync to track already logged extents */
0072     BTRFS_ORDERED_LOGGED,
0073     /* We have already logged all the csums of the ordered extent */
0074     BTRFS_ORDERED_LOGGED_CSUM,
0075     /* We wait for this extent to complete in the current transaction */
0076     BTRFS_ORDERED_PENDING,
0077     /* BTRFS_IOC_ENCODED_WRITE */
0078     BTRFS_ORDERED_ENCODED,
0079 };
0080 
0081 /* BTRFS_ORDERED_* flags that specify the type of the extent. */
0082 #define BTRFS_ORDERED_TYPE_FLAGS ((1UL << BTRFS_ORDERED_REGULAR) |  \
0083                   (1UL << BTRFS_ORDERED_NOCOW) |    \
0084                   (1UL << BTRFS_ORDERED_PREALLOC) | \
0085                   (1UL << BTRFS_ORDERED_COMPRESSED) |   \
0086                   (1UL << BTRFS_ORDERED_DIRECT) |   \
0087                   (1UL << BTRFS_ORDERED_ENCODED))
0088 
0089 struct btrfs_ordered_extent {
0090     /* logical offset in the file */
0091     u64 file_offset;
0092 
0093     /*
0094      * These fields directly correspond to the same fields in
0095      * btrfs_file_extent_item.
0096      */
0097     u64 num_bytes;
0098     u64 ram_bytes;
0099     u64 disk_bytenr;
0100     u64 disk_num_bytes;
0101     u64 offset;
0102 
0103     /* number of bytes that still need writing */
0104     u64 bytes_left;
0105 
0106     /*
0107      * the end of the ordered extent which is behind it but
0108      * didn't update disk_i_size. Please see the comment of
0109      * btrfs_ordered_update_i_size();
0110      */
0111     u64 outstanding_isize;
0112 
0113     /*
0114      * If we get truncated we need to adjust the file extent we enter for
0115      * this ordered extent so that we do not expose stale data.
0116      */
0117     u64 truncated_len;
0118 
0119     /* flags (described above) */
0120     unsigned long flags;
0121 
0122     /* compression algorithm */
0123     int compress_type;
0124 
0125     /* Qgroup reserved space */
0126     int qgroup_rsv;
0127 
0128     /* reference count */
0129     refcount_t refs;
0130 
0131     /* the inode we belong to */
0132     struct inode *inode;
0133 
0134     /* list of checksums for insertion when the extent io is done */
0135     struct list_head list;
0136 
0137     /* used for fast fsyncs */
0138     struct list_head log_list;
0139 
0140     /* used to wait for the BTRFS_ORDERED_COMPLETE bit */
0141     wait_queue_head_t wait;
0142 
0143     /* our friendly rbtree entry */
0144     struct rb_node rb_node;
0145 
0146     /* a per root list of all the pending ordered extents */
0147     struct list_head root_extent_list;
0148 
0149     struct btrfs_work work;
0150 
0151     struct completion completion;
0152     struct btrfs_work flush_work;
0153     struct list_head work_list;
0154 
0155     /*
0156      * Used to reverse-map physical address returned from ZONE_APPEND write
0157      * command in a workqueue context
0158      */
0159     u64 physical;
0160     struct block_device *bdev;
0161 };
0162 
0163 /*
0164  * calculates the total size you need to allocate for an ordered sum
0165  * structure spanning 'bytes' in the file
0166  */
0167 static inline int btrfs_ordered_sum_size(struct btrfs_fs_info *fs_info,
0168                      unsigned long bytes)
0169 {
0170     int num_sectors = (int)DIV_ROUND_UP(bytes, fs_info->sectorsize);
0171 
0172     return sizeof(struct btrfs_ordered_sum) + num_sectors * fs_info->csum_size;
0173 }
0174 
0175 static inline void
0176 btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree *t)
0177 {
0178     spin_lock_init(&t->lock);
0179     t->tree = RB_ROOT;
0180     t->last = NULL;
0181 }
0182 
0183 int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
0184 
0185 void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry);
0186 void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode,
0187                 struct btrfs_ordered_extent *entry);
0188 void btrfs_mark_ordered_io_finished(struct btrfs_inode *inode,
0189                 struct page *page, u64 file_offset,
0190                 u64 num_bytes, bool uptodate);
0191 bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode,
0192                     struct btrfs_ordered_extent **cached,
0193                     u64 file_offset, u64 io_size);
0194 int btrfs_add_ordered_extent(struct btrfs_inode *inode, u64 file_offset,
0195                  u64 num_bytes, u64 ram_bytes, u64 disk_bytenr,
0196                  u64 disk_num_bytes, u64 offset, unsigned flags,
0197                  int compress_type);
0198 void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
0199                struct btrfs_ordered_sum *sum);
0200 struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,
0201                              u64 file_offset);
0202 void btrfs_start_ordered_extent(struct btrfs_ordered_extent *entry, int wait);
0203 int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len);
0204 struct btrfs_ordered_extent *
0205 btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset);
0206 struct btrfs_ordered_extent *btrfs_lookup_first_ordered_range(
0207             struct btrfs_inode *inode, u64 file_offset, u64 len);
0208 struct btrfs_ordered_extent *btrfs_lookup_ordered_range(
0209         struct btrfs_inode *inode,
0210         u64 file_offset,
0211         u64 len);
0212 void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode,
0213                        struct list_head *list);
0214 u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr,
0215                    const u64 range_start, const u64 range_len);
0216 void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr,
0217                   const u64 range_start, const u64 range_len);
0218 void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,
0219                     u64 end,
0220                     struct extent_state **cached_state);
0221 int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pre,
0222                    u64 post);
0223 int __init ordered_data_init(void);
0224 void __cold ordered_data_exit(void);
0225 
0226 #endif