Commit | Line | Data |
---|---|---|
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 |
17 | struct inode; |
18 | struct page; | |
19 | struct extent_state; | |
20 | struct btrfs_inode; | |
21 | struct btrfs_root; | |
22 | struct btrfs_fs_info; | |
23 | ||
e6dcd2dc | 24 | struct btrfs_ordered_sum { |
3edf7d33 | 25 | /* |
5cfe76f8 CH |
26 | * Logical start address and length for of the blocks covered by |
27 | * the sums array. | |
3edf7d33 | 28 | */ |
5cfe76f8 | 29 | u64 logical; |
6e4b2479 | 30 | u32 len; |
5cfe76f8 | 31 | |
e6dcd2dc | 32 | struct list_head list; |
f51a4a18 | 33 | /* last field is a variable length array of csums */ |
1e25a2e3 | 34 | u8 sums[]; |
e6dcd2dc CM |
35 | }; |
36 | ||
eb84ae03 | 37 | /* |
3c198fe0 | 38 | * Bits for btrfs_ordered_extent::flags. |
eb84ae03 CM |
39 | * |
40 | * BTRFS_ORDERED_IO_DONE is set when all of the blocks are written. | |
41 | * It is used to make sure metadata is inserted into the tree only once | |
42 | * per extent. | |
43 | * | |
44 | * BTRFS_ORDERED_COMPLETE is set when the extent is removed from the | |
45 | * rbtree, just before waking any waiters. It is used to indicate the | |
46 | * IO is done and any metadata is inserted into the tree. | |
47 | */ | |
5b840301 | 48 | enum { |
3c198fe0 | 49 | /* |
0b3dcd13 QW |
50 | * Different types for ordered extents, one and only one of the 4 types |
51 | * need to be set when creating ordered extent. | |
3c198fe0 QW |
52 | * |
53 | * REGULAR: For regular non-compressed COW write | |
54 | * NOCOW: For NOCOW write into existing non-hole extent | |
55 | * PREALLOC: For NOCOW write into preallocated extent | |
56 | * COMPRESSED: For compressed COW write | |
57 | */ | |
58 | BTRFS_ORDERED_REGULAR, | |
59 | BTRFS_ORDERED_NOCOW, | |
60 | BTRFS_ORDERED_PREALLOC, | |
61 | BTRFS_ORDERED_COMPRESSED, | |
62 | ||
63 | /* | |
64 | * Extra bit for direct io, can only be set for | |
65 | * REGULAR/NOCOW/PREALLOC. No direct io for compressed extent. | |
66 | */ | |
67 | BTRFS_ORDERED_DIRECT, | |
68 | ||
69 | /* Extra status bits for ordered extents */ | |
70 | ||
5b840301 DS |
71 | /* set when all the pages are written */ |
72 | BTRFS_ORDERED_IO_DONE, | |
73 | /* set when removed from the tree */ | |
74 | BTRFS_ORDERED_COMPLETE, | |
5b840301 DS |
75 | /* We had an io error when writing this out */ |
76 | BTRFS_ORDERED_IOERR, | |
5b840301 DS |
77 | /* Set when we have to truncate an extent */ |
78 | BTRFS_ORDERED_TRUNCATED, | |
48778179 FM |
79 | /* Used during fsync to track already logged extents */ |
80 | BTRFS_ORDERED_LOGGED, | |
81 | /* We have already logged all the csums of the ordered extent */ | |
82 | BTRFS_ORDERED_LOGGED_CSUM, | |
83 | /* We wait for this extent to complete in the current transaction */ | |
84 | BTRFS_ORDERED_PENDING, | |
7c0c7269 OS |
85 | /* BTRFS_IOC_ENCODED_WRITE */ |
86 | BTRFS_ORDERED_ENCODED, | |
5b840301 | 87 | }; |
1af4a0aa | 88 | |
cb36a9bb OS |
89 | /* BTRFS_ORDERED_* flags that specify the type of the extent. */ |
90 | #define BTRFS_ORDERED_TYPE_FLAGS ((1UL << BTRFS_ORDERED_REGULAR) | \ | |
91 | (1UL << BTRFS_ORDERED_NOCOW) | \ | |
92 | (1UL << BTRFS_ORDERED_PREALLOC) | \ | |
93 | (1UL << BTRFS_ORDERED_COMPRESSED) | \ | |
7c0c7269 OS |
94 | (1UL << BTRFS_ORDERED_DIRECT) | \ |
95 | (1UL << BTRFS_ORDERED_ENCODED)) | |
cb36a9bb | 96 | |
e6dcd2dc | 97 | struct btrfs_ordered_extent { |
eb84ae03 | 98 | /* logical offset in the file */ |
e6dcd2dc | 99 | u64 file_offset; |
eb84ae03 | 100 | |
bffe633e OS |
101 | /* |
102 | * These fields directly correspond to the same fields in | |
103 | * btrfs_file_extent_item. | |
104 | */ | |
bffe633e | 105 | u64 num_bytes; |
cb36a9bb OS |
106 | u64 ram_bytes; |
107 | u64 disk_bytenr; | |
bffe633e | 108 | u64 disk_num_bytes; |
cb36a9bb | 109 | u64 offset; |
c8b97818 | 110 | |
8b62b72b CM |
111 | /* number of bytes that still need writing */ |
112 | u64 bytes_left; | |
113 | ||
77cef2ec JB |
114 | /* |
115 | * If we get truncated we need to adjust the file extent we enter for | |
116 | * this ordered extent so that we do not expose stale data. | |
117 | */ | |
118 | u64 truncated_len; | |
119 | ||
eb84ae03 | 120 | /* flags (described above) */ |
e6dcd2dc | 121 | unsigned long flags; |
eb84ae03 | 122 | |
261507a0 LZ |
123 | /* compression algorithm */ |
124 | int compress_type; | |
125 | ||
7dbeaad0 QW |
126 | /* Qgroup reserved space */ |
127 | int qgroup_rsv; | |
128 | ||
eb84ae03 | 129 | /* reference count */ |
e76edab7 | 130 | refcount_t refs; |
eb84ae03 | 131 | |
3eaa2885 CM |
132 | /* the inode we belong to */ |
133 | struct inode *inode; | |
134 | ||
eb84ae03 | 135 | /* list of checksums for insertion when the extent io is done */ |
e6dcd2dc | 136 | struct list_head list; |
eb84ae03 | 137 | |
48778179 FM |
138 | /* used for fast fsyncs */ |
139 | struct list_head log_list; | |
140 | ||
eb84ae03 | 141 | /* used to wait for the BTRFS_ORDERED_COMPLETE bit */ |
e6dcd2dc | 142 | wait_queue_head_t wait; |
eb84ae03 CM |
143 | |
144 | /* our friendly rbtree entry */ | |
e6dcd2dc | 145 | struct rb_node rb_node; |
3eaa2885 CM |
146 | |
147 | /* a per root list of all the pending ordered extents */ | |
148 | struct list_head root_extent_list; | |
5fd02043 | 149 | |
d458b054 | 150 | struct btrfs_work work; |
e6dcd2dc | 151 | |
9afab882 | 152 | struct completion completion; |
d458b054 | 153 | struct btrfs_work flush_work; |
9afab882 | 154 | struct list_head work_list; |
02c372e1 JT |
155 | |
156 | struct list_head bioc_list; | |
9afab882 | 157 | }; |
e6dcd2dc | 158 | |
71df088c | 159 | int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent); |
711f447b CH |
160 | int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
161 | ||
143bede5 | 162 | void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry); |
71fe0a55 | 163 | void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode, |
e6dcd2dc | 164 | struct btrfs_ordered_extent *entry); |
122e9ede CH |
165 | bool btrfs_finish_ordered_extent(struct btrfs_ordered_extent *ordered, |
166 | struct page *page, u64 file_offset, u64 len, | |
167 | bool uptodate); | |
e65f152e QW |
168 | void btrfs_mark_ordered_io_finished(struct btrfs_inode *inode, |
169 | struct page *page, u64 file_offset, | |
711f447b | 170 | u64 num_bytes, bool uptodate); |
58f74b22 QW |
171 | bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode, |
172 | struct btrfs_ordered_extent **cached, | |
f41b6ba9 | 173 | u64 file_offset, u64 io_size); |
cf6d1aa4 BB |
174 | struct btrfs_ordered_extent *btrfs_alloc_ordered_extent( |
175 | struct btrfs_inode *inode, u64 file_offset, | |
176 | u64 num_bytes, u64 ram_bytes, u64 disk_bytenr, | |
177 | u64 disk_num_bytes, u64 offset, unsigned long flags, | |
178 | int compress_type); | |
f9756261 | 179 | void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry, |
143bede5 | 180 | struct btrfs_ordered_sum *sum); |
c3504372 | 181 | struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode, |
e6dcd2dc | 182 | u64 file_offset); |
36d45567 | 183 | void btrfs_start_ordered_extent(struct btrfs_ordered_extent *entry); |
0ef8b726 | 184 | int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len); |
e6dcd2dc | 185 | struct btrfs_ordered_extent * |
6d072c8e | 186 | btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset); |
c095f333 QW |
187 | struct btrfs_ordered_extent *btrfs_lookup_first_ordered_range( |
188 | struct btrfs_inode *inode, u64 file_offset, u64 len); | |
a776c6fa NB |
189 | struct btrfs_ordered_extent *btrfs_lookup_ordered_range( |
190 | struct btrfs_inode *inode, | |
191 | u64 file_offset, | |
192 | u64 len); | |
48778179 FM |
193 | void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode, |
194 | struct list_head *list); | |
6374e57a | 195 | u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr, |
578def7c | 196 | const u64 range_start, const u64 range_len); |
042528f8 | 197 | void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr, |
578def7c | 198 | const u64 range_start, const u64 range_len); |
b272ae22 | 199 | void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start, |
ffa87214 NB |
200 | u64 end, |
201 | struct extent_state **cached_state); | |
632ddfa2 JB |
202 | bool btrfs_try_lock_ordered_range(struct btrfs_inode *inode, u64 start, u64 end, |
203 | struct extent_state **cached_state); | |
b0307e28 CH |
204 | struct btrfs_ordered_extent *btrfs_split_ordered_extent( |
205 | struct btrfs_ordered_extent *ordered, u64 len); | |
aa5ccf29 | 206 | void btrfs_mark_ordered_extent_error(struct btrfs_ordered_extent *ordered); |
6352b91d | 207 | int __init ordered_data_init(void); |
e67c718b | 208 | void __cold ordered_data_exit(void); |
9888c340 | 209 | |
dc17ff8f | 210 | #endif |