xref: /openbmc/linux/fs/btrfs/ordered-data.h (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
19888c340SDavid Sterba /* SPDX-License-Identifier: GPL-2.0 */
2dc17ff8fSChris Mason /*
3dc17ff8fSChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
4dc17ff8fSChris Mason  */
5dc17ff8fSChris Mason 
69888c340SDavid Sterba #ifndef BTRFS_ORDERED_DATA_H
79888c340SDavid Sterba #define BTRFS_ORDERED_DATA_H
8dc17ff8fSChris Mason 
9eb84ae03SChris Mason /* one of these per inode */
10dc17ff8fSChris Mason struct btrfs_ordered_inode_tree {
1149958fd7SJosef Bacik 	spinlock_t lock;
12dc17ff8fSChris Mason 	struct rb_root tree;
13e6dcd2dcSChris Mason 	struct rb_node *last;
14dc17ff8fSChris Mason };
15dc17ff8fSChris Mason 
16e6dcd2dcSChris Mason struct btrfs_ordered_sum {
17d20f7043SChris Mason 	/*
18d20f7043SChris Mason 	 * Logical start address and length for of the blocks covered by
19d20f7043SChris Mason 	 * the sums array.
203edf7d33SChris Mason 	 */
213edf7d33SChris Mason 	u64 logical;
223edf7d33SChris Mason 	u32 len;
23f51a4a18SMiao Xie 
24e6dcd2dcSChris Mason 	struct list_head list;
25f51a4a18SMiao Xie 	/* last field is a variable length array of csums */
261e25a2e3SJohannes Thumshirn 	u8 sums[];
27e6dcd2dcSChris Mason };
28e6dcd2dcSChris Mason 
29eb84ae03SChris Mason /*
303c198fe0SQu Wenruo  * Bits for btrfs_ordered_extent::flags.
31eb84ae03SChris Mason  *
32eb84ae03SChris Mason  * BTRFS_ORDERED_IO_DONE is set when all of the blocks are written.
33eb84ae03SChris Mason  * It is used to make sure metadata is inserted into the tree only once
34eb84ae03SChris Mason  * per extent.
35eb84ae03SChris Mason  *
36eb84ae03SChris Mason  * BTRFS_ORDERED_COMPLETE is set when the extent is removed from the
37eb84ae03SChris Mason  * rbtree, just before waking any waiters.  It is used to indicate the
38eb84ae03SChris Mason  * IO is done and any metadata is inserted into the tree.
39eb84ae03SChris Mason  */
405b840301SDavid Sterba enum {
413c198fe0SQu Wenruo 	/*
420b3dcd13SQu Wenruo 	 * Different types for ordered extents, one and only one of the 4 types
430b3dcd13SQu Wenruo 	 * need to be set when creating ordered extent.
443c198fe0SQu Wenruo 	 *
453c198fe0SQu Wenruo 	 * REGULAR:	For regular non-compressed COW write
463c198fe0SQu Wenruo 	 * NOCOW:	For NOCOW write into existing non-hole extent
473c198fe0SQu Wenruo 	 * PREALLOC:	For NOCOW write into preallocated extent
483c198fe0SQu Wenruo 	 * COMPRESSED:	For compressed COW write
493c198fe0SQu Wenruo 	 */
503c198fe0SQu Wenruo 	BTRFS_ORDERED_REGULAR,
513c198fe0SQu Wenruo 	BTRFS_ORDERED_NOCOW,
523c198fe0SQu Wenruo 	BTRFS_ORDERED_PREALLOC,
533c198fe0SQu Wenruo 	BTRFS_ORDERED_COMPRESSED,
543c198fe0SQu Wenruo 
553c198fe0SQu Wenruo 	/*
563c198fe0SQu Wenruo 	 * Extra bit for direct io, can only be set for
573c198fe0SQu Wenruo 	 * REGULAR/NOCOW/PREALLOC. No direct io for compressed extent.
583c198fe0SQu Wenruo 	 */
593c198fe0SQu Wenruo 	BTRFS_ORDERED_DIRECT,
603c198fe0SQu Wenruo 
613c198fe0SQu Wenruo 	/* Extra status bits for ordered extents */
623c198fe0SQu Wenruo 
635b840301SDavid Sterba 	/* set when all the pages are written */
645b840301SDavid Sterba 	BTRFS_ORDERED_IO_DONE,
655b840301SDavid Sterba 	/* set when removed from the tree */
665b840301SDavid Sterba 	BTRFS_ORDERED_COMPLETE,
675b840301SDavid Sterba 	/* We had an io error when writing this out */
685b840301SDavid Sterba 	BTRFS_ORDERED_IOERR,
695b840301SDavid Sterba 	/* Set when we have to truncate an extent */
705b840301SDavid Sterba 	BTRFS_ORDERED_TRUNCATED,
7148778179SFilipe Manana 	/* Used during fsync to track already logged extents */
7248778179SFilipe Manana 	BTRFS_ORDERED_LOGGED,
7348778179SFilipe Manana 	/* We have already logged all the csums of the ordered extent */
7448778179SFilipe Manana 	BTRFS_ORDERED_LOGGED_CSUM,
7548778179SFilipe Manana 	/* We wait for this extent to complete in the current transaction */
7648778179SFilipe Manana 	BTRFS_ORDERED_PENDING,
777c0c7269SOmar Sandoval 	/* BTRFS_IOC_ENCODED_WRITE */
787c0c7269SOmar Sandoval 	BTRFS_ORDERED_ENCODED,
795b840301SDavid Sterba };
801af4a0aaSLiu Bo 
81cb36a9bbSOmar Sandoval /* BTRFS_ORDERED_* flags that specify the type of the extent. */
82cb36a9bbSOmar Sandoval #define BTRFS_ORDERED_TYPE_FLAGS ((1UL << BTRFS_ORDERED_REGULAR) |	\
83cb36a9bbSOmar Sandoval 				  (1UL << BTRFS_ORDERED_NOCOW) |	\
84cb36a9bbSOmar Sandoval 				  (1UL << BTRFS_ORDERED_PREALLOC) |	\
85cb36a9bbSOmar Sandoval 				  (1UL << BTRFS_ORDERED_COMPRESSED) |	\
867c0c7269SOmar Sandoval 				  (1UL << BTRFS_ORDERED_DIRECT) |	\
877c0c7269SOmar Sandoval 				  (1UL << BTRFS_ORDERED_ENCODED))
88cb36a9bbSOmar Sandoval 
89e6dcd2dcSChris Mason struct btrfs_ordered_extent {
90eb84ae03SChris Mason 	/* logical offset in the file */
91e6dcd2dcSChris Mason 	u64 file_offset;
92eb84ae03SChris Mason 
93bffe633eSOmar Sandoval 	/*
94bffe633eSOmar Sandoval 	 * These fields directly correspond to the same fields in
95bffe633eSOmar Sandoval 	 * btrfs_file_extent_item.
96bffe633eSOmar Sandoval 	 */
97bffe633eSOmar Sandoval 	u64 num_bytes;
98cb36a9bbSOmar Sandoval 	u64 ram_bytes;
99cb36a9bbSOmar Sandoval 	u64 disk_bytenr;
100bffe633eSOmar Sandoval 	u64 disk_num_bytes;
101cb36a9bbSOmar Sandoval 	u64 offset;
102c8b97818SChris Mason 
1038b62b72bSChris Mason 	/* number of bytes that still need writing */
1048b62b72bSChris Mason 	u64 bytes_left;
1058b62b72bSChris Mason 
106b9a8cc5bSMiao Xie 	/*
107b9a8cc5bSMiao Xie 	 * the end of the ordered extent which is behind it but
108b9a8cc5bSMiao Xie 	 * didn't update disk_i_size. Please see the comment of
109b9a8cc5bSMiao Xie 	 * btrfs_ordered_update_i_size();
110b9a8cc5bSMiao Xie 	 */
111b9a8cc5bSMiao Xie 	u64 outstanding_isize;
112b9a8cc5bSMiao Xie 
11377cef2ecSJosef Bacik 	/*
11477cef2ecSJosef Bacik 	 * If we get truncated we need to adjust the file extent we enter for
11577cef2ecSJosef Bacik 	 * this ordered extent so that we do not expose stale data.
11677cef2ecSJosef Bacik 	 */
11777cef2ecSJosef Bacik 	u64 truncated_len;
11877cef2ecSJosef Bacik 
119eb84ae03SChris Mason 	/* flags (described above) */
120e6dcd2dcSChris Mason 	unsigned long flags;
121eb84ae03SChris Mason 
122261507a0SLi Zefan 	/* compression algorithm */
123261507a0SLi Zefan 	int compress_type;
124261507a0SLi Zefan 
1257dbeaad0SQu Wenruo 	/* Qgroup reserved space */
1267dbeaad0SQu Wenruo 	int qgroup_rsv;
1277dbeaad0SQu Wenruo 
128eb84ae03SChris Mason 	/* reference count */
129e76edab7SElena Reshetova 	refcount_t refs;
130eb84ae03SChris Mason 
1313eaa2885SChris Mason 	/* the inode we belong to */
1323eaa2885SChris Mason 	struct inode *inode;
1333eaa2885SChris Mason 
134eb84ae03SChris Mason 	/* list of checksums for insertion when the extent io is done */
135e6dcd2dcSChris Mason 	struct list_head list;
136eb84ae03SChris Mason 
13748778179SFilipe Manana 	/* used for fast fsyncs */
13848778179SFilipe Manana 	struct list_head log_list;
13948778179SFilipe Manana 
140eb84ae03SChris Mason 	/* used to wait for the BTRFS_ORDERED_COMPLETE bit */
141e6dcd2dcSChris Mason 	wait_queue_head_t wait;
142eb84ae03SChris Mason 
143eb84ae03SChris Mason 	/* our friendly rbtree entry */
144e6dcd2dcSChris Mason 	struct rb_node rb_node;
1453eaa2885SChris Mason 
1463eaa2885SChris Mason 	/* a per root list of all the pending ordered extents */
1473eaa2885SChris Mason 	struct list_head root_extent_list;
1485fd02043SJosef Bacik 
149d458b054SQu Wenruo 	struct btrfs_work work;
150e6dcd2dcSChris Mason 
1519afab882SMiao Xie 	struct completion completion;
152d458b054SQu Wenruo 	struct btrfs_work flush_work;
1539afab882SMiao Xie 	struct list_head work_list;
154d8e3fb10SNaohiro Aota };
155d8e3fb10SNaohiro Aota 
156d8e3fb10SNaohiro Aota static inline void
btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree * t)157d8e3fb10SNaohiro Aota btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree *t)
158d8e3fb10SNaohiro Aota {
159d8e3fb10SNaohiro Aota 	spin_lock_init(&t->lock);
1609afab882SMiao Xie 	t->tree = RB_ROOT;
161e6dcd2dcSChris Mason 	t->last = NULL;
162dc17ff8fSChris Mason }
163dc17ff8fSChris Mason 
164dc17ff8fSChris Mason int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent);
16549958fd7SJosef Bacik int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
1666bef4d31SEric Paris 
167e6dcd2dcSChris Mason void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry);
168dc17ff8fSChris Mason void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode,
169dc17ff8fSChris Mason 				struct btrfs_ordered_extent *entry);
170711f447bSChristoph Hellwig bool btrfs_finish_ordered_extent(struct btrfs_ordered_extent *ordered,
171711f447bSChristoph Hellwig 				 struct page *page, u64 file_offset, u64 len,
172143bede5SJeff Mahoney 				 bool uptodate);
17371fe0a55SNikolay Borisov void btrfs_mark_ordered_io_finished(struct btrfs_inode *inode,
174e6dcd2dcSChris Mason 				struct page *page, u64 file_offset,
175e65f152eSQu Wenruo 				u64 num_bytes, bool uptodate);
176e65f152eSQu Wenruo bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode,
177711f447bSChristoph Hellwig 				    struct btrfs_ordered_extent **cached,
17858f74b22SQu Wenruo 				    u64 file_offset, u64 io_size);
1795a1a3df1SJosef Bacik struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
180f41b6ba9SDavid Sterba 			struct btrfs_inode *inode, u64 file_offset,
181cf6d1aa4SBoris Burkov 			u64 num_bytes, u64 ram_bytes, u64 disk_bytenr,
182cf6d1aa4SBoris Burkov 			u64 disk_num_bytes, u64 offset, unsigned long flags,
183cf6d1aa4SBoris Burkov 			int compress_type);
184cf6d1aa4SBoris Burkov void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
185cf6d1aa4SBoris Burkov 			   struct btrfs_ordered_sum *sum);
186e7fbf604SNikolay Borisov struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,
187cb36a9bbSOmar Sandoval 							 u64 file_offset);
1888725bddfSBoris Burkov void btrfs_start_ordered_extent(struct btrfs_ordered_extent *entry);
189cb36a9bbSOmar Sandoval int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len);
190f9756261SNikolay Borisov struct btrfs_ordered_extent *
1913edf7d33SChris Mason btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset);
192c3504372SNikolay Borisov struct btrfs_ordered_extent *btrfs_lookup_first_ordered_range(
193e6dcd2dcSChris Mason 			struct btrfs_inode *inode, u64 file_offset, u64 len);
19436d45567SChristoph Hellwig struct btrfs_ordered_extent *btrfs_lookup_ordered_range(
1950ef8b726SJosef Bacik 		struct btrfs_inode *inode,
196e6dcd2dcSChris Mason 		u64 file_offset,
1976d072c8eSNikolay Borisov 		u64 len);
198c095f333SQu Wenruo void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode,
199c095f333SQu Wenruo 					   struct list_head *list);
200a776c6faSNikolay Borisov u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr,
201a776c6faSNikolay Borisov 			       const u64 range_start, const u64 range_len);
2024b46fce2SJosef Bacik void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr,
2034b46fce2SJosef Bacik 			      const u64 range_start, const u64 range_len);
20448778179SFilipe Manana void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,
20548778179SFilipe Manana 					u64 end,
2066374e57aSChris Mason 					struct extent_state **cached_state);
207578def7cSFilipe Manana bool btrfs_try_lock_ordered_range(struct btrfs_inode *inode, u64 start, u64 end,
208042528f8SFilipe Manana 				  struct extent_state **cached_state);
209578def7cSFilipe Manana struct btrfs_ordered_extent *btrfs_split_ordered_extent(
210b272ae22SDavid Sterba 			struct btrfs_ordered_extent *ordered, u64 len);
211ffa87214SNikolay Borisov int __init ordered_data_init(void);
212ffa87214SNikolay Borisov void __cold ordered_data_exit(void);
213632ddfa2SJosef Bacik 
214632ddfa2SJosef Bacik #endif
215*8f4af4b8SChristoph Hellwig