xfs_dquot.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #ifndef __XFS_DQUOT_H__
  7. #define __XFS_DQUOT_H__
  8. /*
  9. * Dquots are structures that hold quota information about a user or a group,
  10. * much like inodes are for files. In fact, dquots share many characteristics
  11. * with inodes. However, dquots can also be a centralized resource, relative
  12. * to a collection of inodes. In this respect, dquots share some characteristics
  13. * of the superblock.
  14. * XFS dquots exploit both those in its algorithms. They make every attempt
  15. * to not be a bottleneck when quotas are on and have minimal impact, if any,
  16. * when quotas are off.
  17. */
  18. struct xfs_mount;
  19. struct xfs_trans;
  20. enum {
  21. XFS_QLOWSP_1_PCNT = 0,
  22. XFS_QLOWSP_3_PCNT,
  23. XFS_QLOWSP_5_PCNT,
  24. XFS_QLOWSP_MAX
  25. };
  26. struct xfs_dquot_res {
  27. /* Total resources allocated and reserved. */
  28. xfs_qcnt_t reserved;
  29. /* Total resources allocated. */
  30. xfs_qcnt_t count;
  31. /* Absolute and preferred limits. */
  32. xfs_qcnt_t hardlimit;
  33. xfs_qcnt_t softlimit;
  34. /*
  35. * For root dquots, this is the default grace period, in seconds.
  36. * Otherwise, this is when the quota grace period expires,
  37. * in seconds since the Unix epoch.
  38. */
  39. time64_t timer;
  40. /*
  41. * For root dquots, this is the maximum number of warnings that will
  42. * be issued for this quota type. Otherwise, this is the number of
  43. * warnings issued against this quota. Note that none of this is
  44. * implemented.
  45. */
  46. xfs_qwarncnt_t warnings;
  47. };
  48. /*
  49. * The incore dquot structure
  50. */
  51. struct xfs_dquot {
  52. struct list_head q_lru;
  53. struct xfs_mount *q_mount;
  54. xfs_dqtype_t q_type;
  55. uint16_t q_flags;
  56. xfs_dqid_t q_id;
  57. uint q_nrefs;
  58. int q_bufoffset;
  59. xfs_daddr_t q_blkno;
  60. xfs_fileoff_t q_fileoffset;
  61. struct xfs_dquot_res q_blk; /* regular blocks */
  62. struct xfs_dquot_res q_ino; /* inodes */
  63. struct xfs_dquot_res q_rtb; /* realtime blocks */
  64. struct xfs_dq_logitem q_logitem;
  65. xfs_qcnt_t q_prealloc_lo_wmark;
  66. xfs_qcnt_t q_prealloc_hi_wmark;
  67. int64_t q_low_space[XFS_QLOWSP_MAX];
  68. struct mutex q_qlock;
  69. struct completion q_flush;
  70. atomic_t q_pincount;
  71. struct wait_queue_head q_pinwait;
  72. };
  73. /*
  74. * Lock hierarchy for q_qlock:
  75. * XFS_QLOCK_NORMAL is the implicit default,
  76. * XFS_QLOCK_NESTED is the dquot with the higher id in xfs_dqlock2
  77. */
  78. enum {
  79. XFS_QLOCK_NORMAL = 0,
  80. XFS_QLOCK_NESTED,
  81. };
  82. /*
  83. * Manage the q_flush completion queue embedded in the dquot. This completion
  84. * queue synchronizes processes attempting to flush the in-core dquot back to
  85. * disk.
  86. */
  87. static inline void xfs_dqflock(struct xfs_dquot *dqp)
  88. {
  89. wait_for_completion(&dqp->q_flush);
  90. }
  91. static inline bool xfs_dqflock_nowait(struct xfs_dquot *dqp)
  92. {
  93. return try_wait_for_completion(&dqp->q_flush);
  94. }
  95. static inline void xfs_dqfunlock(struct xfs_dquot *dqp)
  96. {
  97. complete(&dqp->q_flush);
  98. }
  99. static inline int xfs_dqlock_nowait(struct xfs_dquot *dqp)
  100. {
  101. return mutex_trylock(&dqp->q_qlock);
  102. }
  103. static inline void xfs_dqlock(struct xfs_dquot *dqp)
  104. {
  105. mutex_lock(&dqp->q_qlock);
  106. }
  107. static inline void xfs_dqunlock(struct xfs_dquot *dqp)
  108. {
  109. mutex_unlock(&dqp->q_qlock);
  110. }
  111. static inline int
  112. xfs_dquot_type(const struct xfs_dquot *dqp)
  113. {
  114. return dqp->q_type & XFS_DQTYPE_REC_MASK;
  115. }
  116. static inline int xfs_this_quota_on(struct xfs_mount *mp, xfs_dqtype_t type)
  117. {
  118. switch (type) {
  119. case XFS_DQTYPE_USER:
  120. return XFS_IS_UQUOTA_ON(mp);
  121. case XFS_DQTYPE_GROUP:
  122. return XFS_IS_GQUOTA_ON(mp);
  123. case XFS_DQTYPE_PROJ:
  124. return XFS_IS_PQUOTA_ON(mp);
  125. default:
  126. return 0;
  127. }
  128. }
  129. static inline struct xfs_dquot *xfs_inode_dquot(
  130. struct xfs_inode *ip,
  131. xfs_dqtype_t type)
  132. {
  133. switch (type) {
  134. case XFS_DQTYPE_USER:
  135. return ip->i_udquot;
  136. case XFS_DQTYPE_GROUP:
  137. return ip->i_gdquot;
  138. case XFS_DQTYPE_PROJ:
  139. return ip->i_pdquot;
  140. default:
  141. return NULL;
  142. }
  143. }
  144. /* Decide if the dquot's limits are actually being enforced. */
  145. static inline bool
  146. xfs_dquot_is_enforced(
  147. const struct xfs_dquot *dqp)
  148. {
  149. switch (xfs_dquot_type(dqp)) {
  150. case XFS_DQTYPE_USER:
  151. return XFS_IS_UQUOTA_ENFORCED(dqp->q_mount);
  152. case XFS_DQTYPE_GROUP:
  153. return XFS_IS_GQUOTA_ENFORCED(dqp->q_mount);
  154. case XFS_DQTYPE_PROJ:
  155. return XFS_IS_PQUOTA_ENFORCED(dqp->q_mount);
  156. }
  157. ASSERT(0);
  158. return false;
  159. }
  160. /*
  161. * Check whether a dquot is under low free space conditions. We assume the quota
  162. * is enabled and enforced.
  163. */
  164. static inline bool xfs_dquot_lowsp(struct xfs_dquot *dqp)
  165. {
  166. int64_t freesp;
  167. freesp = dqp->q_blk.hardlimit - dqp->q_blk.reserved;
  168. if (freesp < dqp->q_low_space[XFS_QLOWSP_1_PCNT])
  169. return true;
  170. return false;
  171. }
  172. void xfs_dquot_to_disk(struct xfs_disk_dquot *ddqp, struct xfs_dquot *dqp);
  173. #define XFS_DQ_IS_LOCKED(dqp) (mutex_is_locked(&((dqp)->q_qlock)))
  174. #define XFS_DQ_IS_DIRTY(dqp) ((dqp)->q_flags & XFS_DQFLAG_DIRTY)
  175. void xfs_qm_dqdestroy(struct xfs_dquot *dqp);
  176. int xfs_qm_dqflush(struct xfs_dquot *dqp, struct xfs_buf **bpp);
  177. void xfs_qm_dqunpin_wait(struct xfs_dquot *dqp);
  178. void xfs_qm_adjust_dqtimers(struct xfs_dquot *d);
  179. void xfs_qm_adjust_dqlimits(struct xfs_dquot *d);
  180. xfs_dqid_t xfs_qm_id_for_quotatype(struct xfs_inode *ip,
  181. xfs_dqtype_t type);
  182. int xfs_qm_dqget(struct xfs_mount *mp, xfs_dqid_t id,
  183. xfs_dqtype_t type, bool can_alloc,
  184. struct xfs_dquot **dqpp);
  185. int xfs_qm_dqget_inode(struct xfs_inode *ip, xfs_dqtype_t type,
  186. bool can_alloc, struct xfs_dquot **dqpp);
  187. int xfs_qm_dqget_next(struct xfs_mount *mp, xfs_dqid_t id,
  188. xfs_dqtype_t type, struct xfs_dquot **dqpp);
  189. int xfs_qm_dqget_uncached(struct xfs_mount *mp,
  190. xfs_dqid_t id, xfs_dqtype_t type,
  191. struct xfs_dquot **dqpp);
  192. void xfs_qm_dqput(struct xfs_dquot *dqp);
  193. void xfs_dqlock2(struct xfs_dquot *, struct xfs_dquot *);
  194. void xfs_dquot_set_prealloc_limits(struct xfs_dquot *);
  195. static inline struct xfs_dquot *xfs_qm_dqhold(struct xfs_dquot *dqp)
  196. {
  197. xfs_dqlock(dqp);
  198. dqp->q_nrefs++;
  199. xfs_dqunlock(dqp);
  200. return dqp;
  201. }
  202. typedef int (*xfs_qm_dqiterate_fn)(struct xfs_dquot *dq,
  203. xfs_dqtype_t type, void *priv);
  204. int xfs_qm_dqiterate(struct xfs_mount *mp, xfs_dqtype_t type,
  205. xfs_qm_dqiterate_fn iter_fn, void *priv);
  206. time64_t xfs_dquot_set_timeout(struct xfs_mount *mp, time64_t timeout);
  207. time64_t xfs_dquot_set_grace_period(time64_t grace);
  208. #endif /* __XFS_DQUOT_H__ */