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 /* Extra status bits for ordered extents */ 51 52 /* Set when all the pages are written. */ 53 BTRFS_ORDERED_IO_DONE, 54 /* Set when removed from the tree. */ 55 BTRFS_ORDERED_COMPLETE, 56 /* We had an io error when writing this out. */ 57 BTRFS_ORDERED_IOERR, 58 /* Set when we have to truncate an extent. */ 59 BTRFS_ORDERED_TRUNCATED, 60 /* Used during fsync to track already logged extents. */ 61 BTRFS_ORDERED_LOGGED, 62 /* We have already logged all the csums of the ordered extent. */ 63 BTRFS_ORDERED_LOGGED_CSUM, 64 /* We wait for this extent to complete in the current transaction. */ 65 BTRFS_ORDERED_PENDING, 66 67 /* 68 * Different types for ordered extents, one and only one of these types 69 * need to be set when creating ordered extent. 70 * 71 * REGULAR: For regular non-compressed COW write 72 * NOCOW: For NOCOW write into existing non-hole extent 73 * PREALLOC: For NOCOW write into preallocated extent 74 * COMPRESSED: For compressed COW write 75 */ 76 BTRFS_ORDERED_REGULAR, 77 BTRFS_ORDERED_NOCOW, 78 BTRFS_ORDERED_PREALLOC, 79 BTRFS_ORDERED_COMPRESSED, 80 81 /* Extra bit for encoded write, must be set with COMPRESSED. */ 82 BTRFS_ORDERED_ENCODED, 83 84 /* 85 * Extra bit for direct io, can only be set for 86 * REGULAR/NOCOW/PREALLOC. Must not be set for COMPRESSED nor ENCODED. 87 */ 88 BTRFS_ORDERED_DIRECT, 89 90 BTRFS_ORDERED_NR_FLAGS, 91 }; 92 static_assert(BTRFS_ORDERED_NR_FLAGS <= BITS_PER_LONG); 93 94 /* One and only one flag can be set. */ 95 #define BTRFS_ORDERED_EXCLUSIVE_FLAGS ((1UL << BTRFS_ORDERED_REGULAR) | \ 96 (1UL << BTRFS_ORDERED_NOCOW) | \ 97 (1UL << BTRFS_ORDERED_PREALLOC) | \ 98 (1UL << BTRFS_ORDERED_COMPRESSED)) 99 100 /* BTRFS_ORDERED_* flags that specify the type of the extent. */ 101 #define BTRFS_ORDERED_TYPE_FLAGS (BTRFS_ORDERED_EXCLUSIVE_FLAGS | \ 102 (1UL << BTRFS_ORDERED_DIRECT) | \ 103 (1UL << BTRFS_ORDERED_ENCODED)) 104 105 struct btrfs_ordered_extent { 106 /* logical offset in the file */ 107 u64 file_offset; 108 109 /* 110 * These fields directly correspond to the same fields in 111 * btrfs_file_extent_item. 112 */ 113 u64 num_bytes; 114 u64 ram_bytes; 115 u64 disk_bytenr; 116 u64 disk_num_bytes; 117 u64 offset; 118 119 /* number of bytes that still need writing */ 120 u64 bytes_left; 121 122 /* 123 * If we get truncated we need to adjust the file extent we enter for 124 * this ordered extent so that we do not expose stale data. 125 */ 126 u64 truncated_len; 127 128 /* flags (described above) */ 129 unsigned long flags; 130 131 /* compression algorithm */ 132 int compress_type; 133 134 /* Qgroup reserved space */ 135 int qgroup_rsv; 136 137 /* reference count */ 138 refcount_t refs; 139 140 /* the inode we belong to */ 141 struct btrfs_inode *inode; 142 143 /* list of checksums for insertion when the extent io is done */ 144 struct list_head csum_list; 145 146 /* used for fast fsyncs */ 147 struct list_head log_list; 148 149 /* used to wait for the BTRFS_ORDERED_COMPLETE bit */ 150 wait_queue_head_t wait; 151 152 /* our friendly rbtree entry */ 153 struct rb_node rb_node; 154 155 /* a per root list of all the pending ordered extents */ 156 struct list_head root_extent_list; 157 158 struct btrfs_work work; 159 160 struct completion completion; 161 struct btrfs_work flush_work; 162 struct list_head work_list; 163 164 struct list_head bioc_list; 165 }; 166 167 int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent); 168 int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); 169 170 void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry); 171 void btrfs_remove_ordered_extent(struct btrfs_ordered_extent *entry); 172 void btrfs_finish_ordered_extent(struct btrfs_ordered_extent *ordered, 173 u64 file_offset, u64 len, bool uptodate); 174 void btrfs_mark_ordered_io_finished(struct btrfs_inode *inode, 175 u64 file_offset, u64 num_bytes, bool uptodate); 176 bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode, 177 struct btrfs_ordered_extent **cached, 178 u64 file_offset, u64 io_size); 179 180 /* 181 * This represents details about the target file extent item of a write operation. 182 */ 183 struct btrfs_file_extent { 184 u64 disk_bytenr; 185 u64 disk_num_bytes; 186 u64 num_bytes; 187 u64 ram_bytes; 188 u64 offset; 189 u8 compression; 190 }; 191 192 struct btrfs_ordered_extent *btrfs_alloc_ordered_extent( 193 struct btrfs_inode *inode, u64 file_offset, 194 const struct btrfs_file_extent *file_extent, unsigned long flags); 195 void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry, 196 struct btrfs_ordered_sum *sum); 197 struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode, 198 u64 file_offset); 199 void btrfs_start_ordered_extent_nowriteback(struct btrfs_ordered_extent *entry, 200 u64 nowriteback_start, u32 nowriteback_len); 201 static inline void btrfs_start_ordered_extent(struct btrfs_ordered_extent *entry) 202 { 203 return btrfs_start_ordered_extent_nowriteback(entry, 0, 0); 204 } 205 206 int btrfs_wait_ordered_range(struct btrfs_inode *inode, u64 start, u64 len); 207 struct btrfs_ordered_extent * 208 btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset); 209 struct btrfs_ordered_extent *btrfs_lookup_first_ordered_range( 210 struct btrfs_inode *inode, u64 file_offset, u64 len); 211 struct btrfs_ordered_extent *btrfs_lookup_ordered_range( 212 struct btrfs_inode *inode, 213 u64 file_offset, 214 u64 len); 215 void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode, 216 struct list_head *list); 217 u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr, 218 const struct btrfs_block_group *bg); 219 void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr, 220 const struct btrfs_block_group *bg); 221 void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start, 222 u64 end, 223 struct extent_state **cached_state); 224 bool btrfs_try_lock_ordered_range(struct btrfs_inode *inode, u64 start, u64 end, 225 struct extent_state **cached_state); 226 struct btrfs_ordered_extent *btrfs_split_ordered_extent( 227 struct btrfs_ordered_extent *ordered, u64 len); 228 void btrfs_mark_ordered_extent_error(struct btrfs_ordered_extent *ordered); 229 int __init ordered_data_init(void); 230 void __cold ordered_data_exit(void); 231 232 #endif 233