1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _BCACHEFS_IO_WRITE_H
3#define _BCACHEFS_IO_WRITE_H
4
5#include "checksum.h"
6#include "io_write_types.h"
7
8#define to_wbio(_bio)			\
9	container_of((_bio), struct bch_write_bio, bio)
10
11void bch2_bio_free_pages_pool(struct bch_fs *, struct bio *);
12void bch2_bio_alloc_pages_pool(struct bch_fs *, struct bio *, size_t);
13
14#ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT
15void bch2_latency_acct(struct bch_dev *, u64, int);
16#else
17static inline void bch2_latency_acct(struct bch_dev *ca, u64 submit_time, int rw) {}
18#endif
19
20void bch2_submit_wbio_replicas(struct bch_write_bio *, struct bch_fs *,
21			       enum bch_data_type, const struct bkey_i *, bool);
22
23#define BCH_WRITE_FLAGS()		\
24	x(ALLOC_NOWAIT)			\
25	x(CACHED)			\
26	x(DATA_ENCODED)			\
27	x(PAGES_STABLE)			\
28	x(PAGES_OWNED)			\
29	x(ONLY_SPECIFIED_DEVS)		\
30	x(WROTE_DATA_INLINE)		\
31	x(FROM_INTERNAL)		\
32	x(CHECK_ENOSPC)			\
33	x(SYNC)				\
34	x(MOVE)				\
35	x(IN_WORKER)			\
36	x(DONE)				\
37	x(IO_ERROR)			\
38	x(CONVERT_UNWRITTEN)
39
40enum __bch_write_flags {
41#define x(f)	__BCH_WRITE_##f,
42	BCH_WRITE_FLAGS()
43#undef x
44};
45
46enum bch_write_flags {
47#define x(f)	BCH_WRITE_##f = BIT(__BCH_WRITE_##f),
48	BCH_WRITE_FLAGS()
49#undef x
50};
51
52static inline struct workqueue_struct *index_update_wq(struct bch_write_op *op)
53{
54	return op->watermark == BCH_WATERMARK_copygc
55		? op->c->copygc_wq
56		: op->c->btree_update_wq;
57}
58
59int bch2_sum_sector_overwrites(struct btree_trans *, struct btree_iter *,
60			       struct bkey_i *, bool *, s64 *, s64 *);
61int bch2_extent_update(struct btree_trans *, subvol_inum,
62		       struct btree_iter *, struct bkey_i *,
63		       struct disk_reservation *, u64, s64 *, bool);
64
65static inline void bch2_write_op_init(struct bch_write_op *op, struct bch_fs *c,
66				      struct bch_io_opts opts)
67{
68	op->c			= c;
69	op->end_io		= NULL;
70	op->flags		= 0;
71	op->written		= 0;
72	op->error		= 0;
73	op->csum_type		= bch2_data_checksum_type(c, opts);
74	op->compression_opt	= opts.compression;
75	op->nr_replicas		= 0;
76	op->nr_replicas_required = c->opts.data_replicas_required;
77	op->watermark		= BCH_WATERMARK_normal;
78	op->incompressible	= 0;
79	op->open_buckets.nr	= 0;
80	op->devs_have.nr	= 0;
81	op->target		= 0;
82	op->opts		= opts;
83	op->subvol		= 0;
84	op->pos			= POS_MAX;
85	op->version		= ZERO_VERSION;
86	op->write_point		= (struct write_point_specifier) { 0 };
87	op->res			= (struct disk_reservation) { 0 };
88	op->new_i_size		= U64_MAX;
89	op->i_sectors_delta	= 0;
90	op->devs_need_flush	= NULL;
91}
92
93CLOSURE_CALLBACK(bch2_write);
94void bch2_write_point_do_index_updates(struct work_struct *);
95
96static inline struct bch_write_bio *wbio_init(struct bio *bio)
97{
98	struct bch_write_bio *wbio = to_wbio(bio);
99
100	memset(&wbio->wbio, 0, sizeof(wbio->wbio));
101	return wbio;
102}
103
104void bch2_write_op_to_text(struct printbuf *, struct bch_write_op *);
105
106void bch2_fs_io_write_exit(struct bch_fs *);
107int bch2_fs_io_write_init(struct bch_fs *);
108
109#endif /* _BCACHEFS_IO_WRITE_H */
110