qgroup.h 15 KB

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