xfs_dquot.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. static inline bool
  42. xfs_dquot_res_over_limits(
  43. const struct xfs_dquot_res *qres)
  44. {
  45. if ((qres->softlimit && qres->softlimit < qres->reserved) ||
  46. (qres->hardlimit && qres->hardlimit < qres->reserved))
  47. return true;
  48. return false;
  49. }
  50. /*
  51. * The incore dquot structure
  52. */
  53. struct xfs_dquot {
  54. struct list_head q_lru;
  55. struct xfs_mount *q_mount;
  56. xfs_dqtype_t q_type;
  57. uint16_t q_flags;
  58. xfs_dqid_t q_id;
  59. uint q_nrefs;
  60. int q_bufoffset;
  61. xfs_daddr_t q_blkno;
  62. xfs_fileoff_t q_fileoffset;
  63. struct xfs_dquot_res q_blk; /* regular blocks */
  64. struct xfs_dquot_res q_ino; /* inodes */
  65. struct xfs_dquot_res q_rtb; /* realtime blocks */
  66. struct xfs_dq_logitem q_logitem;
  67. xfs_qcnt_t q_prealloc_lo_wmark;
  68. xfs_qcnt_t q_prealloc_hi_wmark;
  69. int64_t q_low_space[XFS_QLOWSP_MAX];
  70. struct mutex q_qlock;
  71. struct completion q_flush;
  72. atomic_t q_pincount;
  73. struct wait_queue_head q_pinwait;
  74. };
  75. /*
  76. * Lock hierarchy for q_qlock:
  77. * XFS_QLOCK_NORMAL is the implicit default,
  78. * XFS_QLOCK_NESTED is the dquot with the higher id in xfs_dqlock2
  79. */
  80. enum {
  81. XFS_QLOCK_NORMAL = 0,
  82. XFS_QLOCK_NESTED,
  83. };
  84. /*
  85. * Manage the q_flush completion queue embedded in the dquot. This completion
  86. * queue synchronizes processes attempting to flush the in-core dquot back to
  87. * disk.
  88. */
  89. static inline void xfs_dqflock(struct xfs_dquot *dqp)
  90. {
  91. wait_for_completion(&dqp->q_flush);
  92. }
  93. static inline bool xfs_dqflock_nowait(struct xfs_dquot *dqp)
  94. {
  95. return try_wait_for_completion(&dqp->q_flush);
  96. }
  97. static inline void xfs_dqfunlock(struct xfs_dquot *dqp)
  98. {
  99. complete(&dqp->q_flush);
  100. }
  101. static inline int xfs_dqlock_nowait(struct xfs_dquot *dqp)
  102. {
  103. return mutex_trylock(&dqp->q_qlock);
  104. }
  105. static inline void xfs_dqlock(struct xfs_dquot *dqp)
  106. {
  107. mutex_lock(&dqp->q_qlock);
  108. }
  109. static inline void xfs_dqunlock(struct xfs_dquot *dqp)
  110. {
  111. mutex_unlock(&dqp->q_qlock);
  112. }
  113. static inline int
  114. xfs_dquot_type(const struct xfs_dquot *dqp)
  115. {
  116. return dqp->q_type & XFS_DQTYPE_REC_MASK;
  117. }
  118. static inline int xfs_this_quota_on(struct xfs_mount *mp, xfs_dqtype_t type)
  119. {
  120. switch (type) {
  121. case XFS_DQTYPE_USER:
  122. return XFS_IS_UQUOTA_ON(mp);
  123. case XFS_DQTYPE_GROUP:
  124. return XFS_IS_GQUOTA_ON(mp);
  125. case XFS_DQTYPE_PROJ:
  126. return XFS_IS_PQUOTA_ON(mp);
  127. default:
  128. return 0;
  129. }
  130. }
  131. static inline struct xfs_dquot *xfs_inode_dquot(
  132. struct xfs_inode *ip,
  133. xfs_dqtype_t type)
  134. {
  135. switch (type) {
  136. case XFS_DQTYPE_USER:
  137. return ip->i_udquot;
  138. case XFS_DQTYPE_GROUP:
  139. return ip->i_gdquot;
  140. case XFS_DQTYPE_PROJ:
  141. return ip->i_pdquot;
  142. default:
  143. return NULL;
  144. }
  145. }
  146. /* Decide if the dquot's limits are actually being enforced. */
  147. static inline bool
  148. xfs_dquot_is_enforced(
  149. const struct xfs_dquot *dqp)
  150. {
  151. switch (xfs_dquot_type(dqp)) {
  152. case XFS_DQTYPE_USER:
  153. return XFS_IS_UQUOTA_ENFORCED(dqp->q_mount);
  154. case XFS_DQTYPE_GROUP:
  155. return XFS_IS_GQUOTA_ENFORCED(dqp->q_mount);
  156. case XFS_DQTYPE_PROJ:
  157. return XFS_IS_PQUOTA_ENFORCED(dqp->q_mount);
  158. }
  159. ASSERT(0);
  160. return false;
  161. }
  162. /*
  163. * Check whether a dquot is under low free space conditions. We assume the quota
  164. * is enabled and enforced.
  165. */
  166. static inline bool xfs_dquot_lowsp(struct xfs_dquot *dqp)
  167. {
  168. int64_t freesp;
  169. freesp = dqp->q_blk.hardlimit - dqp->q_blk.reserved;
  170. if (freesp < dqp->q_low_space[XFS_QLOWSP_1_PCNT])
  171. return true;
  172. return false;
  173. }
  174. void xfs_dquot_to_disk(struct xfs_disk_dquot *ddqp, struct xfs_dquot *dqp);
  175. #define XFS_DQ_IS_LOCKED(dqp) (mutex_is_locked(&((dqp)->q_qlock)))
  176. #define XFS_DQ_IS_DIRTY(dqp) ((dqp)->q_flags & XFS_DQFLAG_DIRTY)
  177. void xfs_qm_dqdestroy(struct xfs_dquot *dqp);
  178. int xfs_qm_dqflush(struct xfs_dquot *dqp, struct xfs_buf **bpp);
  179. void xfs_qm_dqunpin_wait(struct xfs_dquot *dqp);
  180. void xfs_qm_adjust_dqtimers(struct xfs_dquot *d);
  181. void xfs_qm_adjust_dqlimits(struct xfs_dquot *d);
  182. xfs_dqid_t xfs_qm_id_for_quotatype(struct xfs_inode *ip,
  183. xfs_dqtype_t type);
  184. int xfs_qm_dqget(struct xfs_mount *mp, xfs_dqid_t id,
  185. xfs_dqtype_t type, bool can_alloc,
  186. struct xfs_dquot **dqpp);
  187. int xfs_qm_dqget_inode(struct xfs_inode *ip, xfs_dqtype_t type,
  188. bool can_alloc, struct xfs_dquot **dqpp);
  189. int xfs_qm_dqget_next(struct xfs_mount *mp, xfs_dqid_t id,
  190. xfs_dqtype_t type, struct xfs_dquot **dqpp);
  191. int xfs_qm_dqget_uncached(struct xfs_mount *mp,
  192. xfs_dqid_t id, xfs_dqtype_t type,
  193. struct xfs_dquot **dqpp);
  194. void xfs_qm_dqput(struct xfs_dquot *dqp);
  195. void xfs_dqlock2(struct xfs_dquot *, struct xfs_dquot *);
  196. void xfs_dquot_set_prealloc_limits(struct xfs_dquot *);
  197. static inline struct xfs_dquot *xfs_qm_dqhold(struct xfs_dquot *dqp)
  198. {
  199. xfs_dqlock(dqp);
  200. dqp->q_nrefs++;
  201. xfs_dqunlock(dqp);
  202. return dqp;
  203. }
  204. typedef int (*xfs_qm_dqiterate_fn)(struct xfs_dquot *dq,
  205. xfs_dqtype_t type, void *priv);
  206. int xfs_qm_dqiterate(struct xfs_mount *mp, xfs_dqtype_t type,
  207. xfs_qm_dqiterate_fn iter_fn, void *priv);
  208. time64_t xfs_dquot_set_timeout(struct xfs_mount *mp, time64_t timeout);
  209. time64_t xfs_dquot_set_grace_period(time64_t grace);
  210. #endif /* __XFS_DQUOT_H__ */