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