10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 37b718769SNathan Scott * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. 4e98c414fSChristoph Hellwig * Copyright (C) 2010 Red Hat, Inc. 57b718769SNathan Scott * All Rights Reserved. 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds #include "xfs.h" 8a844f451SNathan Scott #include "xfs_fs.h" 970a9883cSDave Chinner #include "xfs_shared.h" 10239880efSDave Chinner #include "xfs_format.h" 11239880efSDave Chinner #include "xfs_log_format.h" 12dd401770SDave Chinner #include "xfs_log_priv.h" 13239880efSDave Chinner #include "xfs_trans_resv.h" 141da177e4SLinus Torvalds #include "xfs_mount.h" 15efc27b52SDave Chinner #include "xfs_extent_busy.h" 161da177e4SLinus Torvalds #include "xfs_quota.h" 17239880efSDave Chinner #include "xfs_trans.h" 18a844f451SNathan Scott #include "xfs_trans_priv.h" 19239880efSDave Chinner #include "xfs_log.h" 20ed3b4d6cSDave Chinner #include "xfs_trace.h" 21a4fbe6abSDave Chinner #include "xfs_error.h" 22f8f2835aSBrian Foster #include "xfs_defer.h" 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds kmem_zone_t *xfs_trans_zone; 251da177e4SLinus Torvalds 26b872af2cSDarrick J. Wong #if defined(CONFIG_TRACEPOINTS) 27b872af2cSDarrick J. Wong static void 28b872af2cSDarrick J. Wong xfs_trans_trace_reservations( 29b872af2cSDarrick J. Wong struct xfs_mount *mp) 30b872af2cSDarrick J. Wong { 31b872af2cSDarrick J. Wong struct xfs_trans_res resv; 32b872af2cSDarrick J. Wong struct xfs_trans_res *res; 33b872af2cSDarrick J. Wong struct xfs_trans_res *end_res; 34b872af2cSDarrick J. Wong int i; 35b872af2cSDarrick J. Wong 36b872af2cSDarrick J. Wong res = (struct xfs_trans_res *)M_RES(mp); 37b872af2cSDarrick J. Wong end_res = (struct xfs_trans_res *)(M_RES(mp) + 1); 38b872af2cSDarrick J. Wong for (i = 0; res < end_res; i++, res++) 39b872af2cSDarrick J. Wong trace_xfs_trans_resv_calc(mp, i, res); 40b872af2cSDarrick J. Wong xfs_log_get_max_trans_res(mp, &resv); 41b872af2cSDarrick J. Wong trace_xfs_trans_resv_calc(mp, -1, &resv); 42b872af2cSDarrick J. Wong } 43b872af2cSDarrick J. Wong #else 44b872af2cSDarrick J. Wong # define xfs_trans_trace_reservations(mp) 45b872af2cSDarrick J. Wong #endif 46b872af2cSDarrick J. Wong 474f3b5783SJeff Liu /* 481da177e4SLinus Torvalds * Initialize the precomputed transaction reservation values 491da177e4SLinus Torvalds * in the mount structure. 501da177e4SLinus Torvalds */ 511da177e4SLinus Torvalds void 521da177e4SLinus Torvalds xfs_trans_init( 53025101dcSChristoph Hellwig struct xfs_mount *mp) 541da177e4SLinus Torvalds { 553d3c8b52SJie Liu xfs_trans_resv_calc(mp, M_RES(mp)); 56b872af2cSDarrick J. Wong xfs_trans_trace_reservations(mp); 571da177e4SLinus Torvalds } 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds /* 60b1c1b5b6SDave Chinner * Free the transaction structure. If there is more clean up 61b1c1b5b6SDave Chinner * to do when the structure is freed, add it here. 62b1c1b5b6SDave Chinner */ 63b1c1b5b6SDave Chinner STATIC void 64b1c1b5b6SDave Chinner xfs_trans_free( 65ed3b4d6cSDave Chinner struct xfs_trans *tp) 66b1c1b5b6SDave Chinner { 674ecbfe63SDave Chinner xfs_extent_busy_sort(&tp->t_busy); 684ecbfe63SDave Chinner xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false); 69ed3b4d6cSDave Chinner 70ba18781bSDave Chinner trace_xfs_trans_free(tp, _RET_IP_); 71253f4911SChristoph Hellwig if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT)) 72d9457dc0SJan Kara sb_end_intwrite(tp->t_mountp->m_super); 73b1c1b5b6SDave Chinner xfs_trans_free_dqinfo(tp); 74377bcd5fSCarlos Maiolino kmem_cache_free(xfs_trans_zone, tp); 75b1c1b5b6SDave Chinner } 76b1c1b5b6SDave Chinner 77b1c1b5b6SDave Chinner /* 781da177e4SLinus Torvalds * This is called to create a new transaction which will share the 791da177e4SLinus Torvalds * permanent log reservation of the given transaction. The remaining 801da177e4SLinus Torvalds * unused block and rt extent reservations are also inherited. This 811da177e4SLinus Torvalds * implies that the original transaction is no longer allowed to allocate 821da177e4SLinus Torvalds * blocks. Locks and log items, however, are no inherited. They must 831da177e4SLinus Torvalds * be added to the new transaction explicitly. 841da177e4SLinus Torvalds */ 85f8f2835aSBrian Foster STATIC struct xfs_trans * 861da177e4SLinus Torvalds xfs_trans_dup( 87f8f2835aSBrian Foster struct xfs_trans *tp) 881da177e4SLinus Torvalds { 89f8f2835aSBrian Foster struct xfs_trans *ntp; 901da177e4SLinus Torvalds 91ba18781bSDave Chinner trace_xfs_trans_dup(tp, _RET_IP_); 92ba18781bSDave Chinner 93*32a2b11fSCarlos Maiolino ntp = kmem_cache_zalloc(xfs_trans_zone, GFP_KERNEL | __GFP_NOFAIL); 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds /* 961da177e4SLinus Torvalds * Initialize the new transaction structure. 971da177e4SLinus Torvalds */ 982a3c0accSDave Chinner ntp->t_magic = XFS_TRANS_HEADER_MAGIC; 991da177e4SLinus Torvalds ntp->t_mountp = tp->t_mountp; 100e98c414fSChristoph Hellwig INIT_LIST_HEAD(&ntp->t_items); 101ed3b4d6cSDave Chinner INIT_LIST_HEAD(&ntp->t_busy); 1029d9e6233SBrian Foster INIT_LIST_HEAD(&ntp->t_dfops); 103bba59c5eSBrian Foster ntp->t_firstblock = NULLFSBLOCK; 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); 1061da177e4SLinus Torvalds ASSERT(tp->t_ticket != NULL); 107cfcbbbd0SNathan Scott 108d9457dc0SJan Kara ntp->t_flags = XFS_TRANS_PERM_LOG_RES | 109d9457dc0SJan Kara (tp->t_flags & XFS_TRANS_RESERVE) | 110f74681baSBrian Foster (tp->t_flags & XFS_TRANS_NO_WRITECOUNT) | 111f74681baSBrian Foster (tp->t_flags & XFS_TRANS_RES_FDBLKS); 112d9457dc0SJan Kara /* We gave our writer reference to the new transaction */ 113253f4911SChristoph Hellwig tp->t_flags |= XFS_TRANS_NO_WRITECOUNT; 114cc09c0dcSDave Chinner ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket); 1153e78b9a4SBrian Foster 1163e78b9a4SBrian Foster ASSERT(tp->t_blk_res >= tp->t_blk_res_used); 1171da177e4SLinus Torvalds ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used; 1181da177e4SLinus Torvalds tp->t_blk_res = tp->t_blk_res_used; 1193e78b9a4SBrian Foster 1201da177e4SLinus Torvalds ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used; 1211da177e4SLinus Torvalds tp->t_rtx_res = tp->t_rtx_res_used; 12259c1b082SNathan Scott ntp->t_pflags = tp->t_pflags; 123e021a2e5SBrian Foster 1249d9e6233SBrian Foster /* move deferred ops over to the new tp */ 125ce356d64SBrian Foster xfs_defer_move(ntp, tp); 1261da177e4SLinus Torvalds 1277d095257SChristoph Hellwig xfs_trans_dup_dqinfo(tp, ntp); 1281da177e4SLinus Torvalds return ntp; 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /* 1321da177e4SLinus Torvalds * This is called to reserve free disk blocks and log space for the 1331da177e4SLinus Torvalds * given transaction. This must be done before allocating any resources 1341da177e4SLinus Torvalds * within the transaction. 1351da177e4SLinus Torvalds * 1361da177e4SLinus Torvalds * This will return ENOSPC if there are not enough blocks available. 1371da177e4SLinus Torvalds * It will sleep waiting for available log space. 1381da177e4SLinus Torvalds * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which 1391da177e4SLinus Torvalds * is used by long running transactions. If any one of the reservations 1401da177e4SLinus Torvalds * fails then they will all be backed out. 1411da177e4SLinus Torvalds * 1421da177e4SLinus Torvalds * This does not do quota reservations. That typically is done by the 1431da177e4SLinus Torvalds * caller afterwards. 1441da177e4SLinus Torvalds */ 145253f4911SChristoph Hellwig static int 1461da177e4SLinus Torvalds xfs_trans_reserve( 1473d3c8b52SJie Liu struct xfs_trans *tp, 1483d3c8b52SJie Liu struct xfs_trans_res *resp, 1491da177e4SLinus Torvalds uint blocks, 1503d3c8b52SJie Liu uint rtextents) 1511da177e4SLinus Torvalds { 152dd401770SDave Chinner struct xfs_mount *mp = tp->t_mountp; 15359c1b082SNathan Scott int error = 0; 1540d485adaSDave Chinner bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0; 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds /* Mark this thread as being in a transaction */ 1579070733bSMichal Hocko current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds /* 1601da177e4SLinus Torvalds * Attempt to reserve the needed disk blocks by decrementing 1611da177e4SLinus Torvalds * the number needed from the number available. This will 1621da177e4SLinus Torvalds * fail if the count would go below zero. 1631da177e4SLinus Torvalds */ 1641da177e4SLinus Torvalds if (blocks > 0) { 165dd401770SDave Chinner error = xfs_mod_fdblocks(mp, -((int64_t)blocks), rsvd); 1661da177e4SLinus Torvalds if (error != 0) { 1679070733bSMichal Hocko current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); 1682451337dSDave Chinner return -ENOSPC; 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds tp->t_blk_res += blocks; 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds /* 1741da177e4SLinus Torvalds * Reserve the log space needed for this transaction. 1751da177e4SLinus Torvalds */ 1763d3c8b52SJie Liu if (resp->tr_logres > 0) { 1779006fb91SChristoph Hellwig bool permanent = false; 1789006fb91SChristoph Hellwig 1793d3c8b52SJie Liu ASSERT(tp->t_log_res == 0 || 1803d3c8b52SJie Liu tp->t_log_res == resp->tr_logres); 1813d3c8b52SJie Liu ASSERT(tp->t_log_count == 0 || 1823d3c8b52SJie Liu tp->t_log_count == resp->tr_logcount); 1839006fb91SChristoph Hellwig 1843d3c8b52SJie Liu if (resp->tr_logflags & XFS_TRANS_PERM_LOG_RES) { 1851da177e4SLinus Torvalds tp->t_flags |= XFS_TRANS_PERM_LOG_RES; 1869006fb91SChristoph Hellwig permanent = true; 1871da177e4SLinus Torvalds } else { 1881da177e4SLinus Torvalds ASSERT(tp->t_ticket == NULL); 1891da177e4SLinus Torvalds ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES)); 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds 1929006fb91SChristoph Hellwig if (tp->t_ticket != NULL) { 1933d3c8b52SJie Liu ASSERT(resp->tr_logflags & XFS_TRANS_PERM_LOG_RES); 194dd401770SDave Chinner error = xfs_log_regrant(mp, tp->t_ticket); 1959006fb91SChristoph Hellwig } else { 196dd401770SDave Chinner error = xfs_log_reserve(mp, 1973d3c8b52SJie Liu resp->tr_logres, 1983d3c8b52SJie Liu resp->tr_logcount, 1993d3c8b52SJie Liu &tp->t_ticket, XFS_TRANSACTION, 200710b1e2cSChristoph Hellwig permanent); 2011da177e4SLinus Torvalds } 2029006fb91SChristoph Hellwig 2039006fb91SChristoph Hellwig if (error) 2049006fb91SChristoph Hellwig goto undo_blocks; 2059006fb91SChristoph Hellwig 2063d3c8b52SJie Liu tp->t_log_res = resp->tr_logres; 2073d3c8b52SJie Liu tp->t_log_count = resp->tr_logcount; 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds /* 2111da177e4SLinus Torvalds * Attempt to reserve the needed realtime extents by decrementing 2121da177e4SLinus Torvalds * the number needed from the number available. This will 2131da177e4SLinus Torvalds * fail if the count would go below zero. 2141da177e4SLinus Torvalds */ 2151da177e4SLinus Torvalds if (rtextents > 0) { 216dd401770SDave Chinner error = xfs_mod_frextents(mp, -((int64_t)rtextents)); 2171da177e4SLinus Torvalds if (error) { 2182451337dSDave Chinner error = -ENOSPC; 2191da177e4SLinus Torvalds goto undo_log; 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds tp->t_rtx_res += rtextents; 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds return 0; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds /* 2271da177e4SLinus Torvalds * Error cases jump to one of these labels to undo any 2281da177e4SLinus Torvalds * reservations which have already been performed. 2291da177e4SLinus Torvalds */ 2301da177e4SLinus Torvalds undo_log: 2313d3c8b52SJie Liu if (resp->tr_logres > 0) { 2328b41e3f9SChristoph Hellwig xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket); 2331da177e4SLinus Torvalds tp->t_ticket = NULL; 2341da177e4SLinus Torvalds tp->t_log_res = 0; 2351da177e4SLinus Torvalds tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES; 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds undo_blocks: 2391da177e4SLinus Torvalds if (blocks > 0) { 240dd401770SDave Chinner xfs_mod_fdblocks(mp, (int64_t)blocks, rsvd); 2411da177e4SLinus Torvalds tp->t_blk_res = 0; 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 2449070733bSMichal Hocko current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); 2451da177e4SLinus Torvalds 24659c1b082SNathan Scott return error; 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds 249253f4911SChristoph Hellwig int 250253f4911SChristoph Hellwig xfs_trans_alloc( 251253f4911SChristoph Hellwig struct xfs_mount *mp, 252253f4911SChristoph Hellwig struct xfs_trans_res *resp, 253253f4911SChristoph Hellwig uint blocks, 254253f4911SChristoph Hellwig uint rtextents, 255253f4911SChristoph Hellwig uint flags, 256253f4911SChristoph Hellwig struct xfs_trans **tpp) 257253f4911SChristoph Hellwig { 258253f4911SChristoph Hellwig struct xfs_trans *tp; 259253f4911SChristoph Hellwig int error; 260253f4911SChristoph Hellwig 2618683edb7SDave Chinner /* 2628683edb7SDave Chinner * Allocate the handle before we do our freeze accounting and setting up 2638683edb7SDave Chinner * GFP_NOFS allocation context so that we avoid lockdep false positives 2648683edb7SDave Chinner * by doing GFP_KERNEL allocations inside sb_start_intwrite(). 2658683edb7SDave Chinner */ 266*32a2b11fSCarlos Maiolino tp = kmem_cache_zalloc(xfs_trans_zone, GFP_KERNEL | __GFP_NOFAIL); 267253f4911SChristoph Hellwig if (!(flags & XFS_TRANS_NO_WRITECOUNT)) 268253f4911SChristoph Hellwig sb_start_intwrite(mp->m_super); 269253f4911SChristoph Hellwig 27010ee2526SDarrick J. Wong /* 27110ee2526SDarrick J. Wong * Zero-reservation ("empty") transactions can't modify anything, so 27210ee2526SDarrick J. Wong * they're allowed to run while we're frozen. 27310ee2526SDarrick J. Wong */ 27410ee2526SDarrick J. Wong WARN_ON(resp->tr_logres > 0 && 27510ee2526SDarrick J. Wong mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE); 276f74681baSBrian Foster ASSERT(!(flags & XFS_TRANS_RES_FDBLKS) || 277f74681baSBrian Foster xfs_sb_version_haslazysbcount(&mp->m_sb)); 278253f4911SChristoph Hellwig 279253f4911SChristoph Hellwig tp->t_magic = XFS_TRANS_HEADER_MAGIC; 280253f4911SChristoph Hellwig tp->t_flags = flags; 281253f4911SChristoph Hellwig tp->t_mountp = mp; 282253f4911SChristoph Hellwig INIT_LIST_HEAD(&tp->t_items); 283253f4911SChristoph Hellwig INIT_LIST_HEAD(&tp->t_busy); 2849d9e6233SBrian Foster INIT_LIST_HEAD(&tp->t_dfops); 285bba59c5eSBrian Foster tp->t_firstblock = NULLFSBLOCK; 286253f4911SChristoph Hellwig 287253f4911SChristoph Hellwig error = xfs_trans_reserve(tp, resp, blocks, rtextents); 288253f4911SChristoph Hellwig if (error) { 289253f4911SChristoph Hellwig xfs_trans_cancel(tp); 290253f4911SChristoph Hellwig return error; 291253f4911SChristoph Hellwig } 292253f4911SChristoph Hellwig 293ba18781bSDave Chinner trace_xfs_trans_alloc(tp, _RET_IP_); 294ba18781bSDave Chinner 295253f4911SChristoph Hellwig *tpp = tp; 296253f4911SChristoph Hellwig return 0; 297253f4911SChristoph Hellwig } 298253f4911SChristoph Hellwig 2991da177e4SLinus Torvalds /* 300e89c0413SDarrick J. Wong * Create an empty transaction with no reservation. This is a defensive 301b41b46c2SDave Chinner * mechanism for routines that query metadata without actually modifying them -- 302b41b46c2SDave Chinner * if the metadata being queried is somehow cross-linked (think a btree block 303b41b46c2SDave Chinner * pointer that points higher in the tree), we risk deadlock. However, blocks 304b41b46c2SDave Chinner * grabbed as part of a transaction can be re-grabbed. The verifiers will 305b41b46c2SDave Chinner * notice the corrupt block and the operation will fail back to userspace 306b41b46c2SDave Chinner * without deadlocking. 307e89c0413SDarrick J. Wong * 308b41b46c2SDave Chinner * Note the zero-length reservation; this transaction MUST be cancelled without 309b41b46c2SDave Chinner * any dirty data. 31027fb5a72SDarrick J. Wong * 311b41b46c2SDave Chinner * Callers should obtain freeze protection to avoid a conflict with fs freezing 312b41b46c2SDave Chinner * where we can be grabbing buffers at the same time that freeze is trying to 313b41b46c2SDave Chinner * drain the buffer LRU list. 314e89c0413SDarrick J. Wong */ 315e89c0413SDarrick J. Wong int 316e89c0413SDarrick J. Wong xfs_trans_alloc_empty( 317e89c0413SDarrick J. Wong struct xfs_mount *mp, 318e89c0413SDarrick J. Wong struct xfs_trans **tpp) 319e89c0413SDarrick J. Wong { 320e89c0413SDarrick J. Wong struct xfs_trans_res resv = {0}; 321e89c0413SDarrick J. Wong 322e89c0413SDarrick J. Wong return xfs_trans_alloc(mp, &resv, 0, 0, XFS_TRANS_NO_WRITECOUNT, tpp); 323e89c0413SDarrick J. Wong } 324e89c0413SDarrick J. Wong 325e89c0413SDarrick J. Wong /* 3261da177e4SLinus Torvalds * Record the indicated change to the given field for application 3271da177e4SLinus Torvalds * to the file system's superblock when the transaction commits. 3281da177e4SLinus Torvalds * For now, just store the change in the transaction structure. 3291da177e4SLinus Torvalds * 3301da177e4SLinus Torvalds * Mark the transaction structure to indicate that the superblock 3311da177e4SLinus Torvalds * needs to be updated before committing. 33292821e2bSDavid Chinner * 33392821e2bSDavid Chinner * Because we may not be keeping track of allocated/free inodes and 33492821e2bSDavid Chinner * used filesystem blocks in the superblock, we do not mark the 33592821e2bSDavid Chinner * superblock dirty in this transaction if we modify these fields. 33692821e2bSDavid Chinner * We still need to update the transaction deltas so that they get 33792821e2bSDavid Chinner * applied to the incore superblock, but we don't want them to 33892821e2bSDavid Chinner * cause the superblock to get locked and logged if these are the 33992821e2bSDavid Chinner * only fields in the superblock that the transaction modifies. 3401da177e4SLinus Torvalds */ 3411da177e4SLinus Torvalds void 3421da177e4SLinus Torvalds xfs_trans_mod_sb( 3431da177e4SLinus Torvalds xfs_trans_t *tp, 3441da177e4SLinus Torvalds uint field, 34520f4ebf2SDavid Chinner int64_t delta) 3461da177e4SLinus Torvalds { 34792821e2bSDavid Chinner uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY); 34892821e2bSDavid Chinner xfs_mount_t *mp = tp->t_mountp; 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds switch (field) { 3511da177e4SLinus Torvalds case XFS_TRANS_SB_ICOUNT: 3521da177e4SLinus Torvalds tp->t_icount_delta += delta; 35392821e2bSDavid Chinner if (xfs_sb_version_haslazysbcount(&mp->m_sb)) 35492821e2bSDavid Chinner flags &= ~XFS_TRANS_SB_DIRTY; 3551da177e4SLinus Torvalds break; 3561da177e4SLinus Torvalds case XFS_TRANS_SB_IFREE: 3571da177e4SLinus Torvalds tp->t_ifree_delta += delta; 35892821e2bSDavid Chinner if (xfs_sb_version_haslazysbcount(&mp->m_sb)) 35992821e2bSDavid Chinner flags &= ~XFS_TRANS_SB_DIRTY; 3601da177e4SLinus Torvalds break; 3611da177e4SLinus Torvalds case XFS_TRANS_SB_FDBLOCKS: 3621da177e4SLinus Torvalds /* 3633e78b9a4SBrian Foster * Track the number of blocks allocated in the transaction. 3643e78b9a4SBrian Foster * Make sure it does not exceed the number reserved. If so, 3653e78b9a4SBrian Foster * shutdown as this can lead to accounting inconsistency. 3661da177e4SLinus Torvalds */ 3671da177e4SLinus Torvalds if (delta < 0) { 3681da177e4SLinus Torvalds tp->t_blk_res_used += (uint)-delta; 3693e78b9a4SBrian Foster if (tp->t_blk_res_used > tp->t_blk_res) 3703e78b9a4SBrian Foster xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 371f74681baSBrian Foster } else if (delta > 0 && (tp->t_flags & XFS_TRANS_RES_FDBLKS)) { 372f74681baSBrian Foster int64_t blkres_delta; 373f74681baSBrian Foster 374f74681baSBrian Foster /* 375f74681baSBrian Foster * Return freed blocks directly to the reservation 376f74681baSBrian Foster * instead of the global pool, being careful not to 377f74681baSBrian Foster * overflow the trans counter. This is used to preserve 378f74681baSBrian Foster * reservation across chains of transaction rolls that 379f74681baSBrian Foster * repeatedly free and allocate blocks. 380f74681baSBrian Foster */ 381f74681baSBrian Foster blkres_delta = min_t(int64_t, delta, 382f74681baSBrian Foster UINT_MAX - tp->t_blk_res); 383f74681baSBrian Foster tp->t_blk_res += blkres_delta; 384f74681baSBrian Foster delta -= blkres_delta; 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds tp->t_fdblocks_delta += delta; 38792821e2bSDavid Chinner if (xfs_sb_version_haslazysbcount(&mp->m_sb)) 38892821e2bSDavid Chinner flags &= ~XFS_TRANS_SB_DIRTY; 3891da177e4SLinus Torvalds break; 3901da177e4SLinus Torvalds case XFS_TRANS_SB_RES_FDBLOCKS: 3911da177e4SLinus Torvalds /* 3921da177e4SLinus Torvalds * The allocation has already been applied to the 3931da177e4SLinus Torvalds * in-core superblock's counter. This should only 3941da177e4SLinus Torvalds * be applied to the on-disk superblock. 3951da177e4SLinus Torvalds */ 3961da177e4SLinus Torvalds tp->t_res_fdblocks_delta += delta; 39792821e2bSDavid Chinner if (xfs_sb_version_haslazysbcount(&mp->m_sb)) 39892821e2bSDavid Chinner flags &= ~XFS_TRANS_SB_DIRTY; 3991da177e4SLinus Torvalds break; 4001da177e4SLinus Torvalds case XFS_TRANS_SB_FREXTENTS: 4011da177e4SLinus Torvalds /* 4021da177e4SLinus Torvalds * Track the number of blocks allocated in the 4031da177e4SLinus Torvalds * transaction. Make sure it does not exceed the 4041da177e4SLinus Torvalds * number reserved. 4051da177e4SLinus Torvalds */ 4061da177e4SLinus Torvalds if (delta < 0) { 4071da177e4SLinus Torvalds tp->t_rtx_res_used += (uint)-delta; 4081da177e4SLinus Torvalds ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res); 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds tp->t_frextents_delta += delta; 4111da177e4SLinus Torvalds break; 4121da177e4SLinus Torvalds case XFS_TRANS_SB_RES_FREXTENTS: 4131da177e4SLinus Torvalds /* 4141da177e4SLinus Torvalds * The allocation has already been applied to the 415c41564b5SNathan Scott * in-core superblock's counter. This should only 4161da177e4SLinus Torvalds * be applied to the on-disk superblock. 4171da177e4SLinus Torvalds */ 4181da177e4SLinus Torvalds ASSERT(delta < 0); 4191da177e4SLinus Torvalds tp->t_res_frextents_delta += delta; 4201da177e4SLinus Torvalds break; 4211da177e4SLinus Torvalds case XFS_TRANS_SB_DBLOCKS: 4221da177e4SLinus Torvalds ASSERT(delta > 0); 4231da177e4SLinus Torvalds tp->t_dblocks_delta += delta; 4241da177e4SLinus Torvalds break; 4251da177e4SLinus Torvalds case XFS_TRANS_SB_AGCOUNT: 4261da177e4SLinus Torvalds ASSERT(delta > 0); 4271da177e4SLinus Torvalds tp->t_agcount_delta += delta; 4281da177e4SLinus Torvalds break; 4291da177e4SLinus Torvalds case XFS_TRANS_SB_IMAXPCT: 4301da177e4SLinus Torvalds tp->t_imaxpct_delta += delta; 4311da177e4SLinus Torvalds break; 4321da177e4SLinus Torvalds case XFS_TRANS_SB_REXTSIZE: 4331da177e4SLinus Torvalds tp->t_rextsize_delta += delta; 4341da177e4SLinus Torvalds break; 4351da177e4SLinus Torvalds case XFS_TRANS_SB_RBMBLOCKS: 4361da177e4SLinus Torvalds tp->t_rbmblocks_delta += delta; 4371da177e4SLinus Torvalds break; 4381da177e4SLinus Torvalds case XFS_TRANS_SB_RBLOCKS: 4391da177e4SLinus Torvalds tp->t_rblocks_delta += delta; 4401da177e4SLinus Torvalds break; 4411da177e4SLinus Torvalds case XFS_TRANS_SB_REXTENTS: 4421da177e4SLinus Torvalds tp->t_rextents_delta += delta; 4431da177e4SLinus Torvalds break; 4441da177e4SLinus Torvalds case XFS_TRANS_SB_REXTSLOG: 4451da177e4SLinus Torvalds tp->t_rextslog_delta += delta; 4461da177e4SLinus Torvalds break; 4471da177e4SLinus Torvalds default: 4481da177e4SLinus Torvalds ASSERT(0); 4491da177e4SLinus Torvalds return; 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds 452210c6f1cSDavid Chinner tp->t_flags |= flags; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds /* 4561da177e4SLinus Torvalds * xfs_trans_apply_sb_deltas() is called from the commit code 4571da177e4SLinus Torvalds * to bring the superblock buffer into the current transaction 4581da177e4SLinus Torvalds * and modify it as requested by earlier calls to xfs_trans_mod_sb(). 4591da177e4SLinus Torvalds * 4601da177e4SLinus Torvalds * For now we just look at each field allowed to change and change 4611da177e4SLinus Torvalds * it if necessary. 4621da177e4SLinus Torvalds */ 4631da177e4SLinus Torvalds STATIC void 4641da177e4SLinus Torvalds xfs_trans_apply_sb_deltas( 4651da177e4SLinus Torvalds xfs_trans_t *tp) 4661da177e4SLinus Torvalds { 4672bdf7cd0SChristoph Hellwig xfs_dsb_t *sbp; 4681da177e4SLinus Torvalds xfs_buf_t *bp; 4691da177e4SLinus Torvalds int whole = 0; 4701da177e4SLinus Torvalds 4718c9ce2f7SEric Sandeen bp = xfs_trans_getsb(tp, tp->t_mountp); 4723e6e8afdSChristoph Hellwig sbp = bp->b_addr; 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds /* 4751da177e4SLinus Torvalds * Check that superblock mods match the mods made to AGF counters. 4761da177e4SLinus Torvalds */ 4771da177e4SLinus Torvalds ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) == 4781da177e4SLinus Torvalds (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta + 4791da177e4SLinus Torvalds tp->t_ag_btree_delta)); 4801da177e4SLinus Torvalds 48192821e2bSDavid Chinner /* 48292821e2bSDavid Chinner * Only update the superblock counters if we are logging them 48392821e2bSDavid Chinner */ 48492821e2bSDavid Chinner if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) { 4852bdf7cd0SChristoph Hellwig if (tp->t_icount_delta) 486413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta); 4872bdf7cd0SChristoph Hellwig if (tp->t_ifree_delta) 488413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta); 4892bdf7cd0SChristoph Hellwig if (tp->t_fdblocks_delta) 490413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta); 4912bdf7cd0SChristoph Hellwig if (tp->t_res_fdblocks_delta) 492413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta); 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds 4952bdf7cd0SChristoph Hellwig if (tp->t_frextents_delta) 496413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta); 4972bdf7cd0SChristoph Hellwig if (tp->t_res_frextents_delta) 498413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta); 4991da177e4SLinus Torvalds 5002bdf7cd0SChristoph Hellwig if (tp->t_dblocks_delta) { 501413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta); 5021da177e4SLinus Torvalds whole = 1; 5031da177e4SLinus Torvalds } 5042bdf7cd0SChristoph Hellwig if (tp->t_agcount_delta) { 505413d57c9SMarcin Slusarz be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta); 5061da177e4SLinus Torvalds whole = 1; 5071da177e4SLinus Torvalds } 5082bdf7cd0SChristoph Hellwig if (tp->t_imaxpct_delta) { 5092bdf7cd0SChristoph Hellwig sbp->sb_imax_pct += tp->t_imaxpct_delta; 5101da177e4SLinus Torvalds whole = 1; 5111da177e4SLinus Torvalds } 5122bdf7cd0SChristoph Hellwig if (tp->t_rextsize_delta) { 513413d57c9SMarcin Slusarz be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta); 5141da177e4SLinus Torvalds whole = 1; 5151da177e4SLinus Torvalds } 5162bdf7cd0SChristoph Hellwig if (tp->t_rbmblocks_delta) { 517413d57c9SMarcin Slusarz be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta); 5181da177e4SLinus Torvalds whole = 1; 5191da177e4SLinus Torvalds } 5202bdf7cd0SChristoph Hellwig if (tp->t_rblocks_delta) { 521413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta); 5221da177e4SLinus Torvalds whole = 1; 5231da177e4SLinus Torvalds } 5242bdf7cd0SChristoph Hellwig if (tp->t_rextents_delta) { 525413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta); 5261da177e4SLinus Torvalds whole = 1; 5271da177e4SLinus Torvalds } 5282bdf7cd0SChristoph Hellwig if (tp->t_rextslog_delta) { 5292bdf7cd0SChristoph Hellwig sbp->sb_rextslog += tp->t_rextslog_delta; 5301da177e4SLinus Torvalds whole = 1; 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds 5333443a3bcSDave Chinner xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF); 5341da177e4SLinus Torvalds if (whole) 5351da177e4SLinus Torvalds /* 536c41564b5SNathan Scott * Log the whole thing, the fields are noncontiguous. 5371da177e4SLinus Torvalds */ 5382bdf7cd0SChristoph Hellwig xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1); 5391da177e4SLinus Torvalds else 5401da177e4SLinus Torvalds /* 5411da177e4SLinus Torvalds * Since all the modifiable fields are contiguous, we 5421da177e4SLinus Torvalds * can get away with this. 5431da177e4SLinus Torvalds */ 5442bdf7cd0SChristoph Hellwig xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount), 5452bdf7cd0SChristoph Hellwig offsetof(xfs_dsb_t, sb_frextents) + 5461da177e4SLinus Torvalds sizeof(sbp->sb_frextents) - 1); 5471da177e4SLinus Torvalds } 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds /* 550dc3ffbb1SDave Chinner * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations and 551dc3ffbb1SDave Chinner * apply superblock counter changes to the in-core superblock. The 55245c34141SDavid Chinner * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT 55345c34141SDavid Chinner * applied to the in-core superblock. The idea is that that has already been 55445c34141SDavid Chinner * done. 5551da177e4SLinus Torvalds * 55645c34141SDavid Chinner * If we are not logging superblock counters, then the inode allocated/free and 55745c34141SDavid Chinner * used block counts are not updated in the on disk superblock. In this case, 55845c34141SDavid Chinner * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we 55945c34141SDavid Chinner * still need to update the incore superblock with the changes. 560f18c9a90SDave Chinner * 561f18c9a90SDave Chinner * Deltas for the inode count are +/-64, hence we use a large batch size of 128 562f18c9a90SDave Chinner * so we don't need to take the counter lock on every update. 5631da177e4SLinus Torvalds */ 564f18c9a90SDave Chinner #define XFS_ICOUNT_BATCH 128 565f18c9a90SDave Chinner 56671e330b5SDave Chinner void 5671da177e4SLinus Torvalds xfs_trans_unreserve_and_mod_sb( 5680bd5ddedSDave Chinner struct xfs_trans *tp) 5691da177e4SLinus Torvalds { 5700bd5ddedSDave Chinner struct xfs_mount *mp = tp->t_mountp; 5710d485adaSDave Chinner bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0; 57245c34141SDavid Chinner int64_t blkdelta = 0; 57345c34141SDavid Chinner int64_t rtxdelta = 0; 5741b040712SChristoph Hellwig int64_t idelta = 0; 5751b040712SChristoph Hellwig int64_t ifreedelta = 0; 5760bd5ddedSDave Chinner int error; 5771da177e4SLinus Torvalds 5781b040712SChristoph Hellwig /* calculate deltas */ 57945c34141SDavid Chinner if (tp->t_blk_res > 0) 58045c34141SDavid Chinner blkdelta = tp->t_blk_res; 58145c34141SDavid Chinner if ((tp->t_fdblocks_delta != 0) && 58245c34141SDavid Chinner (xfs_sb_version_haslazysbcount(&mp->m_sb) || 58345c34141SDavid Chinner (tp->t_flags & XFS_TRANS_SB_DIRTY))) 58445c34141SDavid Chinner blkdelta += tp->t_fdblocks_delta; 58545c34141SDavid Chinner 58645c34141SDavid Chinner if (tp->t_rtx_res > 0) 58745c34141SDavid Chinner rtxdelta = tp->t_rtx_res; 58845c34141SDavid Chinner if ((tp->t_frextents_delta != 0) && 58945c34141SDavid Chinner (tp->t_flags & XFS_TRANS_SB_DIRTY)) 59045c34141SDavid Chinner rtxdelta += tp->t_frextents_delta; 59145c34141SDavid Chinner 5921b040712SChristoph Hellwig if (xfs_sb_version_haslazysbcount(&mp->m_sb) || 5931b040712SChristoph Hellwig (tp->t_flags & XFS_TRANS_SB_DIRTY)) { 5941b040712SChristoph Hellwig idelta = tp->t_icount_delta; 5951b040712SChristoph Hellwig ifreedelta = tp->t_ifree_delta; 5961b040712SChristoph Hellwig } 5971b040712SChristoph Hellwig 5981b040712SChristoph Hellwig /* apply the per-cpu counters */ 5991b040712SChristoph Hellwig if (blkdelta) { 6000d485adaSDave Chinner error = xfs_mod_fdblocks(mp, blkdelta, rsvd); 601dc3ffbb1SDave Chinner ASSERT(!error); 6021b040712SChristoph Hellwig } 6031b040712SChristoph Hellwig 6041b040712SChristoph Hellwig if (idelta) { 605f18c9a90SDave Chinner percpu_counter_add_batch(&mp->m_icount, idelta, 606f18c9a90SDave Chinner XFS_ICOUNT_BATCH); 607f18c9a90SDave Chinner if (idelta < 0) 608f18c9a90SDave Chinner ASSERT(__percpu_counter_compare(&mp->m_icount, 0, 609f18c9a90SDave Chinner XFS_ICOUNT_BATCH) >= 0); 6101b040712SChristoph Hellwig } 6111b040712SChristoph Hellwig 6121b040712SChristoph Hellwig if (ifreedelta) { 613f18c9a90SDave Chinner percpu_counter_add(&mp->m_ifree, ifreedelta); 614f18c9a90SDave Chinner if (ifreedelta < 0) 615f18c9a90SDave Chinner ASSERT(percpu_counter_compare(&mp->m_ifree, 0) >= 0); 6161b040712SChristoph Hellwig } 6171b040712SChristoph Hellwig 6180bd5ddedSDave Chinner if (rtxdelta == 0 && !(tp->t_flags & XFS_TRANS_SB_DIRTY)) 6190bd5ddedSDave Chinner return; 6200bd5ddedSDave Chinner 6211b040712SChristoph Hellwig /* apply remaining deltas */ 6220bd5ddedSDave Chinner spin_lock(&mp->m_sb_lock); 623dc3ffbb1SDave Chinner mp->m_sb.sb_frextents += rtxdelta; 624dc3ffbb1SDave Chinner mp->m_sb.sb_dblocks += tp->t_dblocks_delta; 625dc3ffbb1SDave Chinner mp->m_sb.sb_agcount += tp->t_agcount_delta; 626dc3ffbb1SDave Chinner mp->m_sb.sb_imax_pct += tp->t_imaxpct_delta; 627dc3ffbb1SDave Chinner mp->m_sb.sb_rextsize += tp->t_rextsize_delta; 628dc3ffbb1SDave Chinner mp->m_sb.sb_rbmblocks += tp->t_rbmblocks_delta; 629dc3ffbb1SDave Chinner mp->m_sb.sb_rblocks += tp->t_rblocks_delta; 630dc3ffbb1SDave Chinner mp->m_sb.sb_rextents += tp->t_rextents_delta; 631dc3ffbb1SDave Chinner mp->m_sb.sb_rextslog += tp->t_rextslog_delta; 6320bd5ddedSDave Chinner spin_unlock(&mp->m_sb_lock); 6331b040712SChristoph Hellwig 634dc3ffbb1SDave Chinner /* 635dc3ffbb1SDave Chinner * Debug checks outside of the spinlock so they don't lock up the 636dc3ffbb1SDave Chinner * machine if they fail. 637dc3ffbb1SDave Chinner */ 638dc3ffbb1SDave Chinner ASSERT(mp->m_sb.sb_imax_pct >= 0); 639dc3ffbb1SDave Chinner ASSERT(mp->m_sb.sb_rextslog >= 0); 6401b040712SChristoph Hellwig return; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds 643e6631f85SDave Chinner /* Add the given log item to the transaction's list of log items. */ 644e98c414fSChristoph Hellwig void 645e98c414fSChristoph Hellwig xfs_trans_add_item( 646e98c414fSChristoph Hellwig struct xfs_trans *tp, 647e98c414fSChristoph Hellwig struct xfs_log_item *lip) 648e98c414fSChristoph Hellwig { 649f65020a8SJesper Juhl ASSERT(lip->li_mountp == tp->t_mountp); 650f65020a8SJesper Juhl ASSERT(lip->li_ailp == tp->t_mountp->m_ail); 651e6631f85SDave Chinner ASSERT(list_empty(&lip->li_trans)); 652e6631f85SDave Chinner ASSERT(!test_bit(XFS_LI_DIRTY, &lip->li_flags)); 653e98c414fSChristoph Hellwig 654e6631f85SDave Chinner list_add_tail(&lip->li_trans, &tp->t_items); 655ba18781bSDave Chinner trace_xfs_trans_add_item(tp, _RET_IP_); 656e98c414fSChristoph Hellwig } 657e98c414fSChristoph Hellwig 658e98c414fSChristoph Hellwig /* 659e6631f85SDave Chinner * Unlink the log item from the transaction. the log item is no longer 660e6631f85SDave Chinner * considered dirty in this transaction, as the linked transaction has 661e6631f85SDave Chinner * finished, either by abort or commit completion. 662e98c414fSChristoph Hellwig */ 663e98c414fSChristoph Hellwig void 664e98c414fSChristoph Hellwig xfs_trans_del_item( 665e98c414fSChristoph Hellwig struct xfs_log_item *lip) 666e98c414fSChristoph Hellwig { 667e6631f85SDave Chinner clear_bit(XFS_LI_DIRTY, &lip->li_flags); 668e6631f85SDave Chinner list_del_init(&lip->li_trans); 669e98c414fSChristoph Hellwig } 670e98c414fSChristoph Hellwig 671e6631f85SDave Chinner /* Detach and unlock all of the items in a transaction */ 672195cd83dSChristoph Hellwig static void 673e98c414fSChristoph Hellwig xfs_trans_free_items( 674e98c414fSChristoph Hellwig struct xfs_trans *tp, 675eacb24e7SChristoph Hellwig bool abort) 676e98c414fSChristoph Hellwig { 677e6631f85SDave Chinner struct xfs_log_item *lip, *next; 678e98c414fSChristoph Hellwig 679ba18781bSDave Chinner trace_xfs_trans_free_items(tp, _RET_IP_); 680ba18781bSDave Chinner 681e6631f85SDave Chinner list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) { 682e6631f85SDave Chinner xfs_trans_del_item(lip); 683eacb24e7SChristoph Hellwig if (abort) 68422525c17SDave Chinner set_bit(XFS_LI_ABORTED, &lip->li_flags); 685ddf92053SChristoph Hellwig if (lip->li_ops->iop_release) 686ddf92053SChristoph Hellwig lip->li_ops->iop_release(lip); 687e98c414fSChristoph Hellwig } 688e98c414fSChristoph Hellwig } 689e98c414fSChristoph Hellwig 6900e57f6a3SDave Chinner static inline void 6910e57f6a3SDave Chinner xfs_log_item_batch_insert( 6920e57f6a3SDave Chinner struct xfs_ail *ailp, 6931d8c95a3SDave Chinner struct xfs_ail_cursor *cur, 6940e57f6a3SDave Chinner struct xfs_log_item **log_items, 6950e57f6a3SDave Chinner int nr_items, 6960e57f6a3SDave Chinner xfs_lsn_t commit_lsn) 6970e57f6a3SDave Chinner { 6980e57f6a3SDave Chinner int i; 6990e57f6a3SDave Chinner 70057e80956SMatthew Wilcox spin_lock(&ailp->ail_lock); 70157e80956SMatthew Wilcox /* xfs_trans_ail_update_bulk drops ailp->ail_lock */ 7021d8c95a3SDave Chinner xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn); 7030e57f6a3SDave Chinner 704904c17e6SDave Chinner for (i = 0; i < nr_items; i++) { 705904c17e6SDave Chinner struct xfs_log_item *lip = log_items[i]; 706904c17e6SDave Chinner 707e8b78db7SChristoph Hellwig if (lip->li_ops->iop_unpin) 708904c17e6SDave Chinner lip->li_ops->iop_unpin(lip, 0); 709904c17e6SDave Chinner } 7100e57f6a3SDave Chinner } 7110e57f6a3SDave Chinner 7120e57f6a3SDave Chinner /* 7130e57f6a3SDave Chinner * Bulk operation version of xfs_trans_committed that takes a log vector of 7140e57f6a3SDave Chinner * items to insert into the AIL. This uses bulk AIL insertion techniques to 7150e57f6a3SDave Chinner * minimise lock traffic. 716e34a314cSDave Chinner * 717e34a314cSDave Chinner * If we are called with the aborted flag set, it is because a log write during 718e34a314cSDave Chinner * a CIL checkpoint commit has failed. In this case, all the items in the 719ddf92053SChristoph Hellwig * checkpoint have already gone through iop_committed and iop_committing, which 720e34a314cSDave Chinner * means that checkpoint commit abort handling is treated exactly the same 721e34a314cSDave Chinner * as an iclog write error even though we haven't started any IO yet. Hence in 722904c17e6SDave Chinner * this case all we need to do is iop_committed processing, followed by an 723904c17e6SDave Chinner * iop_unpin(aborted) call. 7241d8c95a3SDave Chinner * 7251d8c95a3SDave Chinner * The AIL cursor is used to optimise the insert process. If commit_lsn is not 7261d8c95a3SDave Chinner * at the end of the AIL, the insert cursor avoids the need to walk 7271d8c95a3SDave Chinner * the AIL to find the insertion point on every xfs_log_item_batch_insert() 7281d8c95a3SDave Chinner * call. This saves a lot of needless list walking and is a net win, even 7291d8c95a3SDave Chinner * though it slightly increases that amount of AIL lock traffic to set it up 7301d8c95a3SDave Chinner * and tear it down. 7310e57f6a3SDave Chinner */ 7320e57f6a3SDave Chinner void 7330e57f6a3SDave Chinner xfs_trans_committed_bulk( 7340e57f6a3SDave Chinner struct xfs_ail *ailp, 7350e57f6a3SDave Chinner struct xfs_log_vec *log_vector, 7360e57f6a3SDave Chinner xfs_lsn_t commit_lsn, 737d15cbf2fSChristoph Hellwig bool aborted) 7380e57f6a3SDave Chinner { 7390e57f6a3SDave Chinner #define LOG_ITEM_BATCH_SIZE 32 7400e57f6a3SDave Chinner struct xfs_log_item *log_items[LOG_ITEM_BATCH_SIZE]; 7410e57f6a3SDave Chinner struct xfs_log_vec *lv; 7421d8c95a3SDave Chinner struct xfs_ail_cursor cur; 7430e57f6a3SDave Chinner int i = 0; 7440e57f6a3SDave Chinner 74557e80956SMatthew Wilcox spin_lock(&ailp->ail_lock); 7461d8c95a3SDave Chinner xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn); 74757e80956SMatthew Wilcox spin_unlock(&ailp->ail_lock); 7481d8c95a3SDave Chinner 7490e57f6a3SDave Chinner /* unpin all the log items */ 7500e57f6a3SDave Chinner for (lv = log_vector; lv; lv = lv->lv_next ) { 7510e57f6a3SDave Chinner struct xfs_log_item *lip = lv->lv_item; 7520e57f6a3SDave Chinner xfs_lsn_t item_lsn; 7530e57f6a3SDave Chinner 7540e57f6a3SDave Chinner if (aborted) 75522525c17SDave Chinner set_bit(XFS_LI_ABORTED, &lip->li_flags); 7569ce632a2SChristoph Hellwig 7579ce632a2SChristoph Hellwig if (lip->li_ops->flags & XFS_ITEM_RELEASE_WHEN_COMMITTED) { 7589ce632a2SChristoph Hellwig lip->li_ops->iop_release(lip); 7599ce632a2SChristoph Hellwig continue; 7609ce632a2SChristoph Hellwig } 7619ce632a2SChristoph Hellwig 762e8b78db7SChristoph Hellwig if (lip->li_ops->iop_committed) 763904c17e6SDave Chinner item_lsn = lip->li_ops->iop_committed(lip, commit_lsn); 764e8b78db7SChristoph Hellwig else 765e8b78db7SChristoph Hellwig item_lsn = commit_lsn; 7660e57f6a3SDave Chinner 7671316d4daSDave Chinner /* item_lsn of -1 means the item needs no further processing */ 7680e57f6a3SDave Chinner if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0) 7690e57f6a3SDave Chinner continue; 7700e57f6a3SDave Chinner 771e34a314cSDave Chinner /* 772e34a314cSDave Chinner * if we are aborting the operation, no point in inserting the 773e34a314cSDave Chinner * object into the AIL as we are in a shutdown situation. 774e34a314cSDave Chinner */ 775e34a314cSDave Chinner if (aborted) { 77657e80956SMatthew Wilcox ASSERT(XFS_FORCED_SHUTDOWN(ailp->ail_mount)); 777e8b78db7SChristoph Hellwig if (lip->li_ops->iop_unpin) 778904c17e6SDave Chinner lip->li_ops->iop_unpin(lip, 1); 779e34a314cSDave Chinner continue; 780e34a314cSDave Chinner } 781e34a314cSDave Chinner 7820e57f6a3SDave Chinner if (item_lsn != commit_lsn) { 7830e57f6a3SDave Chinner 7840e57f6a3SDave Chinner /* 7850e57f6a3SDave Chinner * Not a bulk update option due to unusual item_lsn. 7860e57f6a3SDave Chinner * Push into AIL immediately, rechecking the lsn once 7871d8c95a3SDave Chinner * we have the ail lock. Then unpin the item. This does 7881d8c95a3SDave Chinner * not affect the AIL cursor the bulk insert path is 7891d8c95a3SDave Chinner * using. 7900e57f6a3SDave Chinner */ 79157e80956SMatthew Wilcox spin_lock(&ailp->ail_lock); 7920e57f6a3SDave Chinner if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) 7930e57f6a3SDave Chinner xfs_trans_ail_update(ailp, lip, item_lsn); 7940e57f6a3SDave Chinner else 79557e80956SMatthew Wilcox spin_unlock(&ailp->ail_lock); 796e8b78db7SChristoph Hellwig if (lip->li_ops->iop_unpin) 797904c17e6SDave Chinner lip->li_ops->iop_unpin(lip, 0); 7980e57f6a3SDave Chinner continue; 7990e57f6a3SDave Chinner } 8000e57f6a3SDave Chinner 8010e57f6a3SDave Chinner /* Item is a candidate for bulk AIL insert. */ 8020e57f6a3SDave Chinner log_items[i++] = lv->lv_item; 8030e57f6a3SDave Chinner if (i >= LOG_ITEM_BATCH_SIZE) { 8041d8c95a3SDave Chinner xfs_log_item_batch_insert(ailp, &cur, log_items, 8050e57f6a3SDave Chinner LOG_ITEM_BATCH_SIZE, commit_lsn); 8060e57f6a3SDave Chinner i = 0; 8070e57f6a3SDave Chinner } 8080e57f6a3SDave Chinner } 8090e57f6a3SDave Chinner 8100e57f6a3SDave Chinner /* make sure we insert the remainder! */ 8110e57f6a3SDave Chinner if (i) 8121d8c95a3SDave Chinner xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn); 8131d8c95a3SDave Chinner 81457e80956SMatthew Wilcox spin_lock(&ailp->ail_lock); 815e4a1e29cSEric Sandeen xfs_trans_ail_cursor_done(&cur); 81657e80956SMatthew Wilcox spin_unlock(&ailp->ail_lock); 8170e57f6a3SDave Chinner } 8180e57f6a3SDave Chinner 819b1c1b5b6SDave Chinner /* 820b1037058SChristoph Hellwig * Commit the given transaction to the log. 8210924378aSDave Chinner * 8220924378aSDave Chinner * XFS disk error handling mechanism is not based on a typical 8230924378aSDave Chinner * transaction abort mechanism. Logically after the filesystem 8240924378aSDave Chinner * gets marked 'SHUTDOWN', we can't let any new transactions 8250924378aSDave Chinner * be durable - ie. committed to disk - because some metadata might 8260924378aSDave Chinner * be inconsistent. In such cases, this returns an error, and the 8270924378aSDave Chinner * caller may assume that all locked objects joined to the transaction 8280924378aSDave Chinner * have already been unlocked as if the commit had succeeded. 8290924378aSDave Chinner * Do not reference the transaction structure after this call. 8300924378aSDave Chinner */ 83170393313SChristoph Hellwig static int 83270393313SChristoph Hellwig __xfs_trans_commit( 833a3ccd2caSChristoph Hellwig struct xfs_trans *tp, 83470393313SChristoph Hellwig bool regrant) 8350924378aSDave Chinner { 836a3ccd2caSChristoph Hellwig struct xfs_mount *mp = tp->t_mountp; 8370924378aSDave Chinner xfs_lsn_t commit_lsn = -1; 838a3ccd2caSChristoph Hellwig int error = 0; 8390924378aSDave Chinner int sync = tp->t_flags & XFS_TRANS_SYNC; 8400924378aSDave Chinner 841ba18781bSDave Chinner trace_xfs_trans_commit(tp, _RET_IP_); 842ba18781bSDave Chinner 84398719051SBrian Foster /* 84498719051SBrian Foster * Finish deferred items on final commit. Only permanent transactions 84598719051SBrian Foster * should ever have deferred ops. 84698719051SBrian Foster */ 8479d9e6233SBrian Foster WARN_ON_ONCE(!list_empty(&tp->t_dfops) && 84898719051SBrian Foster !(tp->t_flags & XFS_TRANS_PERM_LOG_RES)); 84998719051SBrian Foster if (!regrant && (tp->t_flags & XFS_TRANS_PERM_LOG_RES)) { 850b277c37fSBrian Foster error = xfs_defer_finish_noroll(&tp); 8519b1f4e98SBrian Foster if (error) 852e021a2e5SBrian Foster goto out_unreserve; 853e021a2e5SBrian Foster } 854e021a2e5SBrian Foster 8550924378aSDave Chinner /* 8560924378aSDave Chinner * If there is nothing to be logged by the transaction, 8570924378aSDave Chinner * then unlock all of the items associated with the 8580924378aSDave Chinner * transaction and free the transaction structure. 8590924378aSDave Chinner * Also make sure to return any reserved blocks to 8600924378aSDave Chinner * the free pool. 8610924378aSDave Chinner */ 862a3ccd2caSChristoph Hellwig if (!(tp->t_flags & XFS_TRANS_DIRTY)) 863a3ccd2caSChristoph Hellwig goto out_unreserve; 864a3ccd2caSChristoph Hellwig 865a3ccd2caSChristoph Hellwig if (XFS_FORCED_SHUTDOWN(mp)) { 8662451337dSDave Chinner error = -EIO; 867a3ccd2caSChristoph Hellwig goto out_unreserve; 8680924378aSDave Chinner } 869a3ccd2caSChristoph Hellwig 8700924378aSDave Chinner ASSERT(tp->t_ticket != NULL); 8710924378aSDave Chinner 8720924378aSDave Chinner /* 8730924378aSDave Chinner * If we need to update the superblock, then do it now. 8740924378aSDave Chinner */ 8750924378aSDave Chinner if (tp->t_flags & XFS_TRANS_SB_DIRTY) 8760924378aSDave Chinner xfs_trans_apply_sb_deltas(tp); 8770924378aSDave Chinner xfs_trans_apply_dquot_deltas(tp); 8780924378aSDave Chinner 87970393313SChristoph Hellwig xfs_log_commit_cil(mp, tp, &commit_lsn, regrant); 8801da177e4SLinus Torvalds 8819070733bSMichal Hocko current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); 8820244b960SChristoph Hellwig xfs_trans_free(tp); 8830244b960SChristoph Hellwig 8841da177e4SLinus Torvalds /* 8851da177e4SLinus Torvalds * If the transaction needs to be synchronous, then force the 8861da177e4SLinus Torvalds * log out now and wait for it. 8871da177e4SLinus Torvalds */ 8881da177e4SLinus Torvalds if (sync) { 889656de4ffSChristoph Hellwig error = xfs_log_force_lsn(mp, commit_lsn, XFS_LOG_SYNC, NULL); 890ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_trans_sync); 8911da177e4SLinus Torvalds } else { 892ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_trans_async); 8931da177e4SLinus Torvalds } 8941da177e4SLinus Torvalds 895a3ccd2caSChristoph Hellwig return error; 896a3ccd2caSChristoph Hellwig 897a3ccd2caSChristoph Hellwig out_unreserve: 898a3ccd2caSChristoph Hellwig xfs_trans_unreserve_and_mod_sb(tp); 899a3ccd2caSChristoph Hellwig 900a3ccd2caSChristoph Hellwig /* 901a3ccd2caSChristoph Hellwig * It is indeed possible for the transaction to be not dirty but 902a3ccd2caSChristoph Hellwig * the dqinfo portion to be. All that means is that we have some 903a3ccd2caSChristoph Hellwig * (non-persistent) quota reservations that need to be unreserved. 904a3ccd2caSChristoph Hellwig */ 905a3ccd2caSChristoph Hellwig xfs_trans_unreserve_and_mod_dquots(tp); 906a3ccd2caSChristoph Hellwig if (tp->t_ticket) { 9078b41e3f9SChristoph Hellwig if (regrant && !XLOG_FORCED_SHUTDOWN(mp->m_log)) 9088b41e3f9SChristoph Hellwig xfs_log_ticket_regrant(mp->m_log, tp->t_ticket); 9098b41e3f9SChristoph Hellwig else 9108b41e3f9SChristoph Hellwig xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket); 911ba18781bSDave Chinner tp->t_ticket = NULL; 912a3ccd2caSChristoph Hellwig } 9139070733bSMichal Hocko current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); 914195cd83dSChristoph Hellwig xfs_trans_free_items(tp, !!error); 915a3ccd2caSChristoph Hellwig xfs_trans_free(tp); 916a3ccd2caSChristoph Hellwig 917ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_trans_empty); 918a3ccd2caSChristoph Hellwig return error; 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds 92170393313SChristoph Hellwig int 92270393313SChristoph Hellwig xfs_trans_commit( 92370393313SChristoph Hellwig struct xfs_trans *tp) 92470393313SChristoph Hellwig { 92570393313SChristoph Hellwig return __xfs_trans_commit(tp, false); 92670393313SChristoph Hellwig } 92770393313SChristoph Hellwig 9281da177e4SLinus Torvalds /* 9291da177e4SLinus Torvalds * Unlock all of the transaction's items and free the transaction. 9301da177e4SLinus Torvalds * The transaction must not have modified any of its items, because 9311da177e4SLinus Torvalds * there is no way to restore them to their previous state. 9321da177e4SLinus Torvalds * 9331da177e4SLinus Torvalds * If the transaction has made a log reservation, make sure to release 9341da177e4SLinus Torvalds * it as well. 9351da177e4SLinus Torvalds */ 9361da177e4SLinus Torvalds void 9371da177e4SLinus Torvalds xfs_trans_cancel( 9384906e215SChristoph Hellwig struct xfs_trans *tp) 9391da177e4SLinus Torvalds { 9404906e215SChristoph Hellwig struct xfs_mount *mp = tp->t_mountp; 9414906e215SChristoph Hellwig bool dirty = (tp->t_flags & XFS_TRANS_DIRTY); 9421da177e4SLinus Torvalds 943ba18781bSDave Chinner trace_xfs_trans_cancel(tp, _RET_IP_); 944ba18781bSDave Chinner 94598719051SBrian Foster if (tp->t_flags & XFS_TRANS_PERM_LOG_RES) 9469e28a242SBrian Foster xfs_defer_cancel(tp); 947e021a2e5SBrian Foster 9481da177e4SLinus Torvalds /* 9491da177e4SLinus Torvalds * See if the caller is relying on us to shut down the 9501da177e4SLinus Torvalds * filesystem. This happens in paths where we detect 9511da177e4SLinus Torvalds * corruption and decide to give up. 9521da177e4SLinus Torvalds */ 9534906e215SChristoph Hellwig if (dirty && !XFS_FORCED_SHUTDOWN(mp)) { 9540733af21SRyan Hankins XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp); 9557d04a335SNathan Scott xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 95660a204f0SNathan Scott } 9571da177e4SLinus Torvalds #ifdef DEBUG 9584906e215SChristoph Hellwig if (!dirty && !XFS_FORCED_SHUTDOWN(mp)) { 959e6631f85SDave Chinner struct xfs_log_item *lip; 9601da177e4SLinus Torvalds 961e6631f85SDave Chinner list_for_each_entry(lip, &tp->t_items, li_trans) 962e6631f85SDave Chinner ASSERT(!(lip->li_type == XFS_LI_EFD)); 9631da177e4SLinus Torvalds } 9641da177e4SLinus Torvalds #endif 9651da177e4SLinus Torvalds xfs_trans_unreserve_and_mod_sb(tp); 9667d095257SChristoph Hellwig xfs_trans_unreserve_and_mod_dquots(tp); 9671da177e4SLinus Torvalds 968ba18781bSDave Chinner if (tp->t_ticket) { 9698b41e3f9SChristoph Hellwig xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket); 970ba18781bSDave Chinner tp->t_ticket = NULL; 971ba18781bSDave Chinner } 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds /* mark this thread as no longer being in a transaction */ 9749070733bSMichal Hocko current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); 9751da177e4SLinus Torvalds 976195cd83dSChristoph Hellwig xfs_trans_free_items(tp, dirty); 9771da177e4SLinus Torvalds xfs_trans_free(tp); 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds 980322ff6b8SNiv Sardi /* 981322ff6b8SNiv Sardi * Roll from one trans in the sequence of PERMANENT transactions to 982322ff6b8SNiv Sardi * the next: permanent transactions are only flushed out when 98370393313SChristoph Hellwig * committed with xfs_trans_commit(), but we still want as soon 984322ff6b8SNiv Sardi * as possible to let chunks of it go to the log. So we commit the 985322ff6b8SNiv Sardi * chunk we've been working on and get a new transaction to continue. 986322ff6b8SNiv Sardi */ 987322ff6b8SNiv Sardi int 988254133f5SChristoph Hellwig xfs_trans_roll( 989411350dfSChristoph Hellwig struct xfs_trans **tpp) 990322ff6b8SNiv Sardi { 991411350dfSChristoph Hellwig struct xfs_trans *trans = *tpp; 9923d3c8b52SJie Liu struct xfs_trans_res tres; 993322ff6b8SNiv Sardi int error; 994322ff6b8SNiv Sardi 995ba18781bSDave Chinner trace_xfs_trans_roll(trans, _RET_IP_); 996ba18781bSDave Chinner 997322ff6b8SNiv Sardi /* 998322ff6b8SNiv Sardi * Copy the critical parameters from one trans to the next. 999322ff6b8SNiv Sardi */ 10003d3c8b52SJie Liu tres.tr_logres = trans->t_log_res; 10013d3c8b52SJie Liu tres.tr_logcount = trans->t_log_count; 1002411350dfSChristoph Hellwig 1003322ff6b8SNiv Sardi *tpp = xfs_trans_dup(trans); 1004322ff6b8SNiv Sardi 1005322ff6b8SNiv Sardi /* 1006322ff6b8SNiv Sardi * Commit the current transaction. 1007322ff6b8SNiv Sardi * If this commit failed, then it'd just unlock those items that 1008322ff6b8SNiv Sardi * are not marked ihold. That also means that a filesystem shutdown 1009322ff6b8SNiv Sardi * is in progress. The caller takes the responsibility to cancel 1010322ff6b8SNiv Sardi * the duplicate transaction that gets returned. 1011322ff6b8SNiv Sardi */ 101270393313SChristoph Hellwig error = __xfs_trans_commit(trans, true); 1013322ff6b8SNiv Sardi if (error) 1014d99831ffSEric Sandeen return error; 1015322ff6b8SNiv Sardi 1016322ff6b8SNiv Sardi /* 1017411350dfSChristoph Hellwig * Reserve space in the log for the next transaction. 1018322ff6b8SNiv Sardi * This also pushes items in the "AIL", the list of logged items, 1019322ff6b8SNiv Sardi * out to disk if they are taking up space at the tail of the log 1020322ff6b8SNiv Sardi * that we want to use. This requires that either nothing be locked 1021322ff6b8SNiv Sardi * across this call, or that anything that is locked be logged in 1022322ff6b8SNiv Sardi * the prior and the next transactions. 1023322ff6b8SNiv Sardi */ 10243d3c8b52SJie Liu tres.tr_logflags = XFS_TRANS_PERM_LOG_RES; 1025411350dfSChristoph Hellwig return xfs_trans_reserve(*tpp, &tres, 0, 0); 1026322ff6b8SNiv Sardi } 1027