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