xfs_quotaops.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 int
  18. xfs_qm_fill_state(
  19. struct qc_type_state *tstate,
  20. struct xfs_mount *mp,
  21. xfs_dqtype_t type)
  22. {
  23. struct xfs_inode *ip;
  24. struct xfs_def_quota *defq;
  25. int error;
  26. error = xfs_qm_qino_load(mp, type, &ip);
  27. if (error) {
  28. tstate->ino = NULLFSINO;
  29. return error != -ENOENT ? error : 0;
  30. }
  31. defq = xfs_get_defquota(mp->m_quotainfo, type);
  32. tstate->ino = ip->i_ino;
  33. tstate->flags |= QCI_SYSFILE;
  34. tstate->blocks = ip->i_nblocks;
  35. tstate->nextents = ip->i_df.if_nextents;
  36. tstate->spc_timelimit = (u32)defq->blk.time;
  37. tstate->ino_timelimit = (u32)defq->ino.time;
  38. tstate->rt_spc_timelimit = (u32)defq->rtb.time;
  39. tstate->spc_warnlimit = 0;
  40. tstate->ino_warnlimit = 0;
  41. tstate->rt_spc_warnlimit = 0;
  42. xfs_irele(ip);
  43. return 0;
  44. }
  45. /*
  46. * Return quota status information, such as enforcements, quota file inode
  47. * numbers etc.
  48. */
  49. static int
  50. xfs_fs_get_quota_state(
  51. struct super_block *sb,
  52. struct qc_state *state)
  53. {
  54. struct xfs_mount *mp = XFS_M(sb);
  55. struct xfs_quotainfo *q = mp->m_quotainfo;
  56. int error;
  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. error = xfs_qm_fill_state(&state->s_state[USRQUOTA], mp,
  74. XFS_DQTYPE_USER);
  75. if (error)
  76. return error;
  77. error = xfs_qm_fill_state(&state->s_state[GRPQUOTA], mp,
  78. XFS_DQTYPE_GROUP);
  79. if (error)
  80. return error;
  81. error = xfs_qm_fill_state(&state->s_state[PRJQUOTA], mp,
  82. XFS_DQTYPE_PROJ);
  83. if (error)
  84. return error;
  85. return 0;
  86. }
  87. STATIC xfs_dqtype_t
  88. xfs_quota_type(int type)
  89. {
  90. switch (type) {
  91. case USRQUOTA:
  92. return XFS_DQTYPE_USER;
  93. case GRPQUOTA:
  94. return XFS_DQTYPE_GROUP;
  95. default:
  96. return XFS_DQTYPE_PROJ;
  97. }
  98. }
  99. #define XFS_QC_SETINFO_MASK (QC_TIMER_MASK)
  100. /*
  101. * Adjust quota timers & warnings
  102. */
  103. static int
  104. xfs_fs_set_info(
  105. struct super_block *sb,
  106. int type,
  107. struct qc_info *info)
  108. {
  109. struct xfs_mount *mp = XFS_M(sb);
  110. struct qc_dqblk newlim;
  111. if (sb_rdonly(sb))
  112. return -EROFS;
  113. if (!XFS_IS_QUOTA_ON(mp))
  114. return -ENOSYS;
  115. if (info->i_fieldmask & ~XFS_QC_SETINFO_MASK)
  116. return -EINVAL;
  117. if ((info->i_fieldmask & XFS_QC_SETINFO_MASK) == 0)
  118. return 0;
  119. newlim.d_fieldmask = info->i_fieldmask;
  120. newlim.d_spc_timer = info->i_spc_timelimit;
  121. newlim.d_ino_timer = info->i_ino_timelimit;
  122. newlim.d_rt_spc_timer = info->i_rt_spc_timelimit;
  123. newlim.d_ino_warns = info->i_ino_warnlimit;
  124. newlim.d_spc_warns = info->i_spc_warnlimit;
  125. newlim.d_rt_spc_warns = info->i_rt_spc_warnlimit;
  126. return xfs_qm_scall_setqlim(mp, 0, xfs_quota_type(type), &newlim);
  127. }
  128. static unsigned int
  129. xfs_quota_flags(unsigned int uflags)
  130. {
  131. unsigned int flags = 0;
  132. if (uflags & FS_QUOTA_UDQ_ACCT)
  133. flags |= XFS_UQUOTA_ACCT;
  134. if (uflags & FS_QUOTA_PDQ_ACCT)
  135. flags |= XFS_PQUOTA_ACCT;
  136. if (uflags & FS_QUOTA_GDQ_ACCT)
  137. flags |= XFS_GQUOTA_ACCT;
  138. if (uflags & FS_QUOTA_UDQ_ENFD)
  139. flags |= XFS_UQUOTA_ENFD;
  140. if (uflags & FS_QUOTA_GDQ_ENFD)
  141. flags |= XFS_GQUOTA_ENFD;
  142. if (uflags & FS_QUOTA_PDQ_ENFD)
  143. flags |= XFS_PQUOTA_ENFD;
  144. return flags;
  145. }
  146. STATIC int
  147. xfs_quota_enable(
  148. struct super_block *sb,
  149. unsigned int uflags)
  150. {
  151. struct xfs_mount *mp = XFS_M(sb);
  152. if (sb_rdonly(sb))
  153. return -EROFS;
  154. if (!XFS_IS_QUOTA_ON(mp))
  155. return -ENOSYS;
  156. return xfs_qm_scall_quotaon(mp, xfs_quota_flags(uflags));
  157. }
  158. STATIC int
  159. xfs_quota_disable(
  160. struct super_block *sb,
  161. unsigned int uflags)
  162. {
  163. struct xfs_mount *mp = XFS_M(sb);
  164. if (sb_rdonly(sb))
  165. return -EROFS;
  166. if (!XFS_IS_QUOTA_ON(mp))
  167. return -ENOSYS;
  168. return xfs_qm_scall_quotaoff(mp, xfs_quota_flags(uflags));
  169. }
  170. STATIC int
  171. xfs_fs_rm_xquota(
  172. struct super_block *sb,
  173. unsigned int uflags)
  174. {
  175. struct xfs_mount *mp = XFS_M(sb);
  176. unsigned int flags = 0;
  177. if (sb_rdonly(sb))
  178. return -EROFS;
  179. if (XFS_IS_QUOTA_ON(mp))
  180. return -EINVAL;
  181. if (uflags & ~(FS_USER_QUOTA | FS_GROUP_QUOTA | FS_PROJ_QUOTA))
  182. return -EINVAL;
  183. if (uflags & FS_USER_QUOTA)
  184. flags |= XFS_QMOPT_UQUOTA;
  185. if (uflags & FS_GROUP_QUOTA)
  186. flags |= XFS_QMOPT_GQUOTA;
  187. if (uflags & FS_PROJ_QUOTA)
  188. flags |= XFS_QMOPT_PQUOTA;
  189. return xfs_qm_scall_trunc_qfiles(mp, flags);
  190. }
  191. STATIC int
  192. xfs_fs_get_dqblk(
  193. struct super_block *sb,
  194. struct kqid qid,
  195. struct qc_dqblk *qdq)
  196. {
  197. struct xfs_mount *mp = XFS_M(sb);
  198. xfs_dqid_t id;
  199. if (!XFS_IS_QUOTA_ON(mp))
  200. return -ENOSYS;
  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_ON(mp))
  215. return -ENOSYS;
  216. id = from_kqid(&init_user_ns, *qid);
  217. ret = xfs_qm_scall_getquota_next(mp, &id, xfs_quota_type(qid->type),
  218. qdq);
  219. if (ret)
  220. return ret;
  221. /* ID may be different, so convert back what we got */
  222. *qid = make_kqid(current_user_ns(), qid->type, id);
  223. return 0;
  224. }
  225. STATIC int
  226. xfs_fs_set_dqblk(
  227. struct super_block *sb,
  228. struct kqid qid,
  229. struct qc_dqblk *qdq)
  230. {
  231. struct xfs_mount *mp = XFS_M(sb);
  232. if (sb_rdonly(sb))
  233. return -EROFS;
  234. if (!XFS_IS_QUOTA_ON(mp))
  235. return -ENOSYS;
  236. return xfs_qm_scall_setqlim(mp, from_kqid(&init_user_ns, qid),
  237. xfs_quota_type(qid.type), qdq);
  238. }
  239. const struct quotactl_ops xfs_quotactl_operations = {
  240. .get_state = xfs_fs_get_quota_state,
  241. .set_info = xfs_fs_set_info,
  242. .quota_enable = xfs_quota_enable,
  243. .quota_disable = xfs_quota_disable,
  244. .rm_xquota = xfs_fs_rm_xquota,
  245. .get_dqblk = xfs_fs_get_dqblk,
  246. .get_nextdqblk = xfs_fs_get_nextdqblk,
  247. .set_dqblk = xfs_fs_set_dqblk,
  248. };