1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #ifndef BTRFS_ORDERED_DATA_H
7 #define BTRFS_ORDERED_DATA_H
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"
20 struct btrfs_block_group;
25 struct btrfs_ordered_sum {
27 * Logical start address and length for of the blocks covered by
33 struct list_head list;
34 /* last field is a variable length array of csums */
39 * Bits for btrfs_ordered_extent::flags.
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
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.
51 * Different types for ordered extents, one and only one of the 4 types
52 * need to be set when creating ordered extent.
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
59 BTRFS_ORDERED_REGULAR,
61 BTRFS_ORDERED_PREALLOC,
62 BTRFS_ORDERED_COMPRESSED,
65 * Extra bit for direct io, can only be set for
66 * REGULAR/NOCOW/PREALLOC. No direct io for compressed extent.
70 /* Extra status bits for ordered extents */
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 */
78 /* Set when we have to truncate an extent */
79 BTRFS_ORDERED_TRUNCATED,
80 /* Used during fsync to track already logged extents */
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,
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))
98 struct btrfs_ordered_extent {
99 /* logical offset in the file */
103 * These fields directly correspond to the same fields in
104 * btrfs_file_extent_item.
112 /* number of bytes that still need writing */
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.
121 /* flags (described above) */
124 /* compression algorithm */
127 /* Qgroup reserved space */
130 /* reference count */
133 /* the inode we belong to */
134 struct btrfs_inode *inode;
136 /* list of checksums for insertion when the extent io is done */
137 struct list_head list;
139 /* used for fast fsyncs */
140 struct list_head log_list;
142 /* used to wait for the BTRFS_ORDERED_COMPLETE bit */
143 wait_queue_head_t wait;
145 /* our friendly rbtree entry */
146 struct rb_node rb_node;
148 /* a per root list of all the pending ordered extents */
149 struct list_head root_extent_list;
151 struct btrfs_work work;
153 struct completion completion;
154 struct btrfs_work flush_work;
155 struct list_head work_list;
157 struct list_head bioc_list;
160 int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent);
161 int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
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,
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);
177 * This represents details about the target file extent item of a write operation.
179 struct btrfs_file_extent {
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,
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)
199 return btrfs_start_ordered_extent_nowriteback(entry, 0, 0);
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,
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,
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);