1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2014 Facebook.  All rights reserved.
4 */
5
6#ifndef BTRFS_QGROUP_H
7#define BTRFS_QGROUP_H
8
9#include <linux/types.h>
10#include <linux/spinlock.h>
11#include <linux/rbtree.h>
12#include <linux/kobject.h>
13#include <linux/list.h>
14#include <uapi/linux/btrfs_tree.h>
15
16struct extent_buffer;
17struct extent_changeset;
18struct btrfs_delayed_extent_op;
19struct btrfs_fs_info;
20struct btrfs_root;
21struct btrfs_ioctl_quota_ctl_args;
22struct btrfs_trans_handle;
23struct btrfs_delayed_ref_root;
24struct btrfs_inode;
25
26/*
27 * Btrfs qgroup overview
28 *
29 * Btrfs qgroup splits into 3 main part:
30 * 1) Reserve
31 *    Reserve metadata/data space for incoming operations
32 *    Affect how qgroup limit works
33 *
34 * 2) Trace
35 *    Tell btrfs qgroup to trace dirty extents.
36 *
37 *    Dirty extents including:
38 *    - Newly allocated extents
39 *    - Extents going to be deleted (in this trans)
40 *    - Extents whose owner is going to be modified
41 *
42 *    This is the main part affects whether qgroup numbers will stay
43 *    consistent.
44 *    Btrfs qgroup can trace clean extents and won't cause any problem,
45 *    but it will consume extra CPU time, it should be avoided if possible.
46 *
47 * 3) Account
48 *    Btrfs qgroup will updates its numbers, based on dirty extents traced
49 *    in previous step.
50 *
51 *    Normally at qgroup rescan and transaction commit time.
52 */
53
54/*
55 * Special performance optimization for balance.
56 *
57 * For balance, we need to swap subtree of subvolume and reloc trees.
58 * In theory, we need to trace all subtree blocks of both subvolume and reloc
59 * trees, since their owner has changed during such swap.
60 *
61 * However since balance has ensured that both subtrees are containing the
62 * same contents and have the same tree structures, such swap won't cause
63 * qgroup number change.
64 *
65 * But there is a race window between subtree swap and transaction commit,
66 * during that window, if we increase/decrease tree level or merge/split tree
67 * blocks, we still need to trace the original subtrees.
68 *
69 * So for balance, we use a delayed subtree tracing, whose workflow is:
70 *
71 * 1) Record the subtree root block get swapped.
72 *
73 *    During subtree swap:
74 *    O = Old tree blocks
75 *    N = New tree blocks
76 *          reloc tree                     subvolume tree X
77 *             Root                               Root
78 *            /    \                             /    \
79 *          NA     OB                          OA      OB
80 *        /  |     |  \                      /  |      |  \
81 *      NC  ND     OE  OF                   OC  OD     OE  OF
82 *
83 *   In this case, NA and OA are going to be swapped, record (NA, OA) into
84 *   subvolume tree X.
85 *
86 * 2) After subtree swap.
87 *          reloc tree                     subvolume tree X
88 *             Root                               Root
89 *            /    \                             /    \
90 *          OA     OB                          NA      OB
91 *        /  |     |  \                      /  |      |  \
92 *      OC  OD     OE  OF                   NC  ND     OE  OF
93 *
94 * 3a) COW happens for OB
95 *     If we are going to COW tree block OB, we check OB's bytenr against
96 *     tree X's swapped_blocks structure.
97 *     If it doesn't fit any, nothing will happen.
98 *
99 * 3b) COW happens for NA
100 *     Check NA's bytenr against tree X's swapped_blocks, and get a hit.
101 *     Then we do subtree scan on both subtrees OA and NA.
102 *     Resulting 6 tree blocks to be scanned (OA, OC, OD, NA, NC, ND).
103 *
104 *     Then no matter what we do to subvolume tree X, qgroup numbers will
105 *     still be correct.
106 *     Then NA's record gets removed from X's swapped_blocks.
107 *
108 * 4)  Transaction commit
109 *     Any record in X's swapped_blocks gets removed, since there is no
110 *     modification to the swapped subtrees, no need to trigger heavy qgroup
111 *     subtree rescan for them.
112 */
113
114/*
115 * These flags share the flags field of the btrfs_qgroup_status_item with the
116 * persisted flags defined in btrfs_tree.h.
117 *
118 * To minimize the chance of collision with new persisted status flags, these
119 * count backwards from the MSB.
120 */
121#define BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN		(1ULL << 63)
122#define BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING		(1ULL << 62)
123
124/*
125 * Record a dirty extent, and info qgroup to update quota on it
126 * TODO: Use kmem cache to alloc it.
127 */
128struct btrfs_qgroup_extent_record {
129	struct rb_node node;
130	u64 bytenr;
131	u64 num_bytes;
132
133	/*
134	 * For qgroup reserved data space freeing.
135	 *
136	 * @data_rsv_refroot and @data_rsv will be recorded after
137	 * BTRFS_ADD_DELAYED_EXTENT is called.
138	 * And will be used to free reserved qgroup space at
139	 * transaction commit time.
140	 */
141	u32 data_rsv;		/* reserved data space needs to be freed */
142	u64 data_rsv_refroot;	/* which root the reserved data belongs to */
143	struct ulist *old_roots;
144};
145
146struct btrfs_qgroup_swapped_block {
147	struct rb_node node;
148
149	int level;
150	bool trace_leaf;
151
152	/* bytenr/generation of the tree block in subvolume tree after swap */
153	u64 subvol_bytenr;
154	u64 subvol_generation;
155
156	/* bytenr/generation of the tree block in reloc tree after swap */
157	u64 reloc_bytenr;
158	u64 reloc_generation;
159
160	u64 last_snapshot;
161	struct btrfs_key first_key;
162};
163
164/*
165 * Qgroup reservation types:
166 *
167 * DATA:
168 *	space reserved for data
169 *
170 * META_PERTRANS:
171 * 	Space reserved for metadata (per-transaction)
172 * 	Due to the fact that qgroup data is only updated at transaction commit
173 * 	time, reserved space for metadata must be kept until transaction
174 * 	commits.
175 * 	Any metadata reserved that are used in btrfs_start_transaction() should
176 * 	be of this type.
177 *
178 * META_PREALLOC:
179 *	There are cases where metadata space is reserved before starting
180 *	transaction, and then btrfs_join_transaction() to get a trans handle.
181 *	Any metadata reserved for such usage should be of this type.
182 *	And after join_transaction() part (or all) of such reservation should
183 *	be converted into META_PERTRANS.
184 */
185enum btrfs_qgroup_rsv_type {
186	BTRFS_QGROUP_RSV_DATA,
187	BTRFS_QGROUP_RSV_META_PERTRANS,
188	BTRFS_QGROUP_RSV_META_PREALLOC,
189	BTRFS_QGROUP_RSV_LAST,
190};
191
192/*
193 * Represents how many bytes we have reserved for this qgroup.
194 *
195 * Each type should have different reservation behavior.
196 * E.g, data follows its io_tree flag modification, while
197 * *currently* meta is just reserve-and-clear during transaction.
198 *
199 * TODO: Add new type for reservation which can survive transaction commit.
200 * Current metadata reservation behavior is not suitable for such case.
201 */
202struct btrfs_qgroup_rsv {
203	u64 values[BTRFS_QGROUP_RSV_LAST];
204};
205
206/*
207 * one struct for each qgroup, organized in fs_info->qgroup_tree.
208 */
209struct btrfs_qgroup {
210	u64 qgroupid;
211
212	/*
213	 * state
214	 */
215	u64 rfer;	/* referenced */
216	u64 rfer_cmpr;	/* referenced compressed */
217	u64 excl;	/* exclusive */
218	u64 excl_cmpr;	/* exclusive compressed */
219
220	/*
221	 * limits
222	 */
223	u64 lim_flags;	/* which limits are set */
224	u64 max_rfer;
225	u64 max_excl;
226	u64 rsv_rfer;
227	u64 rsv_excl;
228
229	/*
230	 * reservation tracking
231	 */
232	struct btrfs_qgroup_rsv rsv;
233
234	/*
235	 * lists
236	 */
237	struct list_head groups;  /* groups this group is member of */
238	struct list_head members; /* groups that are members of this group */
239	struct list_head dirty;   /* dirty groups */
240
241	/*
242	 * For qgroup iteration usage.
243	 *
244	 * The iteration list should always be empty until qgroup_iterator_add()
245	 * is called.  And should be reset to empty after the iteration is
246	 * finished.
247	 */
248	struct list_head iterator;
249
250	/*
251	 * For nested iterator usage.
252	 *
253	 * Here we support at most one level of nested iterator calls like:
254	 *
255	 *	LIST_HEAD(all_qgroups);
256	 *	{
257	 *		LIST_HEAD(local_qgroups);
258	 *		qgroup_iterator_add(local_qgroups, qg);
259	 *		qgroup_iterator_nested_add(all_qgroups, qg);
260	 *		do_some_work(local_qgroups);
261	 *		qgroup_iterator_clean(local_qgroups);
262	 *	}
263	 *	do_some_work(all_qgroups);
264	 *	qgroup_iterator_nested_clean(all_qgroups);
265	 */
266	struct list_head nested_iterator;
267	struct rb_node node;	  /* tree of qgroups */
268
269	/*
270	 * temp variables for accounting operations
271	 * Refer to qgroup_shared_accounting() for details.
272	 */
273	u64 old_refcnt;
274	u64 new_refcnt;
275
276	/*
277	 * Sysfs kobjectid
278	 */
279	struct kobject kobj;
280};
281
282struct btrfs_squota_delta {
283	/* The fstree root this delta counts against. */
284	u64 root;
285	/* The number of bytes in the extent being counted. */
286	u64 num_bytes;
287	/* The generation the extent was created in. */
288	u64 generation;
289	/* Whether we are using or freeing the extent. */
290	bool is_inc;
291	/* Whether the extent is data or metadata. */
292	bool is_data;
293};
294
295static inline u64 btrfs_qgroup_subvolid(u64 qgroupid)
296{
297	return (qgroupid & ((1ULL << BTRFS_QGROUP_LEVEL_SHIFT) - 1));
298}
299
300/*
301 * For qgroup event trace points only
302 */
303enum {
304	ENUM_BIT(QGROUP_RESERVE),
305	ENUM_BIT(QGROUP_RELEASE),
306	ENUM_BIT(QGROUP_FREE),
307};
308
309enum btrfs_qgroup_mode {
310	BTRFS_QGROUP_MODE_DISABLED,
311	BTRFS_QGROUP_MODE_FULL,
312	BTRFS_QGROUP_MODE_SIMPLE
313};
314
315enum btrfs_qgroup_mode btrfs_qgroup_mode(struct btrfs_fs_info *fs_info);
316bool btrfs_qgroup_enabled(struct btrfs_fs_info *fs_info);
317bool btrfs_qgroup_full_accounting(struct btrfs_fs_info *fs_info);
318int btrfs_quota_enable(struct btrfs_fs_info *fs_info,
319		       struct btrfs_ioctl_quota_ctl_args *quota_ctl_args);
320int btrfs_quota_disable(struct btrfs_fs_info *fs_info);
321int btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info);
322void btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info);
323int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
324				     bool interruptible);
325int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, u64 dst);
326int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
327			      u64 dst);
328int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid);
329int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid);
330int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid,
331		       struct btrfs_qgroup_limit *limit);
332int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info);
333void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info);
334
335int btrfs_qgroup_trace_extent_nolock(
336		struct btrfs_fs_info *fs_info,
337		struct btrfs_delayed_ref_root *delayed_refs,
338		struct btrfs_qgroup_extent_record *record);
339int btrfs_qgroup_trace_extent_post(struct btrfs_trans_handle *trans,
340				   struct btrfs_qgroup_extent_record *qrecord);
341int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr,
342			      u64 num_bytes);
343int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
344				  struct extent_buffer *eb);
345int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
346			       struct extent_buffer *root_eb,
347			       u64 root_gen, int root_level);
348int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr,
349				u64 num_bytes, struct ulist *old_roots,
350				struct ulist *new_roots);
351int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans);
352int btrfs_run_qgroups(struct btrfs_trans_handle *trans);
353int btrfs_qgroup_check_inherit(struct btrfs_fs_info *fs_info,
354			       struct btrfs_qgroup_inherit *inherit,
355			       size_t size);
356int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
357			 u64 objectid, u64 inode_rootid,
358			 struct btrfs_qgroup_inherit *inherit);
359void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
360			       u64 ref_root, u64 num_bytes,
361			       enum btrfs_qgroup_rsv_type type);
362
363#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
364int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
365			       u64 rfer, u64 excl);
366#endif
367
368/* New io_tree based accurate qgroup reserve API */
369int btrfs_qgroup_reserve_data(struct btrfs_inode *inode,
370			struct extent_changeset **reserved, u64 start, u64 len);
371int btrfs_qgroup_release_data(struct btrfs_inode *inode, u64 start, u64 len, u64 *released);
372int btrfs_qgroup_free_data(struct btrfs_inode *inode,
373			   struct extent_changeset *reserved, u64 start,
374			   u64 len, u64 *freed);
375int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
376			      enum btrfs_qgroup_rsv_type type, bool enforce);
377int __btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
378				enum btrfs_qgroup_rsv_type type, bool enforce,
379				bool noflush);
380/* Reserve metadata space for pertrans and prealloc type */
381static inline int btrfs_qgroup_reserve_meta_pertrans(struct btrfs_root *root,
382				int num_bytes, bool enforce)
383{
384	return __btrfs_qgroup_reserve_meta(root, num_bytes,
385					   BTRFS_QGROUP_RSV_META_PERTRANS,
386					   enforce, false);
387}
388static inline int btrfs_qgroup_reserve_meta_prealloc(struct btrfs_root *root,
389						     int num_bytes, bool enforce,
390						     bool noflush)
391{
392	return __btrfs_qgroup_reserve_meta(root, num_bytes,
393					   BTRFS_QGROUP_RSV_META_PREALLOC,
394					   enforce, noflush);
395}
396
397void __btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes,
398			     enum btrfs_qgroup_rsv_type type);
399
400/* Free per-transaction meta reservation for error handling */
401static inline void btrfs_qgroup_free_meta_pertrans(struct btrfs_root *root,
402						   int num_bytes)
403{
404	__btrfs_qgroup_free_meta(root, num_bytes,
405			BTRFS_QGROUP_RSV_META_PERTRANS);
406}
407
408/* Pre-allocated meta reservation can be freed at need */
409static inline void btrfs_qgroup_free_meta_prealloc(struct btrfs_root *root,
410						   int num_bytes)
411{
412	__btrfs_qgroup_free_meta(root, num_bytes,
413			BTRFS_QGROUP_RSV_META_PREALLOC);
414}
415
416void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root);
417void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes);
418void btrfs_qgroup_check_reserved_leak(struct btrfs_inode *inode);
419
420/* btrfs_qgroup_swapped_blocks related functions */
421void btrfs_qgroup_init_swapped_blocks(
422	struct btrfs_qgroup_swapped_blocks *swapped_blocks);
423
424void btrfs_qgroup_clean_swapped_blocks(struct btrfs_root *root);
425int btrfs_qgroup_add_swapped_blocks(struct btrfs_trans_handle *trans,
426		struct btrfs_root *subvol_root,
427		struct btrfs_block_group *bg,
428		struct extent_buffer *subvol_parent, int subvol_slot,
429		struct extent_buffer *reloc_parent, int reloc_slot,
430		u64 last_snapshot);
431int btrfs_qgroup_trace_subtree_after_cow(struct btrfs_trans_handle *trans,
432		struct btrfs_root *root, struct extent_buffer *eb);
433void btrfs_qgroup_destroy_extent_records(struct btrfs_transaction *trans);
434bool btrfs_check_quota_leak(struct btrfs_fs_info *fs_info);
435void btrfs_free_squota_rsv(struct btrfs_fs_info *fs_info, u64 root, u64 rsv_bytes);
436int btrfs_record_squota_delta(struct btrfs_fs_info *fs_info,
437			      struct btrfs_squota_delta *delta);
438
439#endif
440