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" 12239880efSDave Chinner #include "xfs_trans_resv.h" 131da177e4SLinus Torvalds #include "xfs_mount.h" 14efc27b52SDave Chinner #include "xfs_extent_busy.h" 151da177e4SLinus Torvalds #include "xfs_quota.h" 16239880efSDave Chinner #include "xfs_trans.h" 17a844f451SNathan Scott #include "xfs_trans_priv.h" 18239880efSDave Chinner #include "xfs_log.h" 190020a190SDave Chinner #include "xfs_log_priv.h" 20ed3b4d6cSDave Chinner #include "xfs_trace.h" 21a4fbe6abSDave Chinner #include "xfs_error.h" 22f8f2835aSBrian Foster #include "xfs_defer.h" 233a1af6c3SDarrick J. Wong #include "xfs_inode.h" 24f2f7b9ffSDarrick J. Wong #include "xfs_dquot_item.h" 25f2f7b9ffSDarrick J. Wong #include "xfs_dquot.h" 26766aabd5SDarrick J. Wong #include "xfs_icache.h" 271da177e4SLinus Torvalds 28182696fbSDarrick J. Wong struct kmem_cache *xfs_trans_cache; 291da177e4SLinus Torvalds 30b872af2cSDarrick J. Wong #if defined(CONFIG_TRACEPOINTS) 31b872af2cSDarrick J. Wong static void 32b872af2cSDarrick J. Wong xfs_trans_trace_reservations( 33b872af2cSDarrick J. Wong struct xfs_mount *mp) 34b872af2cSDarrick J. Wong { 35b872af2cSDarrick J. Wong struct xfs_trans_res resv; 36b872af2cSDarrick J. Wong struct xfs_trans_res *res; 37b872af2cSDarrick J. Wong struct xfs_trans_res *end_res; 38b872af2cSDarrick J. Wong int i; 39b872af2cSDarrick J. Wong 40b872af2cSDarrick J. Wong res = (struct xfs_trans_res *)M_RES(mp); 41b872af2cSDarrick J. Wong end_res = (struct xfs_trans_res *)(M_RES(mp) + 1); 42b872af2cSDarrick J. Wong for (i = 0; res < end_res; i++, res++) 43b872af2cSDarrick J. Wong trace_xfs_trans_resv_calc(mp, i, res); 44b872af2cSDarrick J. Wong xfs_log_get_max_trans_res(mp, &resv); 45b872af2cSDarrick J. Wong trace_xfs_trans_resv_calc(mp, -1, &resv); 46b872af2cSDarrick J. Wong } 47b872af2cSDarrick J. Wong #else 48b872af2cSDarrick J. Wong # define xfs_trans_trace_reservations(mp) 49b872af2cSDarrick J. Wong #endif 50b872af2cSDarrick J. Wong 514f3b5783SJeff Liu /* 521da177e4SLinus Torvalds * Initialize the precomputed transaction reservation values 531da177e4SLinus Torvalds * in the mount structure. 541da177e4SLinus Torvalds */ 551da177e4SLinus Torvalds void 561da177e4SLinus Torvalds xfs_trans_init( 57025101dcSChristoph Hellwig struct xfs_mount *mp) 581da177e4SLinus Torvalds { 593d3c8b52SJie Liu xfs_trans_resv_calc(mp, M_RES(mp)); 60b872af2cSDarrick J. Wong xfs_trans_trace_reservations(mp); 611da177e4SLinus Torvalds } 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds /* 64b1c1b5b6SDave Chinner * Free the transaction structure. If there is more clean up 65b1c1b5b6SDave Chinner * to do when the structure is freed, add it here. 66b1c1b5b6SDave Chinner */ 67b1c1b5b6SDave Chinner STATIC void 68b1c1b5b6SDave Chinner xfs_trans_free( 69ed3b4d6cSDave Chinner struct xfs_trans *tp) 70b1c1b5b6SDave Chinner { 714ecbfe63SDave Chinner xfs_extent_busy_sort(&tp->t_busy); 724ecbfe63SDave Chinner xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false); 73ed3b4d6cSDave Chinner 74ba18781bSDave Chinner trace_xfs_trans_free(tp, _RET_IP_); 75756b1c34SDave Chinner xfs_trans_clear_context(tp); 76253f4911SChristoph Hellwig if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT)) 77d9457dc0SJan Kara sb_end_intwrite(tp->t_mountp->m_super); 78b1c1b5b6SDave Chinner xfs_trans_free_dqinfo(tp); 79182696fbSDarrick J. Wong kmem_cache_free(xfs_trans_cache, tp); 80b1c1b5b6SDave Chinner } 81b1c1b5b6SDave Chinner 82b1c1b5b6SDave Chinner /* 831da177e4SLinus Torvalds * This is called to create a new transaction which will share the 841da177e4SLinus Torvalds * permanent log reservation of the given transaction. The remaining 851da177e4SLinus Torvalds * unused block and rt extent reservations are also inherited. This 861da177e4SLinus Torvalds * implies that the original transaction is no longer allowed to allocate 871da177e4SLinus Torvalds * blocks. Locks and log items, however, are no inherited. They must 881da177e4SLinus Torvalds * be added to the new transaction explicitly. 891da177e4SLinus Torvalds */ 90f8f2835aSBrian Foster STATIC struct xfs_trans * 911da177e4SLinus Torvalds xfs_trans_dup( 92f8f2835aSBrian Foster struct xfs_trans *tp) 931da177e4SLinus Torvalds { 94f8f2835aSBrian Foster struct xfs_trans *ntp; 951da177e4SLinus Torvalds 96ba18781bSDave Chinner trace_xfs_trans_dup(tp, _RET_IP_); 97ba18781bSDave Chinner 98182696fbSDarrick J. Wong ntp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL); 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds /* 1011da177e4SLinus Torvalds * Initialize the new transaction structure. 1021da177e4SLinus Torvalds */ 1032a3c0accSDave Chinner ntp->t_magic = XFS_TRANS_HEADER_MAGIC; 1041da177e4SLinus Torvalds ntp->t_mountp = tp->t_mountp; 105e98c414fSChristoph Hellwig INIT_LIST_HEAD(&ntp->t_items); 106ed3b4d6cSDave Chinner INIT_LIST_HEAD(&ntp->t_busy); 1079d9e6233SBrian Foster INIT_LIST_HEAD(&ntp->t_dfops); 108bba59c5eSBrian Foster ntp->t_firstblock = NULLFSBLOCK; 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); 1111da177e4SLinus Torvalds ASSERT(tp->t_ticket != NULL); 112cfcbbbd0SNathan Scott 113d9457dc0SJan Kara ntp->t_flags = XFS_TRANS_PERM_LOG_RES | 114d9457dc0SJan Kara (tp->t_flags & XFS_TRANS_RESERVE) | 115f74681baSBrian Foster (tp->t_flags & XFS_TRANS_NO_WRITECOUNT) | 116f74681baSBrian Foster (tp->t_flags & XFS_TRANS_RES_FDBLKS); 117d9457dc0SJan Kara /* We gave our writer reference to the new transaction */ 118253f4911SChristoph Hellwig tp->t_flags |= XFS_TRANS_NO_WRITECOUNT; 119cc09c0dcSDave Chinner ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket); 1203e78b9a4SBrian Foster 1213e78b9a4SBrian Foster ASSERT(tp->t_blk_res >= tp->t_blk_res_used); 1221da177e4SLinus Torvalds ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used; 1231da177e4SLinus Torvalds tp->t_blk_res = tp->t_blk_res_used; 1243e78b9a4SBrian Foster 1251da177e4SLinus Torvalds ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used; 1261da177e4SLinus Torvalds tp->t_rtx_res = tp->t_rtx_res_used; 127756b1c34SDave Chinner 128756b1c34SDave Chinner xfs_trans_switch_context(tp, ntp); 129e021a2e5SBrian Foster 1309d9e6233SBrian Foster /* move deferred ops over to the new tp */ 131ce356d64SBrian Foster xfs_defer_move(ntp, tp); 1321da177e4SLinus Torvalds 1337d095257SChristoph Hellwig xfs_trans_dup_dqinfo(tp, ntp); 1341da177e4SLinus Torvalds return ntp; 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds /* 1381da177e4SLinus Torvalds * This is called to reserve free disk blocks and log space for the 1391da177e4SLinus Torvalds * given transaction. This must be done before allocating any resources 1401da177e4SLinus Torvalds * within the transaction. 1411da177e4SLinus Torvalds * 1421da177e4SLinus Torvalds * This will return ENOSPC if there are not enough blocks available. 1431da177e4SLinus Torvalds * It will sleep waiting for available log space. 1441da177e4SLinus Torvalds * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which 1451da177e4SLinus Torvalds * is used by long running transactions. If any one of the reservations 1461da177e4SLinus Torvalds * fails then they will all be backed out. 1471da177e4SLinus Torvalds * 1481da177e4SLinus Torvalds * This does not do quota reservations. That typically is done by the 1491da177e4SLinus Torvalds * caller afterwards. 1501da177e4SLinus Torvalds */ 151253f4911SChristoph Hellwig static int 1521da177e4SLinus Torvalds xfs_trans_reserve( 1533d3c8b52SJie Liu struct xfs_trans *tp, 1543d3c8b52SJie Liu struct xfs_trans_res *resp, 1551da177e4SLinus Torvalds uint blocks, 1563d3c8b52SJie Liu uint rtextents) 1571da177e4SLinus Torvalds { 158dd401770SDave Chinner struct xfs_mount *mp = tp->t_mountp; 15959c1b082SNathan Scott int error = 0; 1600d485adaSDave Chinner bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0; 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds /* 1631da177e4SLinus Torvalds * Attempt to reserve the needed disk blocks by decrementing 1641da177e4SLinus Torvalds * the number needed from the number available. This will 1651da177e4SLinus Torvalds * fail if the count would go below zero. 1661da177e4SLinus Torvalds */ 1671da177e4SLinus Torvalds if (blocks > 0) { 168dd401770SDave Chinner error = xfs_mod_fdblocks(mp, -((int64_t)blocks), rsvd); 169756b1c34SDave Chinner if (error != 0) 1702451337dSDave Chinner return -ENOSPC; 1711da177e4SLinus Torvalds tp->t_blk_res += blocks; 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds /* 1751da177e4SLinus Torvalds * Reserve the log space needed for this transaction. 1761da177e4SLinus Torvalds */ 1773d3c8b52SJie Liu if (resp->tr_logres > 0) { 1789006fb91SChristoph Hellwig bool permanent = false; 1799006fb91SChristoph Hellwig 1803d3c8b52SJie Liu ASSERT(tp->t_log_res == 0 || 1813d3c8b52SJie Liu tp->t_log_res == resp->tr_logres); 1823d3c8b52SJie Liu ASSERT(tp->t_log_count == 0 || 1833d3c8b52SJie Liu tp->t_log_count == resp->tr_logcount); 1849006fb91SChristoph Hellwig 1853d3c8b52SJie Liu if (resp->tr_logflags & XFS_TRANS_PERM_LOG_RES) { 1861da177e4SLinus Torvalds tp->t_flags |= XFS_TRANS_PERM_LOG_RES; 1879006fb91SChristoph Hellwig permanent = true; 1881da177e4SLinus Torvalds } else { 1891da177e4SLinus Torvalds ASSERT(tp->t_ticket == NULL); 1901da177e4SLinus Torvalds ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES)); 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds 1939006fb91SChristoph Hellwig if (tp->t_ticket != NULL) { 1943d3c8b52SJie Liu ASSERT(resp->tr_logflags & XFS_TRANS_PERM_LOG_RES); 195dd401770SDave Chinner error = xfs_log_regrant(mp, tp->t_ticket); 1969006fb91SChristoph Hellwig } else { 197dd401770SDave Chinner error = xfs_log_reserve(mp, 1983d3c8b52SJie Liu resp->tr_logres, 1993d3c8b52SJie Liu resp->tr_logcount, 2003d3c8b52SJie Liu &tp->t_ticket, XFS_TRANSACTION, 201710b1e2cSChristoph Hellwig permanent); 2021da177e4SLinus Torvalds } 2039006fb91SChristoph Hellwig 2049006fb91SChristoph Hellwig if (error) 2059006fb91SChristoph Hellwig goto undo_blocks; 2069006fb91SChristoph Hellwig 2073d3c8b52SJie Liu tp->t_log_res = resp->tr_logres; 2083d3c8b52SJie Liu tp->t_log_count = resp->tr_logcount; 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds /* 2121da177e4SLinus Torvalds * Attempt to reserve the needed realtime extents by decrementing 2131da177e4SLinus Torvalds * the number needed from the number available. This will 2141da177e4SLinus Torvalds * fail if the count would go below zero. 2151da177e4SLinus Torvalds */ 2161da177e4SLinus Torvalds if (rtextents > 0) { 217dd401770SDave Chinner error = xfs_mod_frextents(mp, -((int64_t)rtextents)); 2181da177e4SLinus Torvalds if (error) { 2192451337dSDave Chinner error = -ENOSPC; 2201da177e4SLinus Torvalds goto undo_log; 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds tp->t_rtx_res += rtextents; 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds return 0; 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds /* 2281da177e4SLinus Torvalds * Error cases jump to one of these labels to undo any 2291da177e4SLinus Torvalds * reservations which have already been performed. 2301da177e4SLinus Torvalds */ 2311da177e4SLinus Torvalds undo_log: 2323d3c8b52SJie Liu if (resp->tr_logres > 0) { 2338b41e3f9SChristoph Hellwig xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket); 2341da177e4SLinus Torvalds tp->t_ticket = NULL; 2351da177e4SLinus Torvalds tp->t_log_res = 0; 2361da177e4SLinus Torvalds tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES; 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds undo_blocks: 2401da177e4SLinus Torvalds if (blocks > 0) { 241dd401770SDave Chinner xfs_mod_fdblocks(mp, (int64_t)blocks, rsvd); 2421da177e4SLinus Torvalds tp->t_blk_res = 0; 2431da177e4SLinus Torvalds } 24459c1b082SNathan Scott return error; 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds 247253f4911SChristoph Hellwig int 248253f4911SChristoph Hellwig xfs_trans_alloc( 249253f4911SChristoph Hellwig struct xfs_mount *mp, 250253f4911SChristoph Hellwig struct xfs_trans_res *resp, 251253f4911SChristoph Hellwig uint blocks, 252253f4911SChristoph Hellwig uint rtextents, 253253f4911SChristoph Hellwig uint flags, 254253f4911SChristoph Hellwig struct xfs_trans **tpp) 255253f4911SChristoph Hellwig { 256253f4911SChristoph Hellwig struct xfs_trans *tp; 2579febcda6SDarrick J. Wong bool want_retry = true; 258253f4911SChristoph Hellwig int error; 259253f4911SChristoph Hellwig 2608683edb7SDave Chinner /* 2618683edb7SDave Chinner * Allocate the handle before we do our freeze accounting and setting up 2628683edb7SDave Chinner * GFP_NOFS allocation context so that we avoid lockdep false positives 2638683edb7SDave Chinner * by doing GFP_KERNEL allocations inside sb_start_intwrite(). 2648683edb7SDave Chinner */ 2659febcda6SDarrick J. Wong retry: 266182696fbSDarrick J. Wong tp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL); 267253f4911SChristoph Hellwig if (!(flags & XFS_TRANS_NO_WRITECOUNT)) 268253f4911SChristoph Hellwig sb_start_intwrite(mp->m_super); 269756b1c34SDave Chinner xfs_trans_set_context(tp); 270253f4911SChristoph Hellwig 27110ee2526SDarrick J. Wong /* 27210ee2526SDarrick J. Wong * Zero-reservation ("empty") transactions can't modify anything, so 27310ee2526SDarrick J. Wong * they're allowed to run while we're frozen. 27410ee2526SDarrick J. Wong */ 27510ee2526SDarrick J. Wong WARN_ON(resp->tr_logres > 0 && 27610ee2526SDarrick J. Wong mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE); 277f74681baSBrian Foster ASSERT(!(flags & XFS_TRANS_RES_FDBLKS) || 27838c26bfdSDave Chinner xfs_has_lazysbcount(mp)); 279253f4911SChristoph Hellwig 280253f4911SChristoph Hellwig tp->t_magic = XFS_TRANS_HEADER_MAGIC; 281253f4911SChristoph Hellwig tp->t_flags = flags; 282253f4911SChristoph Hellwig tp->t_mountp = mp; 283253f4911SChristoph Hellwig INIT_LIST_HEAD(&tp->t_items); 284253f4911SChristoph Hellwig INIT_LIST_HEAD(&tp->t_busy); 2859d9e6233SBrian Foster INIT_LIST_HEAD(&tp->t_dfops); 286bba59c5eSBrian Foster tp->t_firstblock = NULLFSBLOCK; 287253f4911SChristoph Hellwig 288253f4911SChristoph Hellwig error = xfs_trans_reserve(tp, resp, blocks, rtextents); 2899febcda6SDarrick J. Wong if (error == -ENOSPC && want_retry) { 2909febcda6SDarrick J. Wong xfs_trans_cancel(tp); 2919febcda6SDarrick J. Wong 292a1a7d05aSDarrick J. Wong /* 293a1a7d05aSDarrick J. Wong * We weren't able to reserve enough space for the transaction. 294a1a7d05aSDarrick J. Wong * Flush the other speculative space allocations to free space. 295a1a7d05aSDarrick J. Wong * Do not perform a synchronous scan because callers can hold 296a1a7d05aSDarrick J. Wong * other locks. 297a1a7d05aSDarrick J. Wong */ 298e8d04c2aSDarrick J. Wong xfs_blockgc_flush_all(mp); 2999febcda6SDarrick J. Wong want_retry = false; 3009febcda6SDarrick J. Wong goto retry; 301a1a7d05aSDarrick J. Wong } 302253f4911SChristoph Hellwig if (error) { 303253f4911SChristoph Hellwig xfs_trans_cancel(tp); 304253f4911SChristoph Hellwig return error; 305253f4911SChristoph Hellwig } 306253f4911SChristoph Hellwig 307ba18781bSDave Chinner trace_xfs_trans_alloc(tp, _RET_IP_); 308ba18781bSDave Chinner 309253f4911SChristoph Hellwig *tpp = tp; 310253f4911SChristoph Hellwig return 0; 311253f4911SChristoph Hellwig } 312253f4911SChristoph Hellwig 3131da177e4SLinus Torvalds /* 314e89c0413SDarrick J. Wong * Create an empty transaction with no reservation. This is a defensive 315b41b46c2SDave Chinner * mechanism for routines that query metadata without actually modifying them -- 316b41b46c2SDave Chinner * if the metadata being queried is somehow cross-linked (think a btree block 317b41b46c2SDave Chinner * pointer that points higher in the tree), we risk deadlock. However, blocks 318b41b46c2SDave Chinner * grabbed as part of a transaction can be re-grabbed. The verifiers will 319b41b46c2SDave Chinner * notice the corrupt block and the operation will fail back to userspace 320b41b46c2SDave Chinner * without deadlocking. 321e89c0413SDarrick J. Wong * 322b41b46c2SDave Chinner * Note the zero-length reservation; this transaction MUST be cancelled without 323b41b46c2SDave Chinner * any dirty data. 32427fb5a72SDarrick J. Wong * 325b41b46c2SDave Chinner * Callers should obtain freeze protection to avoid a conflict with fs freezing 326b41b46c2SDave Chinner * where we can be grabbing buffers at the same time that freeze is trying to 327b41b46c2SDave Chinner * drain the buffer LRU list. 328e89c0413SDarrick J. Wong */ 329e89c0413SDarrick J. Wong int 330e89c0413SDarrick J. Wong xfs_trans_alloc_empty( 331e89c0413SDarrick J. Wong struct xfs_mount *mp, 332e89c0413SDarrick J. Wong struct xfs_trans **tpp) 333e89c0413SDarrick J. Wong { 334e89c0413SDarrick J. Wong struct xfs_trans_res resv = {0}; 335e89c0413SDarrick J. Wong 336e89c0413SDarrick J. Wong return xfs_trans_alloc(mp, &resv, 0, 0, XFS_TRANS_NO_WRITECOUNT, tpp); 337e89c0413SDarrick J. Wong } 338e89c0413SDarrick J. Wong 339e89c0413SDarrick J. Wong /* 3401da177e4SLinus Torvalds * Record the indicated change to the given field for application 3411da177e4SLinus Torvalds * to the file system's superblock when the transaction commits. 3421da177e4SLinus Torvalds * For now, just store the change in the transaction structure. 3431da177e4SLinus Torvalds * 3441da177e4SLinus Torvalds * Mark the transaction structure to indicate that the superblock 3451da177e4SLinus Torvalds * needs to be updated before committing. 34692821e2bSDavid Chinner * 34792821e2bSDavid Chinner * Because we may not be keeping track of allocated/free inodes and 34892821e2bSDavid Chinner * used filesystem blocks in the superblock, we do not mark the 34992821e2bSDavid Chinner * superblock dirty in this transaction if we modify these fields. 35092821e2bSDavid Chinner * We still need to update the transaction deltas so that they get 35192821e2bSDavid Chinner * applied to the incore superblock, but we don't want them to 35292821e2bSDavid Chinner * cause the superblock to get locked and logged if these are the 35392821e2bSDavid Chinner * only fields in the superblock that the transaction modifies. 3541da177e4SLinus Torvalds */ 3551da177e4SLinus Torvalds void 3561da177e4SLinus Torvalds xfs_trans_mod_sb( 3571da177e4SLinus Torvalds xfs_trans_t *tp, 3581da177e4SLinus Torvalds uint field, 35920f4ebf2SDavid Chinner int64_t delta) 3601da177e4SLinus Torvalds { 36192821e2bSDavid Chinner uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY); 36292821e2bSDavid Chinner xfs_mount_t *mp = tp->t_mountp; 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds switch (field) { 3651da177e4SLinus Torvalds case XFS_TRANS_SB_ICOUNT: 3661da177e4SLinus Torvalds tp->t_icount_delta += delta; 36738c26bfdSDave Chinner if (xfs_has_lazysbcount(mp)) 36892821e2bSDavid Chinner flags &= ~XFS_TRANS_SB_DIRTY; 3691da177e4SLinus Torvalds break; 3701da177e4SLinus Torvalds case XFS_TRANS_SB_IFREE: 3711da177e4SLinus Torvalds tp->t_ifree_delta += delta; 37238c26bfdSDave Chinner if (xfs_has_lazysbcount(mp)) 37392821e2bSDavid Chinner flags &= ~XFS_TRANS_SB_DIRTY; 3741da177e4SLinus Torvalds break; 3751da177e4SLinus Torvalds case XFS_TRANS_SB_FDBLOCKS: 3761da177e4SLinus Torvalds /* 3773e78b9a4SBrian Foster * Track the number of blocks allocated in the transaction. 3783e78b9a4SBrian Foster * Make sure it does not exceed the number reserved. If so, 3793e78b9a4SBrian Foster * shutdown as this can lead to accounting inconsistency. 3801da177e4SLinus Torvalds */ 3811da177e4SLinus Torvalds if (delta < 0) { 3821da177e4SLinus Torvalds tp->t_blk_res_used += (uint)-delta; 3833e78b9a4SBrian Foster if (tp->t_blk_res_used > tp->t_blk_res) 3843e78b9a4SBrian Foster xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 385f74681baSBrian Foster } else if (delta > 0 && (tp->t_flags & XFS_TRANS_RES_FDBLKS)) { 386f74681baSBrian Foster int64_t blkres_delta; 387f74681baSBrian Foster 388f74681baSBrian Foster /* 389f74681baSBrian Foster * Return freed blocks directly to the reservation 390f74681baSBrian Foster * instead of the global pool, being careful not to 391f74681baSBrian Foster * overflow the trans counter. This is used to preserve 392f74681baSBrian Foster * reservation across chains of transaction rolls that 393f74681baSBrian Foster * repeatedly free and allocate blocks. 394f74681baSBrian Foster */ 395f74681baSBrian Foster blkres_delta = min_t(int64_t, delta, 396f74681baSBrian Foster UINT_MAX - tp->t_blk_res); 397f74681baSBrian Foster tp->t_blk_res += blkres_delta; 398f74681baSBrian Foster delta -= blkres_delta; 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds tp->t_fdblocks_delta += delta; 40138c26bfdSDave Chinner if (xfs_has_lazysbcount(mp)) 40292821e2bSDavid Chinner flags &= ~XFS_TRANS_SB_DIRTY; 4031da177e4SLinus Torvalds break; 4041da177e4SLinus Torvalds case XFS_TRANS_SB_RES_FDBLOCKS: 4051da177e4SLinus Torvalds /* 4061da177e4SLinus Torvalds * The allocation has already been applied to the 4071da177e4SLinus Torvalds * in-core superblock's counter. This should only 4081da177e4SLinus Torvalds * be applied to the on-disk superblock. 4091da177e4SLinus Torvalds */ 4101da177e4SLinus Torvalds tp->t_res_fdblocks_delta += delta; 41138c26bfdSDave Chinner if (xfs_has_lazysbcount(mp)) 41292821e2bSDavid Chinner flags &= ~XFS_TRANS_SB_DIRTY; 4131da177e4SLinus Torvalds break; 4141da177e4SLinus Torvalds case XFS_TRANS_SB_FREXTENTS: 4151da177e4SLinus Torvalds /* 4161da177e4SLinus Torvalds * Track the number of blocks allocated in the 4171da177e4SLinus Torvalds * transaction. Make sure it does not exceed the 4181da177e4SLinus Torvalds * number reserved. 4191da177e4SLinus Torvalds */ 4201da177e4SLinus Torvalds if (delta < 0) { 4211da177e4SLinus Torvalds tp->t_rtx_res_used += (uint)-delta; 4221da177e4SLinus Torvalds ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res); 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds tp->t_frextents_delta += delta; 4251da177e4SLinus Torvalds break; 4261da177e4SLinus Torvalds case XFS_TRANS_SB_RES_FREXTENTS: 4271da177e4SLinus Torvalds /* 4281da177e4SLinus Torvalds * The allocation has already been applied to the 429c41564b5SNathan Scott * in-core superblock's counter. This should only 4301da177e4SLinus Torvalds * be applied to the on-disk superblock. 4311da177e4SLinus Torvalds */ 4321da177e4SLinus Torvalds ASSERT(delta < 0); 4331da177e4SLinus Torvalds tp->t_res_frextents_delta += delta; 4341da177e4SLinus Torvalds break; 4351da177e4SLinus Torvalds case XFS_TRANS_SB_DBLOCKS: 4361da177e4SLinus Torvalds tp->t_dblocks_delta += delta; 4371da177e4SLinus Torvalds break; 4381da177e4SLinus Torvalds case XFS_TRANS_SB_AGCOUNT: 4391da177e4SLinus Torvalds ASSERT(delta > 0); 4401da177e4SLinus Torvalds tp->t_agcount_delta += delta; 4411da177e4SLinus Torvalds break; 4421da177e4SLinus Torvalds case XFS_TRANS_SB_IMAXPCT: 4431da177e4SLinus Torvalds tp->t_imaxpct_delta += delta; 4441da177e4SLinus Torvalds break; 4451da177e4SLinus Torvalds case XFS_TRANS_SB_REXTSIZE: 4461da177e4SLinus Torvalds tp->t_rextsize_delta += delta; 4471da177e4SLinus Torvalds break; 4481da177e4SLinus Torvalds case XFS_TRANS_SB_RBMBLOCKS: 4491da177e4SLinus Torvalds tp->t_rbmblocks_delta += delta; 4501da177e4SLinus Torvalds break; 4511da177e4SLinus Torvalds case XFS_TRANS_SB_RBLOCKS: 4521da177e4SLinus Torvalds tp->t_rblocks_delta += delta; 4531da177e4SLinus Torvalds break; 4541da177e4SLinus Torvalds case XFS_TRANS_SB_REXTENTS: 4551da177e4SLinus Torvalds tp->t_rextents_delta += delta; 4561da177e4SLinus Torvalds break; 4571da177e4SLinus Torvalds case XFS_TRANS_SB_REXTSLOG: 4581da177e4SLinus Torvalds tp->t_rextslog_delta += delta; 4591da177e4SLinus Torvalds break; 4601da177e4SLinus Torvalds default: 4611da177e4SLinus Torvalds ASSERT(0); 4621da177e4SLinus Torvalds return; 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 465210c6f1cSDavid Chinner tp->t_flags |= flags; 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds /* 4691da177e4SLinus Torvalds * xfs_trans_apply_sb_deltas() is called from the commit code 4701da177e4SLinus Torvalds * to bring the superblock buffer into the current transaction 4711da177e4SLinus Torvalds * and modify it as requested by earlier calls to xfs_trans_mod_sb(). 4721da177e4SLinus Torvalds * 4731da177e4SLinus Torvalds * For now we just look at each field allowed to change and change 4741da177e4SLinus Torvalds * it if necessary. 4751da177e4SLinus Torvalds */ 4761da177e4SLinus Torvalds STATIC void 4771da177e4SLinus Torvalds xfs_trans_apply_sb_deltas( 4781da177e4SLinus Torvalds xfs_trans_t *tp) 4791da177e4SLinus Torvalds { 480ed67ebfdSChristoph Hellwig struct xfs_dsb *sbp; 481e8222613SDave Chinner struct xfs_buf *bp; 4821da177e4SLinus Torvalds int whole = 0; 4831da177e4SLinus Torvalds 484cead0b10SChristoph Hellwig bp = xfs_trans_getsb(tp); 4853e6e8afdSChristoph Hellwig sbp = bp->b_addr; 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds /* 48892821e2bSDavid Chinner * Only update the superblock counters if we are logging them 48992821e2bSDavid Chinner */ 49038c26bfdSDave Chinner if (!xfs_has_lazysbcount((tp->t_mountp))) { 4912bdf7cd0SChristoph Hellwig if (tp->t_icount_delta) 492413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta); 4932bdf7cd0SChristoph Hellwig if (tp->t_ifree_delta) 494413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta); 4952bdf7cd0SChristoph Hellwig if (tp->t_fdblocks_delta) 496413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta); 4972bdf7cd0SChristoph Hellwig if (tp->t_res_fdblocks_delta) 498413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta); 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds 501*2229276cSDarrick J. Wong /* 502*2229276cSDarrick J. Wong * Updating frextents requires careful handling because it does not 503*2229276cSDarrick J. Wong * behave like the lazysb counters because we cannot rely on log 504*2229276cSDarrick J. Wong * recovery in older kenels to recompute the value from the rtbitmap. 505*2229276cSDarrick J. Wong * This means that the ondisk frextents must be consistent with the 506*2229276cSDarrick J. Wong * rtbitmap. 507*2229276cSDarrick J. Wong * 508*2229276cSDarrick J. Wong * Therefore, log the frextents change to the ondisk superblock and 509*2229276cSDarrick J. Wong * update the incore superblock so that future calls to xfs_log_sb 510*2229276cSDarrick J. Wong * write the correct value ondisk. 511*2229276cSDarrick J. Wong * 512*2229276cSDarrick J. Wong * Don't touch m_frextents because it includes incore reservations, 513*2229276cSDarrick J. Wong * and those are handled by the unreserve function. 514*2229276cSDarrick J. Wong */ 515*2229276cSDarrick J. Wong if (tp->t_frextents_delta || tp->t_res_frextents_delta) { 516*2229276cSDarrick J. Wong struct xfs_mount *mp = tp->t_mountp; 517*2229276cSDarrick J. Wong int64_t rtxdelta; 518*2229276cSDarrick J. Wong 519*2229276cSDarrick J. Wong rtxdelta = tp->t_frextents_delta + tp->t_res_frextents_delta; 520*2229276cSDarrick J. Wong 521*2229276cSDarrick J. Wong spin_lock(&mp->m_sb_lock); 522*2229276cSDarrick J. Wong be64_add_cpu(&sbp->sb_frextents, rtxdelta); 523*2229276cSDarrick J. Wong mp->m_sb.sb_frextents += rtxdelta; 524*2229276cSDarrick J. Wong spin_unlock(&mp->m_sb_lock); 525*2229276cSDarrick J. Wong } 5261da177e4SLinus Torvalds 5272bdf7cd0SChristoph Hellwig if (tp->t_dblocks_delta) { 528413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta); 5291da177e4SLinus Torvalds whole = 1; 5301da177e4SLinus Torvalds } 5312bdf7cd0SChristoph Hellwig if (tp->t_agcount_delta) { 532413d57c9SMarcin Slusarz be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta); 5331da177e4SLinus Torvalds whole = 1; 5341da177e4SLinus Torvalds } 5352bdf7cd0SChristoph Hellwig if (tp->t_imaxpct_delta) { 5362bdf7cd0SChristoph Hellwig sbp->sb_imax_pct += tp->t_imaxpct_delta; 5371da177e4SLinus Torvalds whole = 1; 5381da177e4SLinus Torvalds } 5392bdf7cd0SChristoph Hellwig if (tp->t_rextsize_delta) { 540413d57c9SMarcin Slusarz be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta); 5411da177e4SLinus Torvalds whole = 1; 5421da177e4SLinus Torvalds } 5432bdf7cd0SChristoph Hellwig if (tp->t_rbmblocks_delta) { 544413d57c9SMarcin Slusarz be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta); 5451da177e4SLinus Torvalds whole = 1; 5461da177e4SLinus Torvalds } 5472bdf7cd0SChristoph Hellwig if (tp->t_rblocks_delta) { 548413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta); 5491da177e4SLinus Torvalds whole = 1; 5501da177e4SLinus Torvalds } 5512bdf7cd0SChristoph Hellwig if (tp->t_rextents_delta) { 552413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta); 5531da177e4SLinus Torvalds whole = 1; 5541da177e4SLinus Torvalds } 5552bdf7cd0SChristoph Hellwig if (tp->t_rextslog_delta) { 5562bdf7cd0SChristoph Hellwig sbp->sb_rextslog += tp->t_rextslog_delta; 5571da177e4SLinus Torvalds whole = 1; 5581da177e4SLinus Torvalds } 5591da177e4SLinus Torvalds 5603443a3bcSDave Chinner xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF); 5611da177e4SLinus Torvalds if (whole) 5621da177e4SLinus Torvalds /* 563c41564b5SNathan Scott * Log the whole thing, the fields are noncontiguous. 5641da177e4SLinus Torvalds */ 565ed67ebfdSChristoph Hellwig xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1); 5661da177e4SLinus Torvalds else 5671da177e4SLinus Torvalds /* 5681da177e4SLinus Torvalds * Since all the modifiable fields are contiguous, we 5691da177e4SLinus Torvalds * can get away with this. 5701da177e4SLinus Torvalds */ 571ed67ebfdSChristoph Hellwig xfs_trans_log_buf(tp, bp, offsetof(struct xfs_dsb, sb_icount), 572ed67ebfdSChristoph Hellwig offsetof(struct xfs_dsb, sb_frextents) + 5731da177e4SLinus Torvalds sizeof(sbp->sb_frextents) - 1); 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds /* 577dc3ffbb1SDave Chinner * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations and 578dc3ffbb1SDave Chinner * apply superblock counter changes to the in-core superblock. The 57945c34141SDavid Chinner * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT 58045c34141SDavid Chinner * applied to the in-core superblock. The idea is that that has already been 58145c34141SDavid Chinner * done. 5821da177e4SLinus Torvalds * 58345c34141SDavid Chinner * If we are not logging superblock counters, then the inode allocated/free and 58445c34141SDavid Chinner * used block counts are not updated in the on disk superblock. In this case, 58545c34141SDavid Chinner * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we 58645c34141SDavid Chinner * still need to update the incore superblock with the changes. 587f18c9a90SDave Chinner * 588f18c9a90SDave Chinner * Deltas for the inode count are +/-64, hence we use a large batch size of 128 589f18c9a90SDave Chinner * so we don't need to take the counter lock on every update. 5901da177e4SLinus Torvalds */ 591f18c9a90SDave Chinner #define XFS_ICOUNT_BATCH 128 592f18c9a90SDave Chinner 59371e330b5SDave Chinner void 5941da177e4SLinus Torvalds xfs_trans_unreserve_and_mod_sb( 5950bd5ddedSDave Chinner struct xfs_trans *tp) 5961da177e4SLinus Torvalds { 5970bd5ddedSDave Chinner struct xfs_mount *mp = tp->t_mountp; 5980d485adaSDave Chinner bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0; 59945c34141SDavid Chinner int64_t blkdelta = 0; 60045c34141SDavid Chinner int64_t rtxdelta = 0; 6011b040712SChristoph Hellwig int64_t idelta = 0; 6021b040712SChristoph Hellwig int64_t ifreedelta = 0; 6030bd5ddedSDave Chinner int error; 6041da177e4SLinus Torvalds 6051b040712SChristoph Hellwig /* calculate deltas */ 60645c34141SDavid Chinner if (tp->t_blk_res > 0) 60745c34141SDavid Chinner blkdelta = tp->t_blk_res; 60845c34141SDavid Chinner if ((tp->t_fdblocks_delta != 0) && 60938c26bfdSDave Chinner (xfs_has_lazysbcount(mp) || 61045c34141SDavid Chinner (tp->t_flags & XFS_TRANS_SB_DIRTY))) 61145c34141SDavid Chinner blkdelta += tp->t_fdblocks_delta; 61245c34141SDavid Chinner 61345c34141SDavid Chinner if (tp->t_rtx_res > 0) 61445c34141SDavid Chinner rtxdelta = tp->t_rtx_res; 61545c34141SDavid Chinner if ((tp->t_frextents_delta != 0) && 61645c34141SDavid Chinner (tp->t_flags & XFS_TRANS_SB_DIRTY)) 61745c34141SDavid Chinner rtxdelta += tp->t_frextents_delta; 61845c34141SDavid Chinner 61938c26bfdSDave Chinner if (xfs_has_lazysbcount(mp) || 6201b040712SChristoph Hellwig (tp->t_flags & XFS_TRANS_SB_DIRTY)) { 6211b040712SChristoph Hellwig idelta = tp->t_icount_delta; 6221b040712SChristoph Hellwig ifreedelta = tp->t_ifree_delta; 6231b040712SChristoph Hellwig } 6241b040712SChristoph Hellwig 6251b040712SChristoph Hellwig /* apply the per-cpu counters */ 6261b040712SChristoph Hellwig if (blkdelta) { 6270d485adaSDave Chinner error = xfs_mod_fdblocks(mp, blkdelta, rsvd); 628dc3ffbb1SDave Chinner ASSERT(!error); 6291b040712SChristoph Hellwig } 6301b040712SChristoph Hellwig 6315825bea0SDave Chinner if (idelta) 632f18c9a90SDave Chinner percpu_counter_add_batch(&mp->m_icount, idelta, 633f18c9a90SDave Chinner XFS_ICOUNT_BATCH); 6341b040712SChristoph Hellwig 6355825bea0SDave Chinner if (ifreedelta) 636f18c9a90SDave Chinner percpu_counter_add(&mp->m_ifree, ifreedelta); 6371b040712SChristoph Hellwig 638*2229276cSDarrick J. Wong if (rtxdelta) { 639*2229276cSDarrick J. Wong error = xfs_mod_frextents(mp, rtxdelta); 640*2229276cSDarrick J. Wong ASSERT(!error); 641*2229276cSDarrick J. Wong } 642*2229276cSDarrick J. Wong 643*2229276cSDarrick J. Wong if (!(tp->t_flags & XFS_TRANS_SB_DIRTY)) 6440bd5ddedSDave Chinner return; 6450bd5ddedSDave Chinner 6461b040712SChristoph Hellwig /* apply remaining deltas */ 6470bd5ddedSDave Chinner spin_lock(&mp->m_sb_lock); 6486543990aSDave Chinner mp->m_sb.sb_fdblocks += tp->t_fdblocks_delta + tp->t_res_fdblocks_delta; 6496543990aSDave Chinner mp->m_sb.sb_icount += idelta; 6506543990aSDave Chinner mp->m_sb.sb_ifree += ifreedelta; 651*2229276cSDarrick J. Wong /* 652*2229276cSDarrick J. Wong * Do not touch sb_frextents here because we are dealing with incore 653*2229276cSDarrick J. Wong * reservation. sb_frextents is not part of the lazy sb counters so it 654*2229276cSDarrick J. Wong * must be consistent with the ondisk rtbitmap and must never include 655*2229276cSDarrick J. Wong * incore reservations. 656*2229276cSDarrick J. Wong */ 657dc3ffbb1SDave Chinner mp->m_sb.sb_dblocks += tp->t_dblocks_delta; 658dc3ffbb1SDave Chinner mp->m_sb.sb_agcount += tp->t_agcount_delta; 659dc3ffbb1SDave Chinner mp->m_sb.sb_imax_pct += tp->t_imaxpct_delta; 660dc3ffbb1SDave Chinner mp->m_sb.sb_rextsize += tp->t_rextsize_delta; 661dc3ffbb1SDave Chinner mp->m_sb.sb_rbmblocks += tp->t_rbmblocks_delta; 662dc3ffbb1SDave Chinner mp->m_sb.sb_rblocks += tp->t_rblocks_delta; 663dc3ffbb1SDave Chinner mp->m_sb.sb_rextents += tp->t_rextents_delta; 664dc3ffbb1SDave Chinner mp->m_sb.sb_rextslog += tp->t_rextslog_delta; 6650bd5ddedSDave Chinner spin_unlock(&mp->m_sb_lock); 6661b040712SChristoph Hellwig 667dc3ffbb1SDave Chinner /* 668dc3ffbb1SDave Chinner * Debug checks outside of the spinlock so they don't lock up the 669dc3ffbb1SDave Chinner * machine if they fail. 670dc3ffbb1SDave Chinner */ 671dc3ffbb1SDave Chinner ASSERT(mp->m_sb.sb_imax_pct >= 0); 672dc3ffbb1SDave Chinner ASSERT(mp->m_sb.sb_rextslog >= 0); 6731b040712SChristoph Hellwig return; 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds 676e6631f85SDave Chinner /* Add the given log item to the transaction's list of log items. */ 677e98c414fSChristoph Hellwig void 678e98c414fSChristoph Hellwig xfs_trans_add_item( 679e98c414fSChristoph Hellwig struct xfs_trans *tp, 680e98c414fSChristoph Hellwig struct xfs_log_item *lip) 681e98c414fSChristoph Hellwig { 682d86142ddSDave Chinner ASSERT(lip->li_log == tp->t_mountp->m_log); 683f65020a8SJesper Juhl ASSERT(lip->li_ailp == tp->t_mountp->m_ail); 684e6631f85SDave Chinner ASSERT(list_empty(&lip->li_trans)); 685e6631f85SDave Chinner ASSERT(!test_bit(XFS_LI_DIRTY, &lip->li_flags)); 686e98c414fSChristoph Hellwig 687e6631f85SDave Chinner list_add_tail(&lip->li_trans, &tp->t_items); 688ba18781bSDave Chinner trace_xfs_trans_add_item(tp, _RET_IP_); 689e98c414fSChristoph Hellwig } 690e98c414fSChristoph Hellwig 691e98c414fSChristoph Hellwig /* 692e6631f85SDave Chinner * Unlink the log item from the transaction. the log item is no longer 693e6631f85SDave Chinner * considered dirty in this transaction, as the linked transaction has 694e6631f85SDave Chinner * finished, either by abort or commit completion. 695e98c414fSChristoph Hellwig */ 696e98c414fSChristoph Hellwig void 697e98c414fSChristoph Hellwig xfs_trans_del_item( 698e98c414fSChristoph Hellwig struct xfs_log_item *lip) 699e98c414fSChristoph Hellwig { 700e6631f85SDave Chinner clear_bit(XFS_LI_DIRTY, &lip->li_flags); 701e6631f85SDave Chinner list_del_init(&lip->li_trans); 702e98c414fSChristoph Hellwig } 703e98c414fSChristoph Hellwig 704e6631f85SDave Chinner /* Detach and unlock all of the items in a transaction */ 705195cd83dSChristoph Hellwig static void 706e98c414fSChristoph Hellwig xfs_trans_free_items( 707e98c414fSChristoph Hellwig struct xfs_trans *tp, 708eacb24e7SChristoph Hellwig bool abort) 709e98c414fSChristoph Hellwig { 710e6631f85SDave Chinner struct xfs_log_item *lip, *next; 711e98c414fSChristoph Hellwig 712ba18781bSDave Chinner trace_xfs_trans_free_items(tp, _RET_IP_); 713ba18781bSDave Chinner 714e6631f85SDave Chinner list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) { 715e6631f85SDave Chinner xfs_trans_del_item(lip); 716eacb24e7SChristoph Hellwig if (abort) 71722525c17SDave Chinner set_bit(XFS_LI_ABORTED, &lip->li_flags); 718ddf92053SChristoph Hellwig if (lip->li_ops->iop_release) 719ddf92053SChristoph Hellwig lip->li_ops->iop_release(lip); 720e98c414fSChristoph Hellwig } 721e98c414fSChristoph Hellwig } 722e98c414fSChristoph Hellwig 7230e57f6a3SDave Chinner static inline void 7240e57f6a3SDave Chinner xfs_log_item_batch_insert( 7250e57f6a3SDave Chinner struct xfs_ail *ailp, 7261d8c95a3SDave Chinner struct xfs_ail_cursor *cur, 7270e57f6a3SDave Chinner struct xfs_log_item **log_items, 7280e57f6a3SDave Chinner int nr_items, 7290e57f6a3SDave Chinner xfs_lsn_t commit_lsn) 7300e57f6a3SDave Chinner { 7310e57f6a3SDave Chinner int i; 7320e57f6a3SDave Chinner 73357e80956SMatthew Wilcox spin_lock(&ailp->ail_lock); 73457e80956SMatthew Wilcox /* xfs_trans_ail_update_bulk drops ailp->ail_lock */ 7351d8c95a3SDave Chinner xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn); 7360e57f6a3SDave Chinner 737904c17e6SDave Chinner for (i = 0; i < nr_items; i++) { 738904c17e6SDave Chinner struct xfs_log_item *lip = log_items[i]; 739904c17e6SDave Chinner 740e8b78db7SChristoph Hellwig if (lip->li_ops->iop_unpin) 741904c17e6SDave Chinner lip->li_ops->iop_unpin(lip, 0); 742904c17e6SDave Chinner } 7430e57f6a3SDave Chinner } 7440e57f6a3SDave Chinner 7450e57f6a3SDave Chinner /* 7460e57f6a3SDave Chinner * Bulk operation version of xfs_trans_committed that takes a log vector of 7470e57f6a3SDave Chinner * items to insert into the AIL. This uses bulk AIL insertion techniques to 7480e57f6a3SDave Chinner * minimise lock traffic. 749e34a314cSDave Chinner * 750e34a314cSDave Chinner * If we are called with the aborted flag set, it is because a log write during 751e34a314cSDave Chinner * a CIL checkpoint commit has failed. In this case, all the items in the 752ddf92053SChristoph Hellwig * checkpoint have already gone through iop_committed and iop_committing, which 753e34a314cSDave Chinner * means that checkpoint commit abort handling is treated exactly the same 754e34a314cSDave Chinner * as an iclog write error even though we haven't started any IO yet. Hence in 755904c17e6SDave Chinner * this case all we need to do is iop_committed processing, followed by an 756904c17e6SDave Chinner * iop_unpin(aborted) call. 7571d8c95a3SDave Chinner * 7581d8c95a3SDave Chinner * The AIL cursor is used to optimise the insert process. If commit_lsn is not 7591d8c95a3SDave Chinner * at the end of the AIL, the insert cursor avoids the need to walk 7601d8c95a3SDave Chinner * the AIL to find the insertion point on every xfs_log_item_batch_insert() 7611d8c95a3SDave Chinner * call. This saves a lot of needless list walking and is a net win, even 7621d8c95a3SDave Chinner * though it slightly increases that amount of AIL lock traffic to set it up 7631d8c95a3SDave Chinner * and tear it down. 7640e57f6a3SDave Chinner */ 7650e57f6a3SDave Chinner void 7660e57f6a3SDave Chinner xfs_trans_committed_bulk( 7670e57f6a3SDave Chinner struct xfs_ail *ailp, 7680e57f6a3SDave Chinner struct xfs_log_vec *log_vector, 7690e57f6a3SDave Chinner xfs_lsn_t commit_lsn, 770d15cbf2fSChristoph Hellwig bool aborted) 7710e57f6a3SDave Chinner { 7720e57f6a3SDave Chinner #define LOG_ITEM_BATCH_SIZE 32 7730e57f6a3SDave Chinner struct xfs_log_item *log_items[LOG_ITEM_BATCH_SIZE]; 7740e57f6a3SDave Chinner struct xfs_log_vec *lv; 7751d8c95a3SDave Chinner struct xfs_ail_cursor cur; 7760e57f6a3SDave Chinner int i = 0; 7770e57f6a3SDave Chinner 77857e80956SMatthew Wilcox spin_lock(&ailp->ail_lock); 7791d8c95a3SDave Chinner xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn); 78057e80956SMatthew Wilcox spin_unlock(&ailp->ail_lock); 7811d8c95a3SDave Chinner 7820e57f6a3SDave Chinner /* unpin all the log items */ 7830e57f6a3SDave Chinner for (lv = log_vector; lv; lv = lv->lv_next ) { 7840e57f6a3SDave Chinner struct xfs_log_item *lip = lv->lv_item; 7850e57f6a3SDave Chinner xfs_lsn_t item_lsn; 7860e57f6a3SDave Chinner 7870e57f6a3SDave Chinner if (aborted) 78822525c17SDave Chinner set_bit(XFS_LI_ABORTED, &lip->li_flags); 7899ce632a2SChristoph Hellwig 7909ce632a2SChristoph Hellwig if (lip->li_ops->flags & XFS_ITEM_RELEASE_WHEN_COMMITTED) { 7919ce632a2SChristoph Hellwig lip->li_ops->iop_release(lip); 7929ce632a2SChristoph Hellwig continue; 7939ce632a2SChristoph Hellwig } 7949ce632a2SChristoph Hellwig 795e8b78db7SChristoph Hellwig if (lip->li_ops->iop_committed) 796904c17e6SDave Chinner item_lsn = lip->li_ops->iop_committed(lip, commit_lsn); 797e8b78db7SChristoph Hellwig else 798e8b78db7SChristoph Hellwig item_lsn = commit_lsn; 7990e57f6a3SDave Chinner 8001316d4daSDave Chinner /* item_lsn of -1 means the item needs no further processing */ 8010e57f6a3SDave Chinner if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0) 8020e57f6a3SDave Chinner continue; 8030e57f6a3SDave Chinner 804e34a314cSDave Chinner /* 805e34a314cSDave Chinner * if we are aborting the operation, no point in inserting the 806e34a314cSDave Chinner * object into the AIL as we are in a shutdown situation. 807e34a314cSDave Chinner */ 808e34a314cSDave Chinner if (aborted) { 8098eda8721SDave Chinner ASSERT(xlog_is_shutdown(ailp->ail_log)); 810e8b78db7SChristoph Hellwig if (lip->li_ops->iop_unpin) 811904c17e6SDave Chinner lip->li_ops->iop_unpin(lip, 1); 812e34a314cSDave Chinner continue; 813e34a314cSDave Chinner } 814e34a314cSDave Chinner 8150e57f6a3SDave Chinner if (item_lsn != commit_lsn) { 8160e57f6a3SDave Chinner 8170e57f6a3SDave Chinner /* 8180e57f6a3SDave Chinner * Not a bulk update option due to unusual item_lsn. 8190e57f6a3SDave Chinner * Push into AIL immediately, rechecking the lsn once 8201d8c95a3SDave Chinner * we have the ail lock. Then unpin the item. This does 8211d8c95a3SDave Chinner * not affect the AIL cursor the bulk insert path is 8221d8c95a3SDave Chinner * using. 8230e57f6a3SDave Chinner */ 82457e80956SMatthew Wilcox spin_lock(&ailp->ail_lock); 8250e57f6a3SDave Chinner if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) 8260e57f6a3SDave Chinner xfs_trans_ail_update(ailp, lip, item_lsn); 8270e57f6a3SDave Chinner else 82857e80956SMatthew Wilcox spin_unlock(&ailp->ail_lock); 829e8b78db7SChristoph Hellwig if (lip->li_ops->iop_unpin) 830904c17e6SDave Chinner lip->li_ops->iop_unpin(lip, 0); 8310e57f6a3SDave Chinner continue; 8320e57f6a3SDave Chinner } 8330e57f6a3SDave Chinner 8340e57f6a3SDave Chinner /* Item is a candidate for bulk AIL insert. */ 8350e57f6a3SDave Chinner log_items[i++] = lv->lv_item; 8360e57f6a3SDave Chinner if (i >= LOG_ITEM_BATCH_SIZE) { 8371d8c95a3SDave Chinner xfs_log_item_batch_insert(ailp, &cur, log_items, 8380e57f6a3SDave Chinner LOG_ITEM_BATCH_SIZE, commit_lsn); 8390e57f6a3SDave Chinner i = 0; 8400e57f6a3SDave Chinner } 8410e57f6a3SDave Chinner } 8420e57f6a3SDave Chinner 8430e57f6a3SDave Chinner /* make sure we insert the remainder! */ 8440e57f6a3SDave Chinner if (i) 8451d8c95a3SDave Chinner xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn); 8461d8c95a3SDave Chinner 84757e80956SMatthew Wilcox spin_lock(&ailp->ail_lock); 848e4a1e29cSEric Sandeen xfs_trans_ail_cursor_done(&cur); 84957e80956SMatthew Wilcox spin_unlock(&ailp->ail_lock); 8500e57f6a3SDave Chinner } 8510e57f6a3SDave Chinner 852b1c1b5b6SDave Chinner /* 853b1037058SChristoph Hellwig * Commit the given transaction to the log. 8540924378aSDave Chinner * 8550924378aSDave Chinner * XFS disk error handling mechanism is not based on a typical 8560924378aSDave Chinner * transaction abort mechanism. Logically after the filesystem 8570924378aSDave Chinner * gets marked 'SHUTDOWN', we can't let any new transactions 8580924378aSDave Chinner * be durable - ie. committed to disk - because some metadata might 8590924378aSDave Chinner * be inconsistent. In such cases, this returns an error, and the 8600924378aSDave Chinner * caller may assume that all locked objects joined to the transaction 8610924378aSDave Chinner * have already been unlocked as if the commit had succeeded. 8620924378aSDave Chinner * Do not reference the transaction structure after this call. 8630924378aSDave Chinner */ 86470393313SChristoph Hellwig static int 86570393313SChristoph Hellwig __xfs_trans_commit( 866a3ccd2caSChristoph Hellwig struct xfs_trans *tp, 86770393313SChristoph Hellwig bool regrant) 8680924378aSDave Chinner { 869a3ccd2caSChristoph Hellwig struct xfs_mount *mp = tp->t_mountp; 8703c4cb76bSDave Chinner struct xlog *log = mp->m_log; 8715f9b4b0dSDave Chinner xfs_csn_t commit_seq = 0; 872a3ccd2caSChristoph Hellwig int error = 0; 8730924378aSDave Chinner int sync = tp->t_flags & XFS_TRANS_SYNC; 8740924378aSDave Chinner 875ba18781bSDave Chinner trace_xfs_trans_commit(tp, _RET_IP_); 876ba18781bSDave Chinner 87798719051SBrian Foster /* 87898719051SBrian Foster * Finish deferred items on final commit. Only permanent transactions 87998719051SBrian Foster * should ever have deferred ops. 88098719051SBrian Foster */ 8819d9e6233SBrian Foster WARN_ON_ONCE(!list_empty(&tp->t_dfops) && 88298719051SBrian Foster !(tp->t_flags & XFS_TRANS_PERM_LOG_RES)); 88398719051SBrian Foster if (!regrant && (tp->t_flags & XFS_TRANS_PERM_LOG_RES)) { 884b277c37fSBrian Foster error = xfs_defer_finish_noroll(&tp); 8859b1f4e98SBrian Foster if (error) 886e021a2e5SBrian Foster goto out_unreserve; 887e021a2e5SBrian Foster } 888e021a2e5SBrian Foster 8890924378aSDave Chinner /* 8900924378aSDave Chinner * If there is nothing to be logged by the transaction, 8910924378aSDave Chinner * then unlock all of the items associated with the 8920924378aSDave Chinner * transaction and free the transaction structure. 8930924378aSDave Chinner * Also make sure to return any reserved blocks to 8940924378aSDave Chinner * the free pool. 8950924378aSDave Chinner */ 896a3ccd2caSChristoph Hellwig if (!(tp->t_flags & XFS_TRANS_DIRTY)) 897a3ccd2caSChristoph Hellwig goto out_unreserve; 898a3ccd2caSChristoph Hellwig 8993c4cb76bSDave Chinner /* 9003c4cb76bSDave Chinner * We must check against log shutdown here because we cannot abort log 9013c4cb76bSDave Chinner * items and leave them dirty, inconsistent and unpinned in memory while 9023c4cb76bSDave Chinner * the log is active. This leaves them open to being written back to 9033c4cb76bSDave Chinner * disk, and that will lead to on-disk corruption. 9043c4cb76bSDave Chinner */ 9053c4cb76bSDave Chinner if (xlog_is_shutdown(log)) { 9062451337dSDave Chinner error = -EIO; 907a3ccd2caSChristoph Hellwig goto out_unreserve; 9080924378aSDave Chinner } 909a3ccd2caSChristoph Hellwig 9100924378aSDave Chinner ASSERT(tp->t_ticket != NULL); 9110924378aSDave Chinner 9120924378aSDave Chinner /* 9130924378aSDave Chinner * If we need to update the superblock, then do it now. 9140924378aSDave Chinner */ 9150924378aSDave Chinner if (tp->t_flags & XFS_TRANS_SB_DIRTY) 9160924378aSDave Chinner xfs_trans_apply_sb_deltas(tp); 9170924378aSDave Chinner xfs_trans_apply_dquot_deltas(tp); 9180924378aSDave Chinner 9193c4cb76bSDave Chinner xlog_cil_commit(log, tp, &commit_seq, regrant); 9201da177e4SLinus Torvalds 9210244b960SChristoph Hellwig xfs_trans_free(tp); 9220244b960SChristoph Hellwig 9231da177e4SLinus Torvalds /* 9241da177e4SLinus Torvalds * If the transaction needs to be synchronous, then force the 9251da177e4SLinus Torvalds * log out now and wait for it. 9261da177e4SLinus Torvalds */ 9271da177e4SLinus Torvalds if (sync) { 9285f9b4b0dSDave Chinner error = xfs_log_force_seq(mp, commit_seq, XFS_LOG_SYNC, NULL); 929ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_trans_sync); 9301da177e4SLinus Torvalds } else { 931ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_trans_async); 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 934a3ccd2caSChristoph Hellwig return error; 935a3ccd2caSChristoph Hellwig 936a3ccd2caSChristoph Hellwig out_unreserve: 937a3ccd2caSChristoph Hellwig xfs_trans_unreserve_and_mod_sb(tp); 938a3ccd2caSChristoph Hellwig 939a3ccd2caSChristoph Hellwig /* 940a3ccd2caSChristoph Hellwig * It is indeed possible for the transaction to be not dirty but 941a3ccd2caSChristoph Hellwig * the dqinfo portion to be. All that means is that we have some 942a3ccd2caSChristoph Hellwig * (non-persistent) quota reservations that need to be unreserved. 943a3ccd2caSChristoph Hellwig */ 944a3ccd2caSChristoph Hellwig xfs_trans_unreserve_and_mod_dquots(tp); 945a3ccd2caSChristoph Hellwig if (tp->t_ticket) { 9463c4cb76bSDave Chinner if (regrant && !xlog_is_shutdown(log)) 9473c4cb76bSDave Chinner xfs_log_ticket_regrant(log, tp->t_ticket); 9488b41e3f9SChristoph Hellwig else 9493c4cb76bSDave Chinner xfs_log_ticket_ungrant(log, tp->t_ticket); 950ba18781bSDave Chinner tp->t_ticket = NULL; 951a3ccd2caSChristoph Hellwig } 952195cd83dSChristoph Hellwig xfs_trans_free_items(tp, !!error); 953a3ccd2caSChristoph Hellwig xfs_trans_free(tp); 954a3ccd2caSChristoph Hellwig 955ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_trans_empty); 956a3ccd2caSChristoph Hellwig return error; 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds 95970393313SChristoph Hellwig int 96070393313SChristoph Hellwig xfs_trans_commit( 96170393313SChristoph Hellwig struct xfs_trans *tp) 96270393313SChristoph Hellwig { 96370393313SChristoph Hellwig return __xfs_trans_commit(tp, false); 96470393313SChristoph Hellwig } 96570393313SChristoph Hellwig 9661da177e4SLinus Torvalds /* 9673c4cb76bSDave Chinner * Unlock all of the transaction's items and free the transaction. If the 9683c4cb76bSDave Chinner * transaction is dirty, we must shut down the filesystem because there is no 9693c4cb76bSDave Chinner * way to restore them to their previous state. 9701da177e4SLinus Torvalds * 9713c4cb76bSDave Chinner * If the transaction has made a log reservation, make sure to release it as 9723c4cb76bSDave Chinner * well. 9733c4cb76bSDave Chinner * 9743c4cb76bSDave Chinner * This is a high level function (equivalent to xfs_trans_commit()) and so can 9753c4cb76bSDave Chinner * be called after the transaction has effectively been aborted due to the mount 9763c4cb76bSDave Chinner * being shut down. However, if the mount has not been shut down and the 9773c4cb76bSDave Chinner * transaction is dirty we will shut the mount down and, in doing so, that 9783c4cb76bSDave Chinner * guarantees that the log is shut down, too. Hence we don't need to be as 9793c4cb76bSDave Chinner * careful with shutdown state and dirty items here as we need to be in 9803c4cb76bSDave Chinner * xfs_trans_commit(). 9811da177e4SLinus Torvalds */ 9821da177e4SLinus Torvalds void 9831da177e4SLinus Torvalds xfs_trans_cancel( 9844906e215SChristoph Hellwig struct xfs_trans *tp) 9851da177e4SLinus Torvalds { 9864906e215SChristoph Hellwig struct xfs_mount *mp = tp->t_mountp; 9873c4cb76bSDave Chinner struct xlog *log = mp->m_log; 9884906e215SChristoph Hellwig bool dirty = (tp->t_flags & XFS_TRANS_DIRTY); 9891da177e4SLinus Torvalds 990ba18781bSDave Chinner trace_xfs_trans_cancel(tp, _RET_IP_); 991ba18781bSDave Chinner 99247a6df7cSDarrick J. Wong /* 99347a6df7cSDarrick J. Wong * It's never valid to cancel a transaction with deferred ops attached, 99447a6df7cSDarrick J. Wong * because the transaction is effectively dirty. Complain about this 99547a6df7cSDarrick J. Wong * loudly before freeing the in-memory defer items. 99647a6df7cSDarrick J. Wong */ 99747a6df7cSDarrick J. Wong if (!list_empty(&tp->t_dfops)) { 99847a6df7cSDarrick J. Wong ASSERT(xfs_is_shutdown(mp) || list_empty(&tp->t_dfops)); 99947a6df7cSDarrick J. Wong ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); 100047a6df7cSDarrick J. Wong dirty = true; 10019e28a242SBrian Foster xfs_defer_cancel(tp); 100247a6df7cSDarrick J. Wong } 1003e021a2e5SBrian Foster 10041da177e4SLinus Torvalds /* 10053c4cb76bSDave Chinner * See if the caller is relying on us to shut down the filesystem. We 10063c4cb76bSDave Chinner * only want an error report if there isn't already a shutdown in 10073c4cb76bSDave Chinner * progress, so we only need to check against the mount shutdown state 10083c4cb76bSDave Chinner * here. 10091da177e4SLinus Torvalds */ 101075c8c50fSDave Chinner if (dirty && !xfs_is_shutdown(mp)) { 10110733af21SRyan Hankins XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp); 10127d04a335SNathan Scott xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 101360a204f0SNathan Scott } 10141da177e4SLinus Torvalds #ifdef DEBUG 10153c4cb76bSDave Chinner /* Log items need to be consistent until the log is shut down. */ 10163c4cb76bSDave Chinner if (!dirty && !xlog_is_shutdown(log)) { 1017e6631f85SDave Chinner struct xfs_log_item *lip; 10181da177e4SLinus Torvalds 1019e6631f85SDave Chinner list_for_each_entry(lip, &tp->t_items, li_trans) 1020d6b8fc6cSKaixu Xia ASSERT(!xlog_item_is_intent_done(lip)); 10211da177e4SLinus Torvalds } 10221da177e4SLinus Torvalds #endif 10231da177e4SLinus Torvalds xfs_trans_unreserve_and_mod_sb(tp); 10247d095257SChristoph Hellwig xfs_trans_unreserve_and_mod_dquots(tp); 10251da177e4SLinus Torvalds 1026ba18781bSDave Chinner if (tp->t_ticket) { 10273c4cb76bSDave Chinner xfs_log_ticket_ungrant(log, tp->t_ticket); 1028ba18781bSDave Chinner tp->t_ticket = NULL; 1029ba18781bSDave Chinner } 10301da177e4SLinus Torvalds 1031195cd83dSChristoph Hellwig xfs_trans_free_items(tp, dirty); 10321da177e4SLinus Torvalds xfs_trans_free(tp); 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds 1035322ff6b8SNiv Sardi /* 1036322ff6b8SNiv Sardi * Roll from one trans in the sequence of PERMANENT transactions to 1037322ff6b8SNiv Sardi * the next: permanent transactions are only flushed out when 103870393313SChristoph Hellwig * committed with xfs_trans_commit(), but we still want as soon 1039322ff6b8SNiv Sardi * as possible to let chunks of it go to the log. So we commit the 1040322ff6b8SNiv Sardi * chunk we've been working on and get a new transaction to continue. 1041322ff6b8SNiv Sardi */ 1042322ff6b8SNiv Sardi int 1043254133f5SChristoph Hellwig xfs_trans_roll( 1044411350dfSChristoph Hellwig struct xfs_trans **tpp) 1045322ff6b8SNiv Sardi { 1046411350dfSChristoph Hellwig struct xfs_trans *trans = *tpp; 10473d3c8b52SJie Liu struct xfs_trans_res tres; 1048322ff6b8SNiv Sardi int error; 1049322ff6b8SNiv Sardi 1050ba18781bSDave Chinner trace_xfs_trans_roll(trans, _RET_IP_); 1051ba18781bSDave Chinner 1052322ff6b8SNiv Sardi /* 1053322ff6b8SNiv Sardi * Copy the critical parameters from one trans to the next. 1054322ff6b8SNiv Sardi */ 10553d3c8b52SJie Liu tres.tr_logres = trans->t_log_res; 10563d3c8b52SJie Liu tres.tr_logcount = trans->t_log_count; 1057411350dfSChristoph Hellwig 1058322ff6b8SNiv Sardi *tpp = xfs_trans_dup(trans); 1059322ff6b8SNiv Sardi 1060322ff6b8SNiv Sardi /* 1061322ff6b8SNiv Sardi * Commit the current transaction. 1062322ff6b8SNiv Sardi * If this commit failed, then it'd just unlock those items that 1063322ff6b8SNiv Sardi * are not marked ihold. That also means that a filesystem shutdown 1064322ff6b8SNiv Sardi * is in progress. The caller takes the responsibility to cancel 1065322ff6b8SNiv Sardi * the duplicate transaction that gets returned. 1066322ff6b8SNiv Sardi */ 106770393313SChristoph Hellwig error = __xfs_trans_commit(trans, true); 1068322ff6b8SNiv Sardi if (error) 1069d99831ffSEric Sandeen return error; 1070322ff6b8SNiv Sardi 1071322ff6b8SNiv Sardi /* 1072411350dfSChristoph Hellwig * Reserve space in the log for the next transaction. 1073322ff6b8SNiv Sardi * This also pushes items in the "AIL", the list of logged items, 1074322ff6b8SNiv Sardi * out to disk if they are taking up space at the tail of the log 1075322ff6b8SNiv Sardi * that we want to use. This requires that either nothing be locked 1076322ff6b8SNiv Sardi * across this call, or that anything that is locked be logged in 1077322ff6b8SNiv Sardi * the prior and the next transactions. 1078322ff6b8SNiv Sardi */ 10793d3c8b52SJie Liu tres.tr_logflags = XFS_TRANS_PERM_LOG_RES; 1080411350dfSChristoph Hellwig return xfs_trans_reserve(*tpp, &tres, 0, 0); 1081322ff6b8SNiv Sardi } 10823a1af6c3SDarrick J. Wong 10833a1af6c3SDarrick J. Wong /* 10843a1af6c3SDarrick J. Wong * Allocate an transaction, lock and join the inode to it, and reserve quota. 10853a1af6c3SDarrick J. Wong * 10863a1af6c3SDarrick J. Wong * The caller must ensure that the on-disk dquots attached to this inode have 10873a1af6c3SDarrick J. Wong * already been allocated and initialized. The caller is responsible for 10883a1af6c3SDarrick J. Wong * releasing ILOCK_EXCL if a new transaction is returned. 10893a1af6c3SDarrick J. Wong */ 10903a1af6c3SDarrick J. Wong int 10913a1af6c3SDarrick J. Wong xfs_trans_alloc_inode( 10923a1af6c3SDarrick J. Wong struct xfs_inode *ip, 10933a1af6c3SDarrick J. Wong struct xfs_trans_res *resv, 10943a1af6c3SDarrick J. Wong unsigned int dblocks, 10953de4eb10SDarrick J. Wong unsigned int rblocks, 10963a1af6c3SDarrick J. Wong bool force, 10973a1af6c3SDarrick J. Wong struct xfs_trans **tpp) 10983a1af6c3SDarrick J. Wong { 10993a1af6c3SDarrick J. Wong struct xfs_trans *tp; 11003a1af6c3SDarrick J. Wong struct xfs_mount *mp = ip->i_mount; 1101766aabd5SDarrick J. Wong bool retried = false; 11023a1af6c3SDarrick J. Wong int error; 11033a1af6c3SDarrick J. Wong 1104766aabd5SDarrick J. Wong retry: 11053de4eb10SDarrick J. Wong error = xfs_trans_alloc(mp, resv, dblocks, 11063de4eb10SDarrick J. Wong rblocks / mp->m_sb.sb_rextsize, 11073a1af6c3SDarrick J. Wong force ? XFS_TRANS_RESERVE : 0, &tp); 11083a1af6c3SDarrick J. Wong if (error) 11093a1af6c3SDarrick J. Wong return error; 11103a1af6c3SDarrick J. Wong 11113a1af6c3SDarrick J. Wong xfs_ilock(ip, XFS_ILOCK_EXCL); 11123a1af6c3SDarrick J. Wong xfs_trans_ijoin(tp, ip, 0); 11133a1af6c3SDarrick J. Wong 11143a1af6c3SDarrick J. Wong error = xfs_qm_dqattach_locked(ip, false); 11153a1af6c3SDarrick J. Wong if (error) { 11163a1af6c3SDarrick J. Wong /* Caller should have allocated the dquots! */ 11173a1af6c3SDarrick J. Wong ASSERT(error != -ENOENT); 11183a1af6c3SDarrick J. Wong goto out_cancel; 11193a1af6c3SDarrick J. Wong } 11203a1af6c3SDarrick J. Wong 11213de4eb10SDarrick J. Wong error = xfs_trans_reserve_quota_nblks(tp, ip, dblocks, rblocks, force); 1122766aabd5SDarrick J. Wong if ((error == -EDQUOT || error == -ENOSPC) && !retried) { 1123766aabd5SDarrick J. Wong xfs_trans_cancel(tp); 1124766aabd5SDarrick J. Wong xfs_iunlock(ip, XFS_ILOCK_EXCL); 1125766aabd5SDarrick J. Wong xfs_blockgc_free_quota(ip, 0); 1126766aabd5SDarrick J. Wong retried = true; 1127766aabd5SDarrick J. Wong goto retry; 1128766aabd5SDarrick J. Wong } 11293a1af6c3SDarrick J. Wong if (error) 11303a1af6c3SDarrick J. Wong goto out_cancel; 11313a1af6c3SDarrick J. Wong 11323a1af6c3SDarrick J. Wong *tpp = tp; 11333a1af6c3SDarrick J. Wong return 0; 11343a1af6c3SDarrick J. Wong 11353a1af6c3SDarrick J. Wong out_cancel: 11363a1af6c3SDarrick J. Wong xfs_trans_cancel(tp); 11373a1af6c3SDarrick J. Wong xfs_iunlock(ip, XFS_ILOCK_EXCL); 11383a1af6c3SDarrick J. Wong return error; 11393a1af6c3SDarrick J. Wong } 1140f2f7b9ffSDarrick J. Wong 1141f2f7b9ffSDarrick J. Wong /* 1142f2f7b9ffSDarrick J. Wong * Allocate an transaction in preparation for inode creation by reserving quota 1143f2f7b9ffSDarrick J. Wong * against the given dquots. Callers are not required to hold any inode locks. 1144f2f7b9ffSDarrick J. Wong */ 1145f2f7b9ffSDarrick J. Wong int 1146f2f7b9ffSDarrick J. Wong xfs_trans_alloc_icreate( 1147f2f7b9ffSDarrick J. Wong struct xfs_mount *mp, 1148f2f7b9ffSDarrick J. Wong struct xfs_trans_res *resv, 1149f2f7b9ffSDarrick J. Wong struct xfs_dquot *udqp, 1150f2f7b9ffSDarrick J. Wong struct xfs_dquot *gdqp, 1151f2f7b9ffSDarrick J. Wong struct xfs_dquot *pdqp, 1152f2f7b9ffSDarrick J. Wong unsigned int dblocks, 1153f2f7b9ffSDarrick J. Wong struct xfs_trans **tpp) 1154f2f7b9ffSDarrick J. Wong { 1155f2f7b9ffSDarrick J. Wong struct xfs_trans *tp; 1156c237dd7cSDarrick J. Wong bool retried = false; 1157f2f7b9ffSDarrick J. Wong int error; 1158f2f7b9ffSDarrick J. Wong 1159c237dd7cSDarrick J. Wong retry: 1160f2f7b9ffSDarrick J. Wong error = xfs_trans_alloc(mp, resv, dblocks, 0, 0, &tp); 1161f2f7b9ffSDarrick J. Wong if (error) 1162f2f7b9ffSDarrick J. Wong return error; 1163f2f7b9ffSDarrick J. Wong 1164f2f7b9ffSDarrick J. Wong error = xfs_trans_reserve_quota_icreate(tp, udqp, gdqp, pdqp, dblocks); 1165c237dd7cSDarrick J. Wong if ((error == -EDQUOT || error == -ENOSPC) && !retried) { 1166c237dd7cSDarrick J. Wong xfs_trans_cancel(tp); 1167c237dd7cSDarrick J. Wong xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0); 1168c237dd7cSDarrick J. Wong retried = true; 1169c237dd7cSDarrick J. Wong goto retry; 1170c237dd7cSDarrick J. Wong } 1171f2f7b9ffSDarrick J. Wong if (error) { 1172f2f7b9ffSDarrick J. Wong xfs_trans_cancel(tp); 1173f2f7b9ffSDarrick J. Wong return error; 1174f2f7b9ffSDarrick J. Wong } 1175f2f7b9ffSDarrick J. Wong 1176f2f7b9ffSDarrick J. Wong *tpp = tp; 1177f2f7b9ffSDarrick J. Wong return 0; 1178f2f7b9ffSDarrick J. Wong } 11797317a03dSDarrick J. Wong 11807317a03dSDarrick J. Wong /* 11817317a03dSDarrick J. Wong * Allocate an transaction, lock and join the inode to it, and reserve quota 11827317a03dSDarrick J. Wong * in preparation for inode attribute changes that include uid, gid, or prid 11837317a03dSDarrick J. Wong * changes. 11847317a03dSDarrick J. Wong * 11857317a03dSDarrick J. Wong * The caller must ensure that the on-disk dquots attached to this inode have 11867317a03dSDarrick J. Wong * already been allocated and initialized. The ILOCK will be dropped when the 11877317a03dSDarrick J. Wong * transaction is committed or cancelled. 11887317a03dSDarrick J. Wong */ 11897317a03dSDarrick J. Wong int 11907317a03dSDarrick J. Wong xfs_trans_alloc_ichange( 11917317a03dSDarrick J. Wong struct xfs_inode *ip, 1192758303d1SDarrick J. Wong struct xfs_dquot *new_udqp, 1193758303d1SDarrick J. Wong struct xfs_dquot *new_gdqp, 1194758303d1SDarrick J. Wong struct xfs_dquot *new_pdqp, 11957317a03dSDarrick J. Wong bool force, 11967317a03dSDarrick J. Wong struct xfs_trans **tpp) 11977317a03dSDarrick J. Wong { 11987317a03dSDarrick J. Wong struct xfs_trans *tp; 11997317a03dSDarrick J. Wong struct xfs_mount *mp = ip->i_mount; 1200758303d1SDarrick J. Wong struct xfs_dquot *udqp; 1201758303d1SDarrick J. Wong struct xfs_dquot *gdqp; 1202758303d1SDarrick J. Wong struct xfs_dquot *pdqp; 1203758303d1SDarrick J. Wong bool retried = false; 12047317a03dSDarrick J. Wong int error; 12057317a03dSDarrick J. Wong 1206758303d1SDarrick J. Wong retry: 12077317a03dSDarrick J. Wong error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp); 12087317a03dSDarrick J. Wong if (error) 12097317a03dSDarrick J. Wong return error; 12107317a03dSDarrick J. Wong 12117317a03dSDarrick J. Wong xfs_ilock(ip, XFS_ILOCK_EXCL); 12127317a03dSDarrick J. Wong xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); 12137317a03dSDarrick J. Wong 12147317a03dSDarrick J. Wong error = xfs_qm_dqattach_locked(ip, false); 12157317a03dSDarrick J. Wong if (error) { 12167317a03dSDarrick J. Wong /* Caller should have allocated the dquots! */ 12177317a03dSDarrick J. Wong ASSERT(error != -ENOENT); 12187317a03dSDarrick J. Wong goto out_cancel; 12197317a03dSDarrick J. Wong } 12207317a03dSDarrick J. Wong 12217317a03dSDarrick J. Wong /* 12227317a03dSDarrick J. Wong * For each quota type, skip quota reservations if the inode's dquots 12237317a03dSDarrick J. Wong * now match the ones that came from the caller, or the caller didn't 1224758303d1SDarrick J. Wong * pass one in. The inode's dquots can change if we drop the ILOCK to 1225758303d1SDarrick J. Wong * perform a blockgc scan, so we must preserve the caller's arguments. 12267317a03dSDarrick J. Wong */ 1227758303d1SDarrick J. Wong udqp = (new_udqp != ip->i_udquot) ? new_udqp : NULL; 1228758303d1SDarrick J. Wong gdqp = (new_gdqp != ip->i_gdquot) ? new_gdqp : NULL; 1229758303d1SDarrick J. Wong pdqp = (new_pdqp != ip->i_pdquot) ? new_pdqp : NULL; 12307317a03dSDarrick J. Wong if (udqp || gdqp || pdqp) { 12315c615f0fSDarrick J. Wong unsigned int qflags = XFS_QMOPT_RES_REGBLKS; 12325c615f0fSDarrick J. Wong 12335c615f0fSDarrick J. Wong if (force) 12345c615f0fSDarrick J. Wong qflags |= XFS_QMOPT_FORCE_RES; 12355c615f0fSDarrick J. Wong 12365c615f0fSDarrick J. Wong /* 12375c615f0fSDarrick J. Wong * Reserve enough quota to handle blocks on disk and reserved 12385c615f0fSDarrick J. Wong * for a delayed allocation. We'll actually transfer the 12395c615f0fSDarrick J. Wong * delalloc reservation between dquots at chown time, even 12405c615f0fSDarrick J. Wong * though that part is only semi-transactional. 12415c615f0fSDarrick J. Wong */ 12425c615f0fSDarrick J. Wong error = xfs_trans_reserve_quota_bydquots(tp, mp, udqp, gdqp, 12436e73a545SChristoph Hellwig pdqp, ip->i_nblocks + ip->i_delayed_blks, 12445c615f0fSDarrick J. Wong 1, qflags); 1245758303d1SDarrick J. Wong if ((error == -EDQUOT || error == -ENOSPC) && !retried) { 1246758303d1SDarrick J. Wong xfs_trans_cancel(tp); 1247758303d1SDarrick J. Wong xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0); 1248758303d1SDarrick J. Wong retried = true; 1249758303d1SDarrick J. Wong goto retry; 1250758303d1SDarrick J. Wong } 12517317a03dSDarrick J. Wong if (error) 12527317a03dSDarrick J. Wong goto out_cancel; 12537317a03dSDarrick J. Wong } 12547317a03dSDarrick J. Wong 12557317a03dSDarrick J. Wong *tpp = tp; 12567317a03dSDarrick J. Wong return 0; 12577317a03dSDarrick J. Wong 12587317a03dSDarrick J. Wong out_cancel: 12597317a03dSDarrick J. Wong xfs_trans_cancel(tp); 12607317a03dSDarrick J. Wong return error; 12617317a03dSDarrick J. Wong } 1262871b9316SDarrick J. Wong 1263871b9316SDarrick J. Wong /* 1264871b9316SDarrick J. Wong * Allocate an transaction, lock and join the directory and child inodes to it, 1265871b9316SDarrick J. Wong * and reserve quota for a directory update. If there isn't sufficient space, 1266871b9316SDarrick J. Wong * @dblocks will be set to zero for a reservationless directory update and 1267871b9316SDarrick J. Wong * @nospace_error will be set to a negative errno describing the space 1268871b9316SDarrick J. Wong * constraint we hit. 1269871b9316SDarrick J. Wong * 1270871b9316SDarrick J. Wong * The caller must ensure that the on-disk dquots attached to this inode have 1271871b9316SDarrick J. Wong * already been allocated and initialized. The ILOCKs will be dropped when the 1272871b9316SDarrick J. Wong * transaction is committed or cancelled. 1273871b9316SDarrick J. Wong */ 1274871b9316SDarrick J. Wong int 1275871b9316SDarrick J. Wong xfs_trans_alloc_dir( 1276871b9316SDarrick J. Wong struct xfs_inode *dp, 1277871b9316SDarrick J. Wong struct xfs_trans_res *resv, 1278871b9316SDarrick J. Wong struct xfs_inode *ip, 1279871b9316SDarrick J. Wong unsigned int *dblocks, 1280871b9316SDarrick J. Wong struct xfs_trans **tpp, 1281871b9316SDarrick J. Wong int *nospace_error) 1282871b9316SDarrick J. Wong { 1283871b9316SDarrick J. Wong struct xfs_trans *tp; 1284871b9316SDarrick J. Wong struct xfs_mount *mp = ip->i_mount; 1285871b9316SDarrick J. Wong unsigned int resblks; 1286871b9316SDarrick J. Wong bool retried = false; 1287871b9316SDarrick J. Wong int error; 1288871b9316SDarrick J. Wong 1289871b9316SDarrick J. Wong retry: 1290871b9316SDarrick J. Wong *nospace_error = 0; 1291871b9316SDarrick J. Wong resblks = *dblocks; 1292871b9316SDarrick J. Wong error = xfs_trans_alloc(mp, resv, resblks, 0, 0, &tp); 1293871b9316SDarrick J. Wong if (error == -ENOSPC) { 1294871b9316SDarrick J. Wong *nospace_error = error; 1295871b9316SDarrick J. Wong resblks = 0; 1296871b9316SDarrick J. Wong error = xfs_trans_alloc(mp, resv, resblks, 0, 0, &tp); 1297871b9316SDarrick J. Wong } 1298871b9316SDarrick J. Wong if (error) 1299871b9316SDarrick J. Wong return error; 1300871b9316SDarrick J. Wong 1301871b9316SDarrick J. Wong xfs_lock_two_inodes(dp, XFS_ILOCK_EXCL, ip, XFS_ILOCK_EXCL); 1302871b9316SDarrick J. Wong 1303871b9316SDarrick J. Wong xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); 1304871b9316SDarrick J. Wong xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); 1305871b9316SDarrick J. Wong 1306871b9316SDarrick J. Wong error = xfs_qm_dqattach_locked(dp, false); 1307871b9316SDarrick J. Wong if (error) { 1308871b9316SDarrick J. Wong /* Caller should have allocated the dquots! */ 1309871b9316SDarrick J. Wong ASSERT(error != -ENOENT); 1310871b9316SDarrick J. Wong goto out_cancel; 1311871b9316SDarrick J. Wong } 1312871b9316SDarrick J. Wong 1313871b9316SDarrick J. Wong error = xfs_qm_dqattach_locked(ip, false); 1314871b9316SDarrick J. Wong if (error) { 1315871b9316SDarrick J. Wong /* Caller should have allocated the dquots! */ 1316871b9316SDarrick J. Wong ASSERT(error != -ENOENT); 1317871b9316SDarrick J. Wong goto out_cancel; 1318871b9316SDarrick J. Wong } 1319871b9316SDarrick J. Wong 1320871b9316SDarrick J. Wong if (resblks == 0) 1321871b9316SDarrick J. Wong goto done; 1322871b9316SDarrick J. Wong 1323871b9316SDarrick J. Wong error = xfs_trans_reserve_quota_nblks(tp, dp, resblks, 0, false); 1324871b9316SDarrick J. Wong if (error == -EDQUOT || error == -ENOSPC) { 1325871b9316SDarrick J. Wong if (!retried) { 1326871b9316SDarrick J. Wong xfs_trans_cancel(tp); 1327871b9316SDarrick J. Wong xfs_blockgc_free_quota(dp, 0); 1328871b9316SDarrick J. Wong retried = true; 1329871b9316SDarrick J. Wong goto retry; 1330871b9316SDarrick J. Wong } 1331871b9316SDarrick J. Wong 1332871b9316SDarrick J. Wong *nospace_error = error; 1333871b9316SDarrick J. Wong resblks = 0; 1334871b9316SDarrick J. Wong error = 0; 1335871b9316SDarrick J. Wong } 1336871b9316SDarrick J. Wong if (error) 1337871b9316SDarrick J. Wong goto out_cancel; 1338871b9316SDarrick J. Wong 1339871b9316SDarrick J. Wong done: 1340871b9316SDarrick J. Wong *tpp = tp; 1341871b9316SDarrick J. Wong *dblocks = resblks; 1342871b9316SDarrick J. Wong return 0; 1343871b9316SDarrick J. Wong 1344871b9316SDarrick J. Wong out_cancel: 1345871b9316SDarrick J. Wong xfs_trans_cancel(tp); 1346871b9316SDarrick J. Wong return error; 1347871b9316SDarrick J. Wong } 1348