LCOV - code coverage report
Current view: top level - fs/xfs - xfs_trans.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-acha @ Mon Jul 31 20:08:06 PDT 2023 Lines: 595 650 91.5 %
Date: 2023-07-31 20:08:07 Functions: 27 27 100.0 %

          Line data    Source code
       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.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             : 
      28             : struct kmem_cache       *xfs_trans_cache;
      29             : 
      30             : #if defined(CONFIG_TRACEPOINTS)
      31             : static void
      32       24115 : xfs_trans_trace_reservations(
      33             :         struct xfs_mount        *mp)
      34             : {
      35       24115 :         struct xfs_trans_res    *res;
      36       24115 :         struct xfs_trans_res    *end_res;
      37       24115 :         int                     i;
      38             : 
      39       24115 :         res = (struct xfs_trans_res *)M_RES(mp);
      40       24115 :         end_res = (struct xfs_trans_res *)(M_RES(mp) + 1);
      41      651105 :         for (i = 0; res < end_res; i++, res++)
      42      626990 :                 trace_xfs_trans_resv_calc(mp, i, res);
      43       24115 : }
      44             : #else
      45             : # define xfs_trans_trace_reservations(mp)
      46             : #endif
      47             : 
      48             : /*
      49             :  * Initialize the precomputed transaction reservation values
      50             :  * in the mount structure.
      51             :  */
      52             : void
      53       24115 : xfs_trans_init(
      54             :         struct xfs_mount        *mp)
      55             : {
      56       24115 :         xfs_trans_resv_calc(mp, M_RES(mp));
      57       24115 :         xfs_trans_trace_reservations(mp);
      58       24115 : }
      59             : 
      60             : /*
      61             :  * Free the transaction structure.  If there is more clean up
      62             :  * to do when the structure is freed, add it here.
      63             :  */
      64             : STATIC void
      65  2728076155 : xfs_trans_free(
      66             :         struct xfs_trans        *tp)
      67             : {
      68  2728076155 :         xfs_extent_busy_sort(&tp->t_busy);
      69  2728295075 :         xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
      70             : 
      71  2728465302 :         if (tp->t_dfops_finished > 0)
      72   403202521 :                 trace_xfs_defer_stats(tp);
      73             : 
      74  2728484793 :         trace_xfs_trans_free(tp, _RET_IP_);
      75  2728356734 :         xfs_trans_clear_context(tp);
      76  2728423870 :         if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
      77   903689419 :                 sb_end_intwrite(tp->t_mountp->m_super);
      78  2728416904 :         xfs_trans_free_dqinfo(tp);
      79  2728299128 :         kmem_cache_free(xfs_trans_cache, tp);
      80  2728550989 : }
      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   705334093 : xfs_trans_dup(
      92             :         struct xfs_trans        *tp)
      93             : {
      94   705334093 :         struct xfs_trans        *ntp;
      95             : 
      96   705334093 :         trace_xfs_trans_dup(tp, _RET_IP_);
      97             : 
      98   705366203 :         ntp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL);
      99             : 
     100             :         /*
     101             :          * Initialize the new transaction structure.
     102             :          */
     103   705468119 :         ntp->t_magic = XFS_TRANS_HEADER_MAGIC;
     104   705468119 :         ntp->t_mountp = tp->t_mountp;
     105   705468119 :         INIT_LIST_HEAD(&ntp->t_items);
     106   705468119 :         INIT_LIST_HEAD(&ntp->t_busy);
     107   705468119 :         INIT_LIST_HEAD(&ntp->t_dfops);
     108   705468119 :         ntp->t_highest_agno = NULLAGNUMBER;
     109             : 
     110   705468119 :         ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
     111   705468119 :         ASSERT(tp->t_ticket != NULL);
     112             : 
     113   705468119 :         ntp->t_flags = XFS_TRANS_PERM_LOG_RES |
     114   705468119 :                        (tp->t_flags & XFS_TRANS_RESERVE) |
     115   705468119 :                        (tp->t_flags & XFS_TRANS_NO_WRITECOUNT) |
     116             :                        (tp->t_flags & XFS_TRANS_RES_FDBLKS);
     117             :         /* We gave our writer reference to the new transaction */
     118   705468119 :         tp->t_flags |= XFS_TRANS_NO_WRITECOUNT;
     119   705468119 :         ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
     120             : 
     121   705392748 :         ASSERT(tp->t_blk_res >= tp->t_blk_res_used);
     122   705392748 :         ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
     123   705392748 :         tp->t_blk_res = tp->t_blk_res_used;
     124             : 
     125   705392748 :         ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
     126   705392748 :         tp->t_rtx_res = tp->t_rtx_res_used;
     127             : 
     128   705392748 :         xfs_trans_switch_context(tp, ntp);
     129             : 
     130             :         /* move deferred ops over to the new tp */
     131   705393515 :         xfs_defer_move(ntp, tp);
     132             : 
     133   705409053 :         xfs_trans_dup_dqinfo(tp, ntp);
     134   705447872 :         return ntp;
     135             : }
     136             : 
     137             : /*
     138             :  * Try to reserve more blocks for a transaction.
     139             :  *
     140             :  * This is for callers that need to attach resources to a transaction, scan
     141             :  * those resources to determine the space reservation requirements, and then
     142             :  * modify the attached resources.  In other words, online repair.  This can
     143             :  * fail due to ENOSPC, so the caller must be able to cancel the transaction
     144             :  * without shutting down the fs.
     145             :  */
     146             : int
     147    91367394 : xfs_trans_reserve_more(
     148             :         struct xfs_trans        *tp,
     149             :         unsigned int            blocks,
     150             :         unsigned int            rtextents)
     151             : {
     152    91367394 :         struct xfs_mount        *mp = tp->t_mountp;
     153    91367394 :         bool                    rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
     154    91367394 :         int                     error = 0;
     155             : 
     156    91367394 :         ASSERT(!(tp->t_flags & XFS_TRANS_DIRTY));
     157             : 
     158             :         /*
     159             :          * Attempt to reserve the needed disk blocks by decrementing
     160             :          * the number needed from the number available.  This will
     161             :          * fail if the count would go below zero.
     162             :          */
     163    91367394 :         if (blocks > 0) {
     164    91367394 :                 error = xfs_mod_fdblocks(mp, -((int64_t)blocks), rsvd);
     165    91367394 :                 if (error)
     166             :                         return -ENOSPC;
     167    91367394 :                 tp->t_blk_res += blocks;
     168             :         }
     169             : 
     170             :         /*
     171             :          * Attempt to reserve the needed realtime extents by decrementing
     172             :          * the number needed from the number available.  This will
     173             :          * fail if the count would go below zero.
     174             :          */
     175    91367394 :         if (rtextents > 0) {
     176           0 :                 error = xfs_mod_frextents(mp, -((int64_t)rtextents));
     177           0 :                 if (error) {
     178           0 :                         error = -ENOSPC;
     179           0 :                         goto out_blocks;
     180             :                 }
     181           0 :                 tp->t_rtx_res += rtextents;
     182             :         }
     183             : 
     184             :         return 0;
     185             : out_blocks:
     186           0 :         if (blocks > 0) {
     187           0 :                 xfs_mod_fdblocks(mp, (int64_t)blocks, rsvd);
     188           0 :                 tp->t_blk_res -= blocks;
     189             :         }
     190             :         return error;
     191             : }
     192             : 
     193             : /*
     194             :  * This is called to reserve free disk blocks and log space for the
     195             :  * given transaction.  This must be done before allocating any resources
     196             :  * within the transaction.
     197             :  *
     198             :  * This will return ENOSPC if there are not enough blocks available.
     199             :  * It will sleep waiting for available log space.
     200             :  * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
     201             :  * is used by long running transactions.  If any one of the reservations
     202             :  * fails then they will all be backed out.
     203             :  *
     204             :  * This does not do quota reservations. That typically is done by the
     205             :  * caller afterwards.
     206             :  */
     207             : static int
     208  2728385428 : xfs_trans_reserve(
     209             :         struct xfs_trans        *tp,
     210             :         struct xfs_trans_res    *resp,
     211             :         uint                    blocks,
     212             :         uint                    rtextents)
     213             : {
     214  2728385428 :         struct xfs_mount        *mp = tp->t_mountp;
     215  2728385428 :         int                     error = 0;
     216  2728385428 :         bool                    rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
     217             : 
     218             :         /*
     219             :          * Attempt to reserve the needed disk blocks by decrementing
     220             :          * the number needed from the number available.  This will
     221             :          * fail if the count would go below zero.
     222             :          */
     223  2728385428 :         if (blocks > 0) {
     224   546905981 :                 error = xfs_mod_fdblocks(mp, -((int64_t)blocks), rsvd);
     225   546907605 :                 if (error != 0)
     226             :                         return -ENOSPC;
     227   539817040 :                 tp->t_blk_res += blocks;
     228             :         }
     229             : 
     230             :         /*
     231             :          * Reserve the log space needed for this transaction.
     232             :          */
     233  2721296487 :         if (resp->tr_logres > 0) {
     234  1601966667 :                 bool    permanent = false;
     235             : 
     236  1601966667 :                 ASSERT(tp->t_log_res == 0 ||
     237             :                        tp->t_log_res == resp->tr_logres);
     238  1601966667 :                 ASSERT(tp->t_log_count == 0 ||
     239             :                        tp->t_log_count == resp->tr_logcount);
     240             : 
     241  1601966667 :                 if (resp->tr_logflags & XFS_TRANS_PERM_LOG_RES) {
     242  1500875950 :                         tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
     243  1500875950 :                         permanent = true;
     244             :                 } else {
     245   101090717 :                         ASSERT(tp->t_ticket == NULL);
     246   101090717 :                         ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
     247             :                 }
     248             : 
     249  1601966667 :                 if (tp->t_ticket != NULL) {
     250   705358506 :                         ASSERT(resp->tr_logflags & XFS_TRANS_PERM_LOG_RES);
     251   705358506 :                         error = xfs_log_regrant(mp, tp->t_ticket);
     252             :                 } else {
     253   896608161 :                         error = xfs_log_reserve(mp, resp->tr_logres,
     254             :                                                 resp->tr_logcount,
     255             :                                                 &tp->t_ticket, permanent);
     256             :                 }
     257             : 
     258  1602228894 :                 if (error)
     259        2160 :                         goto undo_blocks;
     260             : 
     261  1602226734 :                 tp->t_log_res = resp->tr_logres;
     262  1602226734 :                 tp->t_log_count = resp->tr_logcount;
     263             :         }
     264             : 
     265             :         /*
     266             :          * Attempt to reserve the needed realtime extents by decrementing
     267             :          * the number needed from the number available.  This will
     268             :          * fail if the count would go below zero.
     269             :          */
     270  2721556554 :         if (rtextents > 0) {
     271     8854345 :                 error = xfs_mod_frextents(mp, -((int64_t)rtextents));
     272     8854323 :                 if (error) {
     273           0 :                         error = -ENOSPC;
     274           0 :                         goto undo_log;
     275             :                 }
     276     8854323 :                 tp->t_rtx_res += rtextents;
     277             :         }
     278             : 
     279             :         return 0;
     280             : 
     281             :         /*
     282             :          * Error cases jump to one of these labels to undo any
     283             :          * reservations which have already been performed.
     284             :          */
     285             : undo_log:
     286           0 :         if (resp->tr_logres > 0) {
     287           0 :                 xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket);
     288           0 :                 tp->t_ticket = NULL;
     289           0 :                 tp->t_log_res = 0;
     290           0 :                 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
     291             :         }
     292             : 
     293           0 : undo_blocks:
     294        2160 :         if (blocks > 0) {
     295         329 :                 xfs_mod_fdblocks(mp, (int64_t)blocks, rsvd);
     296         329 :                 tp->t_blk_res = 0;
     297             :         }
     298             :         return error;
     299             : }
     300             : 
     301             : int
     302  2019053767 : xfs_trans_alloc(
     303             :         struct xfs_mount        *mp,
     304             :         struct xfs_trans_res    *resp,
     305             :         uint                    blocks,
     306             :         uint                    rtextents,
     307             :         uint                    flags,
     308             :         struct xfs_trans        **tpp)
     309             : {
     310  2019053767 :         struct xfs_trans        *tp;
     311  2019053767 :         bool                    want_retry = true;
     312  2022610447 :         int                     error;
     313             : 
     314             :         /*
     315             :          * Allocate the handle before we do our freeze accounting and setting up
     316             :          * GFP_NOFS allocation context so that we avoid lockdep false positives
     317             :          * by doing GFP_KERNEL allocations inside sb_start_intwrite().
     318             :          */
     319  2022610447 : retry:
     320  2022610447 :         tp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL);
     321  2022772858 :         if (!(flags & XFS_TRANS_NO_WRITECOUNT))
     322   903538793 :                 sb_start_intwrite(mp->m_super);
     323  2022695836 :         xfs_trans_set_context(tp);
     324             : 
     325             :         /*
     326             :          * Zero-reservation ("empty") transactions can't modify anything, so
     327             :          * they're allowed to run while we're frozen.
     328             :          */
     329  4045510346 :         WARN_ON(resp->tr_logres > 0 &&
     330             :                 mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
     331  2022755173 :         ASSERT(!(flags & XFS_TRANS_RES_FDBLKS) ||
     332             :                xfs_has_lazysbcount(mp));
     333             : 
     334  2022755173 :         tp->t_magic = XFS_TRANS_HEADER_MAGIC;
     335  2022755173 :         tp->t_flags = flags;
     336  2022755173 :         tp->t_mountp = mp;
     337  2022755173 :         INIT_LIST_HEAD(&tp->t_items);
     338  2022755173 :         INIT_LIST_HEAD(&tp->t_busy);
     339  2022755173 :         INIT_LIST_HEAD(&tp->t_dfops);
     340  2022755173 :         tp->t_highest_agno = NULLAGNUMBER;
     341             : 
     342  2022755173 :         error = xfs_trans_reserve(tp, resp, blocks, rtextents);
     343  2022919303 :         if (error == -ENOSPC && want_retry) {
     344     3556417 :                 xfs_trans_cancel(tp);
     345             : 
     346             :                 /*
     347             :                  * We weren't able to reserve enough space for the transaction.
     348             :                  * Flush the other speculative space allocations to free space.
     349             :                  * Do not perform a synchronous scan because callers can hold
     350             :                  * other locks.
     351             :                  */
     352     3556005 :                 error = xfs_blockgc_flush_all(mp);
     353     3556680 :                 if (error)
     354           0 :                         return error;
     355     3556680 :                 want_retry = false;
     356     3556680 :                 goto retry;
     357             :         }
     358  2019362886 :         if (error) {
     359     3536222 :                 xfs_trans_cancel(tp);
     360     3536222 :                 return error;
     361             :         }
     362             : 
     363  2015826664 :         trace_xfs_trans_alloc(tp, _RET_IP_);
     364             : 
     365  2015836375 :         *tpp = tp;
     366  2015836375 :         return 0;
     367             : }
     368             : 
     369             : /*
     370             :  * Create an empty transaction with no reservation.  This is a defensive
     371             :  * mechanism for routines that query metadata without actually modifying them --
     372             :  * if the metadata being queried is somehow cross-linked (think a btree block
     373             :  * pointer that points higher in the tree), we risk deadlock.  However, blocks
     374             :  * grabbed as part of a transaction can be re-grabbed.  The verifiers will
     375             :  * notice the corrupt block and the operation will fail back to userspace
     376             :  * without deadlocking.
     377             :  *
     378             :  * Note the zero-length reservation; this transaction MUST be cancelled without
     379             :  * any dirty data.
     380             :  *
     381             :  * Callers should obtain freeze protection to avoid a conflict with fs freezing
     382             :  * where we can be grabbing buffers at the same time that freeze is trying to
     383             :  * drain the buffer LRU list.
     384             :  */
     385             : int
     386  1119223763 : xfs_trans_alloc_empty(
     387             :         struct xfs_mount                *mp,
     388             :         struct xfs_trans                **tpp)
     389             : {
     390  1119223763 :         struct xfs_trans_res            resv = {0};
     391             : 
     392  1119223763 :         return xfs_trans_alloc(mp, &resv, 0, 0, XFS_TRANS_NO_WRITECOUNT, tpp);
     393             : }
     394             : 
     395             : /*
     396             :  * Record the indicated change to the given field for application
     397             :  * to the file system's superblock when the transaction commits.
     398             :  * For now, just store the change in the transaction structure.
     399             :  *
     400             :  * Mark the transaction structure to indicate that the superblock
     401             :  * needs to be updated before committing.
     402             :  *
     403             :  * Because we may not be keeping track of allocated/free inodes and
     404             :  * used filesystem blocks in the superblock, we do not mark the
     405             :  * superblock dirty in this transaction if we modify these fields.
     406             :  * We still need to update the transaction deltas so that they get
     407             :  * applied to the incore superblock, but we don't want them to
     408             :  * cause the superblock to get locked and logged if these are the
     409             :  * only fields in the superblock that the transaction modifies.
     410             :  */
     411             : void
     412   238574495 : xfs_trans_mod_sb(
     413             :         xfs_trans_t     *tp,
     414             :         uint            field,
     415             :         int64_t         delta)
     416             : {
     417   238574495 :         uint32_t        flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
     418   238574495 :         xfs_mount_t     *mp = tp->t_mountp;
     419             : 
     420   238574495 :         switch (field) {
     421      627771 :         case XFS_TRANS_SB_ICOUNT:
     422      627771 :                 tp->t_icount_delta += delta;
     423      627771 :                 if (xfs_has_lazysbcount(mp))
     424      627764 :                         flags &= ~XFS_TRANS_SB_DIRTY;
     425             :                 break;
     426   117532421 :         case XFS_TRANS_SB_IFREE:
     427   117532421 :                 tp->t_ifree_delta += delta;
     428   117532421 :                 if (xfs_has_lazysbcount(mp))
     429   117535667 :                         flags &= ~XFS_TRANS_SB_DIRTY;
     430             :                 break;
     431    92802374 :         case XFS_TRANS_SB_FDBLOCKS:
     432             :                 /*
     433             :                  * Track the number of blocks allocated in the transaction.
     434             :                  * Make sure it does not exceed the number reserved. If so,
     435             :                  * shutdown as this can lead to accounting inconsistency.
     436             :                  */
     437    92802374 :                 if (delta < 0) {
     438    41741905 :                         tp->t_blk_res_used += (uint)-delta;
     439    41741905 :                         if (tp->t_blk_res_used > tp->t_blk_res)
     440           0 :                                 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
     441    51060469 :                 } else if (delta > 0 && (tp->t_flags & XFS_TRANS_RES_FDBLKS)) {
     442       71837 :                         int64_t blkres_delta;
     443             : 
     444             :                         /*
     445             :                          * Return freed blocks directly to the reservation
     446             :                          * instead of the global pool, being careful not to
     447             :                          * overflow the trans counter. This is used to preserve
     448             :                          * reservation across chains of transaction rolls that
     449             :                          * repeatedly free and allocate blocks.
     450             :                          */
     451       71837 :                         blkres_delta = min_t(int64_t, delta,
     452             :                                              UINT_MAX - tp->t_blk_res);
     453       71837 :                         tp->t_blk_res += blkres_delta;
     454       71837 :                         delta -= blkres_delta;
     455             :                 }
     456    92802374 :                 tp->t_fdblocks_delta += delta;
     457    92802374 :                 if (xfs_has_lazysbcount(mp))
     458    92797949 :                         flags &= ~XFS_TRANS_SB_DIRTY;
     459             :                 break;
     460    16262823 :         case XFS_TRANS_SB_RES_FDBLOCKS:
     461             :                 /*
     462             :                  * The allocation has already been applied to the
     463             :                  * in-core superblock's counter.  This should only
     464             :                  * be applied to the on-disk superblock.
     465             :                  */
     466    16262823 :                 tp->t_res_fdblocks_delta += delta;
     467    16262823 :                 if (xfs_has_lazysbcount(mp))
     468    16262827 :                         flags &= ~XFS_TRANS_SB_DIRTY;
     469             :                 break;
     470    11348878 :         case XFS_TRANS_SB_FREXTENTS:
     471             :                 /*
     472             :                  * Track the number of blocks allocated in the
     473             :                  * transaction.  Make sure it does not exceed the
     474             :                  * number reserved.
     475             :                  */
     476    11348878 :                 if (delta < 0) {
     477     8144290 :                         tp->t_rtx_res_used += (uint)-delta;
     478     8144290 :                         ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
     479             :                 }
     480    11348878 :                 tp->t_frextents_delta += delta;
     481    11348878 :                 break;
     482           0 :         case XFS_TRANS_SB_RES_FREXTENTS:
     483             :                 /*
     484             :                  * The allocation has already been applied to the
     485             :                  * in-core superblock's counter.  This should only
     486             :                  * be applied to the on-disk superblock.
     487             :                  */
     488           0 :                 ASSERT(delta < 0);
     489           0 :                 tp->t_res_frextents_delta += delta;
     490           0 :                 break;
     491         126 :         case XFS_TRANS_SB_DBLOCKS:
     492         126 :                 tp->t_dblocks_delta += delta;
     493         126 :                 break;
     494          86 :         case XFS_TRANS_SB_AGCOUNT:
     495          86 :                 ASSERT(delta > 0);
     496          86 :                 tp->t_agcount_delta += delta;
     497          86 :                 break;
     498           0 :         case XFS_TRANS_SB_IMAXPCT:
     499           0 :                 tp->t_imaxpct_delta += delta;
     500           0 :                 break;
     501           0 :         case XFS_TRANS_SB_REXTSIZE:
     502           0 :                 tp->t_rextsize_delta += delta;
     503           0 :                 break;
     504           3 :         case XFS_TRANS_SB_RBMBLOCKS:
     505           3 :                 tp->t_rbmblocks_delta += delta;
     506           3 :                 break;
     507           5 :         case XFS_TRANS_SB_RBLOCKS:
     508           5 :                 tp->t_rblocks_delta += delta;
     509           5 :                 break;
     510           5 :         case XFS_TRANS_SB_REXTENTS:
     511           5 :                 tp->t_rextents_delta += delta;
     512           5 :                 break;
     513           3 :         case XFS_TRANS_SB_REXTSLOG:
     514           3 :                 tp->t_rextslog_delta += delta;
     515           3 :                 break;
     516           0 :         default:
     517           0 :                 ASSERT(0);
     518           0 :                 return;
     519             :         }
     520             : 
     521   238574495 :         tp->t_flags |= flags;
     522             : }
     523             : 
     524             : /*
     525             :  * xfs_trans_apply_sb_deltas() is called from the commit code
     526             :  * to bring the superblock buffer into the current transaction
     527             :  * and modify it as requested by earlier calls to xfs_trans_mod_sb().
     528             :  *
     529             :  * For now we just look at each field allowed to change and change
     530             :  * it if necessary.
     531             :  */
     532             : STATIC void
     533    10062825 : xfs_trans_apply_sb_deltas(
     534             :         xfs_trans_t     *tp)
     535             : {
     536    10062825 :         struct xfs_dsb  *sbp;
     537    10062825 :         struct xfs_buf  *bp;
     538    10062825 :         int             whole = 0;
     539             : 
     540    10062825 :         bp = xfs_trans_getsb(tp);
     541    10062825 :         sbp = bp->b_addr;
     542             : 
     543             :         /*
     544             :          * Only update the superblock counters if we are logging them
     545             :          */
     546    10062825 :         if (!xfs_has_lazysbcount((tp->t_mountp))) {
     547         210 :                 if (tp->t_icount_delta)
     548           2 :                         be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
     549         210 :                 if (tp->t_ifree_delta)
     550         206 :                         be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
     551         210 :                 if (tp->t_fdblocks_delta)
     552           8 :                         be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
     553         210 :                 if (tp->t_res_fdblocks_delta)
     554           0 :                         be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
     555             :         }
     556             : 
     557             :         /*
     558             :          * Updating frextents requires careful handling because it does not
     559             :          * behave like the lazysb counters because we cannot rely on log
     560             :          * recovery in older kenels to recompute the value from the rtbitmap.
     561             :          * This means that the ondisk frextents must be consistent with the
     562             :          * rtbitmap.
     563             :          *
     564             :          * Therefore, log the frextents change to the ondisk superblock and
     565             :          * update the incore superblock so that future calls to xfs_log_sb
     566             :          * write the correct value ondisk.
     567             :          *
     568             :          * Don't touch m_frextents because it includes incore reservations,
     569             :          * and those are handled by the unreserve function.
     570             :          */
     571    10062825 :         if (tp->t_frextents_delta || tp->t_res_frextents_delta) {
     572    10062489 :                 struct xfs_mount        *mp = tp->t_mountp;
     573    10062489 :                 int64_t                 rtxdelta;
     574             : 
     575    10062489 :                 rtxdelta = tp->t_frextents_delta + tp->t_res_frextents_delta;
     576             : 
     577    10062489 :                 spin_lock(&mp->m_sb_lock);
     578    10062489 :                 be64_add_cpu(&sbp->sb_frextents, rtxdelta);
     579    10062489 :                 mp->m_sb.sb_frextents += rtxdelta;
     580    10062489 :                 spin_unlock(&mp->m_sb_lock);
     581             :         }
     582             : 
     583    10062825 :         if (tp->t_dblocks_delta) {
     584         126 :                 be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
     585         126 :                 whole = 1;
     586             :         }
     587    10062825 :         if (tp->t_agcount_delta) {
     588          86 :                 be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
     589          86 :                 whole = 1;
     590             :         }
     591    10062825 :         if (tp->t_imaxpct_delta) {
     592           0 :                 sbp->sb_imax_pct += tp->t_imaxpct_delta;
     593           0 :                 whole = 1;
     594             :         }
     595    10062825 :         if (tp->t_rextsize_delta) {
     596           0 :                 be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
     597           0 :                 whole = 1;
     598             :         }
     599    10062825 :         if (tp->t_rbmblocks_delta) {
     600           3 :                 be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
     601           3 :                 whole = 1;
     602             :         }
     603    10062825 :         if (tp->t_rblocks_delta) {
     604           5 :                 be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
     605           5 :                 whole = 1;
     606             :         }
     607    10062825 :         if (tp->t_rextents_delta) {
     608           5 :                 be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
     609           5 :                 whole = 1;
     610             :         }
     611    10062825 :         if (tp->t_rextslog_delta) {
     612           3 :                 sbp->sb_rextslog += tp->t_rextslog_delta;
     613           3 :                 whole = 1;
     614             :         }
     615             : 
     616    10062825 :         xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
     617    10062825 :         if (whole)
     618             :                 /*
     619             :                  * Log the whole thing, the fields are noncontiguous.
     620             :                  */
     621         131 :                 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1);
     622             :         else
     623             :                 /*
     624             :                  * Since all the modifiable fields are contiguous, we
     625             :                  * can get away with this.
     626             :                  */
     627    10062694 :                 xfs_trans_log_buf(tp, bp, offsetof(struct xfs_dsb, sb_icount),
     628             :                                   offsetof(struct xfs_dsb, sb_frextents) +
     629             :                                   sizeof(sbp->sb_frextents) - 1);
     630    10062825 : }
     631             : 
     632             : /*
     633             :  * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations and
     634             :  * apply superblock counter changes to the in-core superblock.  The
     635             :  * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
     636             :  * applied to the in-core superblock.  The idea is that that has already been
     637             :  * done.
     638             :  *
     639             :  * If we are not logging superblock counters, then the inode allocated/free and
     640             :  * used block counts are not updated in the on disk superblock. In this case,
     641             :  * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
     642             :  * still need to update the incore superblock with the changes.
     643             :  *
     644             :  * Deltas for the inode count are +/-64, hence we use a large batch size of 128
     645             :  * so we don't need to take the counter lock on every update.
     646             :  */
     647             : #define XFS_ICOUNT_BATCH        128
     648             : 
     649             : void
     650  2728092716 : xfs_trans_unreserve_and_mod_sb(
     651             :         struct xfs_trans        *tp)
     652             : {
     653  2728092716 :         struct xfs_mount        *mp = tp->t_mountp;
     654  2728092716 :         bool                    rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
     655  2728092716 :         int64_t                 blkdelta = 0;
     656  2728092716 :         int64_t                 rtxdelta = 0;
     657  2728092716 :         int64_t                 idelta = 0;
     658  2728092716 :         int64_t                 ifreedelta = 0;
     659  2728092716 :         int                     error;
     660             : 
     661             :         /* calculate deltas */
     662  2728092716 :         if (tp->t_blk_res > 0)
     663             :                 blkdelta = tp->t_blk_res;
     664  2728092716 :         if ((tp->t_fdblocks_delta != 0) &&
     665           8 :             (xfs_has_lazysbcount(mp) ||
     666           8 :              (tp->t_flags & XFS_TRANS_SB_DIRTY)))
     667    90569160 :                 blkdelta += tp->t_fdblocks_delta;
     668             : 
     669  2728092716 :         if (tp->t_rtx_res > 0)
     670             :                 rtxdelta = tp->t_rtx_res;
     671  2728092716 :         if ((tp->t_frextents_delta != 0) &&
     672    10062489 :             (tp->t_flags & XFS_TRANS_SB_DIRTY))
     673    10062489 :                 rtxdelta += tp->t_frextents_delta;
     674             : 
     675  2728092716 :         if (xfs_has_lazysbcount(mp) ||
     676         450 :              (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
     677  2728092476 :                 idelta = tp->t_icount_delta;
     678  2728092476 :                 ifreedelta = tp->t_ifree_delta;
     679             :         }
     680             : 
     681             :         /* apply the per-cpu counters */
     682  2728092716 :         if (blkdelta) {
     683   670704052 :                 error = xfs_mod_fdblocks(mp, blkdelta, rsvd);
     684   670701584 :                 ASSERT(!error);
     685             :         }
     686             : 
     687  2728090248 :         if (idelta)
     688      627769 :                 percpu_counter_add_batch(&mp->m_icount, idelta,
     689             :                                          XFS_ICOUNT_BATCH);
     690             : 
     691  2728090249 :         if (ifreedelta)
     692   117541205 :                 percpu_counter_add(&mp->m_ifree, ifreedelta);
     693             : 
     694  2728090090 :         if (rtxdelta) {
     695     4849979 :                 error = xfs_mod_frextents(mp, rtxdelta);
     696     4849977 :                 ASSERT(!error);
     697             :         }
     698             : 
     699  2728090088 :         if (!(tp->t_flags & XFS_TRANS_SB_DIRTY))
     700             :                 return;
     701             : 
     702             :         /* apply remaining deltas */
     703    10062825 :         spin_lock(&mp->m_sb_lock);
     704    10062825 :         mp->m_sb.sb_fdblocks += tp->t_fdblocks_delta + tp->t_res_fdblocks_delta;
     705    10062825 :         mp->m_sb.sb_icount += idelta;
     706    10062825 :         mp->m_sb.sb_ifree += ifreedelta;
     707             :         /*
     708             :          * Do not touch sb_frextents here because we are dealing with incore
     709             :          * reservation.  sb_frextents is not part of the lazy sb counters so it
     710             :          * must be consistent with the ondisk rtbitmap and must never include
     711             :          * incore reservations.
     712             :          */
     713    10062825 :         mp->m_sb.sb_dblocks += tp->t_dblocks_delta;
     714    10062825 :         mp->m_sb.sb_agcount += tp->t_agcount_delta;
     715    10062825 :         mp->m_sb.sb_imax_pct += tp->t_imaxpct_delta;
     716    10062825 :         mp->m_sb.sb_rextsize += tp->t_rextsize_delta;
     717    10062825 :         mp->m_sb.sb_rbmblocks += tp->t_rbmblocks_delta;
     718    10062825 :         mp->m_sb.sb_rblocks += tp->t_rblocks_delta;
     719    10062825 :         mp->m_sb.sb_rextents += tp->t_rextents_delta;
     720    10062825 :         mp->m_sb.sb_rextslog += tp->t_rextslog_delta;
     721    10062825 :         spin_unlock(&mp->m_sb_lock);
     722             : 
     723             :         /*
     724             :          * Debug checks outside of the spinlock so they don't lock up the
     725             :          * machine if they fail.
     726             :          */
     727             :         ASSERT(mp->m_sb.sb_imax_pct >= 0);
     728             :         ASSERT(mp->m_sb.sb_rextslog >= 0);
     729             :         return;
     730             : }
     731             : 
     732             : /* Add the given log item to the transaction's list of log items. */
     733             : void
     734 18673725932 : xfs_trans_add_item(
     735             :         struct xfs_trans        *tp,
     736             :         struct xfs_log_item     *lip)
     737             : {
     738 18673725932 :         ASSERT(lip->li_log == tp->t_mountp->m_log);
     739 18673725932 :         ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
     740 18673725932 :         ASSERT(list_empty(&lip->li_trans));
     741 18673725932 :         ASSERT(!test_bit(XFS_LI_DIRTY, &lip->li_flags));
     742             : 
     743 18673725932 :         list_add_tail(&lip->li_trans, &tp->t_items);
     744 18679886482 :         trace_xfs_trans_add_item(tp, _RET_IP_);
     745 18684721278 : }
     746             : 
     747             : /*
     748             :  * Unlink the log item from the transaction. the log item is no longer
     749             :  * considered dirty in this transaction, as the linked transaction has
     750             :  * finished, either by abort or commit completion.
     751             :  */
     752             : void
     753 18690305049 : xfs_trans_del_item(
     754             :         struct xfs_log_item     *lip)
     755             : {
     756 18690305049 :         clear_bit(XFS_LI_DIRTY, &lip->li_flags);
     757 18691621948 :         list_del_init(&lip->li_trans);
     758 18692970810 : }
     759             : 
     760             : /* Detach and unlock all of the items in a transaction */
     761             : static void
     762  1401743983 : xfs_trans_free_items(
     763             :         struct xfs_trans        *tp,
     764             :         bool                    abort)
     765             : {
     766  1401743983 :         struct xfs_log_item     *lip, *next;
     767             : 
     768  1401743983 :         trace_xfs_trans_free_items(tp, _RET_IP_);
     769             : 
     770  1745265669 :         list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
     771   343405114 :                 xfs_trans_del_item(lip);
     772   343384740 :                 if (abort)
     773       20361 :                         set_bit(XFS_LI_ABORTED, &lip->li_flags);
     774   343384740 :                 if (lip->li_ops->iop_release)
     775   343384740 :                         lip->li_ops->iop_release(lip);
     776             :         }
     777  1401860555 : }
     778             : 
     779             : static inline void
     780    20578076 : xfs_log_item_batch_insert(
     781             :         struct xfs_ail          *ailp,
     782             :         struct xfs_ail_cursor   *cur,
     783             :         struct xfs_log_item     **log_items,
     784             :         int                     nr_items,
     785             :         xfs_lsn_t               commit_lsn)
     786             : {
     787    20578076 :         int     i;
     788             : 
     789    20578076 :         spin_lock(&ailp->ail_lock);
     790             :         /* xfs_trans_ail_update_bulk drops ailp->ail_lock */
     791    20578076 :         xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn);
     792             : 
     793   646272085 :         for (i = 0; i < nr_items; i++) {
     794   605115933 :                 struct xfs_log_item *lip = log_items[i];
     795             : 
     796   605115933 :                 if (lip->li_ops->iop_unpin)
     797   605115933 :                         lip->li_ops->iop_unpin(lip, 0);
     798             :         }
     799    20578076 : }
     800             : 
     801             : /*
     802             :  * Bulk operation version of xfs_trans_committed that takes a log vector of
     803             :  * items to insert into the AIL. This uses bulk AIL insertion techniques to
     804             :  * minimise lock traffic.
     805             :  *
     806             :  * If we are called with the aborted flag set, it is because a log write during
     807             :  * a CIL checkpoint commit has failed. In this case, all the items in the
     808             :  * checkpoint have already gone through iop_committed and iop_committing, which
     809             :  * means that checkpoint commit abort handling is treated exactly the same
     810             :  * as an iclog write error even though we haven't started any IO yet. Hence in
     811             :  * this case all we need to do is iop_committed processing, followed by an
     812             :  * iop_unpin(aborted) call.
     813             :  *
     814             :  * The AIL cursor is used to optimise the insert process. If commit_lsn is not
     815             :  * at the end of the AIL, the insert cursor avoids the need to walk
     816             :  * the AIL to find the insertion point on every xfs_log_item_batch_insert()
     817             :  * call. This saves a lot of needless list walking and is a net win, even
     818             :  * though it slightly increases that amount of AIL lock traffic to set it up
     819             :  * and tear it down.
     820             :  */
     821             : void
     822     2816601 : xfs_trans_committed_bulk(
     823             :         struct xfs_ail          *ailp,
     824             :         struct list_head        *lv_chain,
     825             :         xfs_lsn_t               commit_lsn,
     826             :         bool                    aborted)
     827             : {
     828             : #define LOG_ITEM_BATCH_SIZE     32
     829     2816601 :         struct xfs_log_item     *log_items[LOG_ITEM_BATCH_SIZE];
     830     2816601 :         struct xfs_log_vec      *lv;
     831     2816601 :         struct xfs_ail_cursor   cur;
     832     2816601 :         int                     i = 0;
     833             : 
     834     2816601 :         spin_lock(&ailp->ail_lock);
     835     2816601 :         xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
     836     2816601 :         spin_unlock(&ailp->ail_lock);
     837             : 
     838             :         /* unpin all the log items */
     839   613820408 :         list_for_each_entry(lv, lv_chain, lv_list) {
     840   611003807 :                 struct xfs_log_item     *lip = lv->lv_item;
     841   611003807 :                 xfs_lsn_t               item_lsn;
     842             : 
     843   611003807 :                 if (aborted)
     844     1182695 :                         set_bit(XFS_LI_ABORTED, &lip->li_flags);
     845             : 
     846   611003793 :                 if (lip->li_ops->flags & XFS_ITEM_RELEASE_WHEN_COMMITTED) {
     847     1848796 :                         lip->li_ops->iop_release(lip);
     848     1848796 :                         continue;
     849             :                 }
     850             : 
     851   609154997 :                 if (lip->li_ops->iop_committed)
     852   579867043 :                         item_lsn = lip->li_ops->iop_committed(lip, commit_lsn);
     853             :                 else
     854             :                         item_lsn = commit_lsn;
     855             : 
     856             :                 /* item_lsn of -1 means the item needs no further processing */
     857   609155002 :                 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
     858     2767742 :                         continue;
     859             : 
     860             :                 /*
     861             :                  * if we are aborting the operation, no point in inserting the
     862             :                  * object into the AIL as we are in a shutdown situation.
     863             :                  */
     864   606387260 :                 if (aborted) {
     865     2292648 :                         ASSERT(xlog_is_shutdown(ailp->ail_log));
     866     1146324 :                         if (lip->li_ops->iop_unpin)
     867     1146324 :                                 lip->li_ops->iop_unpin(lip, 1);
     868     1146333 :                         continue;
     869             :                 }
     870             : 
     871   605240936 :                 if (item_lsn != commit_lsn) {
     872             : 
     873             :                         /*
     874             :                          * Not a bulk update option due to unusual item_lsn.
     875             :                          * Push into AIL immediately, rechecking the lsn once
     876             :                          * we have the ail lock. Then unpin the item. This does
     877             :                          * not affect the AIL cursor the bulk insert path is
     878             :                          * using.
     879             :                          */
     880      125003 :                         spin_lock(&ailp->ail_lock);
     881      125003 :                         if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
     882           0 :                                 xfs_trans_ail_update(ailp, lip, item_lsn);
     883             :                         else
     884      125003 :                                 spin_unlock(&ailp->ail_lock);
     885      125003 :                         if (lip->li_ops->iop_unpin)
     886      125003 :                                 lip->li_ops->iop_unpin(lip, 0);
     887      125003 :                         continue;
     888             :                 }
     889             : 
     890             :                 /* Item is a candidate for bulk AIL insert.  */
     891   605115933 :                 log_items[i++] = lv->lv_item;
     892   605115933 :                 if (i >= LOG_ITEM_BATCH_SIZE) {
     893    17825519 :                         xfs_log_item_batch_insert(ailp, &cur, log_items,
     894             :                                         LOG_ITEM_BATCH_SIZE, commit_lsn);
     895    17825519 :                         i = 0;
     896             :                 }
     897             :         }
     898             : 
     899             :         /* make sure we insert the remainder! */
     900     2816601 :         if (i)
     901     2752557 :                 xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
     902             : 
     903     2816601 :         spin_lock(&ailp->ail_lock);
     904     2816601 :         xfs_trans_ail_cursor_done(&cur);
     905     2816601 :         spin_unlock(&ailp->ail_lock);
     906     2816601 : }
     907             : 
     908             : /*
     909             :  * Sort transaction items prior to running precommit operations. This will
     910             :  * attempt to order the items such that they will always be locked in the same
     911             :  * order. Items that have no sort function are moved to the end of the list
     912             :  * and so are locked last.
     913             :  *
     914             :  * This may need refinement as different types of objects add sort functions.
     915             :  *
     916             :  * Function is more complex than it needs to be because we are comparing 64 bit
     917             :  * values and the function only returns 32 bit values.
     918             :  */
     919             : static int
     920  8264995132 : xfs_trans_precommit_sort(
     921             :         void                    *unused_arg,
     922             :         const struct list_head  *a,
     923             :         const struct list_head  *b)
     924             : {
     925  8264995132 :         struct xfs_log_item     *lia = container_of(a,
     926             :                                         struct xfs_log_item, li_trans);
     927  8264995132 :         struct xfs_log_item     *lib = container_of(b,
     928             :                                         struct xfs_log_item, li_trans);
     929  8264995132 :         int64_t                 diff;
     930             : 
     931             :         /*
     932             :          * If both items are non-sortable, leave them alone. If only one is
     933             :          * sortable, move the non-sortable item towards the end of the list.
     934             :          */
     935  8264995132 :         if (!lia->li_ops->iop_sort && !lib->li_ops->iop_sort)
     936             :                 return 0;
     937  4124328810 :         if (!lia->li_ops->iop_sort)
     938             :                 return 1;
     939  3928130888 :         if (!lib->li_ops->iop_sort)
     940             :                 return -1;
     941             : 
     942   887818339 :         diff = lia->li_ops->iop_sort(lia) - lib->li_ops->iop_sort(lib);
     943   887969518 :         if (diff < 0)
     944             :                 return -1;
     945   144477683 :         if (diff > 0)
     946   124963772 :                 return 1;
     947             :         return 0;
     948             : }
     949             : 
     950             : /*
     951             :  * Run transaction precommit functions.
     952             :  *
     953             :  * If there is an error in any of the callouts, then stop immediately and
     954             :  * trigger a shutdown to abort the transaction. There is no recovery possible
     955             :  * from errors at this point as the transaction is dirty....
     956             :  */
     957             : static int
     958  1882396264 : xfs_trans_run_precommits(
     959             :         struct xfs_trans        *tp)
     960             : {
     961  1882396264 :         struct xfs_mount        *mp = tp->t_mountp;
     962  1882396264 :         struct xfs_log_item     *lip, *n;
     963  1882396264 :         int                     error = 0;
     964             : 
     965             :         /*
     966             :          * Sort the item list to avoid ABBA deadlocks with other transactions
     967             :          * running precommit operations that lock multiple shared items such as
     968             :          * inode cluster buffers.
     969             :          */
     970  1882396264 :         list_sort(NULL, &tp->t_items, xfs_trans_precommit_sort);
     971             : 
     972             :         /*
     973             :          * Precommit operations can remove the log item from the transaction
     974             :          * if the log item exists purely to delay modifications until they
     975             :          * can be ordered against other operations. Hence we have to use
     976             :          * list_for_each_entry_safe() here.
     977             :          */
     978  8821154400 :         list_for_each_entry_safe(lip, n, &tp->t_items, li_trans) {
     979  6938742094 :                 if (!test_bit(XFS_LI_DIRTY, &lip->li_flags))
     980   897055707 :                         continue;
     981  6041686387 :                 if (lip->li_ops->iop_precommit) {
     982  2187063638 :                         error = lip->li_ops->iop_precommit(tp, lip);
     983  2187123468 :                         if (error)
     984             :                                 break;
     985             :                 }
     986             :         }
     987  1882412502 :         if (error)
     988         196 :                 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
     989  1882412502 :         return error;
     990             : }
     991             : 
     992             : /*
     993             :  * Commit the given transaction to the log.
     994             :  *
     995             :  * XFS disk error handling mechanism is not based on a typical
     996             :  * transaction abort mechanism. Logically after the filesystem
     997             :  * gets marked 'SHUTDOWN', we can't let any new transactions
     998             :  * be durable - ie. committed to disk - because some metadata might
     999             :  * be inconsistent. In such cases, this returns an error, and the
    1000             :  * caller may assume that all locked objects joined to the transaction
    1001             :  * have already been unlocked as if the commit had succeeded.
    1002             :  * Do not reference the transaction structure after this call.
    1003             :  */
    1004             : static int
    1005  1340571523 : __xfs_trans_commit(
    1006             :         struct xfs_trans        *tp,
    1007             :         bool                    regrant)
    1008             : {
    1009  1340571523 :         struct xfs_mount        *mp = tp->t_mountp;
    1010  1340571523 :         struct xlog             *log = mp->m_log;
    1011  1340571523 :         xfs_csn_t               commit_seq = 0;
    1012  1340571523 :         int                     error = 0;
    1013  1340571523 :         int                     sync = tp->t_flags & XFS_TRANS_SYNC;
    1014             : 
    1015  1340571523 :         trace_xfs_trans_commit(tp, _RET_IP_);
    1016             : 
    1017  1340611789 :         error = xfs_trans_run_precommits(tp);
    1018  1340537960 :         if (error) {
    1019         196 :                 if (tp->t_flags & XFS_TRANS_PERM_LOG_RES)
    1020         191 :                         xfs_defer_cancel(tp);
    1021         196 :                 goto out_unreserve;
    1022             :         }
    1023             : 
    1024             :         /*
    1025             :          * Finish deferred items on final commit. Only permanent transactions
    1026             :          * should ever have deferred ops.
    1027             :          */
    1028  2681068786 :         WARN_ON_ONCE(!list_empty(&tp->t_dfops) &&
    1029             :                      !(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
    1030  1340537764 :         if (!regrant && (tp->t_flags & XFS_TRANS_PERM_LOG_RES)) {
    1031   542486866 :                 error = xfs_defer_finish_noroll(&tp);
    1032   542469286 :                 if (error)
    1033        1842 :                         goto out_unreserve;
    1034             : 
    1035             :                 /* Run precommits from final tx in defer chain. */
    1036   542467444 :                 error = xfs_trans_run_precommits(tp);
    1037   542502264 :                 if (error)
    1038           0 :                         goto out_unreserve;
    1039             :         }
    1040             : 
    1041             :         /*
    1042             :          * If there is nothing to be logged by the transaction,
    1043             :          * then unlock all of the items associated with the
    1044             :          * transaction and free the transaction structure.
    1045             :          * Also make sure to return any reserved blocks to
    1046             :          * the free pool.
    1047             :          */
    1048  1340553162 :         if (!(tp->t_flags & XFS_TRANS_DIRTY))
    1049    14137090 :                 goto out_unreserve;
    1050             : 
    1051             :         /*
    1052             :          * We must check against log shutdown here because we cannot abort log
    1053             :          * items and leave them dirty, inconsistent and unpinned in memory while
    1054             :          * the log is active. This leaves them open to being written back to
    1055             :          * disk, and that will lead to on-disk corruption.
    1056             :          */
    1057  2652832144 :         if (xlog_is_shutdown(log)) {
    1058         519 :                 error = -EIO;
    1059         519 :                 goto out_unreserve;
    1060             :         }
    1061             : 
    1062  1326415553 :         ASSERT(tp->t_ticket != NULL);
    1063             : 
    1064             :         /*
    1065             :          * If we need to update the superblock, then do it now.
    1066             :          */
    1067  1326415553 :         if (tp->t_flags & XFS_TRANS_SB_DIRTY)
    1068    10062825 :                 xfs_trans_apply_sb_deltas(tp);
    1069  1326415553 :         xfs_trans_apply_dquot_deltas(tp);
    1070             : 
    1071  1326615804 :         xlog_cil_commit(log, tp, &commit_seq, regrant);
    1072             : 
    1073  1326687021 :         xfs_trans_free(tp);
    1074             : 
    1075             :         /*
    1076             :          * If the transaction needs to be synchronous, then force the
    1077             :          * log out now and wait for it.
    1078             :          */
    1079  1326720085 :         if (sync) {
    1080      110031 :                 error = xfs_log_force_seq(mp, commit_seq, XFS_LOG_SYNC, NULL);
    1081      110031 :                 XFS_STATS_INC(mp, xs_trans_sync);
    1082             :         } else {
    1083  1326610054 :                 XFS_STATS_INC(mp, xs_trans_async);
    1084             :         }
    1085             : 
    1086             :         return error;
    1087             : 
    1088    14139647 : out_unreserve:
    1089    14139647 :         xfs_trans_unreserve_and_mod_sb(tp);
    1090             : 
    1091             :         /*
    1092             :          * It is indeed possible for the transaction to be not dirty but
    1093             :          * the dqinfo portion to be.  All that means is that we have some
    1094             :          * (non-persistent) quota reservations that need to be unreserved.
    1095             :          */
    1096    14139687 :         xfs_trans_unreserve_and_mod_dquots(tp);
    1097    14139586 :         if (tp->t_ticket) {
    1098    14230139 :                 if (regrant && !xlog_is_shutdown(log))
    1099      122956 :                         xfs_log_ticket_regrant(log, tp->t_ticket);
    1100             :                 else
    1101    13983953 :                         xfs_log_ticket_ungrant(log, tp->t_ticket);
    1102    14107072 :                 tp->t_ticket = NULL;
    1103             :         }
    1104    14139749 :         xfs_trans_free_items(tp, !!error);
    1105    14139764 :         xfs_trans_free(tp);
    1106             : 
    1107    14139742 :         XFS_STATS_INC(mp, xs_trans_empty);
    1108    14139742 :         return error;
    1109             : }
    1110             : 
    1111             : int
    1112   635305024 : xfs_trans_commit(
    1113             :         struct xfs_trans        *tp)
    1114             : {
    1115   635305024 :         return __xfs_trans_commit(tp, false);
    1116             : }
    1117             : 
    1118             : /*
    1119             :  * Unlock all of the transaction's items and free the transaction.  If the
    1120             :  * transaction is dirty, we must shut down the filesystem because there is no
    1121             :  * way to restore them to their previous state.
    1122             :  *
    1123             :  * If the transaction has made a log reservation, make sure to release it as
    1124             :  * well.
    1125             :  *
    1126             :  * This is a high level function (equivalent to xfs_trans_commit()) and so can
    1127             :  * be called after the transaction has effectively been aborted due to the mount
    1128             :  * being shut down. However, if the mount has not been shut down and the
    1129             :  * transaction is dirty we will shut the mount down and, in doing so, that
    1130             :  * guarantees that the log is shut down, too. Hence we don't need to be as
    1131             :  * careful with shutdown state and dirty items here as we need to be in
    1132             :  * xfs_trans_commit().
    1133             :  */
    1134             : void
    1135  1387496145 : xfs_trans_cancel(
    1136             :         struct xfs_trans        *tp)
    1137             : {
    1138  1387496145 :         struct xfs_mount        *mp = tp->t_mountp;
    1139  1387496145 :         struct xlog             *log = mp->m_log;
    1140  1387496145 :         bool                    dirty = (tp->t_flags & XFS_TRANS_DIRTY);
    1141             : 
    1142  1387496145 :         trace_xfs_trans_cancel(tp, _RET_IP_);
    1143             : 
    1144             :         /*
    1145             :          * It's never valid to cancel a transaction with deferred ops attached,
    1146             :          * because the transaction is effectively dirty.  Complain about this
    1147             :          * loudly before freeing the in-memory defer items and shutting down the
    1148             :          * filesystem.
    1149             :          */
    1150  1387609488 :         if (!list_empty(&tp->t_dfops)) {
    1151          52 :                 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
    1152          52 :                 dirty = true;
    1153          52 :                 xfs_defer_cancel(tp);
    1154             :         }
    1155             : 
    1156             :         /*
    1157             :          * See if the caller is relying on us to shut down the filesystem. We
    1158             :          * only want an error report if there isn't already a shutdown in
    1159             :          * progress, so we only need to check against the mount shutdown state
    1160             :          * here.
    1161             :          */
    1162  1387612694 :         if (dirty && !xfs_is_shutdown(mp)) {
    1163           4 :                 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
    1164           4 :                 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
    1165             :         }
    1166             : #ifdef DEBUG
    1167             :         /* Log items need to be consistent until the log is shut down. */
    1168  2775219810 :         if (!dirty && !xlog_is_shutdown(log)) {
    1169  1387607748 :                 struct xfs_log_item *lip;
    1170             : 
    1171  1720657934 :                 list_for_each_entry(lip, &tp->t_items, li_trans)
    1172   333050186 :                         ASSERT(!xlog_item_is_intent_done(lip));
    1173             :         }
    1174             : #endif
    1175  1387609488 :         xfs_trans_unreserve_and_mod_sb(tp);
    1176  1387750826 :         xfs_trans_unreserve_and_mod_dquots(tp);
    1177             : 
    1178  1387808360 :         if (tp->t_ticket) {
    1179   261437249 :                 xfs_log_ticket_ungrant(log, tp->t_ticket);
    1180   261439805 :                 tp->t_ticket = NULL;
    1181             :         }
    1182             : 
    1183  1387810916 :         xfs_trans_free_items(tp, dirty);
    1184  1387743961 :         xfs_trans_free(tp);
    1185  1387823625 : }
    1186             : 
    1187             : /*
    1188             :  * Roll from one trans in the sequence of PERMANENT transactions to
    1189             :  * the next: permanent transactions are only flushed out when
    1190             :  * committed with xfs_trans_commit(), but we still want as soon
    1191             :  * as possible to let chunks of it go to the log. So we commit the
    1192             :  * chunk we've been working on and get a new transaction to continue.
    1193             :  */
    1194             : int
    1195   705339169 : xfs_trans_roll(
    1196             :         struct xfs_trans        **tpp)
    1197             : {
    1198   705339169 :         struct xfs_trans        *trans = *tpp;
    1199   705339169 :         struct xfs_trans_res    tres;
    1200   705339169 :         int                     error;
    1201             : 
    1202   705339169 :         trace_xfs_trans_roll(trans, _RET_IP_);
    1203             : 
    1204             :         /*
    1205             :          * Copy the critical parameters from one trans to the next.
    1206             :          */
    1207   705355276 :         tres.tr_logres = trans->t_log_res;
    1208   705355276 :         tres.tr_logcount = trans->t_log_count;
    1209             : 
    1210   705355276 :         *tpp = xfs_trans_dup(trans);
    1211             : 
    1212             :         /*
    1213             :          * Commit the current transaction.
    1214             :          * If this commit failed, then it'd just unlock those items that
    1215             :          * are not marked ihold. That also means that a filesystem shutdown
    1216             :          * is in progress. The caller takes the responsibility to cancel
    1217             :          * the duplicate transaction that gets returned.
    1218             :          */
    1219   705450321 :         error = __xfs_trans_commit(trans, true);
    1220   705496624 :         if (error)
    1221             :                 return error;
    1222             : 
    1223             :         /*
    1224             :          * Reserve space in the log for the next transaction.
    1225             :          * This also pushes items in the "AIL", the list of logged items,
    1226             :          * out to disk if they are taking up space at the tail of the log
    1227             :          * that we want to use.  This requires that either nothing be locked
    1228             :          * across this call, or that anything that is locked be logged in
    1229             :          * the prior and the next transactions.
    1230             :          */
    1231   705499320 :         tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
    1232   705499320 :         return xfs_trans_reserve(*tpp, &tres, 0, 0);
    1233             : }
    1234             : 
    1235             : /*
    1236             :  * Allocate an transaction, lock and join the inode to it, and reserve quota.
    1237             :  *
    1238             :  * The caller must ensure that the on-disk dquots attached to this inode have
    1239             :  * already been allocated and initialized.  The caller is responsible for
    1240             :  * releasing ILOCK_EXCL if a new transaction is returned.
    1241             :  */
    1242             : int
    1243   388400131 : xfs_trans_alloc_inode(
    1244             :         struct xfs_inode        *ip,
    1245             :         struct xfs_trans_res    *resv,
    1246             :         unsigned int            dblocks,
    1247             :         unsigned int            rblocks,
    1248             :         bool                    force,
    1249             :         struct xfs_trans        **tpp)
    1250             : {
    1251   388400131 :         struct xfs_trans        *tp;
    1252   388400131 :         struct xfs_mount        *mp = ip->i_mount;
    1253   388400131 :         bool                    retried = false;
    1254   388400403 :         int                     error;
    1255             : 
    1256   388400403 : retry:
    1257   388400403 :         error = xfs_trans_alloc(mp, resv, dblocks,
    1258   388400403 :                         rblocks / mp->m_sb.sb_rextsize,
    1259             :                         force ? XFS_TRANS_RESERVE : 0, &tp);
    1260   388443660 :         if (error)
    1261     2761536 :                 return error;
    1262             : 
    1263   385682124 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    1264   385680598 :         xfs_trans_ijoin(tp, ip, 0);
    1265             : 
    1266   385636039 :         error = xfs_qm_dqattach_locked(ip, false);
    1267   385669615 :         if (error) {
    1268             :                 /* Caller should have allocated the dquots! */
    1269           0 :                 ASSERT(error != -ENOENT);
    1270           0 :                 goto out_cancel;
    1271             :         }
    1272             : 
    1273   385669615 :         error = xfs_trans_reserve_quota_nblks(tp, ip, dblocks, rblocks, force);
    1274   385644310 :         if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
    1275         272 :                 xfs_trans_cancel(tp);
    1276         272 :                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1277         272 :                 xfs_blockgc_free_quota(ip, 0);
    1278         272 :                 retried = true;
    1279         272 :                 goto retry;
    1280             :         }
    1281   385644038 :         if (error)
    1282         248 :                 goto out_cancel;
    1283             : 
    1284   385643790 :         *tpp = tp;
    1285   385643790 :         return 0;
    1286             : 
    1287         248 : out_cancel:
    1288         248 :         xfs_trans_cancel(tp);
    1289         248 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1290         248 :         return error;
    1291             : }
    1292             : 
    1293             : 
    1294             : /* Try to reserve more blocks and file quota for a transaction. */
    1295             : int
    1296        5163 : xfs_trans_reserve_more_inode(
    1297             :         struct xfs_trans        *tp,
    1298             :         struct xfs_inode        *ip,
    1299             :         unsigned int            dblocks,
    1300             :         unsigned int            rblocks,
    1301             :         bool                    force_quota)
    1302             : {
    1303        5163 :         struct xfs_mount        *mp = ip->i_mount;
    1304        5163 :         unsigned int            rtx = rblocks / mp->m_sb.sb_rextsize;
    1305        5163 :         int                     error;
    1306             : 
    1307        5163 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    1308             : 
    1309        5163 :         error = xfs_trans_reserve_more(tp, dblocks, rtx);
    1310        5163 :         if (error)
    1311             :                 return error;
    1312             : 
    1313       10306 :         if (!XFS_IS_QUOTA_ON(mp) || xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
    1314             :                 return 0;
    1315             : 
    1316        5112 :         if (tp->t_flags & XFS_TRANS_RESERVE)
    1317           0 :                 force_quota = true;
    1318             : 
    1319        5112 :         error = xfs_trans_reserve_quota_nblks(tp, ip, dblocks, rblocks,
    1320             :                         force_quota);
    1321        5112 :         if (!error)
    1322             :                 return 0;
    1323             : 
    1324             :         /* Quota failed, give back the new reservation. */
    1325           0 :         xfs_mod_fdblocks(mp, dblocks, tp->t_flags & XFS_TRANS_RESERVE);
    1326           0 :         tp->t_blk_res -= dblocks;
    1327           0 :         xfs_mod_frextents(mp, rtx);
    1328           0 :         tp->t_rtx_res -= rtx;
    1329           0 :         return error;
    1330             : }
    1331             : 
    1332             : /*
    1333             :  * Allocate an transaction in preparation for inode creation by reserving quota
    1334             :  * against the given dquots.  Callers are not required to hold any inode locks.
    1335             :  */
    1336             : int
    1337    70070432 : xfs_trans_alloc_icreate(
    1338             :         struct xfs_mount        *mp,
    1339             :         struct xfs_trans_res    *resv,
    1340             :         struct xfs_dquot        *udqp,
    1341             :         struct xfs_dquot        *gdqp,
    1342             :         struct xfs_dquot        *pdqp,
    1343             :         unsigned int            dblocks,
    1344             :         struct xfs_trans        **tpp)
    1345             : {
    1346    70070432 :         struct xfs_trans        *tp;
    1347    70070432 :         bool                    retried = false;
    1348    70073311 :         int                     error;
    1349             : 
    1350    70073311 : retry:
    1351    70073311 :         error = xfs_trans_alloc(mp, resv, dblocks, 0, 0, &tp);
    1352    70089624 :         if (error)
    1353      505847 :                 return error;
    1354             : 
    1355    69583777 :         error = xfs_trans_reserve_quota_icreate(tp, udqp, gdqp, pdqp, dblocks);
    1356    69581774 :         if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
    1357        2879 :                 xfs_trans_cancel(tp);
    1358        2879 :                 xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0);
    1359        2879 :                 retried = true;
    1360        2879 :                 goto retry;
    1361             :         }
    1362    69578895 :         if (error) {
    1363        2743 :                 xfs_trans_cancel(tp);
    1364        2743 :                 return error;
    1365             :         }
    1366             : 
    1367    69576152 :         *tpp = tp;
    1368    69576152 :         return 0;
    1369             : }
    1370             : 
    1371             : /*
    1372             :  * Allocate an transaction, lock and join the inode to it, and reserve quota
    1373             :  * in preparation for inode attribute changes that include uid, gid, or prid
    1374             :  * changes.
    1375             :  *
    1376             :  * The caller must ensure that the on-disk dquots attached to this inode have
    1377             :  * already been allocated and initialized.  The ILOCK will be dropped when the
    1378             :  * transaction is committed or cancelled.
    1379             :  */
    1380             : int
    1381    12070235 : xfs_trans_alloc_ichange(
    1382             :         struct xfs_inode        *ip,
    1383             :         struct xfs_dquot        *new_udqp,
    1384             :         struct xfs_dquot        *new_gdqp,
    1385             :         struct xfs_dquot        *new_pdqp,
    1386             :         bool                    force,
    1387             :         struct xfs_trans        **tpp)
    1388             : {
    1389    12070235 :         struct xfs_trans        *tp;
    1390    12070235 :         struct xfs_mount        *mp = ip->i_mount;
    1391    12070235 :         struct xfs_dquot        *udqp;
    1392    12070235 :         struct xfs_dquot        *gdqp;
    1393    12070235 :         struct xfs_dquot        *pdqp;
    1394    12070235 :         bool                    retried = false;
    1395    12070237 :         int                     error;
    1396             : 
    1397    12070237 : retry:
    1398    12070237 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
    1399    12071168 :         if (error)
    1400           0 :                 return error;
    1401             : 
    1402    12071168 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    1403    12071219 :         xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
    1404             : 
    1405    12071135 :         error = xfs_qm_dqattach_locked(ip, false);
    1406    12071035 :         if (error) {
    1407             :                 /* Caller should have allocated the dquots! */
    1408         292 :                 ASSERT(error != -ENOENT);
    1409         292 :                 goto out_cancel;
    1410             :         }
    1411             : 
    1412             :         /*
    1413             :          * For each quota type, skip quota reservations if the inode's dquots
    1414             :          * now match the ones that came from the caller, or the caller didn't
    1415             :          * pass one in.  The inode's dquots can change if we drop the ILOCK to
    1416             :          * perform a blockgc scan, so we must preserve the caller's arguments.
    1417             :          */
    1418    12070743 :         udqp = (new_udqp != ip->i_udquot) ? new_udqp : NULL;
    1419    12070743 :         gdqp = (new_gdqp != ip->i_gdquot) ? new_gdqp : NULL;
    1420    12070743 :         pdqp = (new_pdqp != ip->i_pdquot) ? new_pdqp : NULL;
    1421    12070743 :         if (udqp || gdqp || pdqp) {
    1422     4478714 :                 unsigned int    qflags = XFS_QMOPT_RES_REGBLKS;
    1423             : 
    1424     4478714 :                 if (force)
    1425     4347916 :                         qflags |= XFS_QMOPT_FORCE_RES;
    1426             : 
    1427             :                 /*
    1428             :                  * Reserve enough quota to handle blocks on disk and reserved
    1429             :                  * for a delayed allocation.  We'll actually transfer the
    1430             :                  * delalloc reservation between dquots at chown time, even
    1431             :                  * though that part is only semi-transactional.
    1432             :                  */
    1433     4478714 :                 error = xfs_trans_reserve_quota_bydquots(tp, mp, udqp, gdqp,
    1434     4478714 :                                 pdqp, ip->i_nblocks + ip->i_delayed_blks,
    1435             :                                 1, qflags);
    1436     4478149 :                 if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
    1437           2 :                         xfs_trans_cancel(tp);
    1438           2 :                         xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0);
    1439           2 :                         retried = true;
    1440           2 :                         goto retry;
    1441             :                 }
    1442     4478147 :                 if (error)
    1443           2 :                         goto out_cancel;
    1444             :         }
    1445             : 
    1446    12070173 :         *tpp = tp;
    1447    12070173 :         return 0;
    1448             : 
    1449         294 : out_cancel:
    1450         294 :         xfs_trans_cancel(tp);
    1451         294 :         return error;
    1452             : }
    1453             : 
    1454             : /*
    1455             :  * Allocate an transaction, lock and join the directory and child inodes to it,
    1456             :  * and reserve quota for a directory update.  If there isn't sufficient space,
    1457             :  * @dblocks will be set to zero for a reservationless directory update and
    1458             :  * @nospace_error will be set to a negative errno describing the space
    1459             :  * constraint we hit.
    1460             :  *
    1461             :  * The caller must ensure that the on-disk dquots attached to this inode have
    1462             :  * already been allocated and initialized.  The ILOCKs will be dropped when the
    1463             :  * transaction is committed or cancelled.
    1464             :  *
    1465             :  * Caller is responsible for unlocking the inodes manually upon return
    1466             :  */
    1467             : int
    1468    48509651 : xfs_trans_alloc_dir(
    1469             :         struct xfs_inode        *dp,
    1470             :         struct xfs_trans_res    *resv,
    1471             :         struct xfs_inode        *ip,
    1472             :         unsigned int            *dblocks,
    1473             :         struct xfs_trans        **tpp,
    1474             :         int                     *nospace_error)
    1475             : {
    1476    48509651 :         struct xfs_trans        *tp;
    1477    48509651 :         struct xfs_mount        *mp = ip->i_mount;
    1478    48509651 :         unsigned int            resblks;
    1479    48509651 :         bool                    retried = false;
    1480    48512754 :         int                     error;
    1481             : 
    1482    48512754 : retry:
    1483    48512754 :         *nospace_error = 0;
    1484    48512754 :         resblks = *dblocks;
    1485    48512754 :         error = xfs_trans_alloc(mp, resv, resblks, 0, 0, &tp);
    1486    48513421 :         if (error == -ENOSPC) {
    1487       71919 :                 *nospace_error = error;
    1488       71919 :                 resblks = 0;
    1489       71919 :                 error = xfs_trans_alloc(mp, resv, resblks, 0, 0, &tp);
    1490             :         }
    1491    48513421 :         if (error)
    1492           1 :                 return error;
    1493             : 
    1494    48513420 :         xfs_lock_two_inodes(dp, XFS_ILOCK_EXCL, ip, XFS_ILOCK_EXCL);
    1495             : 
    1496    48514160 :         xfs_trans_ijoin(tp, dp, 0);
    1497    48512435 :         xfs_trans_ijoin(tp, ip, 0);
    1498             : 
    1499    48510149 :         error = xfs_qm_dqattach_locked(dp, false);
    1500    48514242 :         if (error) {
    1501             :                 /* Caller should have allocated the dquots! */
    1502           0 :                 ASSERT(error != -ENOENT);
    1503           0 :                 goto out_cancel;
    1504             :         }
    1505             : 
    1506    48514242 :         error = xfs_qm_dqattach_locked(ip, false);
    1507    48511222 :         if (error) {
    1508             :                 /* Caller should have allocated the dquots! */
    1509           0 :                 ASSERT(error != -ENOENT);
    1510           0 :                 goto out_cancel;
    1511             :         }
    1512             : 
    1513    48511222 :         if (resblks == 0)
    1514       71918 :                 goto done;
    1515             : 
    1516    48439304 :         error = xfs_trans_reserve_quota_nblks(tp, dp, resblks, 0, false);
    1517    48441090 :         if (error == -EDQUOT || error == -ENOSPC) {
    1518        6020 :                 if (!retried) {
    1519        3103 :                         xfs_trans_cancel(tp);
    1520        3103 :                         xfs_iunlock(dp, XFS_ILOCK_EXCL);
    1521        3103 :                         if (dp != ip)
    1522        3103 :                                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1523        3103 :                         xfs_blockgc_free_quota(dp, 0);
    1524        3103 :                         retried = true;
    1525        3103 :                         goto retry;
    1526             :                 }
    1527             : 
    1528        2917 :                 *nospace_error = error;
    1529        2917 :                 resblks = 0;
    1530        2917 :                 error = 0;
    1531             :         }
    1532    48437987 :         if (error)
    1533           0 :                 goto out_cancel;
    1534             : 
    1535    48435070 : done:
    1536    48509905 :         *tpp = tp;
    1537    48509905 :         *dblocks = resblks;
    1538    48509905 :         return 0;
    1539             : 
    1540           0 : out_cancel:
    1541           0 :         xfs_trans_cancel(tp);
    1542           0 :         return error;
    1543             : }

Generated by: LCOV version 1.14