xfs_quotaops.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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_d.di_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 = defq->blk.warn;
  41. tstate->ino_warnlimit = defq->ino.warn;
  42. tstate->rt_spc_warnlimit = defq->rtb.warn;
  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_RUNNING(mp))
  59. return 0;
  60. state->s_incoredqs = q->qi_dquots;
  61. if (XFS_IS_UQUOTA_RUNNING(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_RUNNING(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_RUNNING(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 | QC_WARNS_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_RUNNING(mp))
  108. return -ENOSYS;
  109. if (!XFS_IS_QUOTA_ON(mp))
  110. return -ESRCH;
  111. if (info->i_fieldmask & ~XFS_QC_SETINFO_MASK)
  112. return -EINVAL;
  113. if ((info->i_fieldmask & XFS_QC_SETINFO_MASK) == 0)
  114. return 0;
  115. newlim.d_fieldmask = info->i_fieldmask;
  116. newlim.d_spc_timer = info->i_spc_timelimit;
  117. newlim.d_ino_timer = info->i_ino_timelimit;
  118. newlim.d_rt_spc_timer = info->i_rt_spc_timelimit;
  119. newlim.d_ino_warns = info->i_ino_warnlimit;
  120. newlim.d_spc_warns = info->i_spc_warnlimit;
  121. newlim.d_rt_spc_warns = info->i_rt_spc_warnlimit;
  122. return xfs_qm_scall_setqlim(mp, 0, xfs_quota_type(type), &newlim);
  123. }
  124. static unsigned int
  125. xfs_quota_flags(unsigned int uflags)
  126. {
  127. unsigned int flags = 0;
  128. if (uflags & FS_QUOTA_UDQ_ACCT)
  129. flags |= XFS_UQUOTA_ACCT;
  130. if (uflags & FS_QUOTA_PDQ_ACCT)
  131. flags |= XFS_PQUOTA_ACCT;
  132. if (uflags & FS_QUOTA_GDQ_ACCT)
  133. flags |= XFS_GQUOTA_ACCT;
  134. if (uflags & FS_QUOTA_UDQ_ENFD)
  135. flags |= XFS_UQUOTA_ENFD;
  136. if (uflags & FS_QUOTA_GDQ_ENFD)
  137. flags |= XFS_GQUOTA_ENFD;
  138. if (uflags & FS_QUOTA_PDQ_ENFD)
  139. flags |= XFS_PQUOTA_ENFD;
  140. return flags;
  141. }
  142. STATIC int
  143. xfs_quota_enable(
  144. struct super_block *sb,
  145. unsigned int uflags)
  146. {
  147. struct xfs_mount *mp = XFS_M(sb);
  148. if (sb_rdonly(sb))
  149. return -EROFS;
  150. if (!XFS_IS_QUOTA_RUNNING(mp))
  151. return -ENOSYS;
  152. return xfs_qm_scall_quotaon(mp, xfs_quota_flags(uflags));
  153. }
  154. STATIC int
  155. xfs_quota_disable(
  156. struct super_block *sb,
  157. unsigned int uflags)
  158. {
  159. struct xfs_mount *mp = XFS_M(sb);
  160. if (sb_rdonly(sb))
  161. return -EROFS;
  162. if (!XFS_IS_QUOTA_RUNNING(mp))
  163. return -ENOSYS;
  164. if (!XFS_IS_QUOTA_ON(mp))
  165. return -EINVAL;
  166. return xfs_qm_scall_quotaoff(mp, xfs_quota_flags(uflags));
  167. }
  168. STATIC int
  169. xfs_fs_rm_xquota(
  170. struct super_block *sb,
  171. unsigned int uflags)
  172. {
  173. struct xfs_mount *mp = XFS_M(sb);
  174. unsigned int flags = 0;
  175. if (sb_rdonly(sb))
  176. return -EROFS;
  177. if (XFS_IS_QUOTA_ON(mp))
  178. return -EINVAL;
  179. if (uflags & ~(FS_USER_QUOTA | FS_GROUP_QUOTA | FS_PROJ_QUOTA))
  180. return -EINVAL;
  181. if (uflags & FS_USER_QUOTA)
  182. flags |= XFS_QMOPT_UQUOTA;
  183. if (uflags & FS_GROUP_QUOTA)
  184. flags |= XFS_QMOPT_GQUOTA;
  185. if (uflags & FS_PROJ_QUOTA)
  186. flags |= XFS_QMOPT_PQUOTA;
  187. return xfs_qm_scall_trunc_qfiles(mp, flags);
  188. }
  189. STATIC int
  190. xfs_fs_get_dqblk(
  191. struct super_block *sb,
  192. struct kqid qid,
  193. struct qc_dqblk *qdq)
  194. {
  195. struct xfs_mount *mp = XFS_M(sb);
  196. xfs_dqid_t id;
  197. if (!XFS_IS_QUOTA_RUNNING(mp))
  198. return -ENOSYS;
  199. if (!XFS_IS_QUOTA_ON(mp))
  200. return -ESRCH;
  201. id = from_kqid(&init_user_ns, qid);
  202. return xfs_qm_scall_getquota(mp, id, xfs_quota_type(qid.type), qdq);
  203. }
  204. /* Return quota info for active quota >= this qid */
  205. STATIC int
  206. xfs_fs_get_nextdqblk(
  207. struct super_block *sb,
  208. struct kqid *qid,
  209. struct qc_dqblk *qdq)
  210. {
  211. int ret;
  212. struct xfs_mount *mp = XFS_M(sb);
  213. xfs_dqid_t id;
  214. if (!XFS_IS_QUOTA_RUNNING(mp))
  215. return -ENOSYS;
  216. if (!XFS_IS_QUOTA_ON(mp))
  217. return -ESRCH;
  218. id = from_kqid(&init_user_ns, *qid);
  219. ret = xfs_qm_scall_getquota_next(mp, &id, xfs_quota_type(qid->type),
  220. qdq);
  221. if (ret)
  222. return ret;
  223. /* ID may be different, so convert back what we got */
  224. *qid = make_kqid(current_user_ns(), qid->type, id);
  225. return 0;
  226. }
  227. STATIC int
  228. xfs_fs_set_dqblk(
  229. struct super_block *sb,
  230. struct kqid qid,
  231. struct qc_dqblk *qdq)
  232. {
  233. struct xfs_mount *mp = XFS_M(sb);
  234. if (sb_rdonly(sb))
  235. return -EROFS;
  236. if (!XFS_IS_QUOTA_RUNNING(mp))
  237. return -ENOSYS;
  238. if (!XFS_IS_QUOTA_ON(mp))
  239. return -ESRCH;
  240. return xfs_qm_scall_setqlim(mp, from_kqid(&init_user_ns, qid),
  241. xfs_quota_type(qid.type), qdq);
  242. }
  243. const struct quotactl_ops xfs_quotactl_operations = {
  244. .get_state = xfs_fs_get_quota_state,
  245. .set_info = xfs_fs_set_info,
  246. .quota_enable = xfs_quota_enable,
  247. .quota_disable = xfs_quota_disable,
  248. .rm_xquota = xfs_fs_rm_xquota,
  249. .get_dqblk = xfs_fs_get_dqblk,
  250. .get_nextdqblk = xfs_fs_get_nextdqblk,
  251. .set_dqblk = xfs_fs_set_dqblk,
  252. };