xref: /openbmc/linux/fs/btrfs/qgroup.h (revision 1c9a5c49)
19888c340SDavid Sterba /* SPDX-License-Identifier: GPL-2.0 */
2fcebe456SJosef Bacik /*
3fcebe456SJosef Bacik  * Copyright (C) 2014 Facebook.  All rights reserved.
4fcebe456SJosef Bacik  */
5fcebe456SJosef Bacik 
69888c340SDavid Sterba #ifndef BTRFS_QGROUP_H
79888c340SDavid Sterba #define BTRFS_QGROUP_H
8fcebe456SJosef Bacik 
9370a11b8SQu Wenruo #include <linux/spinlock.h>
10370a11b8SQu Wenruo #include <linux/rbtree.h>
1149e5fb46SQu Wenruo #include <linux/kobject.h>
123368d001SQu Wenruo #include "ulist.h"
133368d001SQu Wenruo #include "delayed-ref.h"
14e0a8b9a7SDavid Sterba #include "misc.h"
153368d001SQu Wenruo 
16fcebe456SJosef Bacik /*
171d2beaa9SQu Wenruo  * Btrfs qgroup overview
181d2beaa9SQu Wenruo  *
191d2beaa9SQu Wenruo  * Btrfs qgroup splits into 3 main part:
201d2beaa9SQu Wenruo  * 1) Reserve
211d2beaa9SQu Wenruo  *    Reserve metadata/data space for incoming operations
221d2beaa9SQu Wenruo  *    Affect how qgroup limit works
231d2beaa9SQu Wenruo  *
241d2beaa9SQu Wenruo  * 2) Trace
251d2beaa9SQu Wenruo  *    Tell btrfs qgroup to trace dirty extents.
261d2beaa9SQu Wenruo  *
271d2beaa9SQu Wenruo  *    Dirty extents including:
281d2beaa9SQu Wenruo  *    - Newly allocated extents
291d2beaa9SQu Wenruo  *    - Extents going to be deleted (in this trans)
301d2beaa9SQu Wenruo  *    - Extents whose owner is going to be modified
311d2beaa9SQu Wenruo  *
321d2beaa9SQu Wenruo  *    This is the main part affects whether qgroup numbers will stay
331d2beaa9SQu Wenruo  *    consistent.
341d2beaa9SQu Wenruo  *    Btrfs qgroup can trace clean extents and won't cause any problem,
351d2beaa9SQu Wenruo  *    but it will consume extra CPU time, it should be avoided if possible.
361d2beaa9SQu Wenruo  *
371d2beaa9SQu Wenruo  * 3) Account
381d2beaa9SQu Wenruo  *    Btrfs qgroup will updates its numbers, based on dirty extents traced
391d2beaa9SQu Wenruo  *    in previous step.
401d2beaa9SQu Wenruo  *
411d2beaa9SQu Wenruo  *    Normally at qgroup rescan and transaction commit time.
421d2beaa9SQu Wenruo  */
431d2beaa9SQu Wenruo 
441d2beaa9SQu Wenruo /*
45370a11b8SQu Wenruo  * Special performance optimization for balance.
46370a11b8SQu Wenruo  *
47370a11b8SQu Wenruo  * For balance, we need to swap subtree of subvolume and reloc trees.
48370a11b8SQu Wenruo  * In theory, we need to trace all subtree blocks of both subvolume and reloc
49370a11b8SQu Wenruo  * trees, since their owner has changed during such swap.
50370a11b8SQu Wenruo  *
51370a11b8SQu Wenruo  * However since balance has ensured that both subtrees are containing the
52370a11b8SQu Wenruo  * same contents and have the same tree structures, such swap won't cause
53370a11b8SQu Wenruo  * qgroup number change.
54370a11b8SQu Wenruo  *
55370a11b8SQu Wenruo  * But there is a race window between subtree swap and transaction commit,
56370a11b8SQu Wenruo  * during that window, if we increase/decrease tree level or merge/split tree
57370a11b8SQu Wenruo  * blocks, we still need to trace the original subtrees.
58370a11b8SQu Wenruo  *
59370a11b8SQu Wenruo  * So for balance, we use a delayed subtree tracing, whose workflow is:
60370a11b8SQu Wenruo  *
61370a11b8SQu Wenruo  * 1) Record the subtree root block get swapped.
62370a11b8SQu Wenruo  *
63370a11b8SQu Wenruo  *    During subtree swap:
64370a11b8SQu Wenruo  *    O = Old tree blocks
65370a11b8SQu Wenruo  *    N = New tree blocks
66370a11b8SQu Wenruo  *          reloc tree                     subvolume tree X
67370a11b8SQu Wenruo  *             Root                               Root
68370a11b8SQu Wenruo  *            /    \                             /    \
69370a11b8SQu Wenruo  *          NA     OB                          OA      OB
70370a11b8SQu Wenruo  *        /  |     |  \                      /  |      |  \
71370a11b8SQu Wenruo  *      NC  ND     OE  OF                   OC  OD     OE  OF
72370a11b8SQu Wenruo  *
73370a11b8SQu Wenruo  *   In this case, NA and OA are going to be swapped, record (NA, OA) into
74370a11b8SQu Wenruo  *   subvolume tree X.
75370a11b8SQu Wenruo  *
76370a11b8SQu Wenruo  * 2) After subtree swap.
77370a11b8SQu Wenruo  *          reloc tree                     subvolume tree X
78370a11b8SQu Wenruo  *             Root                               Root
79370a11b8SQu Wenruo  *            /    \                             /    \
80370a11b8SQu Wenruo  *          OA     OB                          NA      OB
81370a11b8SQu Wenruo  *        /  |     |  \                      /  |      |  \
82370a11b8SQu Wenruo  *      OC  OD     OE  OF                   NC  ND     OE  OF
83370a11b8SQu Wenruo  *
84370a11b8SQu Wenruo  * 3a) COW happens for OB
85370a11b8SQu Wenruo  *     If we are going to COW tree block OB, we check OB's bytenr against
86370a11b8SQu Wenruo  *     tree X's swapped_blocks structure.
87370a11b8SQu Wenruo  *     If it doesn't fit any, nothing will happen.
88370a11b8SQu Wenruo  *
89370a11b8SQu Wenruo  * 3b) COW happens for NA
90370a11b8SQu Wenruo  *     Check NA's bytenr against tree X's swapped_blocks, and get a hit.
91370a11b8SQu Wenruo  *     Then we do subtree scan on both subtrees OA and NA.
92370a11b8SQu Wenruo  *     Resulting 6 tree blocks to be scanned (OA, OC, OD, NA, NC, ND).
93370a11b8SQu Wenruo  *
94370a11b8SQu Wenruo  *     Then no matter what we do to subvolume tree X, qgroup numbers will
95370a11b8SQu Wenruo  *     still be correct.
96370a11b8SQu Wenruo  *     Then NA's record gets removed from X's swapped_blocks.
97370a11b8SQu Wenruo  *
98370a11b8SQu Wenruo  * 4)  Transaction commit
99370a11b8SQu Wenruo  *     Any record in X's swapped_blocks gets removed, since there is no
100370a11b8SQu Wenruo  *     modification to the swapped subtrees, no need to trigger heavy qgroup
101370a11b8SQu Wenruo  *     subtree rescan for them.
102370a11b8SQu Wenruo  */
103370a11b8SQu Wenruo 
104e562a8bdSQu Wenruo #define BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN		(1UL << 3)
105e15e9f43SQu Wenruo #define BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING		(1UL << 4)
106e562a8bdSQu Wenruo 
107370a11b8SQu Wenruo /*
1083368d001SQu Wenruo  * Record a dirty extent, and info qgroup to update quota on it
1093368d001SQu Wenruo  * TODO: Use kmem cache to alloc it.
1103368d001SQu Wenruo  */
1113368d001SQu Wenruo struct btrfs_qgroup_extent_record {
1123368d001SQu Wenruo 	struct rb_node node;
1133368d001SQu Wenruo 	u64 bytenr;
1143368d001SQu Wenruo 	u64 num_bytes;
1151418bae1SQu Wenruo 
1161418bae1SQu Wenruo 	/*
1171418bae1SQu Wenruo 	 * For qgroup reserved data space freeing.
1181418bae1SQu Wenruo 	 *
1191418bae1SQu Wenruo 	 * @data_rsv_refroot and @data_rsv will be recorded after
1201418bae1SQu Wenruo 	 * BTRFS_ADD_DELAYED_EXTENT is called.
1211418bae1SQu Wenruo 	 * And will be used to free reserved qgroup space at
1221418bae1SQu Wenruo 	 * transaction commit time.
1231418bae1SQu Wenruo 	 */
1241418bae1SQu Wenruo 	u32 data_rsv;		/* reserved data space needs to be freed */
1251418bae1SQu Wenruo 	u64 data_rsv_refroot;	/* which root the reserved data belongs to */
1263368d001SQu Wenruo 	struct ulist *old_roots;
1273368d001SQu Wenruo };
1283368d001SQu Wenruo 
129370a11b8SQu Wenruo struct btrfs_qgroup_swapped_block {
130370a11b8SQu Wenruo 	struct rb_node node;
131370a11b8SQu Wenruo 
132370a11b8SQu Wenruo 	int level;
133370a11b8SQu Wenruo 	bool trace_leaf;
134370a11b8SQu Wenruo 
135370a11b8SQu Wenruo 	/* bytenr/generation of the tree block in subvolume tree after swap */
136370a11b8SQu Wenruo 	u64 subvol_bytenr;
137370a11b8SQu Wenruo 	u64 subvol_generation;
138370a11b8SQu Wenruo 
139370a11b8SQu Wenruo 	/* bytenr/generation of the tree block in reloc tree after swap */
140370a11b8SQu Wenruo 	u64 reloc_bytenr;
141370a11b8SQu Wenruo 	u64 reloc_generation;
142370a11b8SQu Wenruo 
143370a11b8SQu Wenruo 	u64 last_snapshot;
144370a11b8SQu Wenruo 	struct btrfs_key first_key;
145370a11b8SQu Wenruo };
146370a11b8SQu Wenruo 
147733e03a0SQu Wenruo /*
148733e03a0SQu Wenruo  * Qgroup reservation types:
149733e03a0SQu Wenruo  *
150733e03a0SQu Wenruo  * DATA:
151733e03a0SQu Wenruo  *	space reserved for data
152733e03a0SQu Wenruo  *
153733e03a0SQu Wenruo  * META_PERTRANS:
154733e03a0SQu Wenruo  * 	Space reserved for metadata (per-transaction)
155733e03a0SQu Wenruo  * 	Due to the fact that qgroup data is only updated at transaction commit
156733e03a0SQu Wenruo  * 	time, reserved space for metadata must be kept until transaction
157733e03a0SQu Wenruo  * 	commits.
158733e03a0SQu Wenruo  * 	Any metadata reserved that are used in btrfs_start_transaction() should
159733e03a0SQu Wenruo  * 	be of this type.
160733e03a0SQu Wenruo  *
161733e03a0SQu Wenruo  * META_PREALLOC:
162733e03a0SQu Wenruo  *	There are cases where metadata space is reserved before starting
163733e03a0SQu Wenruo  *	transaction, and then btrfs_join_transaction() to get a trans handle.
164733e03a0SQu Wenruo  *	Any metadata reserved for such usage should be of this type.
165733e03a0SQu Wenruo  *	And after join_transaction() part (or all) of such reservation should
166733e03a0SQu Wenruo  *	be converted into META_PERTRANS.
167733e03a0SQu Wenruo  */
168d4e5c920SQu Wenruo enum btrfs_qgroup_rsv_type {
169bbe339ccSDavid Sterba 	BTRFS_QGROUP_RSV_DATA,
170733e03a0SQu Wenruo 	BTRFS_QGROUP_RSV_META_PERTRANS,
171733e03a0SQu Wenruo 	BTRFS_QGROUP_RSV_META_PREALLOC,
172d4e5c920SQu Wenruo 	BTRFS_QGROUP_RSV_LAST,
173d4e5c920SQu Wenruo };
174d4e5c920SQu Wenruo 
175d4e5c920SQu Wenruo /*
176d4e5c920SQu Wenruo  * Represents how many bytes we have reserved for this qgroup.
177d4e5c920SQu Wenruo  *
178d4e5c920SQu Wenruo  * Each type should have different reservation behavior.
179d4e5c920SQu Wenruo  * E.g, data follows its io_tree flag modification, while
18052042d8eSAndrea Gelmini  * *currently* meta is just reserve-and-clear during transaction.
181d4e5c920SQu Wenruo  *
182d4e5c920SQu Wenruo  * TODO: Add new type for reservation which can survive transaction commit.
18352042d8eSAndrea Gelmini  * Current metadata reservation behavior is not suitable for such case.
184d4e5c920SQu Wenruo  */
185d4e5c920SQu Wenruo struct btrfs_qgroup_rsv {
186d4e5c920SQu Wenruo 	u64 values[BTRFS_QGROUP_RSV_LAST];
187d4e5c920SQu Wenruo };
188d4e5c920SQu Wenruo 
18981fb6f77SQu Wenruo /*
1903159fe7bSQu Wenruo  * one struct for each qgroup, organized in fs_info->qgroup_tree.
1913159fe7bSQu Wenruo  */
1923159fe7bSQu Wenruo struct btrfs_qgroup {
1933159fe7bSQu Wenruo 	u64 qgroupid;
1943159fe7bSQu Wenruo 
1953159fe7bSQu Wenruo 	/*
1963159fe7bSQu Wenruo 	 * state
1973159fe7bSQu Wenruo 	 */
1983159fe7bSQu Wenruo 	u64 rfer;	/* referenced */
1993159fe7bSQu Wenruo 	u64 rfer_cmpr;	/* referenced compressed */
2003159fe7bSQu Wenruo 	u64 excl;	/* exclusive */
2013159fe7bSQu Wenruo 	u64 excl_cmpr;	/* exclusive compressed */
2023159fe7bSQu Wenruo 
2033159fe7bSQu Wenruo 	/*
2043159fe7bSQu Wenruo 	 * limits
2053159fe7bSQu Wenruo 	 */
2063159fe7bSQu Wenruo 	u64 lim_flags;	/* which limits are set */
2073159fe7bSQu Wenruo 	u64 max_rfer;
2083159fe7bSQu Wenruo 	u64 max_excl;
2093159fe7bSQu Wenruo 	u64 rsv_rfer;
2103159fe7bSQu Wenruo 	u64 rsv_excl;
2113159fe7bSQu Wenruo 
2123159fe7bSQu Wenruo 	/*
2133159fe7bSQu Wenruo 	 * reservation tracking
2143159fe7bSQu Wenruo 	 */
215d4e5c920SQu Wenruo 	struct btrfs_qgroup_rsv rsv;
2163159fe7bSQu Wenruo 
2173159fe7bSQu Wenruo 	/*
2183159fe7bSQu Wenruo 	 * lists
2193159fe7bSQu Wenruo 	 */
2203159fe7bSQu Wenruo 	struct list_head groups;  /* groups this group is member of */
2213159fe7bSQu Wenruo 	struct list_head members; /* groups that are members of this group */
2223159fe7bSQu Wenruo 	struct list_head dirty;   /* dirty groups */
223*1c9a5c49SQu Wenruo 
224*1c9a5c49SQu Wenruo 	/*
225*1c9a5c49SQu Wenruo 	 * For qgroup iteration usage.
226*1c9a5c49SQu Wenruo 	 *
227*1c9a5c49SQu Wenruo 	 * The iteration list should always be empty until qgroup_iterator_add()
228*1c9a5c49SQu Wenruo 	 * is called.  And should be reset to empty after the iteration is
229*1c9a5c49SQu Wenruo 	 * finished.
230*1c9a5c49SQu Wenruo 	 */
231*1c9a5c49SQu Wenruo 	struct list_head iterator;
2323159fe7bSQu Wenruo 	struct rb_node node;	  /* tree of qgroups */
2333159fe7bSQu Wenruo 
2343159fe7bSQu Wenruo 	/*
2353159fe7bSQu Wenruo 	 * temp variables for accounting operations
2363159fe7bSQu Wenruo 	 * Refer to qgroup_shared_accounting() for details.
2373159fe7bSQu Wenruo 	 */
2383159fe7bSQu Wenruo 	u64 old_refcnt;
2393159fe7bSQu Wenruo 	u64 new_refcnt;
24049e5fb46SQu Wenruo 
24149e5fb46SQu Wenruo 	/*
24249e5fb46SQu Wenruo 	 * Sysfs kobjectid
24349e5fb46SQu Wenruo 	 */
24449e5fb46SQu Wenruo 	struct kobject kobj;
2453159fe7bSQu Wenruo };
2463159fe7bSQu Wenruo 
btrfs_qgroup_subvolid(u64 qgroupid)24749e5fb46SQu Wenruo static inline u64 btrfs_qgroup_subvolid(u64 qgroupid)
24849e5fb46SQu Wenruo {
24949e5fb46SQu Wenruo 	return (qgroupid & ((1ULL << BTRFS_QGROUP_LEVEL_SHIFT) - 1));
25049e5fb46SQu Wenruo }
25149e5fb46SQu Wenruo 
2523159fe7bSQu Wenruo /*
25381fb6f77SQu Wenruo  * For qgroup event trace points only
25481fb6f77SQu Wenruo  */
255e0a8b9a7SDavid Sterba enum {
256e0a8b9a7SDavid Sterba 	ENUM_BIT(QGROUP_RESERVE),
257e0a8b9a7SDavid Sterba 	ENUM_BIT(QGROUP_RELEASE),
258e0a8b9a7SDavid Sterba 	ENUM_BIT(QGROUP_FREE),
259e0a8b9a7SDavid Sterba };
26081fb6f77SQu Wenruo 
261340f1aa2SNikolay Borisov int btrfs_quota_enable(struct btrfs_fs_info *fs_info);
262340f1aa2SNikolay Borisov int btrfs_quota_disable(struct btrfs_fs_info *fs_info);
263fcebe456SJosef Bacik int btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info);
264fcebe456SJosef Bacik void btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info);
265d06f23d6SJeff Mahoney int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
266d06f23d6SJeff Mahoney 				     bool interruptible);
2679f8a6ce6SLu Fengqi int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
2689f8a6ce6SLu Fengqi 			      u64 dst);
26939616c27SLu Fengqi int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
27039616c27SLu Fengqi 			      u64 dst);
27149a05ecdSLu Fengqi int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid);
2723efbee1dSLu Fengqi int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid);
273f0042d5eSLu Fengqi int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid,
274fcebe456SJosef Bacik 		       struct btrfs_qgroup_limit *limit);
275fcebe456SJosef Bacik int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info);
276fcebe456SJosef Bacik void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info);
277fcebe456SJosef Bacik struct btrfs_delayed_extent_op;
278d1b8b94aSQu Wenruo 
279cb93b52cSQu Wenruo /*
28050b3e040SQu Wenruo  * Inform qgroup to trace one dirty extent, its info is recorded in @record.
281fb235dc0SQu Wenruo  * So qgroup can account it at transaction committing time.
282cb93b52cSQu Wenruo  *
283fb235dc0SQu Wenruo  * No lock version, caller must acquire delayed ref lock and allocated memory,
284fb235dc0SQu Wenruo  * then call btrfs_qgroup_trace_extent_post() after exiting lock context.
285cb93b52cSQu Wenruo  *
286cb93b52cSQu Wenruo  * Return 0 for success insert
287cb93b52cSQu Wenruo  * Return >0 for existing record, caller can free @record safely.
288cb93b52cSQu Wenruo  * Error is not possible
289cb93b52cSQu Wenruo  */
29050b3e040SQu Wenruo int btrfs_qgroup_trace_extent_nolock(
291cb93b52cSQu Wenruo 		struct btrfs_fs_info *fs_info,
292bc074524SJeff Mahoney 		struct btrfs_delayed_ref_root *delayed_refs,
2933368d001SQu Wenruo 		struct btrfs_qgroup_extent_record *record);
294cb93b52cSQu Wenruo 
295cb93b52cSQu Wenruo /*
296fb235dc0SQu Wenruo  * Post handler after qgroup_trace_extent_nolock().
297fb235dc0SQu Wenruo  *
298fb235dc0SQu Wenruo  * NOTE: Current qgroup does the expensive backref walk at transaction
299fb235dc0SQu Wenruo  * committing time with TRANS_STATE_COMMIT_DOING, this blocks incoming
300fb235dc0SQu Wenruo  * new transaction.
301fb235dc0SQu Wenruo  * This is designed to allow btrfs_find_all_roots() to get correct new_roots
302fb235dc0SQu Wenruo  * result.
303fb235dc0SQu Wenruo  *
304fb235dc0SQu Wenruo  * However for old_roots there is no need to do backref walk at that time,
305fb235dc0SQu Wenruo  * since we search commit roots to walk backref and result will always be
306fb235dc0SQu Wenruo  * correct.
307fb235dc0SQu Wenruo  *
308fb235dc0SQu Wenruo  * Due to the nature of no lock version, we can't do backref there.
309fb235dc0SQu Wenruo  * So we must call btrfs_qgroup_trace_extent_post() after exiting
310fb235dc0SQu Wenruo  * spinlock context.
311fb235dc0SQu Wenruo  *
312fb235dc0SQu Wenruo  * TODO: If we can fix and prove btrfs_find_all_roots() can get correct result
313fb235dc0SQu Wenruo  * using current root, then we can move all expensive backref walk out of
314fb235dc0SQu Wenruo  * transaction committing, but not now as qgroup accounting will be wrong again.
315fb235dc0SQu Wenruo  */
3168949b9a1SFilipe Manana int btrfs_qgroup_trace_extent_post(struct btrfs_trans_handle *trans,
317fb235dc0SQu Wenruo 				   struct btrfs_qgroup_extent_record *qrecord);
318fb235dc0SQu Wenruo 
319fb235dc0SQu Wenruo /*
32050b3e040SQu Wenruo  * Inform qgroup to trace one dirty extent, specified by @bytenr and
32150b3e040SQu Wenruo  * @num_bytes.
32250b3e040SQu Wenruo  * So qgroup can account it at commit trans time.
323cb93b52cSQu Wenruo  *
324fb235dc0SQu Wenruo  * Better encapsulated version, with memory allocation and backref walk for
325fb235dc0SQu Wenruo  * commit roots.
326fb235dc0SQu Wenruo  * So this can sleep.
327cb93b52cSQu Wenruo  *
328cb93b52cSQu Wenruo  * Return 0 if the operation is done.
329cb93b52cSQu Wenruo  * Return <0 for error, like memory allocation failure or invalid parameter
330cb93b52cSQu Wenruo  * (NULL trans)
331cb93b52cSQu Wenruo  */
332a95f3aafSLu Fengqi int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr,
333e2896e79SDavid Sterba 			      u64 num_bytes);
334cb93b52cSQu Wenruo 
33533d1f05cSQu Wenruo /*
33633d1f05cSQu Wenruo  * Inform qgroup to trace all leaf items of data
33733d1f05cSQu Wenruo  *
33833d1f05cSQu Wenruo  * Return 0 for success
33933d1f05cSQu Wenruo  * Return <0 for error(ENOMEM)
34033d1f05cSQu Wenruo  */
34133d1f05cSQu Wenruo int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
34233d1f05cSQu Wenruo 				  struct extent_buffer *eb);
34333d1f05cSQu Wenruo /*
34433d1f05cSQu Wenruo  * Inform qgroup to trace a whole subtree, including all its child tree
34533d1f05cSQu Wenruo  * blocks and data.
34633d1f05cSQu Wenruo  * The root tree block is specified by @root_eb.
34733d1f05cSQu Wenruo  *
34833d1f05cSQu Wenruo  * Normally used by relocation(tree block swap) and subvolume deletion.
34933d1f05cSQu Wenruo  *
35033d1f05cSQu Wenruo  * Return 0 for success
35133d1f05cSQu Wenruo  * Return <0 for error(ENOMEM or tree search error)
35233d1f05cSQu Wenruo  */
35333d1f05cSQu Wenruo int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
35433d1f05cSQu Wenruo 			       struct extent_buffer *root_eb,
35533d1f05cSQu Wenruo 			       u64 root_gen, int root_level);
3568696d760SLu Fengqi int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr,
3578696d760SLu Fengqi 				u64 num_bytes, struct ulist *old_roots,
3588696d760SLu Fengqi 				struct ulist *new_roots);
359460fb20aSNikolay Borisov int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans);
360280f8bd2SLu Fengqi int btrfs_run_qgroups(struct btrfs_trans_handle *trans);
361a9377422SLu Fengqi int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
362a9377422SLu Fengqi 			 u64 objectid, struct btrfs_qgroup_inherit *inherit);
363297d750bSQu Wenruo void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
364d4e5c920SQu Wenruo 			       u64 ref_root, u64 num_bytes,
365d4e5c920SQu Wenruo 			       enum btrfs_qgroup_rsv_type type);
366fcebe456SJosef Bacik 
367fcebe456SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
368fcebe456SJosef Bacik int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
369fcebe456SJosef Bacik 			       u64 rfer, u64 excl);
370fcebe456SJosef Bacik #endif
371fcebe456SJosef Bacik 
37252472553SQu Wenruo /* New io_tree based accurate qgroup reserve API */
3737661a3e0SNikolay Borisov int btrfs_qgroup_reserve_data(struct btrfs_inode *inode,
374364ecf36SQu Wenruo 			struct extent_changeset **reserved, u64 start, u64 len);
37518234915SBoris Burkov int btrfs_qgroup_release_data(struct btrfs_inode *inode, u64 start, u64 len, u64 *released);
3768b8a979fSNikolay Borisov int btrfs_qgroup_free_data(struct btrfs_inode *inode,
3778b8a979fSNikolay Borisov 			   struct extent_changeset *reserved, u64 start,
37818234915SBoris Burkov 			   u64 len, u64 *freed);
37980e9baedSNikolay Borisov int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
38080e9baedSNikolay Borisov 			      enum btrfs_qgroup_rsv_type type, bool enforce);
381733e03a0SQu Wenruo int __btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
382d4135134SFilipe Manana 				enum btrfs_qgroup_rsv_type type, bool enforce,
383d4135134SFilipe Manana 				bool noflush);
384733e03a0SQu Wenruo /* Reserve metadata space for pertrans and prealloc type */
btrfs_qgroup_reserve_meta_pertrans(struct btrfs_root * root,int num_bytes,bool enforce)385733e03a0SQu Wenruo static inline int btrfs_qgroup_reserve_meta_pertrans(struct btrfs_root *root,
386733e03a0SQu Wenruo 				int num_bytes, bool enforce)
387733e03a0SQu Wenruo {
388733e03a0SQu Wenruo 	return __btrfs_qgroup_reserve_meta(root, num_bytes,
389d4135134SFilipe Manana 					   BTRFS_QGROUP_RSV_META_PERTRANS,
390d4135134SFilipe Manana 					   enforce, false);
391733e03a0SQu Wenruo }
btrfs_qgroup_reserve_meta_prealloc(struct btrfs_root * root,int num_bytes,bool enforce,bool noflush)392733e03a0SQu Wenruo static inline int btrfs_qgroup_reserve_meta_prealloc(struct btrfs_root *root,
393d4135134SFilipe Manana 						     int num_bytes, bool enforce,
394d4135134SFilipe Manana 						     bool noflush)
395733e03a0SQu Wenruo {
396733e03a0SQu Wenruo 	return __btrfs_qgroup_reserve_meta(root, num_bytes,
397d4135134SFilipe Manana 					   BTRFS_QGROUP_RSV_META_PREALLOC,
398d4135134SFilipe Manana 					   enforce, noflush);
399733e03a0SQu Wenruo }
400733e03a0SQu Wenruo 
401733e03a0SQu Wenruo void __btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes,
402733e03a0SQu Wenruo 			     enum btrfs_qgroup_rsv_type type);
403733e03a0SQu Wenruo 
404733e03a0SQu Wenruo /* Free per-transaction meta reservation for error handling */
btrfs_qgroup_free_meta_pertrans(struct btrfs_root * root,int num_bytes)405733e03a0SQu Wenruo static inline void btrfs_qgroup_free_meta_pertrans(struct btrfs_root *root,
406733e03a0SQu Wenruo 						   int num_bytes)
407733e03a0SQu Wenruo {
408733e03a0SQu Wenruo 	__btrfs_qgroup_free_meta(root, num_bytes,
409733e03a0SQu Wenruo 			BTRFS_QGROUP_RSV_META_PERTRANS);
410733e03a0SQu Wenruo }
411733e03a0SQu Wenruo 
412733e03a0SQu Wenruo /* Pre-allocated meta reservation can be freed at need */
btrfs_qgroup_free_meta_prealloc(struct btrfs_root * root,int num_bytes)413733e03a0SQu Wenruo static inline void btrfs_qgroup_free_meta_prealloc(struct btrfs_root *root,
414733e03a0SQu Wenruo 						   int num_bytes)
415733e03a0SQu Wenruo {
416733e03a0SQu Wenruo 	__btrfs_qgroup_free_meta(root, num_bytes,
417733e03a0SQu Wenruo 			BTRFS_QGROUP_RSV_META_PREALLOC);
418733e03a0SQu Wenruo }
419733e03a0SQu Wenruo 
420733e03a0SQu Wenruo /*
421733e03a0SQu Wenruo  * Per-transaction meta reservation should be all freed at transaction commit
422733e03a0SQu Wenruo  * time
423733e03a0SQu Wenruo  */
424733e03a0SQu Wenruo void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root);
425733e03a0SQu Wenruo 
42664cfaef6SQu Wenruo /*
42764cfaef6SQu Wenruo  * Convert @num_bytes of META_PREALLOCATED reservation to META_PERTRANS.
42864cfaef6SQu Wenruo  *
42964cfaef6SQu Wenruo  * This is called when preallocated meta reservation needs to be used.
43064cfaef6SQu Wenruo  * Normally after btrfs_join_transaction() call.
43164cfaef6SQu Wenruo  */
43264cfaef6SQu Wenruo void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes);
43364cfaef6SQu Wenruo 
434cfdd4592SNikolay Borisov void btrfs_qgroup_check_reserved_leak(struct btrfs_inode *inode);
4359888c340SDavid Sterba 
436370a11b8SQu Wenruo /* btrfs_qgroup_swapped_blocks related functions */
437370a11b8SQu Wenruo void btrfs_qgroup_init_swapped_blocks(
438370a11b8SQu Wenruo 	struct btrfs_qgroup_swapped_blocks *swapped_blocks);
439370a11b8SQu Wenruo 
440370a11b8SQu Wenruo void btrfs_qgroup_clean_swapped_blocks(struct btrfs_root *root);
441370a11b8SQu Wenruo int btrfs_qgroup_add_swapped_blocks(struct btrfs_trans_handle *trans,
442370a11b8SQu Wenruo 		struct btrfs_root *subvol_root,
44332da5386SDavid Sterba 		struct btrfs_block_group *bg,
444370a11b8SQu Wenruo 		struct extent_buffer *subvol_parent, int subvol_slot,
445370a11b8SQu Wenruo 		struct extent_buffer *reloc_parent, int reloc_slot,
446370a11b8SQu Wenruo 		u64 last_snapshot);
447f616f5cdSQu Wenruo int btrfs_qgroup_trace_subtree_after_cow(struct btrfs_trans_handle *trans,
448f616f5cdSQu Wenruo 		struct btrfs_root *root, struct extent_buffer *eb);
44981f7eb00SJeff Mahoney void btrfs_qgroup_destroy_extent_records(struct btrfs_transaction *trans);
4505958253cSQu Wenruo bool btrfs_check_quota_leak(struct btrfs_fs_info *fs_info);
451370a11b8SQu Wenruo 
4529888c340SDavid Sterba #endif
453