^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2010 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "xfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "xfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "xfs_shared.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "xfs_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "xfs_log_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "xfs_log_priv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "xfs_trans_resv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "xfs_mount.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "xfs_extent_busy.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "xfs_quota.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "xfs_trans.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "xfs_trans_priv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "xfs_log.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "xfs_trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "xfs_error.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "xfs_defer.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) kmem_zone_t *xfs_trans_zone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #if defined(CONFIG_TRACEPOINTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) xfs_trans_trace_reservations(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct xfs_mount *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct xfs_trans_res resv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct xfs_trans_res *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct xfs_trans_res *end_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) res = (struct xfs_trans_res *)M_RES(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) end_res = (struct xfs_trans_res *)(M_RES(mp) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) for (i = 0; res < end_res; i++, res++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) trace_xfs_trans_resv_calc(mp, i, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) xfs_log_get_max_trans_res(mp, &resv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) trace_xfs_trans_resv_calc(mp, -1, &resv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) # define xfs_trans_trace_reservations(mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * Initialize the precomputed transaction reservation values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * in the mount structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) xfs_trans_init(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct xfs_mount *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) xfs_trans_resv_calc(mp, M_RES(mp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) xfs_trans_trace_reservations(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * Free the transaction structure. If there is more clean up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * to do when the structure is freed, add it here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) STATIC void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) xfs_trans_free(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct xfs_trans *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) xfs_extent_busy_sort(&tp->t_busy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) trace_xfs_trans_free(tp, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) sb_end_intwrite(tp->t_mountp->m_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) xfs_trans_free_dqinfo(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) kmem_cache_free(xfs_trans_zone, tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * This is called to create a new transaction which will share the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * permanent log reservation of the given transaction. The remaining
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * unused block and rt extent reservations are also inherited. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * implies that the original transaction is no longer allowed to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * blocks. Locks and log items, however, are no inherited. They must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * be added to the new transaction explicitly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) STATIC struct xfs_trans *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) xfs_trans_dup(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct xfs_trans *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct xfs_trans *ntp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) trace_xfs_trans_dup(tp, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ntp = kmem_cache_zalloc(xfs_trans_zone, GFP_KERNEL | __GFP_NOFAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * Initialize the new transaction structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) ntp->t_magic = XFS_TRANS_HEADER_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) ntp->t_mountp = tp->t_mountp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) INIT_LIST_HEAD(&ntp->t_items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) INIT_LIST_HEAD(&ntp->t_busy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) INIT_LIST_HEAD(&ntp->t_dfops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) ntp->t_firstblock = NULLFSBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) ASSERT(tp->t_ticket != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) ntp->t_flags = XFS_TRANS_PERM_LOG_RES |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) (tp->t_flags & XFS_TRANS_RESERVE) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) (tp->t_flags & XFS_TRANS_NO_WRITECOUNT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) (tp->t_flags & XFS_TRANS_RES_FDBLKS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /* We gave our writer reference to the new transaction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) tp->t_flags |= XFS_TRANS_NO_WRITECOUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) ASSERT(tp->t_blk_res >= tp->t_blk_res_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) tp->t_blk_res = tp->t_blk_res_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) tp->t_rtx_res = tp->t_rtx_res_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) ntp->t_pflags = tp->t_pflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* move deferred ops over to the new tp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) xfs_defer_move(ntp, tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) xfs_trans_dup_dqinfo(tp, ntp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return ntp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * This is called to reserve free disk blocks and log space for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * given transaction. This must be done before allocating any resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * within the transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * This will return ENOSPC if there are not enough blocks available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * It will sleep waiting for available log space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * is used by long running transactions. If any one of the reservations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * fails then they will all be backed out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * This does not do quota reservations. That typically is done by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * caller afterwards.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) xfs_trans_reserve(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct xfs_trans *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct xfs_trans_res *resp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) uint blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) uint rtextents)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct xfs_mount *mp = tp->t_mountp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* Mark this thread as being in a transaction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * Attempt to reserve the needed disk blocks by decrementing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * the number needed from the number available. This will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * fail if the count would go below zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (blocks > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) error = xfs_mod_fdblocks(mp, -((int64_t)blocks), rsvd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (error != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) tp->t_blk_res += blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * Reserve the log space needed for this transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (resp->tr_logres > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) bool permanent = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ASSERT(tp->t_log_res == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) tp->t_log_res == resp->tr_logres);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) ASSERT(tp->t_log_count == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) tp->t_log_count == resp->tr_logcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (resp->tr_logflags & XFS_TRANS_PERM_LOG_RES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) permanent = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) ASSERT(tp->t_ticket == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (tp->t_ticket != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) ASSERT(resp->tr_logflags & XFS_TRANS_PERM_LOG_RES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) error = xfs_log_regrant(mp, tp->t_ticket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) error = xfs_log_reserve(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) resp->tr_logres,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) resp->tr_logcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) &tp->t_ticket, XFS_TRANSACTION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) permanent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) goto undo_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) tp->t_log_res = resp->tr_logres;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) tp->t_log_count = resp->tr_logcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * Attempt to reserve the needed realtime extents by decrementing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * the number needed from the number available. This will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * fail if the count would go below zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (rtextents > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) error = xfs_mod_frextents(mp, -((int64_t)rtextents));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) error = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) goto undo_log;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) tp->t_rtx_res += rtextents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * Error cases jump to one of these labels to undo any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * reservations which have already been performed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) undo_log:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (resp->tr_logres > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) tp->t_ticket = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) tp->t_log_res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) undo_blocks:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (blocks > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) xfs_mod_fdblocks(mp, (int64_t)blocks, rsvd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) tp->t_blk_res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) xfs_trans_alloc(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct xfs_trans_res *resp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) uint blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) uint rtextents,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) uint flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct xfs_trans **tpp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) struct xfs_trans *tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * Allocate the handle before we do our freeze accounting and setting up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * GFP_NOFS allocation context so that we avoid lockdep false positives
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * by doing GFP_KERNEL allocations inside sb_start_intwrite().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) tp = kmem_cache_zalloc(xfs_trans_zone, GFP_KERNEL | __GFP_NOFAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (!(flags & XFS_TRANS_NO_WRITECOUNT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) sb_start_intwrite(mp->m_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * Zero-reservation ("empty") transactions can't modify anything, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * they're allowed to run while we're frozen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) WARN_ON(resp->tr_logres > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) ASSERT(!(flags & XFS_TRANS_RES_FDBLKS) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) xfs_sb_version_haslazysbcount(&mp->m_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) tp->t_magic = XFS_TRANS_HEADER_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) tp->t_flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) tp->t_mountp = mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) INIT_LIST_HEAD(&tp->t_items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) INIT_LIST_HEAD(&tp->t_busy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) INIT_LIST_HEAD(&tp->t_dfops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) tp->t_firstblock = NULLFSBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) error = xfs_trans_reserve(tp, resp, blocks, rtextents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) trace_xfs_trans_alloc(tp, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) *tpp = tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * Create an empty transaction with no reservation. This is a defensive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * mechanism for routines that query metadata without actually modifying them --
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * if the metadata being queried is somehow cross-linked (think a btree block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * pointer that points higher in the tree), we risk deadlock. However, blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * grabbed as part of a transaction can be re-grabbed. The verifiers will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * notice the corrupt block and the operation will fail back to userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * without deadlocking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * Note the zero-length reservation; this transaction MUST be cancelled without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * any dirty data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * Callers should obtain freeze protection to avoid a conflict with fs freezing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * where we can be grabbing buffers at the same time that freeze is trying to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * drain the buffer LRU list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) xfs_trans_alloc_empty(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) struct xfs_trans **tpp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) struct xfs_trans_res resv = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return xfs_trans_alloc(mp, &resv, 0, 0, XFS_TRANS_NO_WRITECOUNT, tpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * Record the indicated change to the given field for application
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * to the file system's superblock when the transaction commits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * For now, just store the change in the transaction structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * Mark the transaction structure to indicate that the superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * needs to be updated before committing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * Because we may not be keeping track of allocated/free inodes and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * used filesystem blocks in the superblock, we do not mark the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * superblock dirty in this transaction if we modify these fields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * We still need to update the transaction deltas so that they get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * applied to the incore superblock, but we don't want them to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * cause the superblock to get locked and logged if these are the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * only fields in the superblock that the transaction modifies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) xfs_trans_mod_sb(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) xfs_trans_t *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) uint field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) int64_t delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) xfs_mount_t *mp = tp->t_mountp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) switch (field) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) case XFS_TRANS_SB_ICOUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) tp->t_icount_delta += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (xfs_sb_version_haslazysbcount(&mp->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) flags &= ~XFS_TRANS_SB_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) case XFS_TRANS_SB_IFREE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) tp->t_ifree_delta += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (xfs_sb_version_haslazysbcount(&mp->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) flags &= ~XFS_TRANS_SB_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) case XFS_TRANS_SB_FDBLOCKS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * Track the number of blocks allocated in the transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * Make sure it does not exceed the number reserved. If so,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * shutdown as this can lead to accounting inconsistency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (delta < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) tp->t_blk_res_used += (uint)-delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (tp->t_blk_res_used > tp->t_blk_res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) } else if (delta > 0 && (tp->t_flags & XFS_TRANS_RES_FDBLKS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) int64_t blkres_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) * Return freed blocks directly to the reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * instead of the global pool, being careful not to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * overflow the trans counter. This is used to preserve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) * reservation across chains of transaction rolls that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * repeatedly free and allocate blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) blkres_delta = min_t(int64_t, delta,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) UINT_MAX - tp->t_blk_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) tp->t_blk_res += blkres_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) delta -= blkres_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) tp->t_fdblocks_delta += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (xfs_sb_version_haslazysbcount(&mp->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) flags &= ~XFS_TRANS_SB_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) case XFS_TRANS_SB_RES_FDBLOCKS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * The allocation has already been applied to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * in-core superblock's counter. This should only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) * be applied to the on-disk superblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) tp->t_res_fdblocks_delta += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (xfs_sb_version_haslazysbcount(&mp->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) flags &= ~XFS_TRANS_SB_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) case XFS_TRANS_SB_FREXTENTS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * Track the number of blocks allocated in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * transaction. Make sure it does not exceed the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * number reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (delta < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) tp->t_rtx_res_used += (uint)-delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) tp->t_frextents_delta += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) case XFS_TRANS_SB_RES_FREXTENTS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * The allocation has already been applied to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * in-core superblock's counter. This should only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * be applied to the on-disk superblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) ASSERT(delta < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) tp->t_res_frextents_delta += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) case XFS_TRANS_SB_DBLOCKS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) ASSERT(delta > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) tp->t_dblocks_delta += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) case XFS_TRANS_SB_AGCOUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) ASSERT(delta > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) tp->t_agcount_delta += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) case XFS_TRANS_SB_IMAXPCT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) tp->t_imaxpct_delta += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) case XFS_TRANS_SB_REXTSIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) tp->t_rextsize_delta += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) case XFS_TRANS_SB_RBMBLOCKS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) tp->t_rbmblocks_delta += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) case XFS_TRANS_SB_RBLOCKS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) tp->t_rblocks_delta += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) case XFS_TRANS_SB_REXTENTS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) tp->t_rextents_delta += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) case XFS_TRANS_SB_REXTSLOG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) tp->t_rextslog_delta += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) ASSERT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) tp->t_flags |= flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * xfs_trans_apply_sb_deltas() is called from the commit code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * to bring the superblock buffer into the current transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * and modify it as requested by earlier calls to xfs_trans_mod_sb().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) * For now we just look at each field allowed to change and change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * it if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) STATIC void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) xfs_trans_apply_sb_deltas(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) xfs_trans_t *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) xfs_dsb_t *sbp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) xfs_buf_t *bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) int whole = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) bp = xfs_trans_getsb(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) sbp = bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) * Check that superblock mods match the mods made to AGF counters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) tp->t_ag_btree_delta));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * Only update the superblock counters if we are logging them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) if (tp->t_icount_delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (tp->t_ifree_delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (tp->t_fdblocks_delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) if (tp->t_res_fdblocks_delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (tp->t_frextents_delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) if (tp->t_res_frextents_delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) if (tp->t_dblocks_delta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) whole = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) if (tp->t_agcount_delta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) whole = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (tp->t_imaxpct_delta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) sbp->sb_imax_pct += tp->t_imaxpct_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) whole = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (tp->t_rextsize_delta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) whole = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) if (tp->t_rbmblocks_delta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) whole = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) if (tp->t_rblocks_delta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) whole = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (tp->t_rextents_delta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) whole = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (tp->t_rextslog_delta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) sbp->sb_rextslog += tp->t_rextslog_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) whole = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (whole)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) * Log the whole thing, the fields are noncontiguous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) * Since all the modifiable fields are contiguous, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) * can get away with this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) offsetof(xfs_dsb_t, sb_frextents) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) sizeof(sbp->sb_frextents) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * apply superblock counter changes to the in-core superblock. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) * applied to the in-core superblock. The idea is that that has already been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * If we are not logging superblock counters, then the inode allocated/free and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * used block counts are not updated in the on disk superblock. In this case,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) * still need to update the incore superblock with the changes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * Deltas for the inode count are +/-64, hence we use a large batch size of 128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * so we don't need to take the counter lock on every update.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) #define XFS_ICOUNT_BATCH 128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) xfs_trans_unreserve_and_mod_sb(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) struct xfs_trans *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) struct xfs_mount *mp = tp->t_mountp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) int64_t blkdelta = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) int64_t rtxdelta = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) int64_t idelta = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) int64_t ifreedelta = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) /* calculate deltas */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (tp->t_blk_res > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) blkdelta = tp->t_blk_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) if ((tp->t_fdblocks_delta != 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) (tp->t_flags & XFS_TRANS_SB_DIRTY)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) blkdelta += tp->t_fdblocks_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (tp->t_rtx_res > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) rtxdelta = tp->t_rtx_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) if ((tp->t_frextents_delta != 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) (tp->t_flags & XFS_TRANS_SB_DIRTY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) rtxdelta += tp->t_frextents_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) idelta = tp->t_icount_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) ifreedelta = tp->t_ifree_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) /* apply the per-cpu counters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) if (blkdelta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) error = xfs_mod_fdblocks(mp, blkdelta, rsvd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) ASSERT(!error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) if (idelta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) percpu_counter_add_batch(&mp->m_icount, idelta,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) XFS_ICOUNT_BATCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) if (idelta < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) ASSERT(__percpu_counter_compare(&mp->m_icount, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) XFS_ICOUNT_BATCH) >= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) if (ifreedelta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) percpu_counter_add(&mp->m_ifree, ifreedelta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) if (ifreedelta < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) ASSERT(percpu_counter_compare(&mp->m_ifree, 0) >= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) if (rtxdelta == 0 && !(tp->t_flags & XFS_TRANS_SB_DIRTY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) /* apply remaining deltas */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) spin_lock(&mp->m_sb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) mp->m_sb.sb_frextents += rtxdelta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) mp->m_sb.sb_dblocks += tp->t_dblocks_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) mp->m_sb.sb_agcount += tp->t_agcount_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) mp->m_sb.sb_imax_pct += tp->t_imaxpct_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) mp->m_sb.sb_rextsize += tp->t_rextsize_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) mp->m_sb.sb_rbmblocks += tp->t_rbmblocks_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) mp->m_sb.sb_rblocks += tp->t_rblocks_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) mp->m_sb.sb_rextents += tp->t_rextents_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) mp->m_sb.sb_rextslog += tp->t_rextslog_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) spin_unlock(&mp->m_sb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) * Debug checks outside of the spinlock so they don't lock up the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) * machine if they fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) ASSERT(mp->m_sb.sb_imax_pct >= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) ASSERT(mp->m_sb.sb_rextslog >= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) /* Add the given log item to the transaction's list of log items. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) xfs_trans_add_item(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) struct xfs_trans *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) struct xfs_log_item *lip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) ASSERT(lip->li_mountp == tp->t_mountp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) ASSERT(list_empty(&lip->li_trans));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) ASSERT(!test_bit(XFS_LI_DIRTY, &lip->li_flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) list_add_tail(&lip->li_trans, &tp->t_items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) trace_xfs_trans_add_item(tp, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) * Unlink the log item from the transaction. the log item is no longer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) * considered dirty in this transaction, as the linked transaction has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) * finished, either by abort or commit completion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) xfs_trans_del_item(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) struct xfs_log_item *lip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) clear_bit(XFS_LI_DIRTY, &lip->li_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) list_del_init(&lip->li_trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) /* Detach and unlock all of the items in a transaction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) xfs_trans_free_items(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) struct xfs_trans *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) bool abort)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) struct xfs_log_item *lip, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) trace_xfs_trans_free_items(tp, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) xfs_trans_del_item(lip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) if (abort)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) set_bit(XFS_LI_ABORTED, &lip->li_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) if (lip->li_ops->iop_release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) lip->li_ops->iop_release(lip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) xfs_log_item_batch_insert(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) struct xfs_ail *ailp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) struct xfs_ail_cursor *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) struct xfs_log_item **log_items,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) int nr_items,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) xfs_lsn_t commit_lsn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) spin_lock(&ailp->ail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) /* xfs_trans_ail_update_bulk drops ailp->ail_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) for (i = 0; i < nr_items; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) struct xfs_log_item *lip = log_items[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) if (lip->li_ops->iop_unpin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) lip->li_ops->iop_unpin(lip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) * Bulk operation version of xfs_trans_committed that takes a log vector of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) * items to insert into the AIL. This uses bulk AIL insertion techniques to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) * minimise lock traffic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) * If we are called with the aborted flag set, it is because a log write during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) * a CIL checkpoint commit has failed. In this case, all the items in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) * checkpoint have already gone through iop_committed and iop_committing, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) * means that checkpoint commit abort handling is treated exactly the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) * as an iclog write error even though we haven't started any IO yet. Hence in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) * this case all we need to do is iop_committed processing, followed by an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) * iop_unpin(aborted) call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) * The AIL cursor is used to optimise the insert process. If commit_lsn is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * at the end of the AIL, the insert cursor avoids the need to walk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * the AIL to find the insertion point on every xfs_log_item_batch_insert()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) * call. This saves a lot of needless list walking and is a net win, even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) * though it slightly increases that amount of AIL lock traffic to set it up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) * and tear it down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) xfs_trans_committed_bulk(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) struct xfs_ail *ailp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) struct xfs_log_vec *log_vector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) xfs_lsn_t commit_lsn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) bool aborted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) #define LOG_ITEM_BATCH_SIZE 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) struct xfs_log_item *log_items[LOG_ITEM_BATCH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) struct xfs_log_vec *lv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) struct xfs_ail_cursor cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) spin_lock(&ailp->ail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) spin_unlock(&ailp->ail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) /* unpin all the log items */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) for (lv = log_vector; lv; lv = lv->lv_next ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) struct xfs_log_item *lip = lv->lv_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) xfs_lsn_t item_lsn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) if (aborted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) set_bit(XFS_LI_ABORTED, &lip->li_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) if (lip->li_ops->flags & XFS_ITEM_RELEASE_WHEN_COMMITTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) lip->li_ops->iop_release(lip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) if (lip->li_ops->iop_committed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) item_lsn = lip->li_ops->iop_committed(lip, commit_lsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) item_lsn = commit_lsn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) /* item_lsn of -1 means the item needs no further processing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) * if we are aborting the operation, no point in inserting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) * object into the AIL as we are in a shutdown situation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) if (aborted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) ASSERT(XFS_FORCED_SHUTDOWN(ailp->ail_mount));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) if (lip->li_ops->iop_unpin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) lip->li_ops->iop_unpin(lip, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) if (item_lsn != commit_lsn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) * Not a bulk update option due to unusual item_lsn.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) * Push into AIL immediately, rechecking the lsn once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) * we have the ail lock. Then unpin the item. This does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) * not affect the AIL cursor the bulk insert path is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) * using.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) spin_lock(&ailp->ail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) xfs_trans_ail_update(ailp, lip, item_lsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) spin_unlock(&ailp->ail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) if (lip->li_ops->iop_unpin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) lip->li_ops->iop_unpin(lip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) /* Item is a candidate for bulk AIL insert. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) log_items[i++] = lv->lv_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) if (i >= LOG_ITEM_BATCH_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) xfs_log_item_batch_insert(ailp, &cur, log_items,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) LOG_ITEM_BATCH_SIZE, commit_lsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) /* make sure we insert the remainder! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) if (i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) spin_lock(&ailp->ail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) xfs_trans_ail_cursor_done(&cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) spin_unlock(&ailp->ail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) * Commit the given transaction to the log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) * XFS disk error handling mechanism is not based on a typical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) * transaction abort mechanism. Logically after the filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) * gets marked 'SHUTDOWN', we can't let any new transactions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) * be durable - ie. committed to disk - because some metadata might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) * be inconsistent. In such cases, this returns an error, and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) * caller may assume that all locked objects joined to the transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) * have already been unlocked as if the commit had succeeded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) * Do not reference the transaction structure after this call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) __xfs_trans_commit(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) struct xfs_trans *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) bool regrant)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) struct xfs_mount *mp = tp->t_mountp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) xfs_lsn_t commit_lsn = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) int sync = tp->t_flags & XFS_TRANS_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) trace_xfs_trans_commit(tp, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) * Finish deferred items on final commit. Only permanent transactions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) * should ever have deferred ops.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) WARN_ON_ONCE(!list_empty(&tp->t_dfops) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) !(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) if (!regrant && (tp->t_flags & XFS_TRANS_PERM_LOG_RES)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) error = xfs_defer_finish_noroll(&tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) goto out_unreserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) * If there is nothing to be logged by the transaction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) * then unlock all of the items associated with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) * transaction and free the transaction structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) * Also make sure to return any reserved blocks to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) * the free pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) if (!(tp->t_flags & XFS_TRANS_DIRTY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) goto out_unreserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) if (XFS_FORCED_SHUTDOWN(mp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) goto out_unreserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) ASSERT(tp->t_ticket != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) * If we need to update the superblock, then do it now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) if (tp->t_flags & XFS_TRANS_SB_DIRTY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) xfs_trans_apply_sb_deltas(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) xfs_trans_apply_dquot_deltas(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) xfs_log_commit_cil(mp, tp, &commit_lsn, regrant);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) xfs_trans_free(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) * If the transaction needs to be synchronous, then force the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) * log out now and wait for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) if (sync) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) error = xfs_log_force_lsn(mp, commit_lsn, XFS_LOG_SYNC, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) XFS_STATS_INC(mp, xs_trans_sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) XFS_STATS_INC(mp, xs_trans_async);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) out_unreserve:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) xfs_trans_unreserve_and_mod_sb(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) * It is indeed possible for the transaction to be not dirty but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) * the dqinfo portion to be. All that means is that we have some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) * (non-persistent) quota reservations that need to be unreserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) xfs_trans_unreserve_and_mod_dquots(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) if (tp->t_ticket) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) if (regrant && !XLOG_FORCED_SHUTDOWN(mp->m_log))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) xfs_log_ticket_regrant(mp->m_log, tp->t_ticket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) tp->t_ticket = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) xfs_trans_free_items(tp, !!error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) xfs_trans_free(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) XFS_STATS_INC(mp, xs_trans_empty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) xfs_trans_commit(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) struct xfs_trans *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) return __xfs_trans_commit(tp, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) * Unlock all of the transaction's items and free the transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) * The transaction must not have modified any of its items, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) * there is no way to restore them to their previous state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) * If the transaction has made a log reservation, make sure to release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) * it as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) xfs_trans_cancel(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) struct xfs_trans *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) struct xfs_mount *mp = tp->t_mountp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) bool dirty = (tp->t_flags & XFS_TRANS_DIRTY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) trace_xfs_trans_cancel(tp, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) if (tp->t_flags & XFS_TRANS_PERM_LOG_RES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) xfs_defer_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) * See if the caller is relying on us to shut down the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) * filesystem. This happens in paths where we detect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) * corruption and decide to give up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) if (dirty && !XFS_FORCED_SHUTDOWN(mp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) if (!dirty && !XFS_FORCED_SHUTDOWN(mp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) struct xfs_log_item *lip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) list_for_each_entry(lip, &tp->t_items, li_trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) ASSERT(!xlog_item_is_intent_done(lip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) xfs_trans_unreserve_and_mod_sb(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) xfs_trans_unreserve_and_mod_dquots(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) if (tp->t_ticket) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) tp->t_ticket = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) /* mark this thread as no longer being in a transaction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) xfs_trans_free_items(tp, dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) xfs_trans_free(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) * Roll from one trans in the sequence of PERMANENT transactions to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) * the next: permanent transactions are only flushed out when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) * committed with xfs_trans_commit(), but we still want as soon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) * as possible to let chunks of it go to the log. So we commit the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) * chunk we've been working on and get a new transaction to continue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) xfs_trans_roll(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) struct xfs_trans **tpp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) struct xfs_trans *trans = *tpp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) struct xfs_trans_res tres;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) trace_xfs_trans_roll(trans, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) * Copy the critical parameters from one trans to the next.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) tres.tr_logres = trans->t_log_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) tres.tr_logcount = trans->t_log_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) *tpp = xfs_trans_dup(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) * Commit the current transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) * If this commit failed, then it'd just unlock those items that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) * are not marked ihold. That also means that a filesystem shutdown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) * is in progress. The caller takes the responsibility to cancel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) * the duplicate transaction that gets returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) error = __xfs_trans_commit(trans, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) * Reserve space in the log for the next transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) * This also pushes items in the "AIL", the list of logged items,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) * out to disk if they are taking up space at the tail of the log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) * that we want to use. This requires that either nothing be locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) * across this call, or that anything that is locked be logged in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) * the prior and the next transactions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) return xfs_trans_reserve(*tpp, &tres, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) }