xfs_quotaops.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2008, Christoph Hellwig
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_format.h"
  8. #include "xfs_log_format.h"
  9. #include "xfs_trans_resv.h"
  10. #include "xfs_mount.h"
  11. #include "xfs_inode.h"
  12. #include "xfs_quota.h"
  13. #include "xfs_trans.h"
  14. #include "xfs_trace.h"
  15. #include "xfs_icache.h"
  16. #include "xfs_qm.h"
  17. #include <linux/quota.h>
  18. static void
  19. xfs_qm_fill_state(
  20. struct qc_type_state *tstate,
  21. struct xfs_mount *mp,
  22. struct xfs_inode *ip,
  23. xfs_ino_t ino)
  24. {
  25. struct xfs_quotainfo *q = mp->m_quotainfo;
  26. bool tempqip = false;
  27. tstate->ino = ino;
  28. if (!ip && ino == NULLFSINO)
  29. return;
  30. if (!ip) {
  31. if (xfs_iget(mp, NULL, ino, 0, 0, &ip))
  32. return;
  33. tempqip = true;
  34. }
  35. tstate->flags |= QCI_SYSFILE;
  36. tstate->blocks = ip->i_d.di_nblocks;
  37. tstate->nextents = ip->i_d.di_nextents;
  38. tstate->spc_timelimit = q->qi_btimelimit;
  39. tstate->ino_timelimit = q->qi_itimelimit;
  40. tstate->rt_spc_timelimit = q->qi_rtbtimelimit;
  41. tstate->spc_warnlimit = q->qi_bwarnlimit;
  42. tstate->ino_warnlimit = q->qi_iwarnlimit;
  43. tstate->rt_spc_warnlimit = q->qi_rtbwarnlimit;
  44. if (tempqip)
  45. xfs_irele(ip);
  46. }
  47. /*
  48. * Return quota status information, such as enforcements, quota file inode
  49. * numbers etc.
  50. */
  51. static int
  52. xfs_fs_get_quota_state(
  53. struct super_block *sb,
  54. struct qc_state *state)
  55. {
  56. struct xfs_mount *mp = XFS_M(sb);
  57. struct xfs_quotainfo *q = mp->m_quotainfo;
  58. memset(state, 0, sizeof(*state));
  59. if (!XFS_IS_QUOTA_RUNNING(mp))
  60. return 0;
  61. state->s_incoredqs = q->qi_dquots;
  62. if (XFS_IS_UQUOTA_RUNNING(mp))
  63. state->s_state[USRQUOTA].flags |= QCI_ACCT_ENABLED;
  64. if (XFS_IS_UQUOTA_ENFORCED(mp))
  65. state->s_state[USRQUOTA].flags |= QCI_LIMITS_ENFORCED;
  66. if (XFS_IS_GQUOTA_RUNNING(mp))
  67. state->s_state[GRPQUOTA].flags |= QCI_ACCT_ENABLED;
  68. if (XFS_IS_GQUOTA_ENFORCED(mp))
  69. state->s_state[GRPQUOTA].flags |= QCI_LIMITS_ENFORCED;
  70. if (XFS_IS_PQUOTA_RUNNING(mp))
  71. state->s_state[PRJQUOTA].flags |= QCI_ACCT_ENABLED;
  72. if (XFS_IS_PQUOTA_ENFORCED(mp))
  73. state->s_state[PRJQUOTA].flags |= QCI_LIMITS_ENFORCED;
  74. xfs_qm_fill_state(&state->s_state[USRQUOTA], mp, q->qi_uquotaip,
  75. mp->m_sb.sb_uquotino);
  76. xfs_qm_fill_state(&state->s_state[GRPQUOTA], mp, q->qi_gquotaip,
  77. mp->m_sb.sb_gquotino);
  78. xfs_qm_fill_state(&state->s_state[PRJQUOTA], mp, q->qi_pquotaip,
  79. mp->m_sb.sb_pquotino);
  80. return 0;
  81. }
  82. STATIC int
  83. xfs_quota_type(int type)
  84. {
  85. switch (type) {
  86. case USRQUOTA:
  87. return XFS_DQ_USER;
  88. case GRPQUOTA:
  89. return XFS_DQ_GROUP;
  90. default:
  91. return XFS_DQ_PROJ;
  92. }
  93. }
  94. #define XFS_QC_SETINFO_MASK (QC_TIMER_MASK | QC_WARNS_MASK)
  95. /*
  96. * Adjust quota timers & warnings
  97. */
  98. static int
  99. xfs_fs_set_info(
  100. struct super_block *sb,
  101. int type,
  102. struct qc_info *info)
  103. {
  104. struct xfs_mount *mp = XFS_M(sb);
  105. struct qc_dqblk newlim;
  106. if (sb_rdonly(sb))
  107. return -EROFS;
  108. if (!XFS_IS_QUOTA_RUNNING(mp))
  109. return -ENOSYS;
  110. if (!XFS_IS_QUOTA_ON(mp))
  111. return -ESRCH;
  112. if (info->i_fieldmask & ~XFS_QC_SETINFO_MASK)
  113. return -EINVAL;
  114. if ((info->i_fieldmask & XFS_QC_SETINFO_MASK) == 0)
  115. return 0;
  116. newlim.d_fieldmask = info->i_fieldmask;
  117. newlim.d_spc_timer = info->i_spc_timelimit;
  118. newlim.d_ino_timer = info->i_ino_timelimit;
  119. newlim.d_rt_spc_timer = info->i_rt_spc_timelimit;
  120. newlim.d_ino_warns = info->i_ino_warnlimit;
  121. newlim.d_spc_warns = info->i_spc_warnlimit;
  122. newlim.d_rt_spc_warns = info->i_rt_spc_warnlimit;
  123. return xfs_qm_scall_setqlim(mp, 0, xfs_quota_type(type), &newlim);
  124. }
  125. static unsigned int
  126. xfs_quota_flags(unsigned int uflags)
  127. {
  128. unsigned int flags = 0;
  129. if (uflags & FS_QUOTA_UDQ_ACCT)
  130. flags |= XFS_UQUOTA_ACCT;
  131. if (uflags & FS_QUOTA_PDQ_ACCT)
  132. flags |= XFS_PQUOTA_ACCT;
  133. if (uflags & FS_QUOTA_GDQ_ACCT)
  134. flags |= XFS_GQUOTA_ACCT;
  135. if (uflags & FS_QUOTA_UDQ_ENFD)
  136. flags |= XFS_UQUOTA_ENFD;
  137. if (uflags & FS_QUOTA_GDQ_ENFD)
  138. flags |= XFS_GQUOTA_ENFD;
  139. if (uflags & FS_QUOTA_PDQ_ENFD)
  140. flags |= XFS_PQUOTA_ENFD;
  141. return flags;
  142. }
  143. STATIC int
  144. xfs_quota_enable(
  145. struct super_block *sb,
  146. unsigned int uflags)
  147. {
  148. struct xfs_mount *mp = XFS_M(sb);
  149. if (sb_rdonly(sb))
  150. return -EROFS;
  151. if (!XFS_IS_QUOTA_RUNNING(mp))
  152. return -ENOSYS;
  153. return xfs_qm_scall_quotaon(mp, xfs_quota_flags(uflags));
  154. }
  155. STATIC int
  156. xfs_quota_disable(
  157. struct super_block *sb,
  158. unsigned int uflags)
  159. {
  160. struct xfs_mount *mp = XFS_M(sb);
  161. if (sb_rdonly(sb))
  162. return -EROFS;
  163. if (!XFS_IS_QUOTA_RUNNING(mp))
  164. return -ENOSYS;
  165. if (!XFS_IS_QUOTA_ON(mp))
  166. return -EINVAL;
  167. return xfs_qm_scall_quotaoff(mp, xfs_quota_flags(uflags));
  168. }
  169. STATIC int
  170. xfs_fs_rm_xquota(
  171. struct super_block *sb,
  172. unsigned int uflags)
  173. {
  174. struct xfs_mount *mp = XFS_M(sb);
  175. unsigned int flags = 0;
  176. if (sb_rdonly(sb))
  177. return -EROFS;
  178. if (XFS_IS_QUOTA_ON(mp))
  179. return -EINVAL;
  180. if (uflags & ~(FS_USER_QUOTA | FS_GROUP_QUOTA | FS_PROJ_QUOTA))
  181. return -EINVAL;
  182. if (uflags & FS_USER_QUOTA)
  183. flags |= XFS_DQ_USER;
  184. if (uflags & FS_GROUP_QUOTA)
  185. flags |= XFS_DQ_GROUP;
  186. if (uflags & FS_PROJ_QUOTA)
  187. flags |= XFS_DQ_PROJ;
  188. return xfs_qm_scall_trunc_qfiles(mp, flags);
  189. }
  190. STATIC int
  191. xfs_fs_get_dqblk(
  192. struct super_block *sb,
  193. struct kqid qid,
  194. struct qc_dqblk *qdq)
  195. {
  196. struct xfs_mount *mp = XFS_M(sb);
  197. xfs_dqid_t id;
  198. if (!XFS_IS_QUOTA_RUNNING(mp))
  199. return -ENOSYS;
  200. if (!XFS_IS_QUOTA_ON(mp))
  201. return -ESRCH;
  202. id = from_kqid(&init_user_ns, qid);
  203. return xfs_qm_scall_getquota(mp, id, xfs_quota_type(qid.type), qdq);
  204. }
  205. /* Return quota info for active quota >= this qid */
  206. STATIC int
  207. xfs_fs_get_nextdqblk(
  208. struct super_block *sb,
  209. struct kqid *qid,
  210. struct qc_dqblk *qdq)
  211. {
  212. int ret;
  213. struct xfs_mount *mp = XFS_M(sb);
  214. xfs_dqid_t id;
  215. if (!XFS_IS_QUOTA_RUNNING(mp))
  216. return -ENOSYS;
  217. if (!XFS_IS_QUOTA_ON(mp))
  218. return -ESRCH;
  219. id = from_kqid(&init_user_ns, *qid);
  220. ret = xfs_qm_scall_getquota_next(mp, &id, xfs_quota_type(qid->type),
  221. qdq);
  222. if (ret)
  223. return ret;
  224. /* ID may be different, so convert back what we got */
  225. *qid = make_kqid(current_user_ns(), qid->type, id);
  226. return 0;
  227. }
  228. STATIC int
  229. xfs_fs_set_dqblk(
  230. struct super_block *sb,
  231. struct kqid qid,
  232. struct qc_dqblk *qdq)
  233. {
  234. struct xfs_mount *mp = XFS_M(sb);
  235. if (sb_rdonly(sb))
  236. return -EROFS;
  237. if (!XFS_IS_QUOTA_RUNNING(mp))
  238. return -ENOSYS;
  239. if (!XFS_IS_QUOTA_ON(mp))
  240. return -ESRCH;
  241. return xfs_qm_scall_setqlim(mp, from_kqid(&init_user_ns, qid),
  242. xfs_quota_type(qid.type), qdq);
  243. }
  244. const struct quotactl_ops xfs_quotactl_operations = {
  245. .get_state = xfs_fs_get_quota_state,
  246. .set_info = xfs_fs_set_info,
  247. .quota_enable = xfs_quota_enable,
  248. .quota_disable = xfs_quota_disable,
  249. .rm_xquota = xfs_fs_rm_xquota,
  250. .get_dqblk = xfs_fs_get_dqblk,
  251. .get_nextdqblk = xfs_fs_get_nextdqblk,
  252. .set_dqblk = xfs_fs_set_dqblk,
  253. };