1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2007 Oracle. All rights reserved.
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #ifndef BTRFS_ORDERED_DATA_H
7*4882a593Smuzhiyun #define BTRFS_ORDERED_DATA_H
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun /* one of these per inode */
10*4882a593Smuzhiyun struct btrfs_ordered_inode_tree {
11*4882a593Smuzhiyun spinlock_t lock;
12*4882a593Smuzhiyun struct rb_root tree;
13*4882a593Smuzhiyun struct rb_node *last;
14*4882a593Smuzhiyun };
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun struct btrfs_ordered_sum {
17*4882a593Smuzhiyun /* bytenr is the start of this extent on disk */
18*4882a593Smuzhiyun u64 bytenr;
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun /*
21*4882a593Smuzhiyun * this is the length in bytes covered by the sums array below.
22*4882a593Smuzhiyun */
23*4882a593Smuzhiyun int len;
24*4882a593Smuzhiyun struct list_head list;
25*4882a593Smuzhiyun /* last field is a variable length array of csums */
26*4882a593Smuzhiyun u8 sums[];
27*4882a593Smuzhiyun };
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun /*
30*4882a593Smuzhiyun * bits for the flags field:
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * BTRFS_ORDERED_IO_DONE is set when all of the blocks are written.
33*4882a593Smuzhiyun * It is used to make sure metadata is inserted into the tree only once
34*4882a593Smuzhiyun * per extent.
35*4882a593Smuzhiyun *
36*4882a593Smuzhiyun * BTRFS_ORDERED_COMPLETE is set when the extent is removed from the
37*4882a593Smuzhiyun * rbtree, just before waking any waiters. It is used to indicate the
38*4882a593Smuzhiyun * IO is done and any metadata is inserted into the tree.
39*4882a593Smuzhiyun */
40*4882a593Smuzhiyun enum {
41*4882a593Smuzhiyun /* set when all the pages are written */
42*4882a593Smuzhiyun BTRFS_ORDERED_IO_DONE,
43*4882a593Smuzhiyun /* set when removed from the tree */
44*4882a593Smuzhiyun BTRFS_ORDERED_COMPLETE,
45*4882a593Smuzhiyun /* set when we want to write in place */
46*4882a593Smuzhiyun BTRFS_ORDERED_NOCOW,
47*4882a593Smuzhiyun /* writing a zlib compressed extent */
48*4882a593Smuzhiyun BTRFS_ORDERED_COMPRESSED,
49*4882a593Smuzhiyun /* set when writing to preallocated extent */
50*4882a593Smuzhiyun BTRFS_ORDERED_PREALLOC,
51*4882a593Smuzhiyun /* set when we're doing DIO with this extent */
52*4882a593Smuzhiyun BTRFS_ORDERED_DIRECT,
53*4882a593Smuzhiyun /* We had an io error when writing this out */
54*4882a593Smuzhiyun BTRFS_ORDERED_IOERR,
55*4882a593Smuzhiyun /* Set when we have to truncate an extent */
56*4882a593Smuzhiyun BTRFS_ORDERED_TRUNCATED,
57*4882a593Smuzhiyun /* Regular IO for COW */
58*4882a593Smuzhiyun BTRFS_ORDERED_REGULAR,
59*4882a593Smuzhiyun /* Used during fsync to track already logged extents */
60*4882a593Smuzhiyun BTRFS_ORDERED_LOGGED,
61*4882a593Smuzhiyun /* We have already logged all the csums of the ordered extent */
62*4882a593Smuzhiyun BTRFS_ORDERED_LOGGED_CSUM,
63*4882a593Smuzhiyun /* We wait for this extent to complete in the current transaction */
64*4882a593Smuzhiyun BTRFS_ORDERED_PENDING,
65*4882a593Smuzhiyun };
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun struct btrfs_ordered_extent {
68*4882a593Smuzhiyun /* logical offset in the file */
69*4882a593Smuzhiyun u64 file_offset;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun * These fields directly correspond to the same fields in
73*4882a593Smuzhiyun * btrfs_file_extent_item.
74*4882a593Smuzhiyun */
75*4882a593Smuzhiyun u64 disk_bytenr;
76*4882a593Smuzhiyun u64 num_bytes;
77*4882a593Smuzhiyun u64 disk_num_bytes;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /* number of bytes that still need writing */
80*4882a593Smuzhiyun u64 bytes_left;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /*
83*4882a593Smuzhiyun * the end of the ordered extent which is behind it but
84*4882a593Smuzhiyun * didn't update disk_i_size. Please see the comment of
85*4882a593Smuzhiyun * btrfs_ordered_update_i_size();
86*4882a593Smuzhiyun */
87*4882a593Smuzhiyun u64 outstanding_isize;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun /*
90*4882a593Smuzhiyun * If we get truncated we need to adjust the file extent we enter for
91*4882a593Smuzhiyun * this ordered extent so that we do not expose stale data.
92*4882a593Smuzhiyun */
93*4882a593Smuzhiyun u64 truncated_len;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun /* flags (described above) */
96*4882a593Smuzhiyun unsigned long flags;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /* compression algorithm */
99*4882a593Smuzhiyun int compress_type;
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /* Qgroup reserved space */
102*4882a593Smuzhiyun int qgroup_rsv;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /* reference count */
105*4882a593Smuzhiyun refcount_t refs;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun /* the inode we belong to */
108*4882a593Smuzhiyun struct inode *inode;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /* list of checksums for insertion when the extent io is done */
111*4882a593Smuzhiyun struct list_head list;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun /* used for fast fsyncs */
114*4882a593Smuzhiyun struct list_head log_list;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun /* used to wait for the BTRFS_ORDERED_COMPLETE bit */
117*4882a593Smuzhiyun wait_queue_head_t wait;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /* our friendly rbtree entry */
120*4882a593Smuzhiyun struct rb_node rb_node;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /* a per root list of all the pending ordered extents */
123*4882a593Smuzhiyun struct list_head root_extent_list;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun struct btrfs_work work;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun struct completion completion;
128*4882a593Smuzhiyun struct btrfs_work flush_work;
129*4882a593Smuzhiyun struct list_head work_list;
130*4882a593Smuzhiyun };
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun /*
133*4882a593Smuzhiyun * calculates the total size you need to allocate for an ordered sum
134*4882a593Smuzhiyun * structure spanning 'bytes' in the file
135*4882a593Smuzhiyun */
btrfs_ordered_sum_size(struct btrfs_fs_info * fs_info,unsigned long bytes)136*4882a593Smuzhiyun static inline int btrfs_ordered_sum_size(struct btrfs_fs_info *fs_info,
137*4882a593Smuzhiyun unsigned long bytes)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun int num_sectors = (int)DIV_ROUND_UP(bytes, fs_info->sectorsize);
140*4882a593Smuzhiyun int csum_size = btrfs_super_csum_size(fs_info->super_copy);
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun return sizeof(struct btrfs_ordered_sum) + num_sectors * csum_size;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun static inline void
btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree * t)146*4882a593Smuzhiyun btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree *t)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun spin_lock_init(&t->lock);
149*4882a593Smuzhiyun t->tree = RB_ROOT;
150*4882a593Smuzhiyun t->last = NULL;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry);
154*4882a593Smuzhiyun void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode,
155*4882a593Smuzhiyun struct btrfs_ordered_extent *entry);
156*4882a593Smuzhiyun int btrfs_dec_test_ordered_pending(struct btrfs_inode *inode,
157*4882a593Smuzhiyun struct btrfs_ordered_extent **cached,
158*4882a593Smuzhiyun u64 file_offset, u64 io_size, int uptodate);
159*4882a593Smuzhiyun int btrfs_dec_test_first_ordered_pending(struct btrfs_inode *inode,
160*4882a593Smuzhiyun struct btrfs_ordered_extent **cached,
161*4882a593Smuzhiyun u64 *file_offset, u64 io_size,
162*4882a593Smuzhiyun int uptodate);
163*4882a593Smuzhiyun int btrfs_add_ordered_extent(struct btrfs_inode *inode, u64 file_offset,
164*4882a593Smuzhiyun u64 disk_bytenr, u64 num_bytes, u64 disk_num_bytes,
165*4882a593Smuzhiyun int type);
166*4882a593Smuzhiyun int btrfs_add_ordered_extent_dio(struct btrfs_inode *inode, u64 file_offset,
167*4882a593Smuzhiyun u64 disk_bytenr, u64 num_bytes,
168*4882a593Smuzhiyun u64 disk_num_bytes, int type);
169*4882a593Smuzhiyun int btrfs_add_ordered_extent_compress(struct btrfs_inode *inode, u64 file_offset,
170*4882a593Smuzhiyun u64 disk_bytenr, u64 num_bytes,
171*4882a593Smuzhiyun u64 disk_num_bytes, int type,
172*4882a593Smuzhiyun int compress_type);
173*4882a593Smuzhiyun void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
174*4882a593Smuzhiyun struct btrfs_ordered_sum *sum);
175*4882a593Smuzhiyun struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,
176*4882a593Smuzhiyun u64 file_offset);
177*4882a593Smuzhiyun void btrfs_start_ordered_extent(struct btrfs_ordered_extent *entry, int wait);
178*4882a593Smuzhiyun int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len);
179*4882a593Smuzhiyun struct btrfs_ordered_extent *
180*4882a593Smuzhiyun btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset);
181*4882a593Smuzhiyun struct btrfs_ordered_extent *btrfs_lookup_ordered_range(
182*4882a593Smuzhiyun struct btrfs_inode *inode,
183*4882a593Smuzhiyun u64 file_offset,
184*4882a593Smuzhiyun u64 len);
185*4882a593Smuzhiyun void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode,
186*4882a593Smuzhiyun struct list_head *list);
187*4882a593Smuzhiyun int btrfs_find_ordered_sum(struct btrfs_inode *inode, u64 offset,
188*4882a593Smuzhiyun u64 disk_bytenr, u8 *sum, int len);
189*4882a593Smuzhiyun u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr,
190*4882a593Smuzhiyun const u64 range_start, const u64 range_len);
191*4882a593Smuzhiyun void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr,
192*4882a593Smuzhiyun const u64 range_start, const u64 range_len);
193*4882a593Smuzhiyun void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,
194*4882a593Smuzhiyun u64 end,
195*4882a593Smuzhiyun struct extent_state **cached_state);
196*4882a593Smuzhiyun int __init ordered_data_init(void);
197*4882a593Smuzhiyun void __cold ordered_data_exit(void);
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun #endif
200