1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright (C) 2007 Oracle. All rights reserved. 4 */ 5 6 #ifndef BTRFS_ORDERED_DATA_H 7 #define BTRFS_ORDERED_DATA_H 8 9 #include <linux/types.h> 10 #include <linux/list.h> 11 #include <linux/refcount.h> 12 #include <linux/completion.h> 13 #include <linux/rbtree.h> 14 #include <linux/wait.h> 15 #include "async-thread.h" 16 17 struct inode; 18 struct page; 19 struct extent_state; 20 struct btrfs_block_group; 21 struct btrfs_inode; 22 struct btrfs_root; 23 struct btrfs_fs_info; 24 25 struct btrfs_ordered_sum { 26 /* 27 * Logical start address and length for of the blocks covered by 28 * the sums array. 29 */ 30 u64 logical; 31 u32 len; 32 33 struct list_head list; 34 /* last field is a variable length array of csums */ 35 u8 sums[]; 36 }; 37 38 /* 39 * Bits for btrfs_ordered_extent::flags. 40 * 41 * BTRFS_ORDERED_IO_DONE is set when all of the blocks are written. 42 * It is used to make sure metadata is inserted into the tree only once 43 * per extent. 44 * 45 * BTRFS_ORDERED_COMPLETE is set when the extent is removed from the 46 * rbtree, just before waking any waiters. It is used to indicate the 47 * IO is done and any metadata is inserted into the tree. 48 */ 49 enum { 50 /* 51 * Different types for ordered extents, one and only one of the 4 types 52 * need to be set when creating ordered extent. 53 * 54 * REGULAR: For regular non-compressed COW write 55 * NOCOW: For NOCOW write into existing non-hole extent 56 * PREALLOC: For NOCOW write into preallocated extent 57 * COMPRESSED: For compressed COW write 58 */ 59 BTRFS_ORDERED_REGULAR, 60 BTRFS_ORDERED_NOCOW, 61 BTRFS_ORDERED_PREALLOC, 62 BTRFS_ORDERED_COMPRESSED, 63 64 /* 65 * Extra bit for direct io, can only be set for 66 * REGULAR/NOCOW/PREALLOC. No direct io for compressed extent. 67 */ 68 BTRFS_ORDERED_DIRECT, 69 70 /* Extra status bits for ordered extents */ 71 72 /* set when all the pages are written */ 73 BTRFS_ORDERED_IO_DONE, 74 /* set when removed from the tree */ 75 BTRFS_ORDERED_COMPLETE, 76 /* We had an io error when writing this out */ 77 BTRFS_ORDERED_IOERR, 78 /* Set when we have to truncate an extent */ 79 BTRFS_ORDERED_TRUNCATED, 80 /* Used during fsync to track already logged extents */ 81 BTRFS_ORDERED_LOGGED, 82 /* We have already logged all the csums of the ordered extent */ 83 BTRFS_ORDERED_LOGGED_CSUM, 84 /* We wait for this extent to complete in the current transaction */ 85 BTRFS_ORDERED_PENDING, 86 /* BTRFS_IOC_ENCODED_WRITE */ 87 BTRFS_ORDERED_ENCODED, 88 }; 89 90 /* BTRFS_ORDERED_* flags that specify the type of the extent. */ 91 #define BTRFS_ORDERED_TYPE_FLAGS ((1UL << BTRFS_ORDERED_REGULAR) | \ 92 (1UL << BTRFS_ORDERED_NOCOW) | \ 93 (1UL << BTRFS_ORDERED_PREALLOC) | \ 94 (1UL << BTRFS_ORDERED_COMPRESSED) | \ 95 (1UL << BTRFS_ORDERED_DIRECT) | \ 96 (1UL << BTRFS_ORDERED_ENCODED)) 97 98 struct btrfs_ordered_extent { 99 /* logical offset in the file */ 100 u64 file_offset; 101 102 /* 103 * These fields directly correspond to the same fields in 104 * btrfs_file_extent_item. 105 */ 106 u64 num_bytes; 107 u64 ram_bytes; 108 u64 disk_bytenr; 109 u64 disk_num_bytes; 110 u64 offset; 111 112 /* number of bytes that still need writing */ 113 u64 bytes_left; 114 115 /* 116 * If we get truncated we need to adjust the file extent we enter for 117 * this ordered extent so that we do not expose stale data. 118 */ 119 u64 truncated_len; 120 121 /* flags (described above) */ 122 unsigned long flags; 123 124 /* compression algorithm */ 125 int compress_type; 126 127 /* Qgroup reserved space */ 128 int qgroup_rsv; 129 130 /* reference count */ 131 refcount_t refs; 132 133 /* the inode we belong to */ 134 struct btrfs_inode *inode; 135 136 /* list of checksums for insertion when the extent io is done */ 137 struct list_head list; 138 139 /* used for fast fsyncs */ 140 struct list_head log_list; 141 142 /* used to wait for the BTRFS_ORDERED_COMPLETE bit */ 143 wait_queue_head_t wait; 144 145 /* our friendly rbtree entry */ 146 struct rb_node rb_node; 147 148 /* a per root list of all the pending ordered extents */ 149 struct list_head root_extent_list; 150 151 struct btrfs_work work; 152 153 struct completion completion; 154 struct btrfs_work flush_work; 155 struct list_head work_list; 156 157 struct list_head bioc_list; 158 }; 159 160 int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent); 161 int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); 162 163 void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry); 164 void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode, 165 struct btrfs_ordered_extent *entry); 166 void btrfs_finish_ordered_extent(struct btrfs_ordered_extent *ordered, 167 struct folio *folio, u64 file_offset, u64 len, 168 bool uptodate); 169 void btrfs_mark_ordered_io_finished(struct btrfs_inode *inode, 170 struct folio *folio, u64 file_offset, 171 u64 num_bytes, bool uptodate); 172 bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode, 173 struct btrfs_ordered_extent **cached, 174 u64 file_offset, u64 io_size); 175 176 /* 177 * This represents details about the target file extent item of a write operation. 178 */ 179 struct btrfs_file_extent { 180 u64 disk_bytenr; 181 u64 disk_num_bytes; 182 u64 num_bytes; 183 u64 ram_bytes; 184 u64 offset; 185 u8 compression; 186 }; 187 188 struct btrfs_ordered_extent *btrfs_alloc_ordered_extent( 189 struct btrfs_inode *inode, u64 file_offset, 190 const struct btrfs_file_extent *file_extent, unsigned long flags); 191 void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry, 192 struct btrfs_ordered_sum *sum); 193 struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode, 194 u64 file_offset); 195 void btrfs_start_ordered_extent_nowriteback(struct btrfs_ordered_extent *entry, 196 u64 nowriteback_start, u32 nowriteback_len); 197 static inline void btrfs_start_ordered_extent(struct btrfs_ordered_extent *entry) 198 { 199 return btrfs_start_ordered_extent_nowriteback(entry, 0, 0); 200 } 201 202 int btrfs_wait_ordered_range(struct btrfs_inode *inode, u64 start, u64 len); 203 struct btrfs_ordered_extent * 204 btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset); 205 struct btrfs_ordered_extent *btrfs_lookup_first_ordered_range( 206 struct btrfs_inode *inode, u64 file_offset, u64 len); 207 struct btrfs_ordered_extent *btrfs_lookup_ordered_range( 208 struct btrfs_inode *inode, 209 u64 file_offset, 210 u64 len); 211 void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode, 212 struct list_head *list); 213 u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr, 214 const struct btrfs_block_group *bg); 215 void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr, 216 const struct btrfs_block_group *bg); 217 void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start, 218 u64 end, 219 struct extent_state **cached_state); 220 bool btrfs_try_lock_ordered_range(struct btrfs_inode *inode, u64 start, u64 end, 221 struct extent_state **cached_state); 222 struct btrfs_ordered_extent *btrfs_split_ordered_extent( 223 struct btrfs_ordered_extent *ordered, u64 len); 224 void btrfs_mark_ordered_extent_error(struct btrfs_ordered_extent *ordered); 225 int __init ordered_data_init(void); 226 void __cold ordered_data_exit(void); 227 228 #endif 229