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