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" 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 281da177e4SLinus Torvalds kmem_zone_t *xfs_trans_zone; 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); 79377bcd5fSCarlos Maiolino kmem_cache_free(xfs_trans_zone, 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 9832a2b11fSCarlos Maiolino ntp = kmem_cache_zalloc(xfs_trans_zone, 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: 26632a2b11fSCarlos 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); 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) || 278f74681baSBrian Foster xfs_sb_version_haslazysbcount(&mp->m_sb)); 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 */ 298a1a7d05aSDarrick J. Wong error = xfs_blockgc_free_space(mp, NULL); 2999febcda6SDarrick J. Wong if (error) 3009febcda6SDarrick J. Wong return error; 3019febcda6SDarrick J. Wong 3029febcda6SDarrick J. Wong want_retry = false; 3039febcda6SDarrick J. Wong goto retry; 304a1a7d05aSDarrick J. Wong } 305253f4911SChristoph Hellwig if (error) { 306253f4911SChristoph Hellwig xfs_trans_cancel(tp); 307253f4911SChristoph Hellwig return error; 308253f4911SChristoph Hellwig } 309253f4911SChristoph Hellwig 310ba18781bSDave Chinner trace_xfs_trans_alloc(tp, _RET_IP_); 311ba18781bSDave Chinner 312253f4911SChristoph Hellwig *tpp = tp; 313253f4911SChristoph Hellwig return 0; 314253f4911SChristoph Hellwig } 315253f4911SChristoph Hellwig 3161da177e4SLinus Torvalds /* 317e89c0413SDarrick J. Wong * Create an empty transaction with no reservation. This is a defensive 318b41b46c2SDave Chinner * mechanism for routines that query metadata without actually modifying them -- 319b41b46c2SDave Chinner * if the metadata being queried is somehow cross-linked (think a btree block 320b41b46c2SDave Chinner * pointer that points higher in the tree), we risk deadlock. However, blocks 321b41b46c2SDave Chinner * grabbed as part of a transaction can be re-grabbed. The verifiers will 322b41b46c2SDave Chinner * notice the corrupt block and the operation will fail back to userspace 323b41b46c2SDave Chinner * without deadlocking. 324e89c0413SDarrick J. Wong * 325b41b46c2SDave Chinner * Note the zero-length reservation; this transaction MUST be cancelled without 326b41b46c2SDave Chinner * any dirty data. 32727fb5a72SDarrick J. Wong * 328b41b46c2SDave Chinner * Callers should obtain freeze protection to avoid a conflict with fs freezing 329b41b46c2SDave Chinner * where we can be grabbing buffers at the same time that freeze is trying to 330b41b46c2SDave Chinner * drain the buffer LRU list. 331e89c0413SDarrick J. Wong */ 332e89c0413SDarrick J. Wong int 333e89c0413SDarrick J. Wong xfs_trans_alloc_empty( 334e89c0413SDarrick J. Wong struct xfs_mount *mp, 335e89c0413SDarrick J. Wong struct xfs_trans **tpp) 336e89c0413SDarrick J. Wong { 337e89c0413SDarrick J. Wong struct xfs_trans_res resv = {0}; 338e89c0413SDarrick J. Wong 339e89c0413SDarrick J. Wong return xfs_trans_alloc(mp, &resv, 0, 0, XFS_TRANS_NO_WRITECOUNT, tpp); 340e89c0413SDarrick J. Wong } 341e89c0413SDarrick J. Wong 342e89c0413SDarrick J. Wong /* 3431da177e4SLinus Torvalds * Record the indicated change to the given field for application 3441da177e4SLinus Torvalds * to the file system's superblock when the transaction commits. 3451da177e4SLinus Torvalds * For now, just store the change in the transaction structure. 3461da177e4SLinus Torvalds * 3471da177e4SLinus Torvalds * Mark the transaction structure to indicate that the superblock 3481da177e4SLinus Torvalds * needs to be updated before committing. 34992821e2bSDavid Chinner * 35092821e2bSDavid Chinner * Because we may not be keeping track of allocated/free inodes and 35192821e2bSDavid Chinner * used filesystem blocks in the superblock, we do not mark the 35292821e2bSDavid Chinner * superblock dirty in this transaction if we modify these fields. 35392821e2bSDavid Chinner * We still need to update the transaction deltas so that they get 35492821e2bSDavid Chinner * applied to the incore superblock, but we don't want them to 35592821e2bSDavid Chinner * cause the superblock to get locked and logged if these are the 35692821e2bSDavid Chinner * only fields in the superblock that the transaction modifies. 3571da177e4SLinus Torvalds */ 3581da177e4SLinus Torvalds void 3591da177e4SLinus Torvalds xfs_trans_mod_sb( 3601da177e4SLinus Torvalds xfs_trans_t *tp, 3611da177e4SLinus Torvalds uint field, 36220f4ebf2SDavid Chinner int64_t delta) 3631da177e4SLinus Torvalds { 36492821e2bSDavid Chinner uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY); 36592821e2bSDavid Chinner xfs_mount_t *mp = tp->t_mountp; 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds switch (field) { 3681da177e4SLinus Torvalds case XFS_TRANS_SB_ICOUNT: 3691da177e4SLinus Torvalds tp->t_icount_delta += delta; 37092821e2bSDavid Chinner if (xfs_sb_version_haslazysbcount(&mp->m_sb)) 37192821e2bSDavid Chinner flags &= ~XFS_TRANS_SB_DIRTY; 3721da177e4SLinus Torvalds break; 3731da177e4SLinus Torvalds case XFS_TRANS_SB_IFREE: 3741da177e4SLinus Torvalds tp->t_ifree_delta += delta; 37592821e2bSDavid Chinner if (xfs_sb_version_haslazysbcount(&mp->m_sb)) 37692821e2bSDavid Chinner flags &= ~XFS_TRANS_SB_DIRTY; 3771da177e4SLinus Torvalds break; 3781da177e4SLinus Torvalds case XFS_TRANS_SB_FDBLOCKS: 3791da177e4SLinus Torvalds /* 3803e78b9a4SBrian Foster * Track the number of blocks allocated in the transaction. 3813e78b9a4SBrian Foster * Make sure it does not exceed the number reserved. If so, 3823e78b9a4SBrian Foster * shutdown as this can lead to accounting inconsistency. 3831da177e4SLinus Torvalds */ 3841da177e4SLinus Torvalds if (delta < 0) { 3851da177e4SLinus Torvalds tp->t_blk_res_used += (uint)-delta; 3863e78b9a4SBrian Foster if (tp->t_blk_res_used > tp->t_blk_res) 3873e78b9a4SBrian Foster xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 388f74681baSBrian Foster } else if (delta > 0 && (tp->t_flags & XFS_TRANS_RES_FDBLKS)) { 389f74681baSBrian Foster int64_t blkres_delta; 390f74681baSBrian Foster 391f74681baSBrian Foster /* 392f74681baSBrian Foster * Return freed blocks directly to the reservation 393f74681baSBrian Foster * instead of the global pool, being careful not to 394f74681baSBrian Foster * overflow the trans counter. This is used to preserve 395f74681baSBrian Foster * reservation across chains of transaction rolls that 396f74681baSBrian Foster * repeatedly free and allocate blocks. 397f74681baSBrian Foster */ 398f74681baSBrian Foster blkres_delta = min_t(int64_t, delta, 399f74681baSBrian Foster UINT_MAX - tp->t_blk_res); 400f74681baSBrian Foster tp->t_blk_res += blkres_delta; 401f74681baSBrian Foster delta -= blkres_delta; 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds tp->t_fdblocks_delta += delta; 40492821e2bSDavid Chinner if (xfs_sb_version_haslazysbcount(&mp->m_sb)) 40592821e2bSDavid Chinner flags &= ~XFS_TRANS_SB_DIRTY; 4061da177e4SLinus Torvalds break; 4071da177e4SLinus Torvalds case XFS_TRANS_SB_RES_FDBLOCKS: 4081da177e4SLinus Torvalds /* 4091da177e4SLinus Torvalds * The allocation has already been applied to the 4101da177e4SLinus Torvalds * in-core superblock's counter. This should only 4111da177e4SLinus Torvalds * be applied to the on-disk superblock. 4121da177e4SLinus Torvalds */ 4131da177e4SLinus Torvalds tp->t_res_fdblocks_delta += delta; 41492821e2bSDavid Chinner if (xfs_sb_version_haslazysbcount(&mp->m_sb)) 41592821e2bSDavid Chinner flags &= ~XFS_TRANS_SB_DIRTY; 4161da177e4SLinus Torvalds break; 4171da177e4SLinus Torvalds case XFS_TRANS_SB_FREXTENTS: 4181da177e4SLinus Torvalds /* 4191da177e4SLinus Torvalds * Track the number of blocks allocated in the 4201da177e4SLinus Torvalds * transaction. Make sure it does not exceed the 4211da177e4SLinus Torvalds * number reserved. 4221da177e4SLinus Torvalds */ 4231da177e4SLinus Torvalds if (delta < 0) { 4241da177e4SLinus Torvalds tp->t_rtx_res_used += (uint)-delta; 4251da177e4SLinus Torvalds ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res); 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds tp->t_frextents_delta += delta; 4281da177e4SLinus Torvalds break; 4291da177e4SLinus Torvalds case XFS_TRANS_SB_RES_FREXTENTS: 4301da177e4SLinus Torvalds /* 4311da177e4SLinus Torvalds * The allocation has already been applied to the 432c41564b5SNathan Scott * in-core superblock's counter. This should only 4331da177e4SLinus Torvalds * be applied to the on-disk superblock. 4341da177e4SLinus Torvalds */ 4351da177e4SLinus Torvalds ASSERT(delta < 0); 4361da177e4SLinus Torvalds tp->t_res_frextents_delta += delta; 4371da177e4SLinus Torvalds break; 4381da177e4SLinus Torvalds case XFS_TRANS_SB_DBLOCKS: 4391da177e4SLinus Torvalds tp->t_dblocks_delta += delta; 4401da177e4SLinus Torvalds break; 4411da177e4SLinus Torvalds case XFS_TRANS_SB_AGCOUNT: 4421da177e4SLinus Torvalds ASSERT(delta > 0); 4431da177e4SLinus Torvalds tp->t_agcount_delta += delta; 4441da177e4SLinus Torvalds break; 4451da177e4SLinus Torvalds case XFS_TRANS_SB_IMAXPCT: 4461da177e4SLinus Torvalds tp->t_imaxpct_delta += delta; 4471da177e4SLinus Torvalds break; 4481da177e4SLinus Torvalds case XFS_TRANS_SB_REXTSIZE: 4491da177e4SLinus Torvalds tp->t_rextsize_delta += delta; 4501da177e4SLinus Torvalds break; 4511da177e4SLinus Torvalds case XFS_TRANS_SB_RBMBLOCKS: 4521da177e4SLinus Torvalds tp->t_rbmblocks_delta += delta; 4531da177e4SLinus Torvalds break; 4541da177e4SLinus Torvalds case XFS_TRANS_SB_RBLOCKS: 4551da177e4SLinus Torvalds tp->t_rblocks_delta += delta; 4561da177e4SLinus Torvalds break; 4571da177e4SLinus Torvalds case XFS_TRANS_SB_REXTENTS: 4581da177e4SLinus Torvalds tp->t_rextents_delta += delta; 4591da177e4SLinus Torvalds break; 4601da177e4SLinus Torvalds case XFS_TRANS_SB_REXTSLOG: 4611da177e4SLinus Torvalds tp->t_rextslog_delta += delta; 4621da177e4SLinus Torvalds break; 4631da177e4SLinus Torvalds default: 4641da177e4SLinus Torvalds ASSERT(0); 4651da177e4SLinus Torvalds return; 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds 468210c6f1cSDavid Chinner tp->t_flags |= flags; 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds /* 4721da177e4SLinus Torvalds * xfs_trans_apply_sb_deltas() is called from the commit code 4731da177e4SLinus Torvalds * to bring the superblock buffer into the current transaction 4741da177e4SLinus Torvalds * and modify it as requested by earlier calls to xfs_trans_mod_sb(). 4751da177e4SLinus Torvalds * 4761da177e4SLinus Torvalds * For now we just look at each field allowed to change and change 4771da177e4SLinus Torvalds * it if necessary. 4781da177e4SLinus Torvalds */ 4791da177e4SLinus Torvalds STATIC void 4801da177e4SLinus Torvalds xfs_trans_apply_sb_deltas( 4811da177e4SLinus Torvalds xfs_trans_t *tp) 4821da177e4SLinus Torvalds { 4832bdf7cd0SChristoph Hellwig xfs_dsb_t *sbp; 484e8222613SDave Chinner struct xfs_buf *bp; 4851da177e4SLinus Torvalds int whole = 0; 4861da177e4SLinus Torvalds 487cead0b10SChristoph Hellwig bp = xfs_trans_getsb(tp); 4883e6e8afdSChristoph Hellwig sbp = bp->b_addr; 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds /* 49192821e2bSDavid Chinner * Only update the superblock counters if we are logging them 49292821e2bSDavid Chinner */ 49392821e2bSDavid Chinner if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) { 4942bdf7cd0SChristoph Hellwig if (tp->t_icount_delta) 495413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta); 4962bdf7cd0SChristoph Hellwig if (tp->t_ifree_delta) 497413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta); 4982bdf7cd0SChristoph Hellwig if (tp->t_fdblocks_delta) 499413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta); 5002bdf7cd0SChristoph Hellwig if (tp->t_res_fdblocks_delta) 501413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta); 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds 5042bdf7cd0SChristoph Hellwig if (tp->t_frextents_delta) 505413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta); 5062bdf7cd0SChristoph Hellwig if (tp->t_res_frextents_delta) 507413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta); 5081da177e4SLinus Torvalds 5092bdf7cd0SChristoph Hellwig if (tp->t_dblocks_delta) { 510413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta); 5111da177e4SLinus Torvalds whole = 1; 5121da177e4SLinus Torvalds } 5132bdf7cd0SChristoph Hellwig if (tp->t_agcount_delta) { 514413d57c9SMarcin Slusarz be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta); 5151da177e4SLinus Torvalds whole = 1; 5161da177e4SLinus Torvalds } 5172bdf7cd0SChristoph Hellwig if (tp->t_imaxpct_delta) { 5182bdf7cd0SChristoph Hellwig sbp->sb_imax_pct += tp->t_imaxpct_delta; 5191da177e4SLinus Torvalds whole = 1; 5201da177e4SLinus Torvalds } 5212bdf7cd0SChristoph Hellwig if (tp->t_rextsize_delta) { 522413d57c9SMarcin Slusarz be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta); 5231da177e4SLinus Torvalds whole = 1; 5241da177e4SLinus Torvalds } 5252bdf7cd0SChristoph Hellwig if (tp->t_rbmblocks_delta) { 526413d57c9SMarcin Slusarz be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta); 5271da177e4SLinus Torvalds whole = 1; 5281da177e4SLinus Torvalds } 5292bdf7cd0SChristoph Hellwig if (tp->t_rblocks_delta) { 530413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta); 5311da177e4SLinus Torvalds whole = 1; 5321da177e4SLinus Torvalds } 5332bdf7cd0SChristoph Hellwig if (tp->t_rextents_delta) { 534413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta); 5351da177e4SLinus Torvalds whole = 1; 5361da177e4SLinus Torvalds } 5372bdf7cd0SChristoph Hellwig if (tp->t_rextslog_delta) { 5382bdf7cd0SChristoph Hellwig sbp->sb_rextslog += tp->t_rextslog_delta; 5391da177e4SLinus Torvalds whole = 1; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 5423443a3bcSDave Chinner xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF); 5431da177e4SLinus Torvalds if (whole) 5441da177e4SLinus Torvalds /* 545c41564b5SNathan Scott * Log the whole thing, the fields are noncontiguous. 5461da177e4SLinus Torvalds */ 5472bdf7cd0SChristoph Hellwig xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1); 5481da177e4SLinus Torvalds else 5491da177e4SLinus Torvalds /* 5501da177e4SLinus Torvalds * Since all the modifiable fields are contiguous, we 5511da177e4SLinus Torvalds * can get away with this. 5521da177e4SLinus Torvalds */ 5532bdf7cd0SChristoph Hellwig xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount), 5542bdf7cd0SChristoph Hellwig offsetof(xfs_dsb_t, sb_frextents) + 5551da177e4SLinus Torvalds sizeof(sbp->sb_frextents) - 1); 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds /* 559dc3ffbb1SDave Chinner * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations and 560dc3ffbb1SDave Chinner * apply superblock counter changes to the in-core superblock. The 56145c34141SDavid Chinner * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT 56245c34141SDavid Chinner * applied to the in-core superblock. The idea is that that has already been 56345c34141SDavid Chinner * done. 5641da177e4SLinus Torvalds * 56545c34141SDavid Chinner * If we are not logging superblock counters, then the inode allocated/free and 56645c34141SDavid Chinner * used block counts are not updated in the on disk superblock. In this case, 56745c34141SDavid Chinner * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we 56845c34141SDavid Chinner * still need to update the incore superblock with the changes. 569f18c9a90SDave Chinner * 570f18c9a90SDave Chinner * Deltas for the inode count are +/-64, hence we use a large batch size of 128 571f18c9a90SDave Chinner * so we don't need to take the counter lock on every update. 5721da177e4SLinus Torvalds */ 573f18c9a90SDave Chinner #define XFS_ICOUNT_BATCH 128 574f18c9a90SDave Chinner 57571e330b5SDave Chinner void 5761da177e4SLinus Torvalds xfs_trans_unreserve_and_mod_sb( 5770bd5ddedSDave Chinner struct xfs_trans *tp) 5781da177e4SLinus Torvalds { 5790bd5ddedSDave Chinner struct xfs_mount *mp = tp->t_mountp; 5800d485adaSDave Chinner bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0; 58145c34141SDavid Chinner int64_t blkdelta = 0; 58245c34141SDavid Chinner int64_t rtxdelta = 0; 5831b040712SChristoph Hellwig int64_t idelta = 0; 5841b040712SChristoph Hellwig int64_t ifreedelta = 0; 5850bd5ddedSDave Chinner int error; 5861da177e4SLinus Torvalds 5871b040712SChristoph Hellwig /* calculate deltas */ 58845c34141SDavid Chinner if (tp->t_blk_res > 0) 58945c34141SDavid Chinner blkdelta = tp->t_blk_res; 59045c34141SDavid Chinner if ((tp->t_fdblocks_delta != 0) && 59145c34141SDavid Chinner (xfs_sb_version_haslazysbcount(&mp->m_sb) || 59245c34141SDavid Chinner (tp->t_flags & XFS_TRANS_SB_DIRTY))) 59345c34141SDavid Chinner blkdelta += tp->t_fdblocks_delta; 59445c34141SDavid Chinner 59545c34141SDavid Chinner if (tp->t_rtx_res > 0) 59645c34141SDavid Chinner rtxdelta = tp->t_rtx_res; 59745c34141SDavid Chinner if ((tp->t_frextents_delta != 0) && 59845c34141SDavid Chinner (tp->t_flags & XFS_TRANS_SB_DIRTY)) 59945c34141SDavid Chinner rtxdelta += tp->t_frextents_delta; 60045c34141SDavid Chinner 6011b040712SChristoph Hellwig if (xfs_sb_version_haslazysbcount(&mp->m_sb) || 6021b040712SChristoph Hellwig (tp->t_flags & XFS_TRANS_SB_DIRTY)) { 6031b040712SChristoph Hellwig idelta = tp->t_icount_delta; 6041b040712SChristoph Hellwig ifreedelta = tp->t_ifree_delta; 6051b040712SChristoph Hellwig } 6061b040712SChristoph Hellwig 6071b040712SChristoph Hellwig /* apply the per-cpu counters */ 6081b040712SChristoph Hellwig if (blkdelta) { 6090d485adaSDave Chinner error = xfs_mod_fdblocks(mp, blkdelta, rsvd); 610dc3ffbb1SDave Chinner ASSERT(!error); 6111b040712SChristoph Hellwig } 6121b040712SChristoph Hellwig 6135825bea0SDave Chinner if (idelta) 614f18c9a90SDave Chinner percpu_counter_add_batch(&mp->m_icount, idelta, 615f18c9a90SDave Chinner XFS_ICOUNT_BATCH); 6161b040712SChristoph Hellwig 6175825bea0SDave Chinner if (ifreedelta) 618f18c9a90SDave Chinner percpu_counter_add(&mp->m_ifree, ifreedelta); 6191b040712SChristoph Hellwig 6200bd5ddedSDave Chinner if (rtxdelta == 0 && !(tp->t_flags & XFS_TRANS_SB_DIRTY)) 6210bd5ddedSDave Chinner return; 6220bd5ddedSDave Chinner 6231b040712SChristoph Hellwig /* apply remaining deltas */ 6240bd5ddedSDave Chinner spin_lock(&mp->m_sb_lock); 6256543990aSDave Chinner mp->m_sb.sb_fdblocks += tp->t_fdblocks_delta + tp->t_res_fdblocks_delta; 6266543990aSDave Chinner mp->m_sb.sb_icount += idelta; 6276543990aSDave Chinner mp->m_sb.sb_ifree += ifreedelta; 628dc3ffbb1SDave Chinner mp->m_sb.sb_frextents += rtxdelta; 629dc3ffbb1SDave Chinner mp->m_sb.sb_dblocks += tp->t_dblocks_delta; 630dc3ffbb1SDave Chinner mp->m_sb.sb_agcount += tp->t_agcount_delta; 631dc3ffbb1SDave Chinner mp->m_sb.sb_imax_pct += tp->t_imaxpct_delta; 632dc3ffbb1SDave Chinner mp->m_sb.sb_rextsize += tp->t_rextsize_delta; 633dc3ffbb1SDave Chinner mp->m_sb.sb_rbmblocks += tp->t_rbmblocks_delta; 634dc3ffbb1SDave Chinner mp->m_sb.sb_rblocks += tp->t_rblocks_delta; 635dc3ffbb1SDave Chinner mp->m_sb.sb_rextents += tp->t_rextents_delta; 636dc3ffbb1SDave Chinner mp->m_sb.sb_rextslog += tp->t_rextslog_delta; 6370bd5ddedSDave Chinner spin_unlock(&mp->m_sb_lock); 6381b040712SChristoph Hellwig 639dc3ffbb1SDave Chinner /* 640dc3ffbb1SDave Chinner * Debug checks outside of the spinlock so they don't lock up the 641dc3ffbb1SDave Chinner * machine if they fail. 642dc3ffbb1SDave Chinner */ 643dc3ffbb1SDave Chinner ASSERT(mp->m_sb.sb_imax_pct >= 0); 644dc3ffbb1SDave Chinner ASSERT(mp->m_sb.sb_rextslog >= 0); 6451b040712SChristoph Hellwig return; 6461da177e4SLinus Torvalds } 6471da177e4SLinus Torvalds 648e6631f85SDave Chinner /* Add the given log item to the transaction's list of log items. */ 649e98c414fSChristoph Hellwig void 650e98c414fSChristoph Hellwig xfs_trans_add_item( 651e98c414fSChristoph Hellwig struct xfs_trans *tp, 652e98c414fSChristoph Hellwig struct xfs_log_item *lip) 653e98c414fSChristoph Hellwig { 654f65020a8SJesper Juhl ASSERT(lip->li_mountp == tp->t_mountp); 655f65020a8SJesper Juhl ASSERT(lip->li_ailp == tp->t_mountp->m_ail); 656e6631f85SDave Chinner ASSERT(list_empty(&lip->li_trans)); 657e6631f85SDave Chinner ASSERT(!test_bit(XFS_LI_DIRTY, &lip->li_flags)); 658e98c414fSChristoph Hellwig 659e6631f85SDave Chinner list_add_tail(&lip->li_trans, &tp->t_items); 660ba18781bSDave Chinner trace_xfs_trans_add_item(tp, _RET_IP_); 661e98c414fSChristoph Hellwig } 662e98c414fSChristoph Hellwig 663e98c414fSChristoph Hellwig /* 664e6631f85SDave Chinner * Unlink the log item from the transaction. the log item is no longer 665e6631f85SDave Chinner * considered dirty in this transaction, as the linked transaction has 666e6631f85SDave Chinner * finished, either by abort or commit completion. 667e98c414fSChristoph Hellwig */ 668e98c414fSChristoph Hellwig void 669e98c414fSChristoph Hellwig xfs_trans_del_item( 670e98c414fSChristoph Hellwig struct xfs_log_item *lip) 671e98c414fSChristoph Hellwig { 672e6631f85SDave Chinner clear_bit(XFS_LI_DIRTY, &lip->li_flags); 673e6631f85SDave Chinner list_del_init(&lip->li_trans); 674e98c414fSChristoph Hellwig } 675e98c414fSChristoph Hellwig 676e6631f85SDave Chinner /* Detach and unlock all of the items in a transaction */ 677195cd83dSChristoph Hellwig static void 678e98c414fSChristoph Hellwig xfs_trans_free_items( 679e98c414fSChristoph Hellwig struct xfs_trans *tp, 680eacb24e7SChristoph Hellwig bool abort) 681e98c414fSChristoph Hellwig { 682e6631f85SDave Chinner struct xfs_log_item *lip, *next; 683e98c414fSChristoph Hellwig 684ba18781bSDave Chinner trace_xfs_trans_free_items(tp, _RET_IP_); 685ba18781bSDave Chinner 686e6631f85SDave Chinner list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) { 687e6631f85SDave Chinner xfs_trans_del_item(lip); 688eacb24e7SChristoph Hellwig if (abort) 68922525c17SDave Chinner set_bit(XFS_LI_ABORTED, &lip->li_flags); 690ddf92053SChristoph Hellwig if (lip->li_ops->iop_release) 691ddf92053SChristoph Hellwig lip->li_ops->iop_release(lip); 692e98c414fSChristoph Hellwig } 693e98c414fSChristoph Hellwig } 694e98c414fSChristoph Hellwig 6950e57f6a3SDave Chinner static inline void 6960e57f6a3SDave Chinner xfs_log_item_batch_insert( 6970e57f6a3SDave Chinner struct xfs_ail *ailp, 6981d8c95a3SDave Chinner struct xfs_ail_cursor *cur, 6990e57f6a3SDave Chinner struct xfs_log_item **log_items, 7000e57f6a3SDave Chinner int nr_items, 7010e57f6a3SDave Chinner xfs_lsn_t commit_lsn) 7020e57f6a3SDave Chinner { 7030e57f6a3SDave Chinner int i; 7040e57f6a3SDave Chinner 70557e80956SMatthew Wilcox spin_lock(&ailp->ail_lock); 70657e80956SMatthew Wilcox /* xfs_trans_ail_update_bulk drops ailp->ail_lock */ 7071d8c95a3SDave Chinner xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn); 7080e57f6a3SDave Chinner 709904c17e6SDave Chinner for (i = 0; i < nr_items; i++) { 710904c17e6SDave Chinner struct xfs_log_item *lip = log_items[i]; 711904c17e6SDave Chinner 712e8b78db7SChristoph Hellwig if (lip->li_ops->iop_unpin) 713904c17e6SDave Chinner lip->li_ops->iop_unpin(lip, 0); 714904c17e6SDave Chinner } 7150e57f6a3SDave Chinner } 7160e57f6a3SDave Chinner 7170e57f6a3SDave Chinner /* 7180e57f6a3SDave Chinner * Bulk operation version of xfs_trans_committed that takes a log vector of 7190e57f6a3SDave Chinner * items to insert into the AIL. This uses bulk AIL insertion techniques to 7200e57f6a3SDave Chinner * minimise lock traffic. 721e34a314cSDave Chinner * 722e34a314cSDave Chinner * If we are called with the aborted flag set, it is because a log write during 723e34a314cSDave Chinner * a CIL checkpoint commit has failed. In this case, all the items in the 724ddf92053SChristoph Hellwig * checkpoint have already gone through iop_committed and iop_committing, which 725e34a314cSDave Chinner * means that checkpoint commit abort handling is treated exactly the same 726e34a314cSDave Chinner * as an iclog write error even though we haven't started any IO yet. Hence in 727904c17e6SDave Chinner * this case all we need to do is iop_committed processing, followed by an 728904c17e6SDave Chinner * iop_unpin(aborted) call. 7291d8c95a3SDave Chinner * 7301d8c95a3SDave Chinner * The AIL cursor is used to optimise the insert process. If commit_lsn is not 7311d8c95a3SDave Chinner * at the end of the AIL, the insert cursor avoids the need to walk 7321d8c95a3SDave Chinner * the AIL to find the insertion point on every xfs_log_item_batch_insert() 7331d8c95a3SDave Chinner * call. This saves a lot of needless list walking and is a net win, even 7341d8c95a3SDave Chinner * though it slightly increases that amount of AIL lock traffic to set it up 7351d8c95a3SDave Chinner * and tear it down. 7360e57f6a3SDave Chinner */ 7370e57f6a3SDave Chinner void 7380e57f6a3SDave Chinner xfs_trans_committed_bulk( 7390e57f6a3SDave Chinner struct xfs_ail *ailp, 7400e57f6a3SDave Chinner struct xfs_log_vec *log_vector, 7410e57f6a3SDave Chinner xfs_lsn_t commit_lsn, 742d15cbf2fSChristoph Hellwig bool aborted) 7430e57f6a3SDave Chinner { 7440e57f6a3SDave Chinner #define LOG_ITEM_BATCH_SIZE 32 7450e57f6a3SDave Chinner struct xfs_log_item *log_items[LOG_ITEM_BATCH_SIZE]; 7460e57f6a3SDave Chinner struct xfs_log_vec *lv; 7471d8c95a3SDave Chinner struct xfs_ail_cursor cur; 7480e57f6a3SDave Chinner int i = 0; 7490e57f6a3SDave Chinner 75057e80956SMatthew Wilcox spin_lock(&ailp->ail_lock); 7511d8c95a3SDave Chinner xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn); 75257e80956SMatthew Wilcox spin_unlock(&ailp->ail_lock); 7531d8c95a3SDave Chinner 7540e57f6a3SDave Chinner /* unpin all the log items */ 7550e57f6a3SDave Chinner for (lv = log_vector; lv; lv = lv->lv_next ) { 7560e57f6a3SDave Chinner struct xfs_log_item *lip = lv->lv_item; 7570e57f6a3SDave Chinner xfs_lsn_t item_lsn; 7580e57f6a3SDave Chinner 7590e57f6a3SDave Chinner if (aborted) 76022525c17SDave Chinner set_bit(XFS_LI_ABORTED, &lip->li_flags); 7619ce632a2SChristoph Hellwig 7629ce632a2SChristoph Hellwig if (lip->li_ops->flags & XFS_ITEM_RELEASE_WHEN_COMMITTED) { 7639ce632a2SChristoph Hellwig lip->li_ops->iop_release(lip); 7649ce632a2SChristoph Hellwig continue; 7659ce632a2SChristoph Hellwig } 7669ce632a2SChristoph Hellwig 767e8b78db7SChristoph Hellwig if (lip->li_ops->iop_committed) 768904c17e6SDave Chinner item_lsn = lip->li_ops->iop_committed(lip, commit_lsn); 769e8b78db7SChristoph Hellwig else 770e8b78db7SChristoph Hellwig item_lsn = commit_lsn; 7710e57f6a3SDave Chinner 7721316d4daSDave Chinner /* item_lsn of -1 means the item needs no further processing */ 7730e57f6a3SDave Chinner if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0) 7740e57f6a3SDave Chinner continue; 7750e57f6a3SDave Chinner 776e34a314cSDave Chinner /* 777e34a314cSDave Chinner * if we are aborting the operation, no point in inserting the 778e34a314cSDave Chinner * object into the AIL as we are in a shutdown situation. 779e34a314cSDave Chinner */ 780e34a314cSDave Chinner if (aborted) { 78157e80956SMatthew Wilcox ASSERT(XFS_FORCED_SHUTDOWN(ailp->ail_mount)); 782e8b78db7SChristoph Hellwig if (lip->li_ops->iop_unpin) 783904c17e6SDave Chinner lip->li_ops->iop_unpin(lip, 1); 784e34a314cSDave Chinner continue; 785e34a314cSDave Chinner } 786e34a314cSDave Chinner 7870e57f6a3SDave Chinner if (item_lsn != commit_lsn) { 7880e57f6a3SDave Chinner 7890e57f6a3SDave Chinner /* 7900e57f6a3SDave Chinner * Not a bulk update option due to unusual item_lsn. 7910e57f6a3SDave Chinner * Push into AIL immediately, rechecking the lsn once 7921d8c95a3SDave Chinner * we have the ail lock. Then unpin the item. This does 7931d8c95a3SDave Chinner * not affect the AIL cursor the bulk insert path is 7941d8c95a3SDave Chinner * using. 7950e57f6a3SDave Chinner */ 79657e80956SMatthew Wilcox spin_lock(&ailp->ail_lock); 7970e57f6a3SDave Chinner if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) 7980e57f6a3SDave Chinner xfs_trans_ail_update(ailp, lip, item_lsn); 7990e57f6a3SDave Chinner else 80057e80956SMatthew Wilcox spin_unlock(&ailp->ail_lock); 801e8b78db7SChristoph Hellwig if (lip->li_ops->iop_unpin) 802904c17e6SDave Chinner lip->li_ops->iop_unpin(lip, 0); 8030e57f6a3SDave Chinner continue; 8040e57f6a3SDave Chinner } 8050e57f6a3SDave Chinner 8060e57f6a3SDave Chinner /* Item is a candidate for bulk AIL insert. */ 8070e57f6a3SDave Chinner log_items[i++] = lv->lv_item; 8080e57f6a3SDave Chinner if (i >= LOG_ITEM_BATCH_SIZE) { 8091d8c95a3SDave Chinner xfs_log_item_batch_insert(ailp, &cur, log_items, 8100e57f6a3SDave Chinner LOG_ITEM_BATCH_SIZE, commit_lsn); 8110e57f6a3SDave Chinner i = 0; 8120e57f6a3SDave Chinner } 8130e57f6a3SDave Chinner } 8140e57f6a3SDave Chinner 8150e57f6a3SDave Chinner /* make sure we insert the remainder! */ 8160e57f6a3SDave Chinner if (i) 8171d8c95a3SDave Chinner xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn); 8181d8c95a3SDave Chinner 81957e80956SMatthew Wilcox spin_lock(&ailp->ail_lock); 820e4a1e29cSEric Sandeen xfs_trans_ail_cursor_done(&cur); 82157e80956SMatthew Wilcox spin_unlock(&ailp->ail_lock); 8220e57f6a3SDave Chinner } 8230e57f6a3SDave Chinner 824b1c1b5b6SDave Chinner /* 825b1037058SChristoph Hellwig * Commit the given transaction to the log. 8260924378aSDave Chinner * 8270924378aSDave Chinner * XFS disk error handling mechanism is not based on a typical 8280924378aSDave Chinner * transaction abort mechanism. Logically after the filesystem 8290924378aSDave Chinner * gets marked 'SHUTDOWN', we can't let any new transactions 8300924378aSDave Chinner * be durable - ie. committed to disk - because some metadata might 8310924378aSDave Chinner * be inconsistent. In such cases, this returns an error, and the 8320924378aSDave Chinner * caller may assume that all locked objects joined to the transaction 8330924378aSDave Chinner * have already been unlocked as if the commit had succeeded. 8340924378aSDave Chinner * Do not reference the transaction structure after this call. 8350924378aSDave Chinner */ 83670393313SChristoph Hellwig static int 83770393313SChristoph Hellwig __xfs_trans_commit( 838a3ccd2caSChristoph Hellwig struct xfs_trans *tp, 83970393313SChristoph Hellwig bool regrant) 8400924378aSDave Chinner { 841a3ccd2caSChristoph Hellwig struct xfs_mount *mp = tp->t_mountp; 842*5f9b4b0dSDave Chinner xfs_csn_t commit_seq = 0; 843a3ccd2caSChristoph Hellwig int error = 0; 8440924378aSDave Chinner int sync = tp->t_flags & XFS_TRANS_SYNC; 8450924378aSDave Chinner 846ba18781bSDave Chinner trace_xfs_trans_commit(tp, _RET_IP_); 847ba18781bSDave Chinner 84898719051SBrian Foster /* 84998719051SBrian Foster * Finish deferred items on final commit. Only permanent transactions 85098719051SBrian Foster * should ever have deferred ops. 85198719051SBrian Foster */ 8529d9e6233SBrian Foster WARN_ON_ONCE(!list_empty(&tp->t_dfops) && 85398719051SBrian Foster !(tp->t_flags & XFS_TRANS_PERM_LOG_RES)); 85498719051SBrian Foster if (!regrant && (tp->t_flags & XFS_TRANS_PERM_LOG_RES)) { 855b277c37fSBrian Foster error = xfs_defer_finish_noroll(&tp); 8569b1f4e98SBrian Foster if (error) 857e021a2e5SBrian Foster goto out_unreserve; 858e021a2e5SBrian Foster } 859e021a2e5SBrian Foster 8600924378aSDave Chinner /* 8610924378aSDave Chinner * If there is nothing to be logged by the transaction, 8620924378aSDave Chinner * then unlock all of the items associated with the 8630924378aSDave Chinner * transaction and free the transaction structure. 8640924378aSDave Chinner * Also make sure to return any reserved blocks to 8650924378aSDave Chinner * the free pool. 8660924378aSDave Chinner */ 867a3ccd2caSChristoph Hellwig if (!(tp->t_flags & XFS_TRANS_DIRTY)) 868a3ccd2caSChristoph Hellwig goto out_unreserve; 869a3ccd2caSChristoph Hellwig 870a3ccd2caSChristoph Hellwig if (XFS_FORCED_SHUTDOWN(mp)) { 8712451337dSDave Chinner error = -EIO; 872a3ccd2caSChristoph Hellwig goto out_unreserve; 8730924378aSDave Chinner } 874a3ccd2caSChristoph Hellwig 8750924378aSDave Chinner ASSERT(tp->t_ticket != NULL); 8760924378aSDave Chinner 8770924378aSDave Chinner /* 8780924378aSDave Chinner * If we need to update the superblock, then do it now. 8790924378aSDave Chinner */ 8800924378aSDave Chinner if (tp->t_flags & XFS_TRANS_SB_DIRTY) 8810924378aSDave Chinner xfs_trans_apply_sb_deltas(tp); 8820924378aSDave Chinner xfs_trans_apply_dquot_deltas(tp); 8830924378aSDave Chinner 884*5f9b4b0dSDave Chinner xlog_cil_commit(mp->m_log, tp, &commit_seq, regrant); 8851da177e4SLinus Torvalds 8860244b960SChristoph Hellwig xfs_trans_free(tp); 8870244b960SChristoph Hellwig 8881da177e4SLinus Torvalds /* 8891da177e4SLinus Torvalds * If the transaction needs to be synchronous, then force the 8901da177e4SLinus Torvalds * log out now and wait for it. 8911da177e4SLinus Torvalds */ 8921da177e4SLinus Torvalds if (sync) { 893*5f9b4b0dSDave Chinner error = xfs_log_force_seq(mp, commit_seq, XFS_LOG_SYNC, NULL); 894ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_trans_sync); 8951da177e4SLinus Torvalds } else { 896ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_trans_async); 8971da177e4SLinus Torvalds } 8981da177e4SLinus Torvalds 899a3ccd2caSChristoph Hellwig return error; 900a3ccd2caSChristoph Hellwig 901a3ccd2caSChristoph Hellwig out_unreserve: 902a3ccd2caSChristoph Hellwig xfs_trans_unreserve_and_mod_sb(tp); 903a3ccd2caSChristoph Hellwig 904a3ccd2caSChristoph Hellwig /* 905a3ccd2caSChristoph Hellwig * It is indeed possible for the transaction to be not dirty but 906a3ccd2caSChristoph Hellwig * the dqinfo portion to be. All that means is that we have some 907a3ccd2caSChristoph Hellwig * (non-persistent) quota reservations that need to be unreserved. 908a3ccd2caSChristoph Hellwig */ 909a3ccd2caSChristoph Hellwig xfs_trans_unreserve_and_mod_dquots(tp); 910a3ccd2caSChristoph Hellwig if (tp->t_ticket) { 9118b41e3f9SChristoph Hellwig if (regrant && !XLOG_FORCED_SHUTDOWN(mp->m_log)) 9128b41e3f9SChristoph Hellwig xfs_log_ticket_regrant(mp->m_log, tp->t_ticket); 9138b41e3f9SChristoph Hellwig else 9148b41e3f9SChristoph Hellwig xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket); 915ba18781bSDave Chinner tp->t_ticket = NULL; 916a3ccd2caSChristoph Hellwig } 917195cd83dSChristoph Hellwig xfs_trans_free_items(tp, !!error); 918a3ccd2caSChristoph Hellwig xfs_trans_free(tp); 919a3ccd2caSChristoph Hellwig 920ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_trans_empty); 921a3ccd2caSChristoph Hellwig return error; 9221da177e4SLinus Torvalds } 9231da177e4SLinus Torvalds 92470393313SChristoph Hellwig int 92570393313SChristoph Hellwig xfs_trans_commit( 92670393313SChristoph Hellwig struct xfs_trans *tp) 92770393313SChristoph Hellwig { 92870393313SChristoph Hellwig return __xfs_trans_commit(tp, false); 92970393313SChristoph Hellwig } 93070393313SChristoph Hellwig 9311da177e4SLinus Torvalds /* 9321da177e4SLinus Torvalds * Unlock all of the transaction's items and free the transaction. 9331da177e4SLinus Torvalds * The transaction must not have modified any of its items, because 9341da177e4SLinus Torvalds * there is no way to restore them to their previous state. 9351da177e4SLinus Torvalds * 9361da177e4SLinus Torvalds * If the transaction has made a log reservation, make sure to release 9371da177e4SLinus Torvalds * it as well. 9381da177e4SLinus Torvalds */ 9391da177e4SLinus Torvalds void 9401da177e4SLinus Torvalds xfs_trans_cancel( 9414906e215SChristoph Hellwig struct xfs_trans *tp) 9421da177e4SLinus Torvalds { 9434906e215SChristoph Hellwig struct xfs_mount *mp = tp->t_mountp; 9444906e215SChristoph Hellwig bool dirty = (tp->t_flags & XFS_TRANS_DIRTY); 9451da177e4SLinus Torvalds 946ba18781bSDave Chinner trace_xfs_trans_cancel(tp, _RET_IP_); 947ba18781bSDave Chinner 94898719051SBrian Foster if (tp->t_flags & XFS_TRANS_PERM_LOG_RES) 9499e28a242SBrian Foster xfs_defer_cancel(tp); 950e021a2e5SBrian Foster 9511da177e4SLinus Torvalds /* 9521da177e4SLinus Torvalds * See if the caller is relying on us to shut down the 9531da177e4SLinus Torvalds * filesystem. This happens in paths where we detect 9541da177e4SLinus Torvalds * corruption and decide to give up. 9551da177e4SLinus Torvalds */ 9564906e215SChristoph Hellwig if (dirty && !XFS_FORCED_SHUTDOWN(mp)) { 9570733af21SRyan Hankins XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp); 9587d04a335SNathan Scott xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 95960a204f0SNathan Scott } 9601da177e4SLinus Torvalds #ifdef DEBUG 9614906e215SChristoph Hellwig if (!dirty && !XFS_FORCED_SHUTDOWN(mp)) { 962e6631f85SDave Chinner struct xfs_log_item *lip; 9631da177e4SLinus Torvalds 964e6631f85SDave Chinner list_for_each_entry(lip, &tp->t_items, li_trans) 965d6b8fc6cSKaixu Xia ASSERT(!xlog_item_is_intent_done(lip)); 9661da177e4SLinus Torvalds } 9671da177e4SLinus Torvalds #endif 9681da177e4SLinus Torvalds xfs_trans_unreserve_and_mod_sb(tp); 9697d095257SChristoph Hellwig xfs_trans_unreserve_and_mod_dquots(tp); 9701da177e4SLinus Torvalds 971ba18781bSDave Chinner if (tp->t_ticket) { 9728b41e3f9SChristoph Hellwig xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket); 973ba18781bSDave Chinner tp->t_ticket = NULL; 974ba18781bSDave Chinner } 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 } 10273a1af6c3SDarrick J. Wong 10283a1af6c3SDarrick J. Wong /* 10293a1af6c3SDarrick J. Wong * Allocate an transaction, lock and join the inode to it, and reserve quota. 10303a1af6c3SDarrick J. Wong * 10313a1af6c3SDarrick J. Wong * The caller must ensure that the on-disk dquots attached to this inode have 10323a1af6c3SDarrick J. Wong * already been allocated and initialized. The caller is responsible for 10333a1af6c3SDarrick J. Wong * releasing ILOCK_EXCL if a new transaction is returned. 10343a1af6c3SDarrick J. Wong */ 10353a1af6c3SDarrick J. Wong int 10363a1af6c3SDarrick J. Wong xfs_trans_alloc_inode( 10373a1af6c3SDarrick J. Wong struct xfs_inode *ip, 10383a1af6c3SDarrick J. Wong struct xfs_trans_res *resv, 10393a1af6c3SDarrick J. Wong unsigned int dblocks, 10403de4eb10SDarrick J. Wong unsigned int rblocks, 10413a1af6c3SDarrick J. Wong bool force, 10423a1af6c3SDarrick J. Wong struct xfs_trans **tpp) 10433a1af6c3SDarrick J. Wong { 10443a1af6c3SDarrick J. Wong struct xfs_trans *tp; 10453a1af6c3SDarrick J. Wong struct xfs_mount *mp = ip->i_mount; 1046766aabd5SDarrick J. Wong bool retried = false; 10473a1af6c3SDarrick J. Wong int error; 10483a1af6c3SDarrick J. Wong 1049766aabd5SDarrick J. Wong retry: 10503de4eb10SDarrick J. Wong error = xfs_trans_alloc(mp, resv, dblocks, 10513de4eb10SDarrick J. Wong rblocks / mp->m_sb.sb_rextsize, 10523a1af6c3SDarrick J. Wong force ? XFS_TRANS_RESERVE : 0, &tp); 10533a1af6c3SDarrick J. Wong if (error) 10543a1af6c3SDarrick J. Wong return error; 10553a1af6c3SDarrick J. Wong 10563a1af6c3SDarrick J. Wong xfs_ilock(ip, XFS_ILOCK_EXCL); 10573a1af6c3SDarrick J. Wong xfs_trans_ijoin(tp, ip, 0); 10583a1af6c3SDarrick J. Wong 10593a1af6c3SDarrick J. Wong error = xfs_qm_dqattach_locked(ip, false); 10603a1af6c3SDarrick J. Wong if (error) { 10613a1af6c3SDarrick J. Wong /* Caller should have allocated the dquots! */ 10623a1af6c3SDarrick J. Wong ASSERT(error != -ENOENT); 10633a1af6c3SDarrick J. Wong goto out_cancel; 10643a1af6c3SDarrick J. Wong } 10653a1af6c3SDarrick J. Wong 10663de4eb10SDarrick J. Wong error = xfs_trans_reserve_quota_nblks(tp, ip, dblocks, rblocks, force); 1067766aabd5SDarrick J. Wong if ((error == -EDQUOT || error == -ENOSPC) && !retried) { 1068766aabd5SDarrick J. Wong xfs_trans_cancel(tp); 1069766aabd5SDarrick J. Wong xfs_iunlock(ip, XFS_ILOCK_EXCL); 1070766aabd5SDarrick J. Wong xfs_blockgc_free_quota(ip, 0); 1071766aabd5SDarrick J. Wong retried = true; 1072766aabd5SDarrick J. Wong goto retry; 1073766aabd5SDarrick J. Wong } 10743a1af6c3SDarrick J. Wong if (error) 10753a1af6c3SDarrick J. Wong goto out_cancel; 10763a1af6c3SDarrick J. Wong 10773a1af6c3SDarrick J. Wong *tpp = tp; 10783a1af6c3SDarrick J. Wong return 0; 10793a1af6c3SDarrick J. Wong 10803a1af6c3SDarrick J. Wong out_cancel: 10813a1af6c3SDarrick J. Wong xfs_trans_cancel(tp); 10823a1af6c3SDarrick J. Wong xfs_iunlock(ip, XFS_ILOCK_EXCL); 10833a1af6c3SDarrick J. Wong return error; 10843a1af6c3SDarrick J. Wong } 1085f2f7b9ffSDarrick J. Wong 1086f2f7b9ffSDarrick J. Wong /* 1087f2f7b9ffSDarrick J. Wong * Allocate an transaction in preparation for inode creation by reserving quota 1088f2f7b9ffSDarrick J. Wong * against the given dquots. Callers are not required to hold any inode locks. 1089f2f7b9ffSDarrick J. Wong */ 1090f2f7b9ffSDarrick J. Wong int 1091f2f7b9ffSDarrick J. Wong xfs_trans_alloc_icreate( 1092f2f7b9ffSDarrick J. Wong struct xfs_mount *mp, 1093f2f7b9ffSDarrick J. Wong struct xfs_trans_res *resv, 1094f2f7b9ffSDarrick J. Wong struct xfs_dquot *udqp, 1095f2f7b9ffSDarrick J. Wong struct xfs_dquot *gdqp, 1096f2f7b9ffSDarrick J. Wong struct xfs_dquot *pdqp, 1097f2f7b9ffSDarrick J. Wong unsigned int dblocks, 1098f2f7b9ffSDarrick J. Wong struct xfs_trans **tpp) 1099f2f7b9ffSDarrick J. Wong { 1100f2f7b9ffSDarrick J. Wong struct xfs_trans *tp; 1101c237dd7cSDarrick J. Wong bool retried = false; 1102f2f7b9ffSDarrick J. Wong int error; 1103f2f7b9ffSDarrick J. Wong 1104c237dd7cSDarrick J. Wong retry: 1105f2f7b9ffSDarrick J. Wong error = xfs_trans_alloc(mp, resv, dblocks, 0, 0, &tp); 1106f2f7b9ffSDarrick J. Wong if (error) 1107f2f7b9ffSDarrick J. Wong return error; 1108f2f7b9ffSDarrick J. Wong 1109f2f7b9ffSDarrick J. Wong error = xfs_trans_reserve_quota_icreate(tp, udqp, gdqp, pdqp, dblocks); 1110c237dd7cSDarrick J. Wong if ((error == -EDQUOT || error == -ENOSPC) && !retried) { 1111c237dd7cSDarrick J. Wong xfs_trans_cancel(tp); 1112c237dd7cSDarrick J. Wong xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0); 1113c237dd7cSDarrick J. Wong retried = true; 1114c237dd7cSDarrick J. Wong goto retry; 1115c237dd7cSDarrick J. Wong } 1116f2f7b9ffSDarrick J. Wong if (error) { 1117f2f7b9ffSDarrick J. Wong xfs_trans_cancel(tp); 1118f2f7b9ffSDarrick J. Wong return error; 1119f2f7b9ffSDarrick J. Wong } 1120f2f7b9ffSDarrick J. Wong 1121f2f7b9ffSDarrick J. Wong *tpp = tp; 1122f2f7b9ffSDarrick J. Wong return 0; 1123f2f7b9ffSDarrick J. Wong } 11247317a03dSDarrick J. Wong 11257317a03dSDarrick J. Wong /* 11267317a03dSDarrick J. Wong * Allocate an transaction, lock and join the inode to it, and reserve quota 11277317a03dSDarrick J. Wong * in preparation for inode attribute changes that include uid, gid, or prid 11287317a03dSDarrick J. Wong * changes. 11297317a03dSDarrick J. Wong * 11307317a03dSDarrick J. Wong * The caller must ensure that the on-disk dquots attached to this inode have 11317317a03dSDarrick J. Wong * already been allocated and initialized. The ILOCK will be dropped when the 11327317a03dSDarrick J. Wong * transaction is committed or cancelled. 11337317a03dSDarrick J. Wong */ 11347317a03dSDarrick J. Wong int 11357317a03dSDarrick J. Wong xfs_trans_alloc_ichange( 11367317a03dSDarrick J. Wong struct xfs_inode *ip, 1137758303d1SDarrick J. Wong struct xfs_dquot *new_udqp, 1138758303d1SDarrick J. Wong struct xfs_dquot *new_gdqp, 1139758303d1SDarrick J. Wong struct xfs_dquot *new_pdqp, 11407317a03dSDarrick J. Wong bool force, 11417317a03dSDarrick J. Wong struct xfs_trans **tpp) 11427317a03dSDarrick J. Wong { 11437317a03dSDarrick J. Wong struct xfs_trans *tp; 11447317a03dSDarrick J. Wong struct xfs_mount *mp = ip->i_mount; 1145758303d1SDarrick J. Wong struct xfs_dquot *udqp; 1146758303d1SDarrick J. Wong struct xfs_dquot *gdqp; 1147758303d1SDarrick J. Wong struct xfs_dquot *pdqp; 1148758303d1SDarrick J. Wong bool retried = false; 11497317a03dSDarrick J. Wong int error; 11507317a03dSDarrick J. Wong 1151758303d1SDarrick J. Wong retry: 11527317a03dSDarrick J. Wong error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp); 11537317a03dSDarrick J. Wong if (error) 11547317a03dSDarrick J. Wong return error; 11557317a03dSDarrick J. Wong 11567317a03dSDarrick J. Wong xfs_ilock(ip, XFS_ILOCK_EXCL); 11577317a03dSDarrick J. Wong xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); 11587317a03dSDarrick J. Wong 11597317a03dSDarrick J. Wong error = xfs_qm_dqattach_locked(ip, false); 11607317a03dSDarrick J. Wong if (error) { 11617317a03dSDarrick J. Wong /* Caller should have allocated the dquots! */ 11627317a03dSDarrick J. Wong ASSERT(error != -ENOENT); 11637317a03dSDarrick J. Wong goto out_cancel; 11647317a03dSDarrick J. Wong } 11657317a03dSDarrick J. Wong 11667317a03dSDarrick J. Wong /* 11677317a03dSDarrick J. Wong * For each quota type, skip quota reservations if the inode's dquots 11687317a03dSDarrick J. Wong * now match the ones that came from the caller, or the caller didn't 1169758303d1SDarrick J. Wong * pass one in. The inode's dquots can change if we drop the ILOCK to 1170758303d1SDarrick J. Wong * perform a blockgc scan, so we must preserve the caller's arguments. 11717317a03dSDarrick J. Wong */ 1172758303d1SDarrick J. Wong udqp = (new_udqp != ip->i_udquot) ? new_udqp : NULL; 1173758303d1SDarrick J. Wong gdqp = (new_gdqp != ip->i_gdquot) ? new_gdqp : NULL; 1174758303d1SDarrick J. Wong pdqp = (new_pdqp != ip->i_pdquot) ? new_pdqp : NULL; 11757317a03dSDarrick J. Wong if (udqp || gdqp || pdqp) { 11765c615f0fSDarrick J. Wong unsigned int qflags = XFS_QMOPT_RES_REGBLKS; 11775c615f0fSDarrick J. Wong 11785c615f0fSDarrick J. Wong if (force) 11795c615f0fSDarrick J. Wong qflags |= XFS_QMOPT_FORCE_RES; 11805c615f0fSDarrick J. Wong 11815c615f0fSDarrick J. Wong /* 11825c615f0fSDarrick J. Wong * Reserve enough quota to handle blocks on disk and reserved 11835c615f0fSDarrick J. Wong * for a delayed allocation. We'll actually transfer the 11845c615f0fSDarrick J. Wong * delalloc reservation between dquots at chown time, even 11855c615f0fSDarrick J. Wong * though that part is only semi-transactional. 11865c615f0fSDarrick J. Wong */ 11875c615f0fSDarrick J. Wong error = xfs_trans_reserve_quota_bydquots(tp, mp, udqp, gdqp, 11886e73a545SChristoph Hellwig pdqp, ip->i_nblocks + ip->i_delayed_blks, 11895c615f0fSDarrick J. Wong 1, qflags); 1190758303d1SDarrick J. Wong if ((error == -EDQUOT || error == -ENOSPC) && !retried) { 1191758303d1SDarrick J. Wong xfs_trans_cancel(tp); 1192758303d1SDarrick J. Wong xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0); 1193758303d1SDarrick J. Wong retried = true; 1194758303d1SDarrick J. Wong goto retry; 1195758303d1SDarrick J. Wong } 11967317a03dSDarrick J. Wong if (error) 11977317a03dSDarrick J. Wong goto out_cancel; 11987317a03dSDarrick J. Wong } 11997317a03dSDarrick J. Wong 12007317a03dSDarrick J. Wong *tpp = tp; 12017317a03dSDarrick J. Wong return 0; 12027317a03dSDarrick J. Wong 12037317a03dSDarrick J. Wong out_cancel: 12047317a03dSDarrick J. Wong xfs_trans_cancel(tp); 12057317a03dSDarrick J. Wong return error; 12067317a03dSDarrick J. Wong } 1207