blob: 416ae8e1d23c86bfecbbcbc7b9964629e5b21148 [file] [log] [blame]
Josef Bacikfcebe452014-05-13 17:30:47 -07001/*
2 * Copyright (C) 2014 Facebook. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#ifndef __BTRFS_QGROUP__
20#define __BTRFS_QGROUP__
21
Qu Wenruo3368d002015-04-16 14:34:17 +080022#include "ulist.h"
23#include "delayed-ref.h"
24
Josef Bacikfcebe452014-05-13 17:30:47 -070025/*
Qu Wenruo1d2beaa2016-10-18 09:31:26 +080026 * Btrfs qgroup overview
27 *
28 * Btrfs qgroup splits into 3 main part:
29 * 1) Reserve
30 * Reserve metadata/data space for incoming operations
31 * Affect how qgroup limit works
32 *
33 * 2) Trace
34 * Tell btrfs qgroup to trace dirty extents.
35 *
36 * Dirty extents including:
37 * - Newly allocated extents
38 * - Extents going to be deleted (in this trans)
39 * - Extents whose owner is going to be modified
40 *
41 * This is the main part affects whether qgroup numbers will stay
42 * consistent.
43 * Btrfs qgroup can trace clean extents and won't cause any problem,
44 * but it will consume extra CPU time, it should be avoided if possible.
45 *
46 * 3) Account
47 * Btrfs qgroup will updates its numbers, based on dirty extents traced
48 * in previous step.
49 *
50 * Normally at qgroup rescan and transaction commit time.
51 */
52
53/*
Qu Wenruo3368d002015-04-16 14:34:17 +080054 * Record a dirty extent, and info qgroup to update quota on it
55 * TODO: Use kmem cache to alloc it.
56 */
57struct btrfs_qgroup_extent_record {
58 struct rb_node node;
59 u64 bytenr;
60 u64 num_bytes;
61 struct ulist *old_roots;
62};
63
Qu Wenruo81fb6f72015-09-28 16:57:53 +080064/*
65 * For qgroup event trace points only
66 */
67#define QGROUP_RESERVE (1<<0)
68#define QGROUP_RELEASE (1<<1)
69#define QGROUP_FREE (1<<2)
70
Josef Bacikfcebe452014-05-13 17:30:47 -070071int btrfs_quota_enable(struct btrfs_trans_handle *trans,
72 struct btrfs_fs_info *fs_info);
73int btrfs_quota_disable(struct btrfs_trans_handle *trans,
74 struct btrfs_fs_info *fs_info);
75int btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info);
76void btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info);
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -040077int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
78 bool interruptible);
Josef Bacikfcebe452014-05-13 17:30:47 -070079int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
80 struct btrfs_fs_info *fs_info, u64 src, u64 dst);
81int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
82 struct btrfs_fs_info *fs_info, u64 src, u64 dst);
83int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
Dongsheng Yang4087cf22015-01-18 10:59:23 -050084 struct btrfs_fs_info *fs_info, u64 qgroupid);
Josef Bacikfcebe452014-05-13 17:30:47 -070085int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
86 struct btrfs_fs_info *fs_info, u64 qgroupid);
87int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
88 struct btrfs_fs_info *fs_info, u64 qgroupid,
89 struct btrfs_qgroup_limit *limit);
90int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info);
91void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info);
92struct btrfs_delayed_extent_op;
Qu Wenruo3b7d00f2015-04-16 16:40:39 +080093int btrfs_qgroup_prepare_account_extents(struct btrfs_trans_handle *trans,
94 struct btrfs_fs_info *fs_info);
Qu Wenruocb93b522016-08-15 10:36:50 +080095/*
Qu Wenruo50b3e042016-10-18 09:31:27 +080096 * Inform qgroup to trace one dirty extent, its info is recorded in @record.
97 * So qgroup can account it at commit trans time.
Qu Wenruocb93b522016-08-15 10:36:50 +080098 *
99 * No lock version, caller must acquire delayed ref lock and allocate memory.
100 *
101 * Return 0 for success insert
102 * Return >0 for existing record, caller can free @record safely.
103 * Error is not possible
104 */
Qu Wenruo50b3e042016-10-18 09:31:27 +0800105int btrfs_qgroup_trace_extent_nolock(
Qu Wenruocb93b522016-08-15 10:36:50 +0800106 struct btrfs_fs_info *fs_info,
107 struct btrfs_delayed_ref_root *delayed_refs,
108 struct btrfs_qgroup_extent_record *record);
109
110/*
Qu Wenruo50b3e042016-10-18 09:31:27 +0800111 * Inform qgroup to trace one dirty extent, specified by @bytenr and
112 * @num_bytes.
113 * So qgroup can account it at commit trans time.
Qu Wenruocb93b522016-08-15 10:36:50 +0800114 *
115 * Better encapsulated version.
116 *
117 * Return 0 if the operation is done.
118 * Return <0 for error, like memory allocation failure or invalid parameter
119 * (NULL trans)
120 */
Qu Wenruo50b3e042016-10-18 09:31:27 +0800121int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans,
Qu Wenruocb93b522016-08-15 10:36:50 +0800122 struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
123 gfp_t gfp_flag);
124
Qu Wenruo33d1f052016-10-18 09:31:28 +0800125/*
126 * Inform qgroup to trace all leaf items of data
127 *
128 * Return 0 for success
129 * Return <0 for error(ENOMEM)
130 */
131int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400132 struct btrfs_fs_info *fs_info,
Qu Wenruo33d1f052016-10-18 09:31:28 +0800133 struct extent_buffer *eb);
134/*
135 * Inform qgroup to trace a whole subtree, including all its child tree
136 * blocks and data.
137 * The root tree block is specified by @root_eb.
138 *
139 * Normally used by relocation(tree block swap) and subvolume deletion.
140 *
141 * Return 0 for success
142 * Return <0 for error(ENOMEM or tree search error)
143 */
144int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
145 struct btrfs_root *root,
146 struct extent_buffer *root_eb,
147 u64 root_gen, int root_level);
Qu Wenruo442244c2015-04-16 17:18:36 +0800148int
149btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
150 struct btrfs_fs_info *fs_info,
151 u64 bytenr, u64 num_bytes,
152 struct ulist *old_roots, struct ulist *new_roots);
Qu Wenruo550d7a22015-04-16 15:37:33 +0800153int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans,
154 struct btrfs_fs_info *fs_info);
Josef Bacikfcebe452014-05-13 17:30:47 -0700155int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
156 struct btrfs_fs_info *fs_info);
157int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
158 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
159 struct btrfs_qgroup_inherit *inherit);
Qu Wenruo297d7502015-09-08 17:08:37 +0800160void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
161 u64 ref_root, u64 num_bytes);
Qu Wenruo297d7502015-09-08 17:08:37 +0800162/*
163 * TODO: Add proper trace point for it, as btrfs_qgroup_free() is
164 * called by everywhere, can't provide good trace for delayed ref case.
165 */
166static inline void btrfs_qgroup_free_delayed_ref(struct btrfs_fs_info *fs_info,
167 u64 ref_root, u64 num_bytes)
168{
169 btrfs_qgroup_free_refroot(fs_info, ref_root, num_bytes);
Jeff Mahoneybc074522016-06-09 17:27:55 -0400170 trace_btrfs_qgroup_free_delayed_ref(fs_info, ref_root, num_bytes);
Qu Wenruo297d7502015-09-08 17:08:37 +0800171}
Josef Bacikfcebe452014-05-13 17:30:47 -0700172void assert_qgroups_uptodate(struct btrfs_trans_handle *trans);
173
174#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
175int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
176 u64 rfer, u64 excl);
177#endif
178
Qu Wenruo52472552015-10-12 16:05:40 +0800179/* New io_tree based accurate qgroup reserve API */
180int btrfs_qgroup_reserve_data(struct inode *inode, u64 start, u64 len);
Qu Wenruof695fdc2015-10-12 16:28:06 +0800181int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len);
182int btrfs_qgroup_free_data(struct inode *inode, u64 start, u64 len);
Qu Wenruo55eeaf02015-09-08 17:08:38 +0800183
184int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes);
185void btrfs_qgroup_free_meta_all(struct btrfs_root *root);
186void btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes);
Qu Wenruo56fa9d02015-10-13 09:53:10 +0800187void btrfs_qgroup_check_reserved_leak(struct inode *inode);
Josef Bacikfcebe452014-05-13 17:30:47 -0700188#endif /* __BTRFS_QGROUP__ */