Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-block.git] / fs / btrfs / ordered-data.h
CommitLineData
9888c340 1/* SPDX-License-Identifier: GPL-2.0 */
dc17ff8f
CM
2/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
dc17ff8f
CM
4 */
5
9888c340
DS
6#ifndef BTRFS_ORDERED_DATA_H
7#define BTRFS_ORDERED_DATA_H
dc17ff8f 8
602035d7
DS
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>
22b46bdc
DS
15#include "async-thread.h"
16
602035d7
DS
17struct inode;
18struct page;
19struct extent_state;
6149c82b 20struct btrfs_block_group;
602035d7
DS
21struct btrfs_inode;
22struct btrfs_root;
23struct btrfs_fs_info;
24
e6dcd2dc 25struct btrfs_ordered_sum {
3edf7d33 26 /*
5cfe76f8
CH
27 * Logical start address and length for of the blocks covered by
28 * the sums array.
3edf7d33 29 */
5cfe76f8 30 u64 logical;
6e4b2479 31 u32 len;
5cfe76f8 32
e6dcd2dc 33 struct list_head list;
f51a4a18 34 /* last field is a variable length array of csums */
1e25a2e3 35 u8 sums[];
e6dcd2dc
CM
36};
37
eb84ae03 38/*
3c198fe0 39 * Bits for btrfs_ordered_extent::flags.
eb84ae03
CM
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 */
5b840301 49enum {
3c198fe0 50 /*
0b3dcd13
QW
51 * Different types for ordered extents, one and only one of the 4 types
52 * need to be set when creating ordered extent.
3c198fe0
QW
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
5b840301
DS
72 /* set when all the pages are written */
73 BTRFS_ORDERED_IO_DONE,
74 /* set when removed from the tree */
75 BTRFS_ORDERED_COMPLETE,
5b840301
DS
76 /* We had an io error when writing this out */
77 BTRFS_ORDERED_IOERR,
5b840301
DS
78 /* Set when we have to truncate an extent */
79 BTRFS_ORDERED_TRUNCATED,
48778179
FM
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,
7c0c7269
OS
86 /* BTRFS_IOC_ENCODED_WRITE */
87 BTRFS_ORDERED_ENCODED,
5b840301 88};
1af4a0aa 89
cb36a9bb
OS
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) | \
7c0c7269
OS
95 (1UL << BTRFS_ORDERED_DIRECT) | \
96 (1UL << BTRFS_ORDERED_ENCODED))
cb36a9bb 97
e6dcd2dc 98struct btrfs_ordered_extent {
eb84ae03 99 /* logical offset in the file */
e6dcd2dc 100 u64 file_offset;
eb84ae03 101
bffe633e
OS
102 /*
103 * These fields directly correspond to the same fields in
104 * btrfs_file_extent_item.
105 */
bffe633e 106 u64 num_bytes;
cb36a9bb
OS
107 u64 ram_bytes;
108 u64 disk_bytenr;
bffe633e 109 u64 disk_num_bytes;
cb36a9bb 110 u64 offset;
c8b97818 111
8b62b72b
CM
112 /* number of bytes that still need writing */
113 u64 bytes_left;
114
77cef2ec
JB
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
eb84ae03 121 /* flags (described above) */
e6dcd2dc 122 unsigned long flags;
eb84ae03 123
261507a0
LZ
124 /* compression algorithm */
125 int compress_type;
126
7dbeaad0
QW
127 /* Qgroup reserved space */
128 int qgroup_rsv;
129
eb84ae03 130 /* reference count */
e76edab7 131 refcount_t refs;
eb84ae03 132
3eaa2885 133 /* the inode we belong to */
a1f4e3d7 134 struct btrfs_inode *inode;
3eaa2885 135
eb84ae03 136 /* list of checksums for insertion when the extent io is done */
e6dcd2dc 137 struct list_head list;
eb84ae03 138
48778179
FM
139 /* used for fast fsyncs */
140 struct list_head log_list;
141
eb84ae03 142 /* used to wait for the BTRFS_ORDERED_COMPLETE bit */
e6dcd2dc 143 wait_queue_head_t wait;
eb84ae03
CM
144
145 /* our friendly rbtree entry */
e6dcd2dc 146 struct rb_node rb_node;
3eaa2885
CM
147
148 /* a per root list of all the pending ordered extents */
149 struct list_head root_extent_list;
5fd02043 150
d458b054 151 struct btrfs_work work;
e6dcd2dc 152
9afab882 153 struct completion completion;
d458b054 154 struct btrfs_work flush_work;
9afab882 155 struct list_head work_list;
02c372e1
JT
156
157 struct list_head bioc_list;
9afab882 158};
e6dcd2dc 159
71df088c 160int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent);
711f447b
CH
161int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
162
143bede5 163void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry);
71fe0a55 164void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode,
e6dcd2dc 165 struct btrfs_ordered_extent *entry);
c41881ae 166void btrfs_finish_ordered_extent(struct btrfs_ordered_extent *ordered,
aef665d6 167 struct folio *folio, u64 file_offset, u64 len,
122e9ede 168 bool uptodate);
e65f152e 169void btrfs_mark_ordered_io_finished(struct btrfs_inode *inode,
a7922801
JB
170 struct folio *folio, u64 file_offset,
171 u64 num_bytes, bool uptodate);
58f74b22
QW
172bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode,
173 struct btrfs_ordered_extent **cached,
f41b6ba9 174 u64 file_offset, u64 io_size);
e9ea31fb
QW
175
176/*
177 * This represents details about the target file extent item of a write operation.
178 */
179struct 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
cf6d1aa4
BB
188struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
189 struct btrfs_inode *inode, u64 file_offset,
9fec848b 190 const struct btrfs_file_extent *file_extent, unsigned long flags);
f9756261 191void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
143bede5 192 struct btrfs_ordered_sum *sum);
c3504372 193struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,
e6dcd2dc 194 u64 file_offset);
d2da21a6
QW
195void btrfs_start_ordered_extent_nowriteback(struct btrfs_ordered_extent *entry,
196 u64 nowriteback_start, u32 nowriteback_len);
197static inline void btrfs_start_ordered_extent(struct btrfs_ordered_extent *entry)
198{
199 return btrfs_start_ordered_extent_nowriteback(entry, 0, 0);
200}
201
e641e323 202int btrfs_wait_ordered_range(struct btrfs_inode *inode, u64 start, u64 len);
e6dcd2dc 203struct btrfs_ordered_extent *
6d072c8e 204btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset);
c095f333
QW
205struct btrfs_ordered_extent *btrfs_lookup_first_ordered_range(
206 struct btrfs_inode *inode, u64 file_offset, u64 len);
a776c6fa
NB
207struct btrfs_ordered_extent *btrfs_lookup_ordered_range(
208 struct btrfs_inode *inode,
209 u64 file_offset,
210 u64 len);
48778179
FM
211void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode,
212 struct list_head *list);
6374e57a 213u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr,
42317ab4 214 const struct btrfs_block_group *bg);
042528f8 215void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr,
42317ab4 216 const struct btrfs_block_group *bg);
b272ae22 217void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,
ffa87214
NB
218 u64 end,
219 struct extent_state **cached_state);
632ddfa2
JB
220bool btrfs_try_lock_ordered_range(struct btrfs_inode *inode, u64 start, u64 end,
221 struct extent_state **cached_state);
b0307e28
CH
222struct btrfs_ordered_extent *btrfs_split_ordered_extent(
223 struct btrfs_ordered_extent *ordered, u64 len);
aa5ccf29 224void btrfs_mark_ordered_extent_error(struct btrfs_ordered_extent *ordered);
6352b91d 225int __init ordered_data_init(void);
e67c718b 226void __cold ordered_data_exit(void);
9888c340 227
dc17ff8f 228#endif