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