Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Nathan Scott | 4ce3121 | 2005-11-02 14:59:41 +1100 | [diff] [blame] | 3 | * Copyright (c) 2000-2002 Silicon Graphics, Inc. |
| 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include "xfs.h" |
| 7 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 8 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 9 | #include "xfs_format.h" |
| 10 | #include "xfs_log_format.h" |
| 11 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "xfs_inode.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 14 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include "xfs_trans_priv.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 16 | #include "xfs_quota.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "xfs_qm.h" |
Darrick J. Wong | 2cb91ba | 2020-07-14 10:37:35 -0700 | [diff] [blame] | 18 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | STATIC void xfs_trans_alloc_dqinfo(xfs_trans_t *); |
| 21 | |
| 22 | /* |
| 23 | * Add the locked dquot to the transaction. |
| 24 | * The dquot must be locked, and it cannot be associated with any |
| 25 | * transaction. |
| 26 | */ |
| 27 | void |
| 28 | xfs_trans_dqjoin( |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 29 | struct xfs_trans *tp, |
| 30 | struct xfs_dquot *dqp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | ASSERT(XFS_DQ_IS_LOCKED(dqp)); |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 33 | ASSERT(dqp->q_logitem.qli_dquot == dqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * Get a log_item_desc to point at the new item. |
| 37 | */ |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 38 | xfs_trans_add_item(tp, &dqp->q_logitem.qli_item); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* |
| 42 | * This is called to mark the dquot as needing |
| 43 | * to be logged when the transaction is committed. The dquot must |
| 44 | * already be associated with the given transaction. |
| 45 | * Note that it marks the entire transaction as dirty. In the ordinary |
| 46 | * case, this gets called via xfs_trans_commit, after the transaction |
| 47 | * is already dirty. However, there's nothing stop this from getting |
| 48 | * called directly, as done by xfs_qm_scall_setqlim. Hence, the TRANS_DIRTY |
| 49 | * flag. |
| 50 | */ |
| 51 | void |
| 52 | xfs_trans_log_dquot( |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 53 | struct xfs_trans *tp, |
| 54 | struct xfs_dquot *dqp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | ASSERT(XFS_DQ_IS_LOCKED(dqp)); |
| 57 | |
Darrick J. Wong | 4ea1ff3 | 2020-08-17 09:59:51 -0700 | [diff] [blame] | 58 | /* Upgrade the dquot to bigtime format if possible. */ |
| 59 | if (dqp->q_id != 0 && |
| 60 | xfs_sb_version_hasbigtime(&tp->t_mountp->m_sb) && |
| 61 | !(dqp->q_type & XFS_DQTYPE_BIGTIME)) |
| 62 | dqp->q_type |= XFS_DQTYPE_BIGTIME; |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | tp->t_flags |= XFS_TRANS_DIRTY; |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame] | 65 | set_bit(XFS_LI_DIRTY, &dqp->q_logitem.qli_item.li_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Carry forward whatever is left of the quota blk reservation to |
| 70 | * the spanky new transaction |
| 71 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 72 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | xfs_trans_dup_dqinfo( |
Darrick J. Wong | 078f4a7 | 2019-04-17 16:30:24 -0700 | [diff] [blame] | 74 | struct xfs_trans *otp, |
| 75 | struct xfs_trans *ntp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
Darrick J. Wong | 078f4a7 | 2019-04-17 16:30:24 -0700 | [diff] [blame] | 77 | struct xfs_dqtrx *oq, *nq; |
| 78 | int i, j; |
| 79 | struct xfs_dqtrx *oqa, *nqa; |
| 80 | uint64_t blk_res_used; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | if (!otp->t_dqinfo) |
| 83 | return; |
| 84 | |
| 85 | xfs_trans_alloc_dqinfo(ntp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * Because the quota blk reservation is carried forward, |
| 89 | * it is also necessary to carry forward the DQ_DIRTY flag. |
| 90 | */ |
Nan Jia | 339e4f6 | 2015-06-01 10:50:00 +1000 | [diff] [blame] | 91 | if (otp->t_flags & XFS_TRANS_DQ_DIRTY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | ntp->t_flags |= XFS_TRANS_DQ_DIRTY; |
| 93 | |
Chandra Seetharaman | 0e6436d | 2013-06-27 17:25:09 -0500 | [diff] [blame] | 94 | for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) { |
| 95 | oqa = otp->t_dqinfo->dqs[j]; |
| 96 | nqa = ntp->t_dqinfo->dqs[j]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) { |
Brian Foster | 7f884dc | 2015-06-01 07:15:37 +1000 | [diff] [blame] | 98 | blk_res_used = 0; |
| 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | if (oqa[i].qt_dquot == NULL) |
| 101 | break; |
| 102 | oq = &oqa[i]; |
| 103 | nq = &nqa[i]; |
| 104 | |
Brian Foster | 7f884dc | 2015-06-01 07:15:37 +1000 | [diff] [blame] | 105 | if (oq->qt_blk_res && oq->qt_bcount_delta > 0) |
| 106 | blk_res_used = oq->qt_bcount_delta; |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | nq->qt_dquot = oq->qt_dquot; |
| 109 | nq->qt_bcount_delta = nq->qt_icount_delta = 0; |
| 110 | nq->qt_rtbcount_delta = 0; |
| 111 | |
| 112 | /* |
| 113 | * Transfer whatever is left of the reservations. |
| 114 | */ |
Brian Foster | 7f884dc | 2015-06-01 07:15:37 +1000 | [diff] [blame] | 115 | nq->qt_blk_res = oq->qt_blk_res - blk_res_used; |
| 116 | oq->qt_blk_res = blk_res_used; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | nq->qt_rtblk_res = oq->qt_rtblk_res - |
| 119 | oq->qt_rtblk_res_used; |
| 120 | oq->qt_rtblk_res = oq->qt_rtblk_res_used; |
| 121 | |
| 122 | nq->qt_ino_res = oq->qt_ino_res - oq->qt_ino_res_used; |
| 123 | oq->qt_ino_res = oq->qt_ino_res_used; |
| 124 | |
| 125 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Wrap around mod_dquot to account for both user and group quotas. |
| 131 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 132 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | xfs_trans_mod_dquot_byino( |
| 134 | xfs_trans_t *tp, |
| 135 | xfs_inode_t *ip, |
| 136 | uint field, |
Darrick J. Wong | 903b1fc | 2019-04-17 16:30:24 -0700 | [diff] [blame] | 137 | int64_t delta) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 139 | xfs_mount_t *mp = tp->t_mountp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 141 | if (!XFS_IS_QUOTA_RUNNING(mp) || |
| 142 | !XFS_IS_QUOTA_ON(mp) || |
Chandra Seetharaman | 9cad19d | 2013-06-27 17:25:04 -0500 | [diff] [blame] | 143 | xfs_is_quota_inode(&mp->m_sb, ip->i_ino)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | return; |
| 145 | |
| 146 | if (tp->t_dqinfo == NULL) |
| 147 | xfs_trans_alloc_dqinfo(tp); |
| 148 | |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 149 | if (XFS_IS_UQUOTA_ON(mp) && ip->i_udquot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | (void) xfs_trans_mod_dquot(tp, ip->i_udquot, field, delta); |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 151 | if (XFS_IS_GQUOTA_ON(mp) && ip->i_gdquot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | (void) xfs_trans_mod_dquot(tp, ip->i_gdquot, field, delta); |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 153 | if (XFS_IS_PQUOTA_ON(mp) && ip->i_pdquot) |
| 154 | (void) xfs_trans_mod_dquot(tp, ip->i_pdquot, field, delta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Chandra Seetharaman | 113a568 | 2013-06-27 17:25:07 -0500 | [diff] [blame] | 157 | STATIC struct xfs_dqtrx * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | xfs_trans_get_dqtrx( |
Chandra Seetharaman | 113a568 | 2013-06-27 17:25:07 -0500 | [diff] [blame] | 159 | struct xfs_trans *tp, |
| 160 | struct xfs_dquot *dqp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
Chandra Seetharaman | 113a568 | 2013-06-27 17:25:07 -0500 | [diff] [blame] | 162 | int i; |
| 163 | struct xfs_dqtrx *qa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Darrick J. Wong | 00a342e | 2020-07-15 17:47:13 -0700 | [diff] [blame] | 165 | switch (xfs_dquot_type(dqp)) { |
| 166 | case XFS_DQTYPE_USER: |
Chandra Seetharaman | 0e6436d | 2013-06-27 17:25:09 -0500 | [diff] [blame] | 167 | qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_USR]; |
Darrick J. Wong | 00a342e | 2020-07-15 17:47:13 -0700 | [diff] [blame] | 168 | break; |
| 169 | case XFS_DQTYPE_GROUP: |
Chandra Seetharaman | 0e6436d | 2013-06-27 17:25:09 -0500 | [diff] [blame] | 170 | qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_GRP]; |
Darrick J. Wong | 00a342e | 2020-07-15 17:47:13 -0700 | [diff] [blame] | 171 | break; |
| 172 | case XFS_DQTYPE_PROJ: |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 173 | qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_PRJ]; |
Darrick J. Wong | 00a342e | 2020-07-15 17:47:13 -0700 | [diff] [blame] | 174 | break; |
| 175 | default: |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 176 | return NULL; |
Darrick J. Wong | 00a342e | 2020-07-15 17:47:13 -0700 | [diff] [blame] | 177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Christoph Hellwig | 191f848 | 2010-04-20 17:01:53 +1000 | [diff] [blame] | 179 | for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | if (qa[i].qt_dquot == NULL || |
Christoph Hellwig | 191f848 | 2010-04-20 17:01:53 +1000 | [diff] [blame] | 181 | qa[i].qt_dquot == dqp) |
| 182 | return &qa[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Christoph Hellwig | 191f848 | 2010-04-20 17:01:53 +1000 | [diff] [blame] | 185 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Make the changes in the transaction structure. |
| 190 | * The moral equivalent to xfs_trans_mod_sb(). |
| 191 | * We don't touch any fields in the dquot, so we don't care |
| 192 | * if it's locked or not (most of the time it won't be). |
| 193 | */ |
| 194 | void |
| 195 | xfs_trans_mod_dquot( |
Darrick J. Wong | 078f4a7 | 2019-04-17 16:30:24 -0700 | [diff] [blame] | 196 | struct xfs_trans *tp, |
| 197 | struct xfs_dquot *dqp, |
| 198 | uint field, |
| 199 | int64_t delta) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Darrick J. Wong | 078f4a7 | 2019-04-17 16:30:24 -0700 | [diff] [blame] | 201 | struct xfs_dqtrx *qtrx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | ASSERT(tp); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 204 | ASSERT(XFS_IS_QUOTA_RUNNING(tp->t_mountp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | qtrx = NULL; |
| 206 | |
| 207 | if (tp->t_dqinfo == NULL) |
| 208 | xfs_trans_alloc_dqinfo(tp); |
| 209 | /* |
| 210 | * Find either the first free slot or the slot that belongs |
| 211 | * to this dquot. |
| 212 | */ |
| 213 | qtrx = xfs_trans_get_dqtrx(tp, dqp); |
| 214 | ASSERT(qtrx); |
| 215 | if (qtrx->qt_dquot == NULL) |
| 216 | qtrx->qt_dquot = dqp; |
| 217 | |
Darrick J. Wong | 2cb91ba | 2020-07-14 10:37:35 -0700 | [diff] [blame] | 218 | if (delta) { |
| 219 | trace_xfs_trans_mod_dquot_before(qtrx); |
| 220 | trace_xfs_trans_mod_dquot(tp, dqp, field, delta); |
| 221 | } |
| 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | switch (field) { |
| 224 | |
| 225 | /* |
| 226 | * regular disk blk reservation |
| 227 | */ |
| 228 | case XFS_TRANS_DQ_RES_BLKS: |
Darrick J. Wong | 903b1fc | 2019-04-17 16:30:24 -0700 | [diff] [blame] | 229 | qtrx->qt_blk_res += delta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | break; |
| 231 | |
| 232 | /* |
| 233 | * inode reservation |
| 234 | */ |
| 235 | case XFS_TRANS_DQ_RES_INOS: |
Darrick J. Wong | 903b1fc | 2019-04-17 16:30:24 -0700 | [diff] [blame] | 236 | qtrx->qt_ino_res += delta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | break; |
| 238 | |
| 239 | /* |
| 240 | * disk blocks used. |
| 241 | */ |
| 242 | case XFS_TRANS_DQ_BCOUNT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | qtrx->qt_bcount_delta += delta; |
| 244 | break; |
| 245 | |
| 246 | case XFS_TRANS_DQ_DELBCOUNT: |
| 247 | qtrx->qt_delbcnt_delta += delta; |
| 248 | break; |
| 249 | |
| 250 | /* |
| 251 | * Inode Count |
| 252 | */ |
| 253 | case XFS_TRANS_DQ_ICOUNT: |
| 254 | if (qtrx->qt_ino_res && delta > 0) { |
Darrick J. Wong | 903b1fc | 2019-04-17 16:30:24 -0700 | [diff] [blame] | 255 | qtrx->qt_ino_res_used += delta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used); |
| 257 | } |
| 258 | qtrx->qt_icount_delta += delta; |
| 259 | break; |
| 260 | |
| 261 | /* |
| 262 | * rtblk reservation |
| 263 | */ |
| 264 | case XFS_TRANS_DQ_RES_RTBLKS: |
Darrick J. Wong | 903b1fc | 2019-04-17 16:30:24 -0700 | [diff] [blame] | 265 | qtrx->qt_rtblk_res += delta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | break; |
| 267 | |
| 268 | /* |
| 269 | * rtblk count |
| 270 | */ |
| 271 | case XFS_TRANS_DQ_RTBCOUNT: |
| 272 | if (qtrx->qt_rtblk_res && delta > 0) { |
Darrick J. Wong | 903b1fc | 2019-04-17 16:30:24 -0700 | [diff] [blame] | 273 | qtrx->qt_rtblk_res_used += delta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | ASSERT(qtrx->qt_rtblk_res >= qtrx->qt_rtblk_res_used); |
| 275 | } |
| 276 | qtrx->qt_rtbcount_delta += delta; |
| 277 | break; |
| 278 | |
| 279 | case XFS_TRANS_DQ_DELRTBCOUNT: |
| 280 | qtrx->qt_delrtb_delta += delta; |
| 281 | break; |
| 282 | |
| 283 | default: |
| 284 | ASSERT(0); |
| 285 | } |
Darrick J. Wong | 2cb91ba | 2020-07-14 10:37:35 -0700 | [diff] [blame] | 286 | |
| 287 | if (delta) |
| 288 | trace_xfs_trans_mod_dquot_after(qtrx); |
| 289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | tp->t_flags |= XFS_TRANS_DQ_DIRTY; |
| 291 | } |
| 292 | |
| 293 | |
| 294 | /* |
Dave Chinner | b0a9dab | 2013-07-10 07:04:01 +1000 | [diff] [blame] | 295 | * Given an array of dqtrx structures, lock all the dquots associated and join |
| 296 | * them to the transaction, provided they have been modified. We know that the |
Christoph Hellwig | 10f73d2 | 2013-11-06 03:45:36 -0800 | [diff] [blame] | 297 | * highest number of dquots of one type - usr, grp and prj - involved in a |
| 298 | * transaction is 3 so we don't need to make this very generic. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | */ |
| 300 | STATIC void |
| 301 | xfs_trans_dqlockedjoin( |
Darrick J. Wong | 078f4a7 | 2019-04-17 16:30:24 -0700 | [diff] [blame] | 302 | struct xfs_trans *tp, |
| 303 | struct xfs_dqtrx *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { |
| 305 | ASSERT(q[0].qt_dquot != NULL); |
| 306 | if (q[1].qt_dquot == NULL) { |
| 307 | xfs_dqlock(q[0].qt_dquot); |
| 308 | xfs_trans_dqjoin(tp, q[0].qt_dquot); |
| 309 | } else { |
| 310 | ASSERT(XFS_QM_TRANS_MAXDQS == 2); |
| 311 | xfs_dqlock2(q[0].qt_dquot, q[1].qt_dquot); |
| 312 | xfs_trans_dqjoin(tp, q[0].qt_dquot); |
| 313 | xfs_trans_dqjoin(tp, q[1].qt_dquot); |
| 314 | } |
| 315 | } |
| 316 | |
Darrick J. Wong | d92c881 | 2020-07-14 10:37:34 -0700 | [diff] [blame] | 317 | /* Apply dqtrx changes to the quota reservation counters. */ |
| 318 | static inline void |
| 319 | xfs_apply_quota_reservation_deltas( |
| 320 | struct xfs_dquot_res *res, |
| 321 | uint64_t reserved, |
| 322 | int64_t res_used, |
| 323 | int64_t count_delta) |
| 324 | { |
| 325 | if (reserved != 0) { |
| 326 | /* |
| 327 | * Subtle math here: If reserved > res_used (the normal case), |
| 328 | * we're simply subtracting the unused transaction quota |
| 329 | * reservation from the dquot reservation. |
| 330 | * |
| 331 | * If, however, res_used > reserved, then we have allocated |
| 332 | * more quota blocks than were reserved for the transaction. |
| 333 | * We must add that excess to the dquot reservation since it |
| 334 | * tracks (usage + resv) and by definition we didn't reserve |
| 335 | * that excess. |
| 336 | */ |
| 337 | res->reserved -= abs(reserved - res_used); |
| 338 | } else if (count_delta != 0) { |
| 339 | /* |
| 340 | * These blks were never reserved, either inside a transaction |
| 341 | * or outside one (in a delayed allocation). Also, this isn't |
| 342 | * always a negative number since we sometimes deliberately |
| 343 | * skip quota reservations. |
| 344 | */ |
| 345 | res->reserved += count_delta; |
| 346 | } |
| 347 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
| 349 | /* |
| 350 | * Called by xfs_trans_commit() and similar in spirit to |
| 351 | * xfs_trans_apply_sb_deltas(). |
| 352 | * Go thru all the dquots belonging to this transaction and modify the |
| 353 | * INCORE dquot to reflect the actual usages. |
| 354 | * Unreserve just the reservations done by this transaction. |
| 355 | * dquot is still left locked at exit. |
| 356 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 357 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | xfs_trans_apply_dquot_deltas( |
Brian Foster | 4b6eae2e | 2013-03-18 10:51:45 -0400 | [diff] [blame] | 359 | struct xfs_trans *tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | { |
| 361 | int i, j; |
Brian Foster | 4b6eae2e | 2013-03-18 10:51:45 -0400 | [diff] [blame] | 362 | struct xfs_dquot *dqp; |
| 363 | struct xfs_dqtrx *qtrx, *qa; |
Darrick J. Wong | 903b1fc | 2019-04-17 16:30:24 -0700 | [diff] [blame] | 364 | int64_t totalbdelta; |
| 365 | int64_t totalrtbdelta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 367 | if (!(tp->t_flags & XFS_TRANS_DQ_DIRTY)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | return; |
| 369 | |
| 370 | ASSERT(tp->t_dqinfo); |
Chandra Seetharaman | 0e6436d | 2013-06-27 17:25:09 -0500 | [diff] [blame] | 371 | for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) { |
| 372 | qa = tp->t_dqinfo->dqs[j]; |
| 373 | if (qa[0].qt_dquot == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
| 376 | /* |
| 377 | * Lock all of the dquots and join them to the transaction. |
| 378 | */ |
| 379 | xfs_trans_dqlockedjoin(tp, qa); |
| 380 | |
| 381 | for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) { |
Darrick J. Wong | d92c881 | 2020-07-14 10:37:34 -0700 | [diff] [blame] | 382 | uint64_t blk_res_used; |
| 383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | qtrx = &qa[i]; |
| 385 | /* |
| 386 | * The array of dquots is filled |
| 387 | * sequentially, not sparsely. |
| 388 | */ |
| 389 | if ((dqp = qtrx->qt_dquot) == NULL) |
| 390 | break; |
| 391 | |
| 392 | ASSERT(XFS_DQ_IS_LOCKED(dqp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
| 394 | /* |
| 395 | * adjust the actual number of blocks used |
| 396 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
| 398 | /* |
| 399 | * The issue here is - sometimes we don't make a blkquota |
| 400 | * reservation intentionally to be fair to users |
| 401 | * (when the amount is small). On the other hand, |
| 402 | * delayed allocs do make reservations, but that's |
| 403 | * outside of a transaction, so we have no |
| 404 | * idea how much was really reserved. |
| 405 | * So, here we've accumulated delayed allocation blks and |
| 406 | * non-delay blks. The assumption is that the |
| 407 | * delayed ones are always reserved (outside of a |
| 408 | * transaction), and the others may or may not have |
| 409 | * quota reservations. |
| 410 | */ |
| 411 | totalbdelta = qtrx->qt_bcount_delta + |
| 412 | qtrx->qt_delbcnt_delta; |
| 413 | totalrtbdelta = qtrx->qt_rtbcount_delta + |
| 414 | qtrx->qt_delrtb_delta; |
Darrick J. Wong | 2cb91ba | 2020-07-14 10:37:35 -0700 | [diff] [blame] | 415 | |
| 416 | if (totalbdelta != 0 || totalrtbdelta != 0 || |
| 417 | qtrx->qt_icount_delta != 0) { |
| 418 | trace_xfs_trans_apply_dquot_deltas_before(dqp); |
| 419 | trace_xfs_trans_apply_dquot_deltas(qtrx); |
| 420 | } |
| 421 | |
Christoph Hellwig | ea15ab3 | 2011-07-13 13:43:50 +0200 | [diff] [blame] | 422 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | if (totalbdelta < 0) |
Darrick J. Wong | be37d40 | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 424 | ASSERT(dqp->q_blk.count >= -totalbdelta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
| 426 | if (totalrtbdelta < 0) |
Darrick J. Wong | be37d40 | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 427 | ASSERT(dqp->q_rtb.count >= -totalrtbdelta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
| 429 | if (qtrx->qt_icount_delta < 0) |
Darrick J. Wong | be37d40 | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 430 | ASSERT(dqp->q_ino.count >= -qtrx->qt_icount_delta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | #endif |
| 432 | if (totalbdelta) |
Darrick J. Wong | be37d40 | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 433 | dqp->q_blk.count += totalbdelta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
| 435 | if (qtrx->qt_icount_delta) |
Darrick J. Wong | be37d40 | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 436 | dqp->q_ino.count += qtrx->qt_icount_delta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
| 438 | if (totalrtbdelta) |
Darrick J. Wong | be37d40 | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 439 | dqp->q_rtb.count += totalrtbdelta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
Darrick J. Wong | 2cb91ba | 2020-07-14 10:37:35 -0700 | [diff] [blame] | 441 | if (totalbdelta != 0 || totalrtbdelta != 0 || |
| 442 | qtrx->qt_icount_delta != 0) |
| 443 | trace_xfs_trans_apply_dquot_deltas_after(dqp); |
| 444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | /* |
| 446 | * Get any default limits in use. |
| 447 | * Start/reset the timer(s) if needed. |
| 448 | */ |
Darrick J. Wong | c51df73 | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 449 | if (dqp->q_id) { |
Darrick J. Wong | c8c753e | 2020-07-14 10:37:33 -0700 | [diff] [blame] | 450 | xfs_qm_adjust_dqlimits(dqp); |
| 451 | xfs_qm_adjust_dqtimers(dqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Darrick J. Wong | 985a78f | 2020-07-14 10:37:13 -0700 | [diff] [blame] | 454 | dqp->q_flags |= XFS_DQFLAG_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | /* |
| 456 | * add this to the list of items to get logged |
| 457 | */ |
| 458 | xfs_trans_log_dquot(tp, dqp); |
| 459 | /* |
| 460 | * Take off what's left of the original reservation. |
| 461 | * In case of delayed allocations, there's no |
| 462 | * reservation that a transaction structure knows of. |
| 463 | */ |
Darrick J. Wong | d92c881 | 2020-07-14 10:37:34 -0700 | [diff] [blame] | 464 | blk_res_used = max_t(int64_t, 0, qtrx->qt_bcount_delta); |
| 465 | xfs_apply_quota_reservation_deltas(&dqp->q_blk, |
| 466 | qtrx->qt_blk_res, blk_res_used, |
| 467 | qtrx->qt_bcount_delta); |
Brian Foster | 7f884dc | 2015-06-01 07:15:37 +1000 | [diff] [blame] | 468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | /* |
| 470 | * Adjust the RT reservation. |
| 471 | */ |
Darrick J. Wong | d92c881 | 2020-07-14 10:37:34 -0700 | [diff] [blame] | 472 | xfs_apply_quota_reservation_deltas(&dqp->q_rtb, |
| 473 | qtrx->qt_rtblk_res, |
| 474 | qtrx->qt_rtblk_res_used, |
| 475 | qtrx->qt_rtbcount_delta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
| 477 | /* |
| 478 | * Adjust the inode reservation. |
| 479 | */ |
Darrick J. Wong | d92c881 | 2020-07-14 10:37:34 -0700 | [diff] [blame] | 480 | ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used); |
| 481 | xfs_apply_quota_reservation_deltas(&dqp->q_ino, |
| 482 | qtrx->qt_ino_res, |
| 483 | qtrx->qt_ino_res_used, |
| 484 | qtrx->qt_icount_delta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Darrick J. Wong | be37d40 | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 486 | ASSERT(dqp->q_blk.reserved >= dqp->q_blk.count); |
| 487 | ASSERT(dqp->q_ino.reserved >= dqp->q_ino.count); |
| 488 | ASSERT(dqp->q_rtb.reserved >= dqp->q_rtb.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * Release the reservations, and adjust the dquots accordingly. |
| 495 | * This is called only when the transaction is being aborted. If by |
| 496 | * any chance we have done dquot modifications incore (ie. deltas) already, |
| 497 | * we simply throw those away, since that's the expected behavior |
| 498 | * when a transaction is curtailed without a commit. |
| 499 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 500 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | xfs_trans_unreserve_and_mod_dquots( |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 502 | struct xfs_trans *tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | { |
| 504 | int i, j; |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 505 | struct xfs_dquot *dqp; |
Darrick J. Wong | 078f4a7 | 2019-04-17 16:30:24 -0700 | [diff] [blame] | 506 | struct xfs_dqtrx *qtrx, *qa; |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 507 | bool locked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
| 509 | if (!tp->t_dqinfo || !(tp->t_flags & XFS_TRANS_DQ_DIRTY)) |
| 510 | return; |
| 511 | |
Chandra Seetharaman | 0e6436d | 2013-06-27 17:25:09 -0500 | [diff] [blame] | 512 | for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) { |
| 513 | qa = tp->t_dqinfo->dqs[j]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) { |
| 516 | qtrx = &qa[i]; |
| 517 | /* |
| 518 | * We assume that the array of dquots is filled |
| 519 | * sequentially, not sparsely. |
| 520 | */ |
| 521 | if ((dqp = qtrx->qt_dquot) == NULL) |
| 522 | break; |
| 523 | /* |
| 524 | * Unreserve the original reservation. We don't care |
| 525 | * about the number of blocks used field, or deltas. |
| 526 | * Also we don't bother to zero the fields. |
| 527 | */ |
Thiago Farina | 667a929 | 2012-11-12 21:32:59 -0200 | [diff] [blame] | 528 | locked = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | if (qtrx->qt_blk_res) { |
| 530 | xfs_dqlock(dqp); |
Thiago Farina | 667a929 | 2012-11-12 21:32:59 -0200 | [diff] [blame] | 531 | locked = true; |
Darrick J. Wong | 784e80f | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 532 | dqp->q_blk.reserved -= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | (xfs_qcnt_t)qtrx->qt_blk_res; |
| 534 | } |
| 535 | if (qtrx->qt_ino_res) { |
| 536 | if (!locked) { |
| 537 | xfs_dqlock(dqp); |
Thiago Farina | 667a929 | 2012-11-12 21:32:59 -0200 | [diff] [blame] | 538 | locked = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } |
Darrick J. Wong | 784e80f | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 540 | dqp->q_ino.reserved -= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | (xfs_qcnt_t)qtrx->qt_ino_res; |
| 542 | } |
| 543 | |
| 544 | if (qtrx->qt_rtblk_res) { |
| 545 | if (!locked) { |
| 546 | xfs_dqlock(dqp); |
Thiago Farina | 667a929 | 2012-11-12 21:32:59 -0200 | [diff] [blame] | 547 | locked = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | } |
Darrick J. Wong | 784e80f | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 549 | dqp->q_rtb.reserved -= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | (xfs_qcnt_t)qtrx->qt_rtblk_res; |
| 551 | } |
| 552 | if (locked) |
| 553 | xfs_dqunlock(dqp); |
| 554 | |
| 555 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
Christoph Hellwig | a210c1a | 2010-01-17 22:36:19 +0000 | [diff] [blame] | 559 | STATIC void |
| 560 | xfs_quota_warn( |
| 561 | struct xfs_mount *mp, |
| 562 | struct xfs_dquot *dqp, |
| 563 | int type) |
| 564 | { |
Darrick J. Wong | e6eb603 | 2020-07-15 17:50:57 -0700 | [diff] [blame] | 565 | enum quota_type qtype; |
Masatake YAMATO | ffc671f | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 566 | |
Darrick J. Wong | e6eb603 | 2020-07-15 17:50:57 -0700 | [diff] [blame] | 567 | switch (xfs_dquot_type(dqp)) { |
| 568 | case XFS_DQTYPE_PROJ: |
Masatake YAMATO | ffc671f | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 569 | qtype = PRJQUOTA; |
Darrick J. Wong | e6eb603 | 2020-07-15 17:50:57 -0700 | [diff] [blame] | 570 | break; |
| 571 | case XFS_DQTYPE_USER: |
Masatake YAMATO | ffc671f | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 572 | qtype = USRQUOTA; |
Darrick J. Wong | e6eb603 | 2020-07-15 17:50:57 -0700 | [diff] [blame] | 573 | break; |
| 574 | case XFS_DQTYPE_GROUP: |
Masatake YAMATO | ffc671f | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 575 | qtype = GRPQUOTA; |
Darrick J. Wong | e6eb603 | 2020-07-15 17:50:57 -0700 | [diff] [blame] | 576 | break; |
| 577 | default: |
| 578 | return; |
| 579 | } |
Masatake YAMATO | ffc671f | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 580 | |
Darrick J. Wong | c51df73 | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 581 | quota_send_warning(make_kqid(&init_user_ns, qtype, dqp->q_id), |
Eric W. Biederman | 431f197 | 2012-09-16 02:32:43 -0700 | [diff] [blame] | 582 | mp->m_super->s_dev, type); |
Christoph Hellwig | a210c1a | 2010-01-17 22:36:19 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | /* |
Darrick J. Wong | 292b47b | 2020-07-14 10:37:34 -0700 | [diff] [blame] | 586 | * Decide if we can make an additional reservation against a quota resource. |
| 587 | * Returns an inode QUOTA_NL_ warning code and whether or not it's fatal. |
| 588 | * |
| 589 | * Note that we assume that the numeric difference between the inode and block |
| 590 | * warning codes will always be 3 since it's userspace ABI now, and will never |
| 591 | * decrease the quota reservation, so the *BELOW messages are irrelevant. |
| 592 | */ |
| 593 | static inline int |
| 594 | xfs_dqresv_check( |
| 595 | struct xfs_dquot_res *res, |
| 596 | struct xfs_quota_limits *qlim, |
| 597 | int64_t delta, |
| 598 | bool *fatal) |
| 599 | { |
| 600 | xfs_qcnt_t hardlimit = res->hardlimit; |
| 601 | xfs_qcnt_t softlimit = res->softlimit; |
| 602 | xfs_qcnt_t total_count = res->reserved + delta; |
| 603 | |
| 604 | BUILD_BUG_ON(QUOTA_NL_BHARDWARN != QUOTA_NL_IHARDWARN + 3); |
| 605 | BUILD_BUG_ON(QUOTA_NL_BSOFTLONGWARN != QUOTA_NL_ISOFTLONGWARN + 3); |
| 606 | BUILD_BUG_ON(QUOTA_NL_BSOFTWARN != QUOTA_NL_ISOFTWARN + 3); |
| 607 | |
| 608 | *fatal = false; |
| 609 | if (delta <= 0) |
| 610 | return QUOTA_NL_NOWARN; |
| 611 | |
| 612 | if (!hardlimit) |
| 613 | hardlimit = qlim->hard; |
| 614 | if (!softlimit) |
| 615 | softlimit = qlim->soft; |
| 616 | |
| 617 | if (hardlimit && total_count > hardlimit) { |
| 618 | *fatal = true; |
| 619 | return QUOTA_NL_IHARDWARN; |
| 620 | } |
| 621 | |
| 622 | if (softlimit && total_count > softlimit) { |
| 623 | time64_t now = ktime_get_real_seconds(); |
| 624 | |
| 625 | if ((res->timer != 0 && now > res->timer) || |
| 626 | (res->warnings != 0 && res->warnings >= qlim->warn)) { |
| 627 | *fatal = true; |
| 628 | return QUOTA_NL_ISOFTLONGWARN; |
| 629 | } |
| 630 | |
Darrick J. Wong | 4b8628d | 2020-07-14 10:37:35 -0700 | [diff] [blame] | 631 | res->warnings++; |
Darrick J. Wong | 292b47b | 2020-07-14 10:37:34 -0700 | [diff] [blame] | 632 | return QUOTA_NL_ISOFTWARN; |
| 633 | } |
| 634 | |
| 635 | return QUOTA_NL_NOWARN; |
| 636 | } |
| 637 | |
| 638 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | * This reserves disk blocks and inodes against a dquot. |
| 640 | * Flags indicate if the dquot is to be locked here and also |
| 641 | * if the blk reservation is for RT or regular blocks. |
| 642 | * Sending in XFS_QMOPT_FORCE_RES flag skips the quota check. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | */ |
| 644 | STATIC int |
| 645 | xfs_trans_dqresv( |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 646 | struct xfs_trans *tp, |
| 647 | struct xfs_mount *mp, |
| 648 | struct xfs_dquot *dqp, |
| 649 | int64_t nblks, |
| 650 | long ninos, |
| 651 | uint flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | { |
Pavel Reichl | c072fbe | 2019-11-12 17:04:26 -0800 | [diff] [blame] | 653 | struct xfs_quotainfo *q = mp->m_quotainfo; |
Carlos Maiolino | be60794 | 2016-02-08 11:27:55 +1100 | [diff] [blame] | 654 | struct xfs_def_quota *defq; |
Darrick J. Wong | 292b47b | 2020-07-14 10:37:34 -0700 | [diff] [blame] | 655 | struct xfs_dquot_res *blkres; |
| 656 | struct xfs_quota_limits *qlim; |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 657 | |
| 658 | xfs_dqlock(dqp); |
| 659 | |
Eric Sandeen | ce6e7e79c | 2020-05-21 13:07:00 -0700 | [diff] [blame] | 660 | defq = xfs_get_defquota(q, xfs_dquot_type(dqp)); |
Carlos Maiolino | be60794 | 2016-02-08 11:27:55 +1100 | [diff] [blame] | 661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | if (flags & XFS_TRANS_DQ_RES_BLKS) { |
Darrick J. Wong | 292b47b | 2020-07-14 10:37:34 -0700 | [diff] [blame] | 663 | blkres = &dqp->q_blk; |
| 664 | qlim = &defq->blk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | } else { |
Darrick J. Wong | 292b47b | 2020-07-14 10:37:34 -0700 | [diff] [blame] | 666 | blkres = &dqp->q_rtb; |
| 667 | qlim = &defq->rtb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Darrick J. Wong | c51df73 | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 670 | if ((flags & XFS_QMOPT_FORCE_RES) == 0 && dqp->q_id && |
Darrick J. Wong | dbcbc7b | 2020-07-15 17:48:31 -0700 | [diff] [blame] | 671 | xfs_dquot_is_enforced(dqp)) { |
Darrick J. Wong | 292b47b | 2020-07-14 10:37:34 -0700 | [diff] [blame] | 672 | int quota_nl; |
| 673 | bool fatal; |
| 674 | |
| 675 | /* |
| 676 | * dquot is locked already. See if we'd go over the hardlimit |
| 677 | * or exceed the timelimit if we'd reserve resources. |
| 678 | */ |
| 679 | quota_nl = xfs_dqresv_check(blkres, qlim, nblks, &fatal); |
| 680 | if (quota_nl != QUOTA_NL_NOWARN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | /* |
Darrick J. Wong | 292b47b | 2020-07-14 10:37:34 -0700 | [diff] [blame] | 682 | * Quota block warning codes are 3 more than the inode |
| 683 | * codes, which we check above. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | */ |
Darrick J. Wong | 292b47b | 2020-07-14 10:37:34 -0700 | [diff] [blame] | 685 | xfs_quota_warn(mp, dqp, quota_nl + 3); |
| 686 | if (fatal) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | goto error_return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | } |
Christoph Hellwig | 4d1f88d | 2010-01-13 22:05:49 +0000 | [diff] [blame] | 689 | |
Darrick J. Wong | 292b47b | 2020-07-14 10:37:34 -0700 | [diff] [blame] | 690 | quota_nl = xfs_dqresv_check(&dqp->q_ino, &defq->ino, ninos, |
| 691 | &fatal); |
| 692 | if (quota_nl != QUOTA_NL_NOWARN) { |
| 693 | xfs_quota_warn(mp, dqp, quota_nl); |
| 694 | if (fatal) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | goto error_return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | * Change the reservation, but not the actual usage. |
Darrick J. Wong | be37d40 | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 701 | * Note that q_blk.reserved = q_blk.count + resv |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | */ |
Darrick J. Wong | 292b47b | 2020-07-14 10:37:34 -0700 | [diff] [blame] | 703 | blkres->reserved += (xfs_qcnt_t)nblks; |
| 704 | dqp->q_ino.reserved += (xfs_qcnt_t)ninos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
| 706 | /* |
| 707 | * note the reservation amt in the trans struct too, |
| 708 | * so that the transaction knows how much was reserved by |
| 709 | * it against this particular dquot. |
| 710 | * We don't do this when we are reserving for a delayed allocation, |
| 711 | * because we don't have the luxury of a transaction envelope then. |
| 712 | */ |
| 713 | if (tp) { |
| 714 | ASSERT(tp->t_dqinfo); |
| 715 | ASSERT(flags & XFS_QMOPT_RESBLK_MASK); |
| 716 | if (nblks != 0) |
| 717 | xfs_trans_mod_dquot(tp, dqp, |
| 718 | flags & XFS_QMOPT_RESBLK_MASK, |
| 719 | nblks); |
| 720 | if (ninos != 0) |
| 721 | xfs_trans_mod_dquot(tp, dqp, |
| 722 | XFS_TRANS_DQ_RES_INOS, |
| 723 | ninos); |
| 724 | } |
Darrick J. Wong | be37d40 | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 725 | ASSERT(dqp->q_blk.reserved >= dqp->q_blk.count); |
| 726 | ASSERT(dqp->q_rtb.reserved >= dqp->q_rtb.count); |
| 727 | ASSERT(dqp->q_ino.reserved >= dqp->q_ino.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
Christoph Hellwig | 4d1f88d | 2010-01-13 22:05:49 +0000 | [diff] [blame] | 729 | xfs_dqunlock(dqp); |
| 730 | return 0; |
| 731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | error_return: |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 733 | xfs_dqunlock(dqp); |
Darrick J. Wong | 00a342e | 2020-07-15 17:47:13 -0700 | [diff] [blame] | 734 | if (xfs_dquot_type(dqp) == XFS_DQTYPE_PROJ) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 735 | return -ENOSPC; |
| 736 | return -EDQUOT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | |
| 740 | /* |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 741 | * Given dquot(s), make disk block and/or inode reservations against them. |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 742 | * The fact that this does the reservation against user, group and |
| 743 | * project quotas is important, because this follows a all-or-nothing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | * approach. |
| 745 | * |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 746 | * flags = XFS_QMOPT_FORCE_RES evades limit enforcement. Used by chown. |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 747 | * XFS_QMOPT_ENOSPC returns ENOSPC not EDQUOT. Used by pquota. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | * XFS_TRANS_DQ_RES_BLKS reserves regular disk blocks |
| 749 | * XFS_TRANS_DQ_RES_RTBLKS reserves realtime disk blocks |
| 750 | * dquots are unlocked on return, if they were not locked by caller. |
| 751 | */ |
| 752 | int |
| 753 | xfs_trans_reserve_quota_bydquots( |
Chandra Seetharaman | 113a568 | 2013-06-27 17:25:07 -0500 | [diff] [blame] | 754 | struct xfs_trans *tp, |
| 755 | struct xfs_mount *mp, |
| 756 | struct xfs_dquot *udqp, |
| 757 | struct xfs_dquot *gdqp, |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 758 | struct xfs_dquot *pdqp, |
Darrick J. Wong | 903b1fc | 2019-04-17 16:30:24 -0700 | [diff] [blame] | 759 | int64_t nblks, |
Chandra Seetharaman | 113a568 | 2013-06-27 17:25:07 -0500 | [diff] [blame] | 760 | long ninos, |
| 761 | uint flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | { |
Chandra Seetharaman | 113a568 | 2013-06-27 17:25:07 -0500 | [diff] [blame] | 763 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 765 | if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp)) |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 766 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | |
| 768 | if (tp && tp->t_dqinfo == NULL) |
| 769 | xfs_trans_alloc_dqinfo(tp); |
| 770 | |
| 771 | ASSERT(flags & XFS_QMOPT_RESBLK_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
| 773 | if (udqp) { |
Eric Sandeen | dcf1ccc | 2020-05-21 13:06:59 -0700 | [diff] [blame] | 774 | error = xfs_trans_dqresv(tp, mp, udqp, nblks, ninos, flags); |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 775 | if (error) |
| 776 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | if (gdqp) { |
Eric Sandeen | dcf1ccc | 2020-05-21 13:06:59 -0700 | [diff] [blame] | 780 | error = xfs_trans_dqresv(tp, mp, gdqp, nblks, ninos, flags); |
Chandra Seetharaman | 113a568 | 2013-06-27 17:25:07 -0500 | [diff] [blame] | 781 | if (error) |
| 782 | goto unwind_usr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | } |
| 784 | |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 785 | if (pdqp) { |
| 786 | error = xfs_trans_dqresv(tp, mp, pdqp, nblks, ninos, flags); |
| 787 | if (error) |
| 788 | goto unwind_grp; |
| 789 | } |
| 790 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | /* |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 792 | * Didn't change anything critical, so, no need to log |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | */ |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 794 | return 0; |
Chandra Seetharaman | 113a568 | 2013-06-27 17:25:07 -0500 | [diff] [blame] | 795 | |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 796 | unwind_grp: |
| 797 | flags |= XFS_QMOPT_FORCE_RES; |
| 798 | if (gdqp) |
| 799 | xfs_trans_dqresv(tp, mp, gdqp, -nblks, -ninos, flags); |
Chandra Seetharaman | 113a568 | 2013-06-27 17:25:07 -0500 | [diff] [blame] | 800 | unwind_usr: |
| 801 | flags |= XFS_QMOPT_FORCE_RES; |
| 802 | if (udqp) |
| 803 | xfs_trans_dqresv(tp, mp, udqp, -nblks, -ninos, flags); |
| 804 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | |
| 808 | /* |
| 809 | * Lock the dquot and change the reservation if we can. |
| 810 | * This doesn't change the actual usage, just the reservation. |
| 811 | * The inode sent in is locked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 813 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | xfs_trans_reserve_quota_nblks( |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 815 | struct xfs_trans *tp, |
| 816 | struct xfs_inode *ip, |
Darrick J. Wong | 903b1fc | 2019-04-17 16:30:24 -0700 | [diff] [blame] | 817 | int64_t nblks, |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 818 | long ninos, |
| 819 | uint flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | { |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 821 | struct xfs_mount *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 823 | if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp)) |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 824 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
Chandra Seetharaman | 9cad19d | 2013-06-27 17:25:04 -0500 | [diff] [blame] | 826 | ASSERT(!xfs_is_quota_inode(&mp->m_sb, ip->i_ino)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 828 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Eric Sandeen | dcf1ccc | 2020-05-21 13:06:59 -0700 | [diff] [blame] | 829 | ASSERT((flags & ~(XFS_QMOPT_FORCE_RES)) == XFS_TRANS_DQ_RES_RTBLKS || |
| 830 | (flags & ~(XFS_QMOPT_FORCE_RES)) == XFS_TRANS_DQ_RES_BLKS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | |
| 832 | /* |
| 833 | * Reserve nblks against these dquots, with trans as the mediator. |
| 834 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 835 | return xfs_trans_reserve_quota_bydquots(tp, mp, |
| 836 | ip->i_udquot, ip->i_gdquot, |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 837 | ip->i_pdquot, |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 838 | nblks, ninos, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | /* |
| 842 | * This routine is called to allocate a quotaoff log item. |
| 843 | */ |
Pavel Reichl | d0bdfb1 | 2019-11-12 17:04:27 -0800 | [diff] [blame] | 844 | struct xfs_qoff_logitem * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | xfs_trans_get_qoff_item( |
Pavel Reichl | d0bdfb1 | 2019-11-12 17:04:27 -0800 | [diff] [blame] | 846 | struct xfs_trans *tp, |
| 847 | struct xfs_qoff_logitem *startqoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | uint flags) |
| 849 | { |
Pavel Reichl | d0bdfb1 | 2019-11-12 17:04:27 -0800 | [diff] [blame] | 850 | struct xfs_qoff_logitem *q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | |
| 852 | ASSERT(tp != NULL); |
| 853 | |
| 854 | q = xfs_qm_qoff_logitem_init(tp->t_mountp, startqoff, flags); |
| 855 | ASSERT(q != NULL); |
| 856 | |
| 857 | /* |
| 858 | * Get a log_item_desc to point at the new item. |
| 859 | */ |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 860 | xfs_trans_add_item(tp, &q->qql_item); |
| 861 | return q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | |
| 865 | /* |
| 866 | * This is called to mark the quotaoff logitem as needing |
| 867 | * to be logged when the transaction is committed. The logitem must |
| 868 | * already be associated with the given transaction. |
| 869 | */ |
| 870 | void |
| 871 | xfs_trans_log_quotaoff_item( |
Pavel Reichl | d0bdfb1 | 2019-11-12 17:04:27 -0800 | [diff] [blame] | 872 | struct xfs_trans *tp, |
| 873 | struct xfs_qoff_logitem *qlp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | tp->t_flags |= XFS_TRANS_DIRTY; |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame] | 876 | set_bit(XFS_LI_DIRTY, &qlp->qql_item.li_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | STATIC void |
| 880 | xfs_trans_alloc_dqinfo( |
| 881 | xfs_trans_t *tp) |
| 882 | { |
Carlos Maiolino | 32a2b11 | 2020-07-22 09:23:10 -0700 | [diff] [blame] | 883 | tp->t_dqinfo = kmem_cache_zalloc(xfs_qm_dqtrxzone, |
| 884 | GFP_KERNEL | __GFP_NOFAIL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 887 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | xfs_trans_free_dqinfo( |
| 889 | xfs_trans_t *tp) |
| 890 | { |
| 891 | if (!tp->t_dqinfo) |
| 892 | return; |
Carlos Maiolino | 377bcd5 | 2019-11-14 12:43:04 -0800 | [diff] [blame] | 893 | kmem_cache_free(xfs_qm_dqtrxzone, tp->t_dqinfo); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 894 | tp->t_dqinfo = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | } |