qgroup.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  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. #ifndef __BTRFS_QGROUP__
  19. #define __BTRFS_QGROUP__
  20. #include "ulist.h"
  21. #include "delayed-ref.h"
  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. * Record a dirty extent, and info qgroup to update quota on it
  51. * TODO: Use kmem cache to alloc it.
  52. */
  53. struct btrfs_qgroup_extent_record {
  54. struct rb_node node;
  55. u64 bytenr;
  56. u64 num_bytes;
  57. struct ulist *old_roots;
  58. };
  59. /*
  60. * one struct for each qgroup, organized in fs_info->qgroup_tree.
  61. */
  62. struct btrfs_qgroup {
  63. u64 qgroupid;
  64. /*
  65. * state
  66. */
  67. u64 rfer; /* referenced */
  68. u64 rfer_cmpr; /* referenced compressed */
  69. u64 excl; /* exclusive */
  70. u64 excl_cmpr; /* exclusive compressed */
  71. /*
  72. * limits
  73. */
  74. u64 lim_flags; /* which limits are set */
  75. u64 max_rfer;
  76. u64 max_excl;
  77. u64 rsv_rfer;
  78. u64 rsv_excl;
  79. /*
  80. * reservation tracking
  81. */
  82. u64 reserved;
  83. /*
  84. * lists
  85. */
  86. struct list_head groups; /* groups this group is member of */
  87. struct list_head members; /* groups that are members of this group */
  88. struct list_head dirty; /* dirty groups */
  89. struct rb_node node; /* tree of qgroups */
  90. /*
  91. * temp variables for accounting operations
  92. * Refer to qgroup_shared_accounting() for details.
  93. */
  94. u64 old_refcnt;
  95. u64 new_refcnt;
  96. };
  97. /*
  98. * For qgroup event trace points only
  99. */
  100. #define QGROUP_RESERVE (1<<0)
  101. #define QGROUP_RELEASE (1<<1)
  102. #define QGROUP_FREE (1<<2)
  103. int btrfs_quota_enable(struct btrfs_trans_handle *trans,
  104. struct btrfs_fs_info *fs_info);
  105. int btrfs_quota_disable(struct btrfs_trans_handle *trans,
  106. struct btrfs_fs_info *fs_info);
  107. int btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info);
  108. void btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info);
  109. int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
  110. bool interruptible);
  111. int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
  112. struct btrfs_fs_info *fs_info, u64 src, u64 dst);
  113. int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
  114. struct btrfs_fs_info *fs_info, u64 src, u64 dst);
  115. int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
  116. struct btrfs_fs_info *fs_info, u64 qgroupid);
  117. int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
  118. struct btrfs_fs_info *fs_info, u64 qgroupid);
  119. int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
  120. struct btrfs_fs_info *fs_info, u64 qgroupid,
  121. struct btrfs_qgroup_limit *limit);
  122. int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info);
  123. void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info);
  124. struct btrfs_delayed_extent_op;
  125. /*
  126. * Inform qgroup to trace one dirty extent, its info is recorded in @record.
  127. * So qgroup can account it at transaction committing time.
  128. *
  129. * No lock version, caller must acquire delayed ref lock and allocated memory,
  130. * then call btrfs_qgroup_trace_extent_post() after exiting lock context.
  131. *
  132. * Return 0 for success insert
  133. * Return >0 for existing record, caller can free @record safely.
  134. * Error is not possible
  135. */
  136. int btrfs_qgroup_trace_extent_nolock(
  137. struct btrfs_fs_info *fs_info,
  138. struct btrfs_delayed_ref_root *delayed_refs,
  139. struct btrfs_qgroup_extent_record *record);
  140. /*
  141. * Post handler after qgroup_trace_extent_nolock().
  142. *
  143. * NOTE: Current qgroup does the expensive backref walk at transaction
  144. * committing time with TRANS_STATE_COMMIT_DOING, this blocks incoming
  145. * new transaction.
  146. * This is designed to allow btrfs_find_all_roots() to get correct new_roots
  147. * result.
  148. *
  149. * However for old_roots there is no need to do backref walk at that time,
  150. * since we search commit roots to walk backref and result will always be
  151. * correct.
  152. *
  153. * Due to the nature of no lock version, we can't do backref there.
  154. * So we must call btrfs_qgroup_trace_extent_post() after exiting
  155. * spinlock context.
  156. *
  157. * TODO: If we can fix and prove btrfs_find_all_roots() can get correct result
  158. * using current root, then we can move all expensive backref walk out of
  159. * transaction committing, but not now as qgroup accounting will be wrong again.
  160. */
  161. int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
  162. struct btrfs_qgroup_extent_record *qrecord);
  163. /*
  164. * Inform qgroup to trace one dirty extent, specified by @bytenr and
  165. * @num_bytes.
  166. * So qgroup can account it at commit trans time.
  167. *
  168. * Better encapsulated version, with memory allocation and backref walk for
  169. * commit roots.
  170. * So this can sleep.
  171. *
  172. * Return 0 if the operation is done.
  173. * Return <0 for error, like memory allocation failure or invalid parameter
  174. * (NULL trans)
  175. */
  176. int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans,
  177. struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
  178. gfp_t gfp_flag);
  179. /*
  180. * Inform qgroup to trace all leaf items of data
  181. *
  182. * Return 0 for success
  183. * Return <0 for error(ENOMEM)
  184. */
  185. int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
  186. struct btrfs_fs_info *fs_info,
  187. struct extent_buffer *eb);
  188. /*
  189. * Inform qgroup to trace a whole subtree, including all its child tree
  190. * blocks and data.
  191. * The root tree block is specified by @root_eb.
  192. *
  193. * Normally used by relocation(tree block swap) and subvolume deletion.
  194. *
  195. * Return 0 for success
  196. * Return <0 for error(ENOMEM or tree search error)
  197. */
  198. int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
  199. struct btrfs_root *root,
  200. struct extent_buffer *root_eb,
  201. u64 root_gen, int root_level);
  202. int
  203. btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
  204. struct btrfs_fs_info *fs_info,
  205. u64 bytenr, u64 num_bytes,
  206. struct ulist *old_roots, struct ulist *new_roots);
  207. int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans,
  208. struct btrfs_fs_info *fs_info);
  209. int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
  210. struct btrfs_fs_info *fs_info);
  211. int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
  212. struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
  213. struct btrfs_qgroup_inherit *inherit);
  214. void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
  215. u64 ref_root, u64 num_bytes);
  216. static inline void btrfs_qgroup_free_delayed_ref(struct btrfs_fs_info *fs_info,
  217. u64 ref_root, u64 num_bytes)
  218. {
  219. trace_btrfs_qgroup_free_delayed_ref(fs_info, ref_root, num_bytes);
  220. btrfs_qgroup_free_refroot(fs_info, ref_root, num_bytes);
  221. }
  222. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  223. int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
  224. u64 rfer, u64 excl);
  225. #endif
  226. /* New io_tree based accurate qgroup reserve API */
  227. int btrfs_qgroup_reserve_data(struct inode *inode,
  228. struct extent_changeset **reserved, u64 start, u64 len);
  229. int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len);
  230. int btrfs_qgroup_free_data(struct inode *inode,
  231. struct extent_changeset *reserved, u64 start, u64 len);
  232. int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
  233. bool enforce);
  234. void btrfs_qgroup_free_meta_all(struct btrfs_root *root);
  235. void btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes);
  236. void btrfs_qgroup_check_reserved_leak(struct inode *inode);
  237. #endif /* __BTRFS_QGROUP__ */