xfs_quotaops.c 6.2 KB

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