1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. 4 * Copyright (C) 2010 Red Hat, Inc. 5 * All Rights Reserved. 6 */ 7 #include "xfs_platform.h" 8 #include "xfs_fs.h" 9 #include "xfs_shared.h" 10 #include "xfs_format.h" 11 #include "xfs_log_format.h" 12 #include "xfs_trans_resv.h" 13 #include "xfs_mount.h" 14 #include "xfs_extent_busy.h" 15 #include "xfs_quota.h" 16 #include "xfs_trans.h" 17 #include "xfs_trans_priv.h" 18 #include "xfs_log.h" 19 #include "xfs_log_priv.h" 20 #include "xfs_trace.h" 21 #include "xfs_error.h" 22 #include "xfs_defer.h" 23 #include "xfs_inode.h" 24 #include "xfs_dquot_item.h" 25 #include "xfs_dquot.h" 26 #include "xfs_icache.h" 27 #include "xfs_rtbitmap.h" 28 #include "xfs_rtgroup.h" 29 #include "xfs_sb.h" 30 31 struct kmem_cache *xfs_trans_cache; 32 33 #if defined(CONFIG_TRACEPOINTS) 34 static void 35 xfs_trans_trace_reservations( 36 struct xfs_mount *mp) 37 { 38 struct xfs_trans_res *res; 39 struct xfs_trans_res *end_res; 40 int i; 41 42 res = (struct xfs_trans_res *)M_RES(mp); 43 end_res = (struct xfs_trans_res *)(M_RES(mp) + 1); 44 for (i = 0; res < end_res; i++, res++) 45 trace_xfs_trans_resv_calc(mp, i, res); 46 } 47 #else 48 # define xfs_trans_trace_reservations(mp) 49 #endif 50 51 /* 52 * Initialize the precomputed transaction reservation values 53 * in the mount structure. 54 */ 55 void 56 xfs_trans_init( 57 struct xfs_mount *mp) 58 { 59 xfs_trans_resv_calc(mp, M_RES(mp)); 60 xfs_trans_trace_reservations(mp); 61 } 62 63 /* 64 * Free the transaction structure. If there is more clean up 65 * to do when the structure is freed, add it here. 66 */ 67 STATIC void 68 xfs_trans_free( 69 struct xfs_trans *tp) 70 { 71 xfs_extent_busy_sort(&tp->t_busy); 72 xfs_extent_busy_clear(&tp->t_busy, false); 73 74 trace_xfs_trans_free(tp, _RET_IP_); 75 memalloc_nofs_restore(tp->t_pflags); 76 if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT)) 77 sb_end_intwrite(tp->t_mountp->m_super); 78 xfs_trans_free_dqinfo(tp); 79 kmem_cache_free(xfs_trans_cache, tp); 80 } 81 82 /* 83 * This is called to create a new transaction which will share the 84 * permanent log reservation of the given transaction. The remaining 85 * unused block and rt extent reservations are also inherited. This 86 * implies that the original transaction is no longer allowed to allocate 87 * blocks. Locks and log items, however, are no inherited. They must 88 * be added to the new transaction explicitly. 89 */ 90 STATIC struct xfs_trans * 91 xfs_trans_dup( 92 struct xfs_trans *tp) 93 { 94 struct xfs_trans *ntp; 95 96 trace_xfs_trans_dup(tp, _RET_IP_); 97 98 ntp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL); 99 100 /* 101 * Initialize the new transaction structure. 102 */ 103 ntp->t_mountp = tp->t_mountp; 104 INIT_LIST_HEAD(&ntp->t_items); 105 INIT_LIST_HEAD(&ntp->t_busy); 106 INIT_LIST_HEAD(&ntp->t_dfops); 107 ntp->t_highest_agno = NULLAGNUMBER; 108 109 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); 110 ASSERT(tp->t_ticket != NULL); 111 112 ntp->t_flags = XFS_TRANS_PERM_LOG_RES | 113 (tp->t_flags & XFS_TRANS_RESERVE) | 114 (tp->t_flags & XFS_TRANS_NO_WRITECOUNT) | 115 (tp->t_flags & XFS_TRANS_RES_FDBLKS); 116 /* We gave our writer reference to the new transaction */ 117 tp->t_flags |= XFS_TRANS_NO_WRITECOUNT; 118 ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket); 119 120 ASSERT(tp->t_blk_res >= tp->t_blk_res_used); 121 ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used; 122 tp->t_blk_res = tp->t_blk_res_used; 123 124 ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used; 125 tp->t_rtx_res = tp->t_rtx_res_used; 126 127 /* move deferred ops over to the new tp */ 128 xfs_defer_move(ntp, tp); 129 130 xfs_trans_dup_dqinfo(tp, ntp); 131 return ntp; 132 } 133 134 /* 135 * This is called to reserve free disk blocks and log space for the given 136 * transaction before allocating any resources within the transaction. 137 * 138 * This will return ENOSPC if there are not enough blocks available. 139 * It will sleep waiting for available log space. 140 * 141 * This does not do quota reservations. That typically is done by the caller 142 * afterwards. 143 */ 144 static int 145 xfs_trans_reserve( 146 struct xfs_trans *tp, 147 struct xfs_trans_res *resp, 148 uint blocks, 149 uint rtextents) 150 { 151 struct xfs_mount *mp = tp->t_mountp; 152 int error = 0; 153 bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0; 154 155 ASSERT(resp->tr_logres > 0); 156 157 /* 158 * Attempt to reserve the needed disk blocks by decrementing the number 159 * needed from the number available. This will fail if the count would 160 * go below zero. 161 */ 162 if (blocks > 0) { 163 error = xfs_dec_fdblocks(mp, blocks, rsvd); 164 if (error != 0) 165 return -ENOSPC; 166 tp->t_blk_res += blocks; 167 } 168 169 /* 170 * Reserve the log space needed for this transaction. 171 */ 172 if (resp->tr_logflags & XFS_TRANS_PERM_LOG_RES) 173 tp->t_flags |= XFS_TRANS_PERM_LOG_RES; 174 error = xfs_log_reserve(mp, resp->tr_logres, resp->tr_logcount, 175 &tp->t_ticket, (tp->t_flags & XFS_TRANS_PERM_LOG_RES)); 176 if (error) 177 goto undo_blocks; 178 179 tp->t_log_res = resp->tr_logres; 180 tp->t_log_count = resp->tr_logcount; 181 182 /* 183 * Attempt to reserve the needed realtime extents by decrementing the 184 * number needed from the number available. This will fail if the 185 * count would go below zero. 186 */ 187 if (rtextents > 0) { 188 error = xfs_dec_frextents(mp, rtextents); 189 if (error) { 190 error = -ENOSPC; 191 goto undo_log; 192 } 193 tp->t_rtx_res += rtextents; 194 } 195 196 return 0; 197 198 undo_log: 199 xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket); 200 tp->t_ticket = NULL; 201 tp->t_log_res = 0; 202 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES; 203 undo_blocks: 204 if (blocks > 0) { 205 xfs_add_fdblocks(mp, blocks); 206 tp->t_blk_res = 0; 207 } 208 return error; 209 } 210 211 static struct xfs_trans * 212 __xfs_trans_alloc( 213 struct xfs_mount *mp, 214 uint flags) 215 { 216 struct xfs_trans *tp; 217 218 ASSERT(!(flags & XFS_TRANS_RES_FDBLKS) || xfs_has_lazysbcount(mp)); 219 220 tp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL); 221 if (!(flags & XFS_TRANS_NO_WRITECOUNT)) 222 sb_start_intwrite(mp->m_super); 223 /* 224 * Save NOFS state for xfs_trans_free() to restore. Only the 225 * transaction owner thread may touch tp->t_pflags. 226 */ 227 tp->t_pflags = memalloc_nofs_save(); 228 tp->t_flags = flags; 229 tp->t_mountp = mp; 230 INIT_LIST_HEAD(&tp->t_items); 231 INIT_LIST_HEAD(&tp->t_busy); 232 INIT_LIST_HEAD(&tp->t_dfops); 233 tp->t_highest_agno = NULLAGNUMBER; 234 return tp; 235 } 236 237 int 238 xfs_trans_alloc( 239 struct xfs_mount *mp, 240 struct xfs_trans_res *resp, 241 uint blocks, 242 uint rtextents, 243 uint flags, 244 struct xfs_trans **tpp) 245 { 246 struct xfs_trans *tp; 247 bool want_retry = true; 248 int error; 249 250 ASSERT(resp->tr_logres > 0); 251 252 /* 253 * Allocate the handle before we do our freeze accounting and setting up 254 * GFP_NOFS allocation context so that we avoid lockdep false positives 255 * by doing GFP_KERNEL allocations inside sb_start_intwrite(). 256 */ 257 retry: 258 tp = __xfs_trans_alloc(mp, flags); 259 WARN_ON(mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE); 260 error = xfs_trans_reserve(tp, resp, blocks, rtextents); 261 if (error == -ENOSPC && want_retry) { 262 xfs_trans_cancel(tp); 263 264 /* 265 * We weren't able to reserve enough space for the transaction. 266 * Flush the other speculative space allocations to free space. 267 * Do not perform a synchronous scan because callers can hold 268 * other locks. 269 */ 270 error = xfs_blockgc_flush_all(mp); 271 if (error) 272 return error; 273 want_retry = false; 274 goto retry; 275 } 276 if (error) { 277 xfs_trans_cancel(tp); 278 return error; 279 } 280 281 trace_xfs_trans_alloc(tp, _RET_IP_); 282 283 *tpp = tp; 284 return 0; 285 } 286 287 /* 288 * Create an empty transaction with no reservation. This is a defensive 289 * mechanism for routines that query metadata without actually modifying them -- 290 * if the metadata being queried is somehow cross-linked (think a btree block 291 * pointer that points higher in the tree), we risk deadlock. However, blocks 292 * grabbed as part of a transaction can be re-grabbed. The verifiers will 293 * notice the corrupt block and the operation will fail back to userspace 294 * without deadlocking. 295 * 296 * Note the zero-length reservation; this transaction MUST be cancelled without 297 * any dirty data. 298 * 299 * Callers should obtain freeze protection to avoid a conflict with fs freezing 300 * where we can be grabbing buffers at the same time that freeze is trying to 301 * drain the buffer LRU list. 302 */ 303 struct xfs_trans * 304 xfs_trans_alloc_empty( 305 struct xfs_mount *mp) 306 { 307 return __xfs_trans_alloc(mp, XFS_TRANS_NO_WRITECOUNT); 308 } 309 310 /* 311 * Record the indicated change to the given field for application 312 * to the file system's superblock when the transaction commits. 313 * For now, just store the change in the transaction structure. 314 * 315 * Mark the transaction structure to indicate that the superblock 316 * needs to be updated before committing. 317 * 318 * Because we may not be keeping track of allocated/free inodes and 319 * used filesystem blocks in the superblock, we do not mark the 320 * superblock dirty in this transaction if we modify these fields. 321 * We still need to update the transaction deltas so that they get 322 * applied to the incore superblock, but we don't want them to 323 * cause the superblock to get locked and logged if these are the 324 * only fields in the superblock that the transaction modifies. 325 */ 326 void 327 xfs_trans_mod_sb( 328 xfs_trans_t *tp, 329 uint field, 330 int64_t delta) 331 { 332 uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY); 333 xfs_mount_t *mp = tp->t_mountp; 334 335 switch (field) { 336 case XFS_TRANS_SB_ICOUNT: 337 tp->t_icount_delta += delta; 338 if (xfs_has_lazysbcount(mp)) 339 flags &= ~XFS_TRANS_SB_DIRTY; 340 break; 341 case XFS_TRANS_SB_IFREE: 342 tp->t_ifree_delta += delta; 343 if (xfs_has_lazysbcount(mp)) 344 flags &= ~XFS_TRANS_SB_DIRTY; 345 break; 346 case XFS_TRANS_SB_FDBLOCKS: 347 /* 348 * Track the number of blocks allocated in the transaction. 349 * Make sure it does not exceed the number reserved. If so, 350 * shutdown as this can lead to accounting inconsistency. 351 */ 352 if (delta < 0) { 353 tp->t_blk_res_used += (uint)-delta; 354 if (tp->t_blk_res_used > tp->t_blk_res) 355 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 356 } else if (delta > 0 && (tp->t_flags & XFS_TRANS_RES_FDBLKS)) { 357 int64_t blkres_delta; 358 359 /* 360 * Return freed blocks directly to the reservation 361 * instead of the global pool, being careful not to 362 * overflow the trans counter. This is used to preserve 363 * reservation across chains of transaction rolls that 364 * repeatedly free and allocate blocks. 365 */ 366 blkres_delta = min_t(int64_t, delta, 367 UINT_MAX - tp->t_blk_res); 368 tp->t_blk_res += blkres_delta; 369 delta -= blkres_delta; 370 } 371 tp->t_fdblocks_delta += delta; 372 if (xfs_has_lazysbcount(mp)) 373 flags &= ~XFS_TRANS_SB_DIRTY; 374 break; 375 case XFS_TRANS_SB_RES_FDBLOCKS: 376 /* 377 * The allocation has already been applied to the 378 * in-core superblock's counter. This should only 379 * be applied to the on-disk superblock. 380 */ 381 tp->t_res_fdblocks_delta += delta; 382 if (xfs_has_lazysbcount(mp)) 383 flags &= ~XFS_TRANS_SB_DIRTY; 384 break; 385 case XFS_TRANS_SB_FREXTENTS: 386 /* 387 * Track the number of blocks allocated in the 388 * transaction. Make sure it does not exceed the 389 * number reserved. 390 */ 391 if (delta < 0) { 392 tp->t_rtx_res_used += (uint)-delta; 393 ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res); 394 } 395 tp->t_frextents_delta += delta; 396 if (xfs_has_rtgroups(mp)) 397 flags &= ~XFS_TRANS_SB_DIRTY; 398 break; 399 case XFS_TRANS_SB_RES_FREXTENTS: 400 /* 401 * The allocation has already been applied to the 402 * in-core superblock's counter. This should only 403 * be applied to the on-disk superblock. 404 */ 405 ASSERT(delta < 0); 406 tp->t_res_frextents_delta += delta; 407 if (xfs_has_rtgroups(mp)) 408 flags &= ~XFS_TRANS_SB_DIRTY; 409 break; 410 case XFS_TRANS_SB_DBLOCKS: 411 tp->t_dblocks_delta += delta; 412 break; 413 case XFS_TRANS_SB_AGCOUNT: 414 ASSERT(delta > 0); 415 tp->t_agcount_delta += delta; 416 break; 417 case XFS_TRANS_SB_IMAXPCT: 418 tp->t_imaxpct_delta += delta; 419 break; 420 case XFS_TRANS_SB_REXTSIZE: 421 tp->t_rextsize_delta += delta; 422 break; 423 case XFS_TRANS_SB_RBMBLOCKS: 424 tp->t_rbmblocks_delta += delta; 425 break; 426 case XFS_TRANS_SB_RBLOCKS: 427 tp->t_rblocks_delta += delta; 428 break; 429 case XFS_TRANS_SB_REXTENTS: 430 tp->t_rextents_delta += delta; 431 break; 432 case XFS_TRANS_SB_REXTSLOG: 433 tp->t_rextslog_delta += delta; 434 break; 435 case XFS_TRANS_SB_RGCOUNT: 436 ASSERT(delta > 0); 437 tp->t_rgcount_delta += delta; 438 break; 439 default: 440 ASSERT(0); 441 return; 442 } 443 444 tp->t_flags |= flags; 445 } 446 447 /* 448 * xfs_trans_apply_sb_deltas() is called from the commit code 449 * to bring the superblock buffer into the current transaction 450 * and modify it as requested by earlier calls to xfs_trans_mod_sb(). 451 * 452 * For now we just look at each field allowed to change and change 453 * it if necessary. 454 */ 455 STATIC void 456 xfs_trans_apply_sb_deltas( 457 struct xfs_trans *tp) 458 { 459 struct xfs_mount *mp = tp->t_mountp; 460 struct xfs_buf *bp = xfs_trans_getsb(tp); 461 struct xfs_dsb *sbp = bp->b_addr; 462 int whole = 0; 463 464 /* 465 * Only update the superblock counters if we are logging them 466 */ 467 if (!xfs_has_lazysbcount(mp)) { 468 if (tp->t_icount_delta) 469 be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta); 470 if (tp->t_ifree_delta) 471 be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta); 472 if (tp->t_fdblocks_delta) 473 be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta); 474 if (tp->t_res_fdblocks_delta) 475 be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta); 476 } 477 478 /* 479 * sb_frextents was added to the lazy sb counters when the rt groups 480 * feature was introduced. This is possible because we know that all 481 * kernels supporting rtgroups will also recompute frextents from the 482 * realtime bitmap. 483 * 484 * For older file systems, updating frextents requires careful handling 485 * because we cannot rely on log recovery in older kernels to recompute 486 * the value from the rtbitmap. This means that the ondisk frextents 487 * must be consistent with the rtbitmap. 488 * 489 * Therefore, log the frextents change to the ondisk superblock and 490 * update the incore superblock so that future calls to xfs_log_sb 491 * write the correct value ondisk. 492 */ 493 if ((tp->t_frextents_delta || tp->t_res_frextents_delta) && 494 !xfs_has_rtgroups(mp)) { 495 int64_t rtxdelta; 496 497 rtxdelta = tp->t_frextents_delta + tp->t_res_frextents_delta; 498 499 spin_lock(&mp->m_sb_lock); 500 be64_add_cpu(&sbp->sb_frextents, rtxdelta); 501 mp->m_sb.sb_frextents += rtxdelta; 502 spin_unlock(&mp->m_sb_lock); 503 } 504 505 if (tp->t_dblocks_delta) { 506 be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta); 507 mp->m_ddev_targp->bt_nr_sectors += 508 XFS_FSB_TO_BB(mp, tp->t_dblocks_delta); 509 whole = 1; 510 } 511 if (tp->t_agcount_delta) { 512 be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta); 513 whole = 1; 514 } 515 if (tp->t_imaxpct_delta) { 516 sbp->sb_imax_pct += tp->t_imaxpct_delta; 517 whole = 1; 518 } 519 if (tp->t_rextsize_delta) { 520 be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta); 521 522 /* 523 * Because the ondisk sb records rtgroup size in units of rt 524 * extents, any time we update the rt extent size we have to 525 * recompute the ondisk rtgroup block log. The incore values 526 * will be recomputed in xfs_trans_unreserve_and_mod_sb. 527 */ 528 if (xfs_has_rtgroups(mp)) { 529 sbp->sb_rgblklog = xfs_compute_rgblklog( 530 be32_to_cpu(sbp->sb_rgextents), 531 be32_to_cpu(sbp->sb_rextsize)); 532 } 533 whole = 1; 534 } 535 if (tp->t_rbmblocks_delta) { 536 be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta); 537 whole = 1; 538 } 539 if (tp->t_rblocks_delta) { 540 be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta); 541 mp->m_rtdev_targp->bt_nr_sectors += 542 XFS_FSB_TO_BB(mp, tp->t_rblocks_delta); 543 whole = 1; 544 } 545 if (tp->t_rextents_delta) { 546 be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta); 547 whole = 1; 548 } 549 if (tp->t_rextslog_delta) { 550 sbp->sb_rextslog += tp->t_rextslog_delta; 551 whole = 1; 552 } 553 if (tp->t_rgcount_delta) { 554 be32_add_cpu(&sbp->sb_rgcount, tp->t_rgcount_delta); 555 whole = 1; 556 } 557 558 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF); 559 if (whole) 560 /* 561 * Log the whole thing, the fields are noncontiguous. 562 */ 563 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1); 564 else 565 /* 566 * Since all the modifiable fields are contiguous, we 567 * can get away with this. 568 */ 569 xfs_trans_log_buf(tp, bp, offsetof(struct xfs_dsb, sb_icount), 570 offsetof(struct xfs_dsb, sb_frextents) + 571 sizeof(sbp->sb_frextents) - 1); 572 } 573 574 /* 575 * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations and 576 * apply superblock counter changes to the in-core superblock. The 577 * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT 578 * applied to the in-core superblock. The idea is that that has already been 579 * done. 580 * 581 * If we are not logging superblock counters, then the inode allocated/free and 582 * used block counts are not updated in the on disk superblock. In this case, 583 * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we 584 * still need to update the incore superblock with the changes. 585 * 586 * Deltas for the inode count are +/-64, hence we use a large batch size of 128 587 * so we don't need to take the counter lock on every update. 588 */ 589 #define XFS_ICOUNT_BATCH 128 590 591 void 592 xfs_trans_unreserve_and_mod_sb( 593 struct xfs_trans *tp) 594 { 595 struct xfs_mount *mp = tp->t_mountp; 596 int64_t blkdelta = tp->t_blk_res; 597 int64_t rtxdelta = tp->t_rtx_res; 598 int64_t idelta = 0; 599 int64_t ifreedelta = 0; 600 601 /* 602 * Calculate the deltas. 603 * 604 * t_fdblocks_delta and t_frextents_delta can be positive or negative: 605 * 606 * - positive values indicate blocks freed in the transaction. 607 * - negative values indicate blocks allocated in the transaction 608 * 609 * Negative values can only happen if the transaction has a block 610 * reservation that covers the allocated block. The end result is 611 * that the calculated delta values must always be positive and we 612 * can only put back previous allocated or reserved blocks here. 613 */ 614 ASSERT(tp->t_blk_res || tp->t_fdblocks_delta >= 0); 615 if (xfs_has_lazysbcount(mp) || (tp->t_flags & XFS_TRANS_SB_DIRTY)) { 616 blkdelta += tp->t_fdblocks_delta; 617 ASSERT(blkdelta >= 0); 618 } 619 620 ASSERT(tp->t_rtx_res || tp->t_frextents_delta >= 0); 621 if (xfs_has_rtgroups(mp) || (tp->t_flags & XFS_TRANS_SB_DIRTY)) { 622 rtxdelta += tp->t_frextents_delta; 623 ASSERT(rtxdelta >= 0); 624 } 625 626 if (xfs_has_lazysbcount(mp) || (tp->t_flags & XFS_TRANS_SB_DIRTY)) { 627 idelta = tp->t_icount_delta; 628 ifreedelta = tp->t_ifree_delta; 629 } 630 631 /* apply the per-cpu counters */ 632 if (blkdelta) 633 xfs_add_fdblocks(mp, blkdelta); 634 635 if (idelta) 636 percpu_counter_add_batch(&mp->m_icount, idelta, 637 XFS_ICOUNT_BATCH); 638 639 if (ifreedelta) 640 percpu_counter_add(&mp->m_ifree, ifreedelta); 641 642 if (rtxdelta) 643 xfs_add_frextents(mp, rtxdelta); 644 645 if (!(tp->t_flags & XFS_TRANS_SB_DIRTY)) 646 return; 647 648 /* apply remaining deltas */ 649 spin_lock(&mp->m_sb_lock); 650 mp->m_sb.sb_fdblocks += tp->t_fdblocks_delta + tp->t_res_fdblocks_delta; 651 mp->m_sb.sb_icount += idelta; 652 mp->m_sb.sb_ifree += ifreedelta; 653 /* 654 * Do not touch sb_frextents here because it is handled in 655 * xfs_trans_apply_sb_deltas for file systems where it isn't a lazy 656 * counter anyway. 657 */ 658 mp->m_sb.sb_dblocks += tp->t_dblocks_delta; 659 mp->m_sb.sb_agcount += tp->t_agcount_delta; 660 mp->m_sb.sb_imax_pct += tp->t_imaxpct_delta; 661 if (tp->t_rextsize_delta) 662 xfs_mount_sb_set_rextsize(mp, &mp->m_sb, 663 mp->m_sb.sb_rextsize + tp->t_rextsize_delta); 664 mp->m_sb.sb_rbmblocks += tp->t_rbmblocks_delta; 665 mp->m_sb.sb_rblocks += tp->t_rblocks_delta; 666 mp->m_sb.sb_rextents += tp->t_rextents_delta; 667 mp->m_sb.sb_rextslog += tp->t_rextslog_delta; 668 mp->m_sb.sb_rgcount += tp->t_rgcount_delta; 669 spin_unlock(&mp->m_sb_lock); 670 671 /* 672 * Debug checks outside of the spinlock so they don't lock up the 673 * machine if they fail. 674 */ 675 ASSERT(mp->m_sb.sb_imax_pct >= 0); 676 ASSERT(mp->m_sb.sb_rextslog >= 0); 677 } 678 679 /* Add the given log item to the transaction's list of log items. */ 680 void 681 xfs_trans_add_item( 682 struct xfs_trans *tp, 683 struct xfs_log_item *lip) 684 { 685 ASSERT(lip->li_log == tp->t_mountp->m_log); 686 ASSERT(lip->li_ailp == tp->t_mountp->m_ail); 687 ASSERT(list_empty(&lip->li_trans)); 688 ASSERT(!test_bit(XFS_LI_DIRTY, &lip->li_flags)); 689 690 list_add_tail(&lip->li_trans, &tp->t_items); 691 trace_xfs_trans_add_item(tp, _RET_IP_); 692 } 693 694 /* 695 * Unlink the log item from the transaction. the log item is no longer 696 * considered dirty in this transaction, as the linked transaction has 697 * finished, either by abort or commit completion. 698 */ 699 void 700 xfs_trans_del_item( 701 struct xfs_log_item *lip) 702 { 703 clear_bit(XFS_LI_DIRTY, &lip->li_flags); 704 list_del_init(&lip->li_trans); 705 } 706 707 /* Detach and unlock all of the items in a transaction */ 708 static void 709 xfs_trans_free_items( 710 struct xfs_trans *tp, 711 bool abort) 712 { 713 struct xfs_log_item *lip, *next; 714 715 trace_xfs_trans_free_items(tp, _RET_IP_); 716 717 list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) { 718 xfs_trans_del_item(lip); 719 if (abort) { 720 trace_xfs_trans_free_abort(lip); 721 set_bit(XFS_LI_ABORTED, &lip->li_flags); 722 } 723 if (lip->li_ops->iop_release) 724 lip->li_ops->iop_release(lip); 725 } 726 } 727 728 /* 729 * Sort transaction items prior to running precommit operations. This will 730 * attempt to order the items such that they will always be locked in the same 731 * order. Items that have no sort function are moved to the end of the list 732 * and so are locked last. 733 * 734 * This may need refinement as different types of objects add sort functions. 735 * 736 * Function is more complex than it needs to be because we are comparing 64 bit 737 * values and the function only returns 32 bit values. 738 */ 739 static int 740 xfs_trans_precommit_sort( 741 void *unused_arg, 742 const struct list_head *a, 743 const struct list_head *b) 744 { 745 struct xfs_log_item *lia = container_of(a, 746 struct xfs_log_item, li_trans); 747 struct xfs_log_item *lib = container_of(b, 748 struct xfs_log_item, li_trans); 749 int64_t diff; 750 751 /* 752 * If both items are non-sortable, leave them alone. If only one is 753 * sortable, move the non-sortable item towards the end of the list. 754 */ 755 if (!lia->li_ops->iop_sort && !lib->li_ops->iop_sort) 756 return 0; 757 if (!lia->li_ops->iop_sort) 758 return 1; 759 if (!lib->li_ops->iop_sort) 760 return -1; 761 762 diff = lia->li_ops->iop_sort(lia) - lib->li_ops->iop_sort(lib); 763 if (diff < 0) 764 return -1; 765 if (diff > 0) 766 return 1; 767 return 0; 768 } 769 770 /* 771 * Run transaction precommit functions. 772 * 773 * If there is an error in any of the callouts, then stop immediately and 774 * trigger a shutdown to abort the transaction. There is no recovery possible 775 * from errors at this point as the transaction is dirty.... 776 */ 777 static int 778 xfs_trans_run_precommits( 779 struct xfs_trans *tp) 780 { 781 struct xfs_mount *mp = tp->t_mountp; 782 struct xfs_log_item *lip, *n; 783 int error = 0; 784 785 /* 786 * Sort the item list to avoid ABBA deadlocks with other transactions 787 * running precommit operations that lock multiple shared items such as 788 * inode cluster buffers. 789 */ 790 list_sort(NULL, &tp->t_items, xfs_trans_precommit_sort); 791 792 /* 793 * Precommit operations can remove the log item from the transaction 794 * if the log item exists purely to delay modifications until they 795 * can be ordered against other operations. Hence we have to use 796 * list_for_each_entry_safe() here. 797 */ 798 list_for_each_entry_safe(lip, n, &tp->t_items, li_trans) { 799 if (!test_bit(XFS_LI_DIRTY, &lip->li_flags)) 800 continue; 801 if (lip->li_ops->iop_precommit) { 802 error = lip->li_ops->iop_precommit(tp, lip); 803 if (error) 804 break; 805 } 806 } 807 if (error) 808 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 809 return error; 810 } 811 812 /* 813 * Commit the given transaction to the log. 814 * 815 * XFS disk error handling mechanism is not based on a typical 816 * transaction abort mechanism. Logically after the filesystem 817 * gets marked 'SHUTDOWN', we can't let any new transactions 818 * be durable - ie. committed to disk - because some metadata might 819 * be inconsistent. In such cases, this returns an error, and the 820 * caller may assume that all locked objects joined to the transaction 821 * have already been unlocked as if the commit had succeeded. 822 * Do not reference the transaction structure after this call. 823 */ 824 static int 825 __xfs_trans_commit( 826 struct xfs_trans *tp, 827 bool regrant) 828 { 829 struct xfs_mount *mp = tp->t_mountp; 830 struct xlog *log = mp->m_log; 831 xfs_csn_t commit_seq = 0; 832 int error = 0; 833 int sync = tp->t_flags & XFS_TRANS_SYNC; 834 835 trace_xfs_trans_commit(tp, _RET_IP_); 836 837 /* 838 * Commit per-transaction changes that are not already tracked through 839 * log items. This can add dirty log items to the transaction. 840 */ 841 if (tp->t_flags & XFS_TRANS_SB_DIRTY) 842 xfs_trans_apply_sb_deltas(tp); 843 xfs_trans_apply_dquot_deltas(tp); 844 845 error = xfs_trans_run_precommits(tp); 846 if (error) 847 goto out_unreserve; 848 849 /* 850 * If there is nothing to be logged by the transaction, 851 * then unlock all of the items associated with the 852 * transaction and free the transaction structure. 853 * Also make sure to return any reserved blocks to 854 * the free pool. 855 */ 856 if (!(tp->t_flags & XFS_TRANS_DIRTY)) 857 goto out_unreserve; 858 859 /* 860 * We must check against log shutdown here because we cannot abort log 861 * items and leave them dirty, inconsistent and unpinned in memory while 862 * the log is active. This leaves them open to being written back to 863 * disk, and that will lead to on-disk corruption. 864 */ 865 if (xlog_is_shutdown(log)) { 866 error = -EIO; 867 goto out_unreserve; 868 } 869 870 ASSERT(tp->t_ticket != NULL); 871 872 xlog_cil_commit(log, tp, &commit_seq, regrant); 873 874 xfs_trans_free(tp); 875 876 /* 877 * If the transaction needs to be synchronous, then force the 878 * log out now and wait for it. 879 */ 880 if (sync) { 881 error = xfs_log_force_seq(mp, commit_seq, XFS_LOG_SYNC, NULL); 882 XFS_STATS_INC(mp, xs_trans_sync); 883 } else { 884 XFS_STATS_INC(mp, xs_trans_async); 885 } 886 887 return error; 888 889 out_unreserve: 890 xfs_trans_unreserve_and_mod_sb(tp); 891 892 /* 893 * It is indeed possible for the transaction to be not dirty but 894 * the dqinfo portion to be. All that means is that we have some 895 * (non-persistent) quota reservations that need to be unreserved. 896 */ 897 xfs_trans_unreserve_and_mod_dquots(tp, true); 898 if (tp->t_ticket) { 899 if (regrant && !xlog_is_shutdown(log)) 900 xfs_log_ticket_regrant(log, tp->t_ticket); 901 else 902 xfs_log_ticket_ungrant(log, tp->t_ticket); 903 tp->t_ticket = NULL; 904 } 905 xfs_trans_free_items(tp, !!error); 906 xfs_trans_free(tp); 907 908 XFS_STATS_INC(mp, xs_trans_empty); 909 return error; 910 } 911 912 int 913 xfs_trans_commit( 914 struct xfs_trans *tp) 915 { 916 /* 917 * Finish deferred items on final commit. Only permanent transactions 918 * should ever have deferred ops. 919 */ 920 WARN_ON_ONCE(!list_empty(&tp->t_dfops) && 921 !(tp->t_flags & XFS_TRANS_PERM_LOG_RES)); 922 if (tp->t_flags & XFS_TRANS_PERM_LOG_RES) { 923 int error = xfs_defer_finish_noroll(&tp); 924 if (error) { 925 xfs_trans_cancel(tp); 926 return error; 927 } 928 } 929 930 return __xfs_trans_commit(tp, false); 931 } 932 933 /* 934 * Unlock all of the transaction's items and free the transaction. If the 935 * transaction is dirty, we must shut down the filesystem because there is no 936 * way to restore them to their previous state. 937 * 938 * If the transaction has made a log reservation, make sure to release it as 939 * well. 940 * 941 * This is a high level function (equivalent to xfs_trans_commit()) and so can 942 * be called after the transaction has effectively been aborted due to the mount 943 * being shut down. However, if the mount has not been shut down and the 944 * transaction is dirty we will shut the mount down and, in doing so, that 945 * guarantees that the log is shut down, too. Hence we don't need to be as 946 * careful with shutdown state and dirty items here as we need to be in 947 * xfs_trans_commit(). 948 */ 949 void 950 xfs_trans_cancel_error( 951 struct xfs_trans *tp, 952 int error) 953 { 954 struct xfs_mount *mp = tp->t_mountp; 955 struct xlog *log = mp->m_log; 956 bool dirty = (tp->t_flags & XFS_TRANS_DIRTY); 957 958 trace_xfs_trans_cancel(tp, _RET_IP_); 959 960 /* 961 * It's never valid to cancel a transaction with deferred ops attached, 962 * because the transaction is effectively dirty. Complain about this 963 * loudly before freeing the in-memory defer items and shutting down the 964 * filesystem. 965 */ 966 if (!list_empty(&tp->t_dfops)) { 967 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); 968 dirty = true; 969 xfs_defer_cancel(tp); 970 } 971 972 /* 973 * See if the caller is relying on us to shut down the filesystem. We 974 * only want an error report if there isn't already a shutdown in 975 * progress, so we only need to check against the mount shutdown state 976 * here. 977 */ 978 if (dirty && !xfs_is_shutdown(mp)) { 979 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, 980 error, mp); 981 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 982 } 983 #ifdef DEBUG 984 /* Log items need to be consistent until the log is shut down. */ 985 if (!dirty && !xlog_is_shutdown(log)) { 986 struct xfs_log_item *lip; 987 988 list_for_each_entry(lip, &tp->t_items, li_trans) 989 ASSERT(!xlog_item_is_intent_done(lip)); 990 } 991 #endif 992 xfs_trans_unreserve_and_mod_sb(tp); 993 xfs_trans_unreserve_and_mod_dquots(tp, false); 994 995 if (tp->t_ticket) { 996 xfs_log_ticket_ungrant(log, tp->t_ticket); 997 tp->t_ticket = NULL; 998 } 999 1000 xfs_trans_free_items(tp, dirty); 1001 xfs_trans_free(tp); 1002 } 1003 1004 /* 1005 * Roll from one trans in the sequence of PERMANENT transactions to the next: 1006 * permanent transactions are only flushed out when committed with 1007 * xfs_trans_commit(), but we still want as soon as possible to let chunks of it 1008 * go to the log. So we commit the chunk we've been working on and get a new 1009 * transaction to continue. 1010 */ 1011 int 1012 xfs_trans_roll( 1013 struct xfs_trans **tpp) 1014 { 1015 struct xfs_trans *tp = *tpp; 1016 unsigned int log_res = tp->t_log_res; 1017 unsigned int log_count = tp->t_log_count; 1018 int error; 1019 1020 trace_xfs_trans_roll(tp, _RET_IP_); 1021 1022 ASSERT(log_res > 0); 1023 1024 /* 1025 * Copy the critical parameters from one trans to the next. 1026 */ 1027 *tpp = xfs_trans_dup(tp); 1028 1029 /* 1030 * Commit the current transaction. 1031 * 1032 * If this commit failed, then it'd just unlock those items that are not 1033 * marked ihold. That also means that a filesystem shutdown is in 1034 * progress. The caller takes the responsibility to cancel the 1035 * duplicate transaction that gets returned. 1036 */ 1037 error = __xfs_trans_commit(tp, true); 1038 1039 tp = *tpp; 1040 /* 1041 * __xfs_trans_commit cleared the NOFS flag by calling into 1042 * xfs_trans_free. Set it again here before doing memory 1043 * allocations. 1044 */ 1045 tp->t_pflags = memalloc_nofs_save(); 1046 1047 if (error) 1048 return error; 1049 1050 /* 1051 * Reserve space in the log for the next transaction. 1052 * 1053 * This also pushes items in the AIL out to disk if they are taking up 1054 * space at the tail of the log that we want to use. This requires that 1055 * either nothing be locked across this call, or that anything that is 1056 * locked be logged in the prior and the next transactions. 1057 */ 1058 error = xfs_log_regrant(tp->t_mountp, tp->t_ticket); 1059 if (error) 1060 return error; 1061 tp->t_log_res = log_res; 1062 tp->t_log_count = log_count; 1063 return 0; 1064 } 1065 1066 /* 1067 * Allocate an transaction, lock and join the inode to it, and reserve quota. 1068 * 1069 * The caller must ensure that the on-disk dquots attached to this inode have 1070 * already been allocated and initialized. The caller is responsible for 1071 * releasing ILOCK_EXCL if a new transaction is returned. 1072 */ 1073 int 1074 xfs_trans_alloc_inode( 1075 struct xfs_inode *ip, 1076 struct xfs_trans_res *resv, 1077 unsigned int dblocks, 1078 unsigned int rblocks, 1079 bool force, 1080 struct xfs_trans **tpp) 1081 { 1082 struct xfs_trans *tp; 1083 struct xfs_mount *mp = ip->i_mount; 1084 bool retried = false; 1085 int error; 1086 1087 retry: 1088 error = xfs_trans_alloc(mp, resv, dblocks, 1089 xfs_extlen_to_rtxlen(mp, rblocks), 1090 force ? XFS_TRANS_RESERVE : 0, &tp); 1091 if (error) 1092 return error; 1093 1094 xfs_ilock(ip, XFS_ILOCK_EXCL); 1095 xfs_trans_ijoin(tp, ip, 0); 1096 1097 error = xfs_qm_dqattach_locked(ip, false); 1098 if (error) { 1099 /* Caller should have allocated the dquots! */ 1100 ASSERT(error != -ENOENT); 1101 goto out_cancel; 1102 } 1103 1104 error = xfs_trans_reserve_quota_nblks(tp, ip, dblocks, rblocks, force); 1105 if ((error == -EDQUOT || error == -ENOSPC) && !retried) { 1106 xfs_trans_cancel(tp); 1107 xfs_iunlock(ip, XFS_ILOCK_EXCL); 1108 xfs_blockgc_free_quota(ip, 0); 1109 retried = true; 1110 goto retry; 1111 } 1112 if (error) 1113 goto out_cancel; 1114 1115 *tpp = tp; 1116 return 0; 1117 1118 out_cancel: 1119 xfs_trans_cancel(tp); 1120 xfs_iunlock(ip, XFS_ILOCK_EXCL); 1121 return error; 1122 } 1123 1124 /* 1125 * Try to reserve more blocks for a transaction. 1126 * 1127 * This is for callers that need to attach resources to a transaction, scan 1128 * those resources to determine the space reservation requirements, and then 1129 * modify the attached resources. In other words, online repair. This can 1130 * fail due to ENOSPC, so the caller must be able to cancel the transaction 1131 * without shutting down the fs. 1132 */ 1133 int 1134 xfs_trans_reserve_more( 1135 struct xfs_trans *tp, 1136 unsigned int blocks, 1137 unsigned int rtextents) 1138 { 1139 bool rsvd = tp->t_flags & XFS_TRANS_RESERVE; 1140 1141 if (blocks && xfs_dec_fdblocks(tp->t_mountp, blocks, rsvd)) 1142 return -ENOSPC; 1143 if (rtextents && xfs_dec_frextents(tp->t_mountp, rtextents)) { 1144 if (blocks) 1145 xfs_add_fdblocks(tp->t_mountp, blocks); 1146 return -ENOSPC; 1147 } 1148 tp->t_blk_res += blocks; 1149 tp->t_rtx_res += rtextents; 1150 return 0; 1151 } 1152 1153 /* 1154 * Try to reserve more blocks and file quota for a transaction. Same 1155 * conditions of usage as xfs_trans_reserve_more. 1156 */ 1157 int 1158 xfs_trans_reserve_more_inode( 1159 struct xfs_trans *tp, 1160 struct xfs_inode *ip, 1161 unsigned int dblocks, 1162 unsigned int rblocks, 1163 bool force_quota) 1164 { 1165 struct xfs_mount *mp = ip->i_mount; 1166 unsigned int rtx = xfs_extlen_to_rtxlen(mp, rblocks); 1167 int error; 1168 1169 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); 1170 1171 error = xfs_trans_reserve_more(tp, dblocks, rtx); 1172 if (error) 1173 return error; 1174 1175 if (!XFS_IS_QUOTA_ON(mp) || xfs_is_quota_inode(&mp->m_sb, I_INO(ip))) 1176 return 0; 1177 1178 if (tp->t_flags & XFS_TRANS_RESERVE) 1179 force_quota = true; 1180 1181 error = xfs_trans_reserve_quota_nblks(tp, ip, dblocks, rblocks, 1182 force_quota); 1183 if (!error) 1184 return 0; 1185 1186 /* Quota failed, give back the new reservation. */ 1187 xfs_add_fdblocks(mp, dblocks); 1188 tp->t_blk_res -= dblocks; 1189 xfs_add_frextents(mp, rtx); 1190 tp->t_rtx_res -= rtx; 1191 return error; 1192 } 1193 1194 /* 1195 * Allocate an transaction in preparation for inode creation by reserving quota 1196 * against the given dquots. Callers are not required to hold any inode locks. 1197 */ 1198 int 1199 xfs_trans_alloc_icreate( 1200 struct xfs_mount *mp, 1201 struct xfs_trans_res *resv, 1202 struct xfs_dquot *udqp, 1203 struct xfs_dquot *gdqp, 1204 struct xfs_dquot *pdqp, 1205 unsigned int dblocks, 1206 struct xfs_trans **tpp) 1207 { 1208 struct xfs_trans *tp; 1209 bool retried = false; 1210 bool flushed = false; 1211 int error; 1212 1213 retry: 1214 error = xfs_trans_alloc(mp, resv, dblocks, 0, 0, &tp); 1215 if (error == -ENOSPC && !flushed) { 1216 /* 1217 * Flush all delalloc blocks to reclaim space from speculative 1218 * preallocation. This is similar to the quota retry below 1219 * but targets FS-wide ENOSPC. 1220 */ 1221 xfs_flush_inodes(mp); 1222 flushed = true; 1223 goto retry; 1224 } 1225 if (error) 1226 return error; 1227 1228 error = xfs_trans_reserve_quota_icreate(tp, udqp, gdqp, pdqp, dblocks); 1229 if ((error == -EDQUOT || error == -ENOSPC) && !retried) { 1230 xfs_trans_cancel(tp); 1231 xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0); 1232 retried = true; 1233 goto retry; 1234 } 1235 if (error) { 1236 xfs_trans_cancel(tp); 1237 return error; 1238 } 1239 1240 *tpp = tp; 1241 return 0; 1242 } 1243 1244 /* 1245 * Allocate an transaction, lock and join the inode to it, and reserve quota 1246 * in preparation for inode attribute changes that include uid, gid, or prid 1247 * changes. 1248 * 1249 * The caller must ensure that the on-disk dquots attached to this inode have 1250 * already been allocated and initialized. The ILOCK will be dropped when the 1251 * transaction is committed or cancelled. 1252 */ 1253 int 1254 xfs_trans_alloc_ichange( 1255 struct xfs_inode *ip, 1256 struct xfs_dquot *new_udqp, 1257 struct xfs_dquot *new_gdqp, 1258 struct xfs_dquot *new_pdqp, 1259 bool force, 1260 struct xfs_trans **tpp) 1261 { 1262 struct xfs_trans *tp; 1263 struct xfs_mount *mp = ip->i_mount; 1264 struct xfs_dquot *udqp; 1265 struct xfs_dquot *gdqp; 1266 struct xfs_dquot *pdqp; 1267 bool retried = false; 1268 int error; 1269 1270 retry: 1271 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp); 1272 if (error) 1273 return error; 1274 1275 xfs_ilock(ip, XFS_ILOCK_EXCL); 1276 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); 1277 1278 if (xfs_is_metadir_inode(ip)) 1279 goto out; 1280 1281 error = xfs_qm_dqattach_locked(ip, false); 1282 if (error) { 1283 /* Caller should have allocated the dquots! */ 1284 ASSERT(error != -ENOENT); 1285 goto out_cancel; 1286 } 1287 1288 /* 1289 * For each quota type, skip quota reservations if the inode's dquots 1290 * now match the ones that came from the caller, or the caller didn't 1291 * pass one in. The inode's dquots can change if we drop the ILOCK to 1292 * perform a blockgc scan, so we must preserve the caller's arguments. 1293 */ 1294 udqp = (new_udqp != ip->i_udquot) ? new_udqp : NULL; 1295 gdqp = (new_gdqp != ip->i_gdquot) ? new_gdqp : NULL; 1296 pdqp = (new_pdqp != ip->i_pdquot) ? new_pdqp : NULL; 1297 if (udqp || gdqp || pdqp) { 1298 xfs_filblks_t dblocks, rblocks; 1299 unsigned int qflags = XFS_QMOPT_RES_REGBLKS; 1300 bool isrt = XFS_IS_REALTIME_INODE(ip); 1301 1302 if (force) 1303 qflags |= XFS_QMOPT_FORCE_RES; 1304 1305 if (isrt) { 1306 error = xfs_iread_extents(tp, ip, XFS_DATA_FORK); 1307 if (error) 1308 goto out_cancel; 1309 } 1310 1311 xfs_inode_count_blocks(tp, ip, &dblocks, &rblocks); 1312 1313 if (isrt) 1314 rblocks += ip->i_delayed_blks; 1315 else 1316 dblocks += ip->i_delayed_blks; 1317 1318 /* 1319 * Reserve enough quota to handle blocks on disk and reserved 1320 * for a delayed allocation. We'll actually transfer the 1321 * delalloc reservation between dquots at chown time, even 1322 * though that part is only semi-transactional. 1323 */ 1324 error = xfs_trans_reserve_quota_bydquots(tp, mp, udqp, gdqp, 1325 pdqp, dblocks, 1, qflags); 1326 if ((error == -EDQUOT || error == -ENOSPC) && !retried) { 1327 xfs_trans_cancel(tp); 1328 xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0); 1329 retried = true; 1330 goto retry; 1331 } 1332 if (error) 1333 goto out_cancel; 1334 1335 /* Do the same for realtime. */ 1336 qflags = XFS_QMOPT_RES_RTBLKS | (qflags & XFS_QMOPT_FORCE_RES); 1337 error = xfs_trans_reserve_quota_bydquots(tp, mp, udqp, gdqp, 1338 pdqp, rblocks, 0, qflags); 1339 if ((error == -EDQUOT || error == -ENOSPC) && !retried) { 1340 xfs_trans_cancel(tp); 1341 xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0); 1342 retried = true; 1343 goto retry; 1344 } 1345 if (error) 1346 goto out_cancel; 1347 } 1348 1349 out: 1350 *tpp = tp; 1351 return 0; 1352 1353 out_cancel: 1354 xfs_trans_cancel(tp); 1355 return error; 1356 } 1357 1358 /* 1359 * Allocate an transaction, lock and join the directory and child inodes to it, 1360 * and reserve quota for a directory update. If there isn't sufficient space, 1361 * @dblocks will be set to zero for a reservationless directory update and 1362 * @nospace_error will be set to a negative errno describing the space 1363 * constraint we hit. 1364 * 1365 * The caller must ensure that the on-disk dquots attached to this inode have 1366 * already been allocated and initialized. The ILOCKs will be dropped when the 1367 * transaction is committed or cancelled. 1368 * 1369 * Caller is responsible for unlocking the inodes manually upon return 1370 */ 1371 int 1372 xfs_trans_alloc_dir( 1373 struct xfs_inode *dp, 1374 struct xfs_trans_res *resv, 1375 struct xfs_inode *ip, 1376 unsigned int *dblocks, 1377 struct xfs_trans **tpp, 1378 int *nospace_error) 1379 { 1380 struct xfs_trans *tp; 1381 struct xfs_mount *mp = ip->i_mount; 1382 unsigned int resblks; 1383 bool retried = false; 1384 int error; 1385 1386 retry: 1387 *nospace_error = 0; 1388 resblks = *dblocks; 1389 error = xfs_trans_alloc(mp, resv, resblks, 0, 0, &tp); 1390 if (error == -ENOSPC) { 1391 *nospace_error = error; 1392 resblks = 0; 1393 error = xfs_trans_alloc(mp, resv, resblks, 0, 0, &tp); 1394 } 1395 if (error) 1396 return error; 1397 1398 xfs_lock_two_inodes(dp, XFS_ILOCK_EXCL, ip, XFS_ILOCK_EXCL); 1399 1400 xfs_trans_ijoin(tp, dp, 0); 1401 xfs_trans_ijoin(tp, ip, 0); 1402 1403 error = xfs_qm_dqattach_locked(dp, false); 1404 if (error) { 1405 /* Caller should have allocated the dquots! */ 1406 ASSERT(error != -ENOENT); 1407 goto out_cancel; 1408 } 1409 1410 error = xfs_qm_dqattach_locked(ip, false); 1411 if (error) { 1412 /* Caller should have allocated the dquots! */ 1413 ASSERT(error != -ENOENT); 1414 goto out_cancel; 1415 } 1416 1417 if (resblks == 0) 1418 goto done; 1419 1420 error = xfs_trans_reserve_quota_nblks(tp, dp, resblks, 0, false); 1421 if (error == -EDQUOT || error == -ENOSPC) { 1422 if (!retried) { 1423 xfs_trans_cancel(tp); 1424 xfs_iunlock(dp, XFS_ILOCK_EXCL); 1425 if (dp != ip) 1426 xfs_iunlock(ip, XFS_ILOCK_EXCL); 1427 xfs_blockgc_free_quota(dp, 0); 1428 retried = true; 1429 goto retry; 1430 } 1431 1432 *nospace_error = error; 1433 resblks = 0; 1434 error = 0; 1435 } 1436 if (error) 1437 goto out_cancel; 1438 1439 done: 1440 *tpp = tp; 1441 *dblocks = resblks; 1442 return 0; 1443 1444 out_cancel: 1445 xfs_trans_cancel(tp); 1446 xfs_iunlock(dp, XFS_ILOCK_EXCL); 1447 if (dp != ip) 1448 xfs_iunlock(ip, XFS_ILOCK_EXCL); 1449 return error; 1450 } 1451