qgroup.h 14 KB

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