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"
272c2b981bSDarrick J. Wong #include "xfs_rtbitmap.h"
281da177e4SLinus Torvalds
29182696fbSDarrick J. Wong struct kmem_cache *xfs_trans_cache;
301da177e4SLinus Torvalds
31b872af2cSDarrick J. Wong #if defined(CONFIG_TRACEPOINTS)
32b872af2cSDarrick J. Wong static void
xfs_trans_trace_reservations(struct xfs_mount * mp)33b872af2cSDarrick J. Wong xfs_trans_trace_reservations(
34b872af2cSDarrick J. Wong struct xfs_mount *mp)
35b872af2cSDarrick J. Wong {
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 }
45b872af2cSDarrick J. Wong #else
46b872af2cSDarrick J. Wong # define xfs_trans_trace_reservations(mp)
47b872af2cSDarrick J. Wong #endif
48b872af2cSDarrick J. Wong
494f3b5783SJeff Liu /*
501da177e4SLinus Torvalds * Initialize the precomputed transaction reservation values
511da177e4SLinus Torvalds * in the mount structure.
521da177e4SLinus Torvalds */
531da177e4SLinus Torvalds void
xfs_trans_init(struct xfs_mount * mp)541da177e4SLinus Torvalds xfs_trans_init(
55025101dcSChristoph Hellwig struct xfs_mount *mp)
561da177e4SLinus Torvalds {
573d3c8b52SJie Liu xfs_trans_resv_calc(mp, M_RES(mp));
58b872af2cSDarrick J. Wong xfs_trans_trace_reservations(mp);
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds
611da177e4SLinus Torvalds /*
62b1c1b5b6SDave Chinner * Free the transaction structure. If there is more clean up
63b1c1b5b6SDave Chinner * to do when the structure is freed, add it here.
64b1c1b5b6SDave Chinner */
65b1c1b5b6SDave Chinner STATIC void
xfs_trans_free(struct xfs_trans * tp)66b1c1b5b6SDave Chinner xfs_trans_free(
67ed3b4d6cSDave Chinner struct xfs_trans *tp)
68b1c1b5b6SDave Chinner {
694ecbfe63SDave Chinner xfs_extent_busy_sort(&tp->t_busy);
704ecbfe63SDave Chinner xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
71ed3b4d6cSDave Chinner
72ba18781bSDave Chinner trace_xfs_trans_free(tp, _RET_IP_);
73756b1c34SDave Chinner xfs_trans_clear_context(tp);
74253f4911SChristoph Hellwig if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
75d9457dc0SJan Kara sb_end_intwrite(tp->t_mountp->m_super);
76b1c1b5b6SDave Chinner xfs_trans_free_dqinfo(tp);
77182696fbSDarrick J. Wong kmem_cache_free(xfs_trans_cache, tp);
78b1c1b5b6SDave Chinner }
79b1c1b5b6SDave Chinner
80b1c1b5b6SDave Chinner /*
811da177e4SLinus Torvalds * This is called to create a new transaction which will share the
821da177e4SLinus Torvalds * permanent log reservation of the given transaction. The remaining
831da177e4SLinus Torvalds * unused block and rt extent reservations are also inherited. This
841da177e4SLinus Torvalds * implies that the original transaction is no longer allowed to allocate
851da177e4SLinus Torvalds * blocks. Locks and log items, however, are no inherited. They must
861da177e4SLinus Torvalds * be added to the new transaction explicitly.
871da177e4SLinus Torvalds */
88f8f2835aSBrian Foster STATIC struct xfs_trans *
xfs_trans_dup(struct xfs_trans * tp)891da177e4SLinus Torvalds xfs_trans_dup(
90f8f2835aSBrian Foster struct xfs_trans *tp)
911da177e4SLinus Torvalds {
92f8f2835aSBrian Foster struct xfs_trans *ntp;
931da177e4SLinus Torvalds
94ba18781bSDave Chinner trace_xfs_trans_dup(tp, _RET_IP_);
95ba18781bSDave Chinner
96182696fbSDarrick J. Wong ntp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL);
971da177e4SLinus Torvalds
981da177e4SLinus Torvalds /*
991da177e4SLinus Torvalds * Initialize the new transaction structure.
1001da177e4SLinus Torvalds */
1012a3c0accSDave Chinner ntp->t_magic = XFS_TRANS_HEADER_MAGIC;
1021da177e4SLinus Torvalds ntp->t_mountp = tp->t_mountp;
103e98c414fSChristoph Hellwig INIT_LIST_HEAD(&ntp->t_items);
104ed3b4d6cSDave Chinner INIT_LIST_HEAD(&ntp->t_busy);
1059d9e6233SBrian Foster INIT_LIST_HEAD(&ntp->t_dfops);
106692b6cddSDave Chinner ntp->t_highest_agno = NULLAGNUMBER;
1071da177e4SLinus Torvalds
1081da177e4SLinus Torvalds ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1091da177e4SLinus Torvalds ASSERT(tp->t_ticket != NULL);
110cfcbbbd0SNathan Scott
111d9457dc0SJan Kara ntp->t_flags = XFS_TRANS_PERM_LOG_RES |
112d9457dc0SJan Kara (tp->t_flags & XFS_TRANS_RESERVE) |
113f74681baSBrian Foster (tp->t_flags & XFS_TRANS_NO_WRITECOUNT) |
114f74681baSBrian Foster (tp->t_flags & XFS_TRANS_RES_FDBLKS);
115d9457dc0SJan Kara /* We gave our writer reference to the new transaction */
116253f4911SChristoph Hellwig tp->t_flags |= XFS_TRANS_NO_WRITECOUNT;
117cc09c0dcSDave Chinner ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
1183e78b9a4SBrian Foster
1193e78b9a4SBrian Foster ASSERT(tp->t_blk_res >= tp->t_blk_res_used);
1201da177e4SLinus Torvalds ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
1211da177e4SLinus Torvalds tp->t_blk_res = tp->t_blk_res_used;
1223e78b9a4SBrian Foster
1231da177e4SLinus Torvalds ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
1241da177e4SLinus Torvalds tp->t_rtx_res = tp->t_rtx_res_used;
125756b1c34SDave Chinner
126756b1c34SDave Chinner xfs_trans_switch_context(tp, ntp);
127e021a2e5SBrian Foster
1289d9e6233SBrian Foster /* move deferred ops over to the new tp */
129ce356d64SBrian Foster xfs_defer_move(ntp, tp);
1301da177e4SLinus Torvalds
1317d095257SChristoph Hellwig xfs_trans_dup_dqinfo(tp, ntp);
1321da177e4SLinus Torvalds return ntp;
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds
1351da177e4SLinus Torvalds /*
1361da177e4SLinus Torvalds * This is called to reserve free disk blocks and log space for the
1371da177e4SLinus Torvalds * given transaction. This must be done before allocating any resources
1381da177e4SLinus Torvalds * within the transaction.
1391da177e4SLinus Torvalds *
1401da177e4SLinus Torvalds * This will return ENOSPC if there are not enough blocks available.
1411da177e4SLinus Torvalds * It will sleep waiting for available log space.
1421da177e4SLinus Torvalds * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
1431da177e4SLinus Torvalds * is used by long running transactions. If any one of the reservations
1441da177e4SLinus Torvalds * fails then they will all be backed out.
1451da177e4SLinus Torvalds *
1461da177e4SLinus Torvalds * This does not do quota reservations. That typically is done by the
1471da177e4SLinus Torvalds * caller afterwards.
1481da177e4SLinus Torvalds */
149253f4911SChristoph Hellwig static int
xfs_trans_reserve(struct xfs_trans * tp,struct xfs_trans_res * resp,uint blocks,uint rtextents)1501da177e4SLinus Torvalds xfs_trans_reserve(
1513d3c8b52SJie Liu struct xfs_trans *tp,
1523d3c8b52SJie Liu struct xfs_trans_res *resp,
1531da177e4SLinus Torvalds uint blocks,
1543d3c8b52SJie Liu uint rtextents)
1551da177e4SLinus Torvalds {
156dd401770SDave Chinner struct xfs_mount *mp = tp->t_mountp;
15759c1b082SNathan Scott int error = 0;
1580d485adaSDave Chinner bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
1591da177e4SLinus Torvalds
1601da177e4SLinus Torvalds /*
1611da177e4SLinus Torvalds * Attempt to reserve the needed disk blocks by decrementing
1621da177e4SLinus Torvalds * the number needed from the number available. This will
1631da177e4SLinus Torvalds * fail if the count would go below zero.
1641da177e4SLinus Torvalds */
1651da177e4SLinus Torvalds if (blocks > 0) {
166*f30f656eSChristoph Hellwig error = xfs_dec_fdblocks(mp, blocks, rsvd);
167756b1c34SDave Chinner if (error != 0)
1682451337dSDave Chinner return -ENOSPC;
1691da177e4SLinus Torvalds tp->t_blk_res += blocks;
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds
1721da177e4SLinus Torvalds /*
1731da177e4SLinus Torvalds * Reserve the log space needed for this transaction.
1741da177e4SLinus Torvalds */
1753d3c8b52SJie Liu if (resp->tr_logres > 0) {
1769006fb91SChristoph Hellwig bool permanent = false;
1779006fb91SChristoph Hellwig
1783d3c8b52SJie Liu ASSERT(tp->t_log_res == 0 ||
1793d3c8b52SJie Liu tp->t_log_res == resp->tr_logres);
1803d3c8b52SJie Liu ASSERT(tp->t_log_count == 0 ||
1813d3c8b52SJie Liu tp->t_log_count == resp->tr_logcount);
1829006fb91SChristoph Hellwig
1833d3c8b52SJie Liu if (resp->tr_logflags & XFS_TRANS_PERM_LOG_RES) {
1841da177e4SLinus Torvalds tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
1859006fb91SChristoph Hellwig permanent = true;
1861da177e4SLinus Torvalds } else {
1871da177e4SLinus Torvalds ASSERT(tp->t_ticket == NULL);
1881da177e4SLinus Torvalds ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds
1919006fb91SChristoph Hellwig if (tp->t_ticket != NULL) {
1923d3c8b52SJie Liu ASSERT(resp->tr_logflags & XFS_TRANS_PERM_LOG_RES);
193dd401770SDave Chinner error = xfs_log_regrant(mp, tp->t_ticket);
1949006fb91SChristoph Hellwig } else {
195c7610dceSDave Chinner error = xfs_log_reserve(mp, resp->tr_logres,
1963d3c8b52SJie Liu resp->tr_logcount,
197c7610dceSDave Chinner &tp->t_ticket, permanent);
1981da177e4SLinus Torvalds }
1999006fb91SChristoph Hellwig
2009006fb91SChristoph Hellwig if (error)
2019006fb91SChristoph Hellwig goto undo_blocks;
2029006fb91SChristoph Hellwig
2033d3c8b52SJie Liu tp->t_log_res = resp->tr_logres;
2043d3c8b52SJie Liu tp->t_log_count = resp->tr_logcount;
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds
2071da177e4SLinus Torvalds /*
2081da177e4SLinus Torvalds * Attempt to reserve the needed realtime extents by decrementing
2091da177e4SLinus Torvalds * the number needed from the number available. This will
2101da177e4SLinus Torvalds * fail if the count would go below zero.
2111da177e4SLinus Torvalds */
2121da177e4SLinus Torvalds if (rtextents > 0) {
213*f30f656eSChristoph Hellwig error = xfs_dec_frextents(mp, rtextents);
2141da177e4SLinus Torvalds if (error) {
2152451337dSDave Chinner error = -ENOSPC;
2161da177e4SLinus Torvalds goto undo_log;
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds tp->t_rtx_res += rtextents;
2191da177e4SLinus Torvalds }
2201da177e4SLinus Torvalds
2211da177e4SLinus Torvalds return 0;
2221da177e4SLinus Torvalds
2231da177e4SLinus Torvalds /*
2241da177e4SLinus Torvalds * Error cases jump to one of these labels to undo any
2251da177e4SLinus Torvalds * reservations which have already been performed.
2261da177e4SLinus Torvalds */
2271da177e4SLinus Torvalds undo_log:
2283d3c8b52SJie Liu if (resp->tr_logres > 0) {
2298b41e3f9SChristoph Hellwig xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket);
2301da177e4SLinus Torvalds tp->t_ticket = NULL;
2311da177e4SLinus Torvalds tp->t_log_res = 0;
2321da177e4SLinus Torvalds tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
2331da177e4SLinus Torvalds }
2341da177e4SLinus Torvalds
2351da177e4SLinus Torvalds undo_blocks:
2361da177e4SLinus Torvalds if (blocks > 0) {
237*f30f656eSChristoph Hellwig xfs_add_fdblocks(mp, blocks);
2381da177e4SLinus Torvalds tp->t_blk_res = 0;
2391da177e4SLinus Torvalds }
24059c1b082SNathan Scott return error;
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds
243253f4911SChristoph Hellwig int
xfs_trans_alloc(struct xfs_mount * mp,struct xfs_trans_res * resp,uint blocks,uint rtextents,uint flags,struct xfs_trans ** tpp)244253f4911SChristoph Hellwig xfs_trans_alloc(
245253f4911SChristoph Hellwig struct xfs_mount *mp,
246253f4911SChristoph Hellwig struct xfs_trans_res *resp,
247253f4911SChristoph Hellwig uint blocks,
248253f4911SChristoph Hellwig uint rtextents,
249253f4911SChristoph Hellwig uint flags,
250253f4911SChristoph Hellwig struct xfs_trans **tpp)
251253f4911SChristoph Hellwig {
252253f4911SChristoph Hellwig struct xfs_trans *tp;
2539febcda6SDarrick J. Wong bool want_retry = true;
254253f4911SChristoph Hellwig int error;
255253f4911SChristoph Hellwig
2568683edb7SDave Chinner /*
2578683edb7SDave Chinner * Allocate the handle before we do our freeze accounting and setting up
2588683edb7SDave Chinner * GFP_NOFS allocation context so that we avoid lockdep false positives
2598683edb7SDave Chinner * by doing GFP_KERNEL allocations inside sb_start_intwrite().
2608683edb7SDave Chinner */
2619febcda6SDarrick J. Wong retry:
262182696fbSDarrick J. Wong tp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL);
263253f4911SChristoph Hellwig if (!(flags & XFS_TRANS_NO_WRITECOUNT))
264253f4911SChristoph Hellwig sb_start_intwrite(mp->m_super);
265756b1c34SDave Chinner xfs_trans_set_context(tp);
266253f4911SChristoph Hellwig
26710ee2526SDarrick J. Wong /*
26810ee2526SDarrick J. Wong * Zero-reservation ("empty") transactions can't modify anything, so
26910ee2526SDarrick J. Wong * they're allowed to run while we're frozen.
27010ee2526SDarrick J. Wong */
27110ee2526SDarrick J. Wong WARN_ON(resp->tr_logres > 0 &&
27210ee2526SDarrick J. Wong mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
273f74681baSBrian Foster ASSERT(!(flags & XFS_TRANS_RES_FDBLKS) ||
27438c26bfdSDave Chinner xfs_has_lazysbcount(mp));
275253f4911SChristoph Hellwig
276253f4911SChristoph Hellwig tp->t_magic = XFS_TRANS_HEADER_MAGIC;
277253f4911SChristoph Hellwig tp->t_flags = flags;
278253f4911SChristoph Hellwig tp->t_mountp = mp;
279253f4911SChristoph Hellwig INIT_LIST_HEAD(&tp->t_items);
280253f4911SChristoph Hellwig INIT_LIST_HEAD(&tp->t_busy);
2819d9e6233SBrian Foster INIT_LIST_HEAD(&tp->t_dfops);
282692b6cddSDave Chinner tp->t_highest_agno = NULLAGNUMBER;
283253f4911SChristoph Hellwig
284253f4911SChristoph Hellwig error = xfs_trans_reserve(tp, resp, blocks, rtextents);
2859febcda6SDarrick J. Wong if (error == -ENOSPC && want_retry) {
2869febcda6SDarrick J. Wong xfs_trans_cancel(tp);
2879febcda6SDarrick J. Wong
288a1a7d05aSDarrick J. Wong /*
289a1a7d05aSDarrick J. Wong * We weren't able to reserve enough space for the transaction.
290a1a7d05aSDarrick J. Wong * Flush the other speculative space allocations to free space.
291a1a7d05aSDarrick J. Wong * Do not perform a synchronous scan because callers can hold
292a1a7d05aSDarrick J. Wong * other locks.
293a1a7d05aSDarrick J. Wong */
294d4d12c02SDave Chinner error = xfs_blockgc_flush_all(mp);
295d4d12c02SDave Chinner if (error)
296d4d12c02SDave Chinner return error;
2979febcda6SDarrick J. Wong want_retry = false;
2989febcda6SDarrick J. Wong goto retry;
299a1a7d05aSDarrick J. Wong }
300253f4911SChristoph Hellwig if (error) {
301253f4911SChristoph Hellwig xfs_trans_cancel(tp);
302253f4911SChristoph Hellwig return error;
303253f4911SChristoph Hellwig }
304253f4911SChristoph Hellwig
305ba18781bSDave Chinner trace_xfs_trans_alloc(tp, _RET_IP_);
306ba18781bSDave Chinner
307253f4911SChristoph Hellwig *tpp = tp;
308253f4911SChristoph Hellwig return 0;
309253f4911SChristoph Hellwig }
310253f4911SChristoph Hellwig
3111da177e4SLinus Torvalds /*
312e89c0413SDarrick J. Wong * Create an empty transaction with no reservation. This is a defensive
313b41b46c2SDave Chinner * mechanism for routines that query metadata without actually modifying them --
314b41b46c2SDave Chinner * if the metadata being queried is somehow cross-linked (think a btree block
315b41b46c2SDave Chinner * pointer that points higher in the tree), we risk deadlock. However, blocks
316b41b46c2SDave Chinner * grabbed as part of a transaction can be re-grabbed. The verifiers will
317b41b46c2SDave Chinner * notice the corrupt block and the operation will fail back to userspace
318b41b46c2SDave Chinner * without deadlocking.
319e89c0413SDarrick J. Wong *
320b41b46c2SDave Chinner * Note the zero-length reservation; this transaction MUST be cancelled without
321b41b46c2SDave Chinner * any dirty data.
32227fb5a72SDarrick J. Wong *
323b41b46c2SDave Chinner * Callers should obtain freeze protection to avoid a conflict with fs freezing
324b41b46c2SDave Chinner * where we can be grabbing buffers at the same time that freeze is trying to
325b41b46c2SDave Chinner * drain the buffer LRU list.
326e89c0413SDarrick J. Wong */
327e89c0413SDarrick J. Wong int
xfs_trans_alloc_empty(struct xfs_mount * mp,struct xfs_trans ** tpp)328e89c0413SDarrick J. Wong xfs_trans_alloc_empty(
329e89c0413SDarrick J. Wong struct xfs_mount *mp,
330e89c0413SDarrick J. Wong struct xfs_trans **tpp)
331e89c0413SDarrick J. Wong {
332e89c0413SDarrick J. Wong struct xfs_trans_res resv = {0};
333e89c0413SDarrick J. Wong
334e89c0413SDarrick J. Wong return xfs_trans_alloc(mp, &resv, 0, 0, XFS_TRANS_NO_WRITECOUNT, tpp);
335e89c0413SDarrick J. Wong }
336e89c0413SDarrick J. Wong
337e89c0413SDarrick J. Wong /*
3381da177e4SLinus Torvalds * Record the indicated change to the given field for application
3391da177e4SLinus Torvalds * to the file system's superblock when the transaction commits.
3401da177e4SLinus Torvalds * For now, just store the change in the transaction structure.
3411da177e4SLinus Torvalds *
3421da177e4SLinus Torvalds * Mark the transaction structure to indicate that the superblock
3431da177e4SLinus Torvalds * needs to be updated before committing.
34492821e2bSDavid Chinner *
34592821e2bSDavid Chinner * Because we may not be keeping track of allocated/free inodes and
34692821e2bSDavid Chinner * used filesystem blocks in the superblock, we do not mark the
34792821e2bSDavid Chinner * superblock dirty in this transaction if we modify these fields.
34892821e2bSDavid Chinner * We still need to update the transaction deltas so that they get
34992821e2bSDavid Chinner * applied to the incore superblock, but we don't want them to
35092821e2bSDavid Chinner * cause the superblock to get locked and logged if these are the
35192821e2bSDavid Chinner * only fields in the superblock that the transaction modifies.
3521da177e4SLinus Torvalds */
3531da177e4SLinus Torvalds void
xfs_trans_mod_sb(xfs_trans_t * tp,uint field,int64_t delta)3541da177e4SLinus Torvalds xfs_trans_mod_sb(
3551da177e4SLinus Torvalds xfs_trans_t *tp,
3561da177e4SLinus Torvalds uint field,
35720f4ebf2SDavid Chinner int64_t delta)
3581da177e4SLinus Torvalds {
35992821e2bSDavid Chinner uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
36092821e2bSDavid Chinner xfs_mount_t *mp = tp->t_mountp;
3611da177e4SLinus Torvalds
3621da177e4SLinus Torvalds switch (field) {
3631da177e4SLinus Torvalds case XFS_TRANS_SB_ICOUNT:
3641da177e4SLinus Torvalds tp->t_icount_delta += delta;
36538c26bfdSDave Chinner if (xfs_has_lazysbcount(mp))
36692821e2bSDavid Chinner flags &= ~XFS_TRANS_SB_DIRTY;
3671da177e4SLinus Torvalds break;
3681da177e4SLinus Torvalds case XFS_TRANS_SB_IFREE:
3691da177e4SLinus Torvalds tp->t_ifree_delta += delta;
37038c26bfdSDave Chinner if (xfs_has_lazysbcount(mp))
37192821e2bSDavid Chinner flags &= ~XFS_TRANS_SB_DIRTY;
3721da177e4SLinus Torvalds break;
3731da177e4SLinus Torvalds case XFS_TRANS_SB_FDBLOCKS:
3741da177e4SLinus Torvalds /*
3753e78b9a4SBrian Foster * Track the number of blocks allocated in the transaction.
3763e78b9a4SBrian Foster * Make sure it does not exceed the number reserved. If so,
3773e78b9a4SBrian Foster * shutdown as this can lead to accounting inconsistency.
3781da177e4SLinus Torvalds */
3791da177e4SLinus Torvalds if (delta < 0) {
3801da177e4SLinus Torvalds tp->t_blk_res_used += (uint)-delta;
3813e78b9a4SBrian Foster if (tp->t_blk_res_used > tp->t_blk_res)
3823e78b9a4SBrian Foster xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
383f74681baSBrian Foster } else if (delta > 0 && (tp->t_flags & XFS_TRANS_RES_FDBLKS)) {
384f74681baSBrian Foster int64_t blkres_delta;
385f74681baSBrian Foster
386f74681baSBrian Foster /*
387f74681baSBrian Foster * Return freed blocks directly to the reservation
388f74681baSBrian Foster * instead of the global pool, being careful not to
389f74681baSBrian Foster * overflow the trans counter. This is used to preserve
390f74681baSBrian Foster * reservation across chains of transaction rolls that
391f74681baSBrian Foster * repeatedly free and allocate blocks.
392f74681baSBrian Foster */
393f74681baSBrian Foster blkres_delta = min_t(int64_t, delta,
394f74681baSBrian Foster UINT_MAX - tp->t_blk_res);
395f74681baSBrian Foster tp->t_blk_res += blkres_delta;
396f74681baSBrian Foster delta -= blkres_delta;
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds tp->t_fdblocks_delta += delta;
39938c26bfdSDave Chinner if (xfs_has_lazysbcount(mp))
40092821e2bSDavid Chinner flags &= ~XFS_TRANS_SB_DIRTY;
4011da177e4SLinus Torvalds break;
4021da177e4SLinus Torvalds case XFS_TRANS_SB_RES_FDBLOCKS:
4031da177e4SLinus Torvalds /*
4041da177e4SLinus Torvalds * The allocation has already been applied to the
4051da177e4SLinus Torvalds * in-core superblock's counter. This should only
4061da177e4SLinus Torvalds * be applied to the on-disk superblock.
4071da177e4SLinus Torvalds */
4081da177e4SLinus Torvalds tp->t_res_fdblocks_delta += delta;
40938c26bfdSDave Chinner if (xfs_has_lazysbcount(mp))
41092821e2bSDavid Chinner flags &= ~XFS_TRANS_SB_DIRTY;
4111da177e4SLinus Torvalds break;
4121da177e4SLinus Torvalds case XFS_TRANS_SB_FREXTENTS:
4131da177e4SLinus Torvalds /*
4141da177e4SLinus Torvalds * Track the number of blocks allocated in the
4151da177e4SLinus Torvalds * transaction. Make sure it does not exceed the
4161da177e4SLinus Torvalds * number reserved.
4171da177e4SLinus Torvalds */
4181da177e4SLinus Torvalds if (delta < 0) {
4191da177e4SLinus Torvalds tp->t_rtx_res_used += (uint)-delta;
4201da177e4SLinus Torvalds ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds tp->t_frextents_delta += delta;
4231da177e4SLinus Torvalds break;
4241da177e4SLinus Torvalds case XFS_TRANS_SB_RES_FREXTENTS:
4251da177e4SLinus Torvalds /*
4261da177e4SLinus Torvalds * The allocation has already been applied to the
427c41564b5SNathan Scott * in-core superblock's counter. This should only
4281da177e4SLinus Torvalds * be applied to the on-disk superblock.
4291da177e4SLinus Torvalds */
4301da177e4SLinus Torvalds ASSERT(delta < 0);
4311da177e4SLinus Torvalds tp->t_res_frextents_delta += delta;
4321da177e4SLinus Torvalds break;
4331da177e4SLinus Torvalds case XFS_TRANS_SB_DBLOCKS:
4341da177e4SLinus Torvalds tp->t_dblocks_delta += delta;
4351da177e4SLinus Torvalds break;
4361da177e4SLinus Torvalds case XFS_TRANS_SB_AGCOUNT:
4371da177e4SLinus Torvalds ASSERT(delta > 0);
4381da177e4SLinus Torvalds tp->t_agcount_delta += delta;
4391da177e4SLinus Torvalds break;
4401da177e4SLinus Torvalds case XFS_TRANS_SB_IMAXPCT:
4411da177e4SLinus Torvalds tp->t_imaxpct_delta += delta;
4421da177e4SLinus Torvalds break;
4431da177e4SLinus Torvalds case XFS_TRANS_SB_REXTSIZE:
4441da177e4SLinus Torvalds tp->t_rextsize_delta += delta;
4451da177e4SLinus Torvalds break;
4461da177e4SLinus Torvalds case XFS_TRANS_SB_RBMBLOCKS:
4471da177e4SLinus Torvalds tp->t_rbmblocks_delta += delta;
4481da177e4SLinus Torvalds break;
4491da177e4SLinus Torvalds case XFS_TRANS_SB_RBLOCKS:
4501da177e4SLinus Torvalds tp->t_rblocks_delta += delta;
4511da177e4SLinus Torvalds break;
4521da177e4SLinus Torvalds case XFS_TRANS_SB_REXTENTS:
4531da177e4SLinus Torvalds tp->t_rextents_delta += delta;
4541da177e4SLinus Torvalds break;
4551da177e4SLinus Torvalds case XFS_TRANS_SB_REXTSLOG:
4561da177e4SLinus Torvalds tp->t_rextslog_delta += delta;
4571da177e4SLinus Torvalds break;
4581da177e4SLinus Torvalds default:
4591da177e4SLinus Torvalds ASSERT(0);
4601da177e4SLinus Torvalds return;
4611da177e4SLinus Torvalds }
4621da177e4SLinus Torvalds
463210c6f1cSDavid Chinner tp->t_flags |= flags;
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds
4661da177e4SLinus Torvalds /*
4671da177e4SLinus Torvalds * xfs_trans_apply_sb_deltas() is called from the commit code
4681da177e4SLinus Torvalds * to bring the superblock buffer into the current transaction
4691da177e4SLinus Torvalds * and modify it as requested by earlier calls to xfs_trans_mod_sb().
4701da177e4SLinus Torvalds *
4711da177e4SLinus Torvalds * For now we just look at each field allowed to change and change
4721da177e4SLinus Torvalds * it if necessary.
4731da177e4SLinus Torvalds */
4741da177e4SLinus Torvalds STATIC void
xfs_trans_apply_sb_deltas(xfs_trans_t * tp)4751da177e4SLinus Torvalds xfs_trans_apply_sb_deltas(
4761da177e4SLinus Torvalds xfs_trans_t *tp)
4771da177e4SLinus Torvalds {
478ed67ebfdSChristoph Hellwig struct xfs_dsb *sbp;
479e8222613SDave Chinner struct xfs_buf *bp;
4801da177e4SLinus Torvalds int whole = 0;
4811da177e4SLinus Torvalds
482cead0b10SChristoph Hellwig bp = xfs_trans_getsb(tp);
4833e6e8afdSChristoph Hellwig sbp = bp->b_addr;
4841da177e4SLinus Torvalds
4851da177e4SLinus Torvalds /*
48692821e2bSDavid Chinner * Only update the superblock counters if we are logging them
48792821e2bSDavid Chinner */
48838c26bfdSDave Chinner if (!xfs_has_lazysbcount((tp->t_mountp))) {
4892bdf7cd0SChristoph Hellwig if (tp->t_icount_delta)
490413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
4912bdf7cd0SChristoph Hellwig if (tp->t_ifree_delta)
492413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
4932bdf7cd0SChristoph Hellwig if (tp->t_fdblocks_delta)
494413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
4952bdf7cd0SChristoph Hellwig if (tp->t_res_fdblocks_delta)
496413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
4971da177e4SLinus Torvalds }
4981da177e4SLinus Torvalds
4992229276cSDarrick J. Wong /*
5002229276cSDarrick J. Wong * Updating frextents requires careful handling because it does not
5012229276cSDarrick J. Wong * behave like the lazysb counters because we cannot rely on log
5022229276cSDarrick J. Wong * recovery in older kenels to recompute the value from the rtbitmap.
5032229276cSDarrick J. Wong * This means that the ondisk frextents must be consistent with the
5042229276cSDarrick J. Wong * rtbitmap.
5052229276cSDarrick J. Wong *
5062229276cSDarrick J. Wong * Therefore, log the frextents change to the ondisk superblock and
5072229276cSDarrick J. Wong * update the incore superblock so that future calls to xfs_log_sb
5082229276cSDarrick J. Wong * write the correct value ondisk.
5092229276cSDarrick J. Wong *
5102229276cSDarrick J. Wong * Don't touch m_frextents because it includes incore reservations,
5112229276cSDarrick J. Wong * and those are handled by the unreserve function.
5122229276cSDarrick J. Wong */
5132229276cSDarrick J. Wong if (tp->t_frextents_delta || tp->t_res_frextents_delta) {
5142229276cSDarrick J. Wong struct xfs_mount *mp = tp->t_mountp;
5152229276cSDarrick J. Wong int64_t rtxdelta;
5162229276cSDarrick J. Wong
5172229276cSDarrick J. Wong rtxdelta = tp->t_frextents_delta + tp->t_res_frextents_delta;
5182229276cSDarrick J. Wong
5192229276cSDarrick J. Wong spin_lock(&mp->m_sb_lock);
5202229276cSDarrick J. Wong be64_add_cpu(&sbp->sb_frextents, rtxdelta);
5212229276cSDarrick J. Wong mp->m_sb.sb_frextents += rtxdelta;
5222229276cSDarrick J. Wong spin_unlock(&mp->m_sb_lock);
5232229276cSDarrick J. Wong }
5241da177e4SLinus Torvalds
5252bdf7cd0SChristoph Hellwig if (tp->t_dblocks_delta) {
526413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
5271da177e4SLinus Torvalds whole = 1;
5281da177e4SLinus Torvalds }
5292bdf7cd0SChristoph Hellwig if (tp->t_agcount_delta) {
530413d57c9SMarcin Slusarz be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
5311da177e4SLinus Torvalds whole = 1;
5321da177e4SLinus Torvalds }
5332bdf7cd0SChristoph Hellwig if (tp->t_imaxpct_delta) {
5342bdf7cd0SChristoph Hellwig sbp->sb_imax_pct += tp->t_imaxpct_delta;
5351da177e4SLinus Torvalds whole = 1;
5361da177e4SLinus Torvalds }
5372bdf7cd0SChristoph Hellwig if (tp->t_rextsize_delta) {
538413d57c9SMarcin Slusarz be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
5391da177e4SLinus Torvalds whole = 1;
5401da177e4SLinus Torvalds }
5412bdf7cd0SChristoph Hellwig if (tp->t_rbmblocks_delta) {
542413d57c9SMarcin Slusarz be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
5431da177e4SLinus Torvalds whole = 1;
5441da177e4SLinus Torvalds }
5452bdf7cd0SChristoph Hellwig if (tp->t_rblocks_delta) {
546413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
5471da177e4SLinus Torvalds whole = 1;
5481da177e4SLinus Torvalds }
5492bdf7cd0SChristoph Hellwig if (tp->t_rextents_delta) {
550413d57c9SMarcin Slusarz be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
5511da177e4SLinus Torvalds whole = 1;
5521da177e4SLinus Torvalds }
5532bdf7cd0SChristoph Hellwig if (tp->t_rextslog_delta) {
5542bdf7cd0SChristoph Hellwig sbp->sb_rextslog += tp->t_rextslog_delta;
5551da177e4SLinus Torvalds whole = 1;
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds
5583443a3bcSDave Chinner xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
5591da177e4SLinus Torvalds if (whole)
5601da177e4SLinus Torvalds /*
561c41564b5SNathan Scott * Log the whole thing, the fields are noncontiguous.
5621da177e4SLinus Torvalds */
563ed67ebfdSChristoph Hellwig xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1);
5641da177e4SLinus Torvalds else
5651da177e4SLinus Torvalds /*
5661da177e4SLinus Torvalds * Since all the modifiable fields are contiguous, we
5671da177e4SLinus Torvalds * can get away with this.
5681da177e4SLinus Torvalds */
569ed67ebfdSChristoph Hellwig xfs_trans_log_buf(tp, bp, offsetof(struct xfs_dsb, sb_icount),
570ed67ebfdSChristoph Hellwig offsetof(struct xfs_dsb, sb_frextents) +
5711da177e4SLinus Torvalds sizeof(sbp->sb_frextents) - 1);
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds
5741da177e4SLinus Torvalds /*
575dc3ffbb1SDave Chinner * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations and
576dc3ffbb1SDave Chinner * apply superblock counter changes to the in-core superblock. The
57745c34141SDavid Chinner * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
57845c34141SDavid Chinner * applied to the in-core superblock. The idea is that that has already been
57945c34141SDavid Chinner * done.
5801da177e4SLinus Torvalds *
58145c34141SDavid Chinner * If we are not logging superblock counters, then the inode allocated/free and
58245c34141SDavid Chinner * used block counts are not updated in the on disk superblock. In this case,
58345c34141SDavid Chinner * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
58445c34141SDavid Chinner * still need to update the incore superblock with the changes.
585f18c9a90SDave Chinner *
586f18c9a90SDave Chinner * Deltas for the inode count are +/-64, hence we use a large batch size of 128
587f18c9a90SDave Chinner * so we don't need to take the counter lock on every update.
5881da177e4SLinus Torvalds */
589f18c9a90SDave Chinner #define XFS_ICOUNT_BATCH 128
590f18c9a90SDave Chinner
59171e330b5SDave Chinner void
xfs_trans_unreserve_and_mod_sb(struct xfs_trans * tp)5921da177e4SLinus Torvalds xfs_trans_unreserve_and_mod_sb(
5930bd5ddedSDave Chinner struct xfs_trans *tp)
5941da177e4SLinus Torvalds {
5950bd5ddedSDave Chinner struct xfs_mount *mp = tp->t_mountp;
5965e1e4d4fSChristoph Hellwig int64_t blkdelta = tp->t_blk_res;
5975e1e4d4fSChristoph Hellwig int64_t rtxdelta = tp->t_rtx_res;
5981b040712SChristoph Hellwig int64_t idelta = 0;
5991b040712SChristoph Hellwig int64_t ifreedelta = 0;
6001da177e4SLinus Torvalds
6015e1e4d4fSChristoph Hellwig /*
6025e1e4d4fSChristoph Hellwig * Calculate the deltas.
6035e1e4d4fSChristoph Hellwig *
6045e1e4d4fSChristoph Hellwig * t_fdblocks_delta and t_frextents_delta can be positive or negative:
6055e1e4d4fSChristoph Hellwig *
6065e1e4d4fSChristoph Hellwig * - positive values indicate blocks freed in the transaction.
6075e1e4d4fSChristoph Hellwig * - negative values indicate blocks allocated in the transaction
6085e1e4d4fSChristoph Hellwig *
6095e1e4d4fSChristoph Hellwig * Negative values can only happen if the transaction has a block
6105e1e4d4fSChristoph Hellwig * reservation that covers the allocated block. The end result is
6115e1e4d4fSChristoph Hellwig * that the calculated delta values must always be positive and we
6125e1e4d4fSChristoph Hellwig * can only put back previous allocated or reserved blocks here.
6135e1e4d4fSChristoph Hellwig */
6145e1e4d4fSChristoph Hellwig ASSERT(tp->t_blk_res || tp->t_fdblocks_delta >= 0);
6155e1e4d4fSChristoph Hellwig if (xfs_has_lazysbcount(mp) || (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
61645c34141SDavid Chinner blkdelta += tp->t_fdblocks_delta;
6175e1e4d4fSChristoph Hellwig ASSERT(blkdelta >= 0);
6185e1e4d4fSChristoph Hellwig }
61945c34141SDavid Chinner
6205e1e4d4fSChristoph Hellwig ASSERT(tp->t_rtx_res || tp->t_frextents_delta >= 0);
6215e1e4d4fSChristoph Hellwig if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
62245c34141SDavid Chinner rtxdelta += tp->t_frextents_delta;
6235e1e4d4fSChristoph Hellwig ASSERT(rtxdelta >= 0);
6245e1e4d4fSChristoph Hellwig }
62545c34141SDavid Chinner
6265e1e4d4fSChristoph Hellwig if (xfs_has_lazysbcount(mp) || (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
6271b040712SChristoph Hellwig idelta = tp->t_icount_delta;
6281b040712SChristoph Hellwig ifreedelta = tp->t_ifree_delta;
6291b040712SChristoph Hellwig }
6301b040712SChristoph Hellwig
6311b040712SChristoph Hellwig /* apply the per-cpu counters */
632*f30f656eSChristoph Hellwig if (blkdelta)
633*f30f656eSChristoph Hellwig xfs_add_fdblocks(mp, blkdelta);
6341b040712SChristoph Hellwig
6355825bea0SDave Chinner if (idelta)
636f18c9a90SDave Chinner percpu_counter_add_batch(&mp->m_icount, idelta,
637f18c9a90SDave Chinner XFS_ICOUNT_BATCH);
6381b040712SChristoph Hellwig
6395825bea0SDave Chinner if (ifreedelta)
640f18c9a90SDave Chinner percpu_counter_add(&mp->m_ifree, ifreedelta);
6411b040712SChristoph Hellwig
642*f30f656eSChristoph Hellwig if (rtxdelta)
643*f30f656eSChristoph Hellwig xfs_add_frextents(mp, rtxdelta);
6442229276cSDarrick J. Wong
6452229276cSDarrick J. Wong if (!(tp->t_flags & XFS_TRANS_SB_DIRTY))
6460bd5ddedSDave Chinner return;
6470bd5ddedSDave Chinner
6481b040712SChristoph Hellwig /* apply remaining deltas */
6490bd5ddedSDave Chinner spin_lock(&mp->m_sb_lock);
6506543990aSDave Chinner mp->m_sb.sb_fdblocks += tp->t_fdblocks_delta + tp->t_res_fdblocks_delta;
6516543990aSDave Chinner mp->m_sb.sb_icount += idelta;
6526543990aSDave Chinner mp->m_sb.sb_ifree += ifreedelta;
6532229276cSDarrick J. Wong /*
6542229276cSDarrick J. Wong * Do not touch sb_frextents here because we are dealing with incore
6552229276cSDarrick J. Wong * reservation. sb_frextents is not part of the lazy sb counters so it
6562229276cSDarrick J. Wong * must be consistent with the ondisk rtbitmap and must never include
6572229276cSDarrick J. Wong * incore reservations.
6582229276cSDarrick J. Wong */
659dc3ffbb1SDave Chinner mp->m_sb.sb_dblocks += tp->t_dblocks_delta;
660dc3ffbb1SDave Chinner mp->m_sb.sb_agcount += tp->t_agcount_delta;
661dc3ffbb1SDave Chinner mp->m_sb.sb_imax_pct += tp->t_imaxpct_delta;
662dc3ffbb1SDave Chinner mp->m_sb.sb_rextsize += tp->t_rextsize_delta;
663ef5a83b7SDarrick J. Wong if (tp->t_rextsize_delta) {
664ef5a83b7SDarrick J. Wong mp->m_rtxblklog = log2_if_power2(mp->m_sb.sb_rextsize);
665ef5a83b7SDarrick J. Wong mp->m_rtxblkmask = mask64_if_power2(mp->m_sb.sb_rextsize);
666ef5a83b7SDarrick J. Wong }
667dc3ffbb1SDave Chinner mp->m_sb.sb_rbmblocks += tp->t_rbmblocks_delta;
668dc3ffbb1SDave Chinner mp->m_sb.sb_rblocks += tp->t_rblocks_delta;
669dc3ffbb1SDave Chinner mp->m_sb.sb_rextents += tp->t_rextents_delta;
670dc3ffbb1SDave Chinner mp->m_sb.sb_rextslog += tp->t_rextslog_delta;
6710bd5ddedSDave Chinner spin_unlock(&mp->m_sb_lock);
6721b040712SChristoph Hellwig
673dc3ffbb1SDave Chinner /*
674dc3ffbb1SDave Chinner * Debug checks outside of the spinlock so they don't lock up the
675dc3ffbb1SDave Chinner * machine if they fail.
676dc3ffbb1SDave Chinner */
677dc3ffbb1SDave Chinner ASSERT(mp->m_sb.sb_imax_pct >= 0);
678dc3ffbb1SDave Chinner ASSERT(mp->m_sb.sb_rextslog >= 0);
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds
681e6631f85SDave Chinner /* Add the given log item to the transaction's list of log items. */
682e98c414fSChristoph Hellwig void
xfs_trans_add_item(struct xfs_trans * tp,struct xfs_log_item * lip)683e98c414fSChristoph Hellwig xfs_trans_add_item(
684e98c414fSChristoph Hellwig struct xfs_trans *tp,
685e98c414fSChristoph Hellwig struct xfs_log_item *lip)
686e98c414fSChristoph Hellwig {
687d86142ddSDave Chinner ASSERT(lip->li_log == tp->t_mountp->m_log);
688f65020a8SJesper Juhl ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
689e6631f85SDave Chinner ASSERT(list_empty(&lip->li_trans));
690e6631f85SDave Chinner ASSERT(!test_bit(XFS_LI_DIRTY, &lip->li_flags));
691e98c414fSChristoph Hellwig
692e6631f85SDave Chinner list_add_tail(&lip->li_trans, &tp->t_items);
693ba18781bSDave Chinner trace_xfs_trans_add_item(tp, _RET_IP_);
694e98c414fSChristoph Hellwig }
695e98c414fSChristoph Hellwig
696e98c414fSChristoph Hellwig /*
697e6631f85SDave Chinner * Unlink the log item from the transaction. the log item is no longer
698e6631f85SDave Chinner * considered dirty in this transaction, as the linked transaction has
699e6631f85SDave Chinner * finished, either by abort or commit completion.
700e98c414fSChristoph Hellwig */
701e98c414fSChristoph Hellwig void
xfs_trans_del_item(struct xfs_log_item * lip)702e98c414fSChristoph Hellwig xfs_trans_del_item(
703e98c414fSChristoph Hellwig struct xfs_log_item *lip)
704e98c414fSChristoph Hellwig {
705e6631f85SDave Chinner clear_bit(XFS_LI_DIRTY, &lip->li_flags);
706e6631f85SDave Chinner list_del_init(&lip->li_trans);
707e98c414fSChristoph Hellwig }
708e98c414fSChristoph Hellwig
709e6631f85SDave Chinner /* Detach and unlock all of the items in a transaction */
710195cd83dSChristoph Hellwig static void
xfs_trans_free_items(struct xfs_trans * tp,bool abort)711e98c414fSChristoph Hellwig xfs_trans_free_items(
712e98c414fSChristoph Hellwig struct xfs_trans *tp,
713eacb24e7SChristoph Hellwig bool abort)
714e98c414fSChristoph Hellwig {
715e6631f85SDave Chinner struct xfs_log_item *lip, *next;
716e98c414fSChristoph Hellwig
717ba18781bSDave Chinner trace_xfs_trans_free_items(tp, _RET_IP_);
718ba18781bSDave Chinner
719e6631f85SDave Chinner list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
720e6631f85SDave Chinner xfs_trans_del_item(lip);
721eacb24e7SChristoph Hellwig if (abort)
72222525c17SDave Chinner set_bit(XFS_LI_ABORTED, &lip->li_flags);
723ddf92053SChristoph Hellwig if (lip->li_ops->iop_release)
724ddf92053SChristoph Hellwig lip->li_ops->iop_release(lip);
725e98c414fSChristoph Hellwig }
726e98c414fSChristoph Hellwig }
727e98c414fSChristoph Hellwig
728b1c1b5b6SDave Chinner /*
729fad743d7SDave Chinner * Sort transaction items prior to running precommit operations. This will
730fad743d7SDave Chinner * attempt to order the items such that they will always be locked in the same
731fad743d7SDave Chinner * order. Items that have no sort function are moved to the end of the list
732fad743d7SDave Chinner * and so are locked last.
733fad743d7SDave Chinner *
734fad743d7SDave Chinner * This may need refinement as different types of objects add sort functions.
735fad743d7SDave Chinner *
736fad743d7SDave Chinner * Function is more complex than it needs to be because we are comparing 64 bit
737fad743d7SDave Chinner * values and the function only returns 32 bit values.
738fad743d7SDave Chinner */
739fad743d7SDave Chinner static int
xfs_trans_precommit_sort(void * unused_arg,const struct list_head * a,const struct list_head * b)740fad743d7SDave Chinner xfs_trans_precommit_sort(
741fad743d7SDave Chinner void *unused_arg,
742fad743d7SDave Chinner const struct list_head *a,
743fad743d7SDave Chinner const struct list_head *b)
744fad743d7SDave Chinner {
745fad743d7SDave Chinner struct xfs_log_item *lia = container_of(a,
746fad743d7SDave Chinner struct xfs_log_item, li_trans);
747fad743d7SDave Chinner struct xfs_log_item *lib = container_of(b,
748fad743d7SDave Chinner struct xfs_log_item, li_trans);
749fad743d7SDave Chinner int64_t diff;
750fad743d7SDave Chinner
751fad743d7SDave Chinner /*
752fad743d7SDave Chinner * If both items are non-sortable, leave them alone. If only one is
753fad743d7SDave Chinner * sortable, move the non-sortable item towards the end of the list.
754fad743d7SDave Chinner */
755fad743d7SDave Chinner if (!lia->li_ops->iop_sort && !lib->li_ops->iop_sort)
756fad743d7SDave Chinner return 0;
757fad743d7SDave Chinner if (!lia->li_ops->iop_sort)
758fad743d7SDave Chinner return 1;
759fad743d7SDave Chinner if (!lib->li_ops->iop_sort)
760fad743d7SDave Chinner return -1;
761fad743d7SDave Chinner
762fad743d7SDave Chinner diff = lia->li_ops->iop_sort(lia) - lib->li_ops->iop_sort(lib);
763fad743d7SDave Chinner if (diff < 0)
764fad743d7SDave Chinner return -1;
765fad743d7SDave Chinner if (diff > 0)
766fad743d7SDave Chinner return 1;
767fad743d7SDave Chinner return 0;
768fad743d7SDave Chinner }
769fad743d7SDave Chinner
770fad743d7SDave Chinner /*
771fad743d7SDave Chinner * Run transaction precommit functions.
772fad743d7SDave Chinner *
773fad743d7SDave Chinner * If there is an error in any of the callouts, then stop immediately and
774fad743d7SDave Chinner * trigger a shutdown to abort the transaction. There is no recovery possible
775fad743d7SDave Chinner * from errors at this point as the transaction is dirty....
776fad743d7SDave Chinner */
777fad743d7SDave Chinner static int
xfs_trans_run_precommits(struct xfs_trans * tp)778fad743d7SDave Chinner xfs_trans_run_precommits(
779fad743d7SDave Chinner struct xfs_trans *tp)
780fad743d7SDave Chinner {
781fad743d7SDave Chinner struct xfs_mount *mp = tp->t_mountp;
782fad743d7SDave Chinner struct xfs_log_item *lip, *n;
783fad743d7SDave Chinner int error = 0;
784fad743d7SDave Chinner
785fad743d7SDave Chinner /*
786fad743d7SDave Chinner * Sort the item list to avoid ABBA deadlocks with other transactions
787fad743d7SDave Chinner * running precommit operations that lock multiple shared items such as
788fad743d7SDave Chinner * inode cluster buffers.
789fad743d7SDave Chinner */
790fad743d7SDave Chinner list_sort(NULL, &tp->t_items, xfs_trans_precommit_sort);
791fad743d7SDave Chinner
792fad743d7SDave Chinner /*
793fad743d7SDave Chinner * Precommit operations can remove the log item from the transaction
794fad743d7SDave Chinner * if the log item exists purely to delay modifications until they
795fad743d7SDave Chinner * can be ordered against other operations. Hence we have to use
796fad743d7SDave Chinner * list_for_each_entry_safe() here.
797fad743d7SDave Chinner */
798fad743d7SDave Chinner list_for_each_entry_safe(lip, n, &tp->t_items, li_trans) {
799fad743d7SDave Chinner if (!test_bit(XFS_LI_DIRTY, &lip->li_flags))
800fad743d7SDave Chinner continue;
801fad743d7SDave Chinner if (lip->li_ops->iop_precommit) {
802fad743d7SDave Chinner error = lip->li_ops->iop_precommit(tp, lip);
803fad743d7SDave Chinner if (error)
804fad743d7SDave Chinner break;
805fad743d7SDave Chinner }
806fad743d7SDave Chinner }
807fad743d7SDave Chinner if (error)
808fad743d7SDave Chinner xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
809fad743d7SDave Chinner return error;
810fad743d7SDave Chinner }
811fad743d7SDave Chinner
812fad743d7SDave Chinner /*
813b1037058SChristoph Hellwig * Commit the given transaction to the log.
8140924378aSDave Chinner *
8150924378aSDave Chinner * XFS disk error handling mechanism is not based on a typical
8160924378aSDave Chinner * transaction abort mechanism. Logically after the filesystem
8170924378aSDave Chinner * gets marked 'SHUTDOWN', we can't let any new transactions
8180924378aSDave Chinner * be durable - ie. committed to disk - because some metadata might
8190924378aSDave Chinner * be inconsistent. In such cases, this returns an error, and the
8200924378aSDave Chinner * caller may assume that all locked objects joined to the transaction
8210924378aSDave Chinner * have already been unlocked as if the commit had succeeded.
8220924378aSDave Chinner * Do not reference the transaction structure after this call.
8230924378aSDave Chinner */
82470393313SChristoph Hellwig static int
__xfs_trans_commit(struct xfs_trans * tp,bool regrant)82570393313SChristoph Hellwig __xfs_trans_commit(
826a3ccd2caSChristoph Hellwig struct xfs_trans *tp,
82770393313SChristoph Hellwig bool regrant)
8280924378aSDave Chinner {
829a3ccd2caSChristoph Hellwig struct xfs_mount *mp = tp->t_mountp;
8303c4cb76bSDave Chinner struct xlog *log = mp->m_log;
8315f9b4b0dSDave Chinner xfs_csn_t commit_seq = 0;
832a3ccd2caSChristoph Hellwig int error = 0;
8330924378aSDave Chinner int sync = tp->t_flags & XFS_TRANS_SYNC;
8340924378aSDave Chinner
835ba18781bSDave Chinner trace_xfs_trans_commit(tp, _RET_IP_);
836ba18781bSDave Chinner
837fad743d7SDave Chinner error = xfs_trans_run_precommits(tp);
838fad743d7SDave Chinner if (error) {
839fad743d7SDave Chinner if (tp->t_flags & XFS_TRANS_PERM_LOG_RES)
840fad743d7SDave Chinner xfs_defer_cancel(tp);
841fad743d7SDave Chinner goto out_unreserve;
842fad743d7SDave Chinner }
843fad743d7SDave Chinner
84498719051SBrian Foster /*
84598719051SBrian Foster * Finish deferred items on final commit. Only permanent transactions
84698719051SBrian Foster * should ever have deferred ops.
84798719051SBrian Foster */
8489d9e6233SBrian Foster WARN_ON_ONCE(!list_empty(&tp->t_dfops) &&
84998719051SBrian Foster !(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
85098719051SBrian Foster if (!regrant && (tp->t_flags & XFS_TRANS_PERM_LOG_RES)) {
851b277c37fSBrian Foster error = xfs_defer_finish_noroll(&tp);
8529b1f4e98SBrian Foster if (error)
853e021a2e5SBrian Foster goto out_unreserve;
854cb042117SDave Chinner
855cb042117SDave Chinner /* Run precommits from final tx in defer chain. */
856cb042117SDave Chinner error = xfs_trans_run_precommits(tp);
857cb042117SDave Chinner if (error)
858cb042117SDave Chinner goto out_unreserve;
859e021a2e5SBrian Foster }
860e021a2e5SBrian Foster
8610924378aSDave Chinner /*
8620924378aSDave Chinner * If there is nothing to be logged by the transaction,
8630924378aSDave Chinner * then unlock all of the items associated with the
8640924378aSDave Chinner * transaction and free the transaction structure.
8650924378aSDave Chinner * Also make sure to return any reserved blocks to
8660924378aSDave Chinner * the free pool.
8670924378aSDave Chinner */
868a3ccd2caSChristoph Hellwig if (!(tp->t_flags & XFS_TRANS_DIRTY))
869a3ccd2caSChristoph Hellwig goto out_unreserve;
870a3ccd2caSChristoph Hellwig
8713c4cb76bSDave Chinner /*
8723c4cb76bSDave Chinner * We must check against log shutdown here because we cannot abort log
8733c4cb76bSDave Chinner * items and leave them dirty, inconsistent and unpinned in memory while
8743c4cb76bSDave Chinner * the log is active. This leaves them open to being written back to
8753c4cb76bSDave Chinner * disk, and that will lead to on-disk corruption.
8763c4cb76bSDave Chinner */
8773c4cb76bSDave Chinner if (xlog_is_shutdown(log)) {
8782451337dSDave Chinner error = -EIO;
879a3ccd2caSChristoph Hellwig goto out_unreserve;
8800924378aSDave Chinner }
881a3ccd2caSChristoph Hellwig
8820924378aSDave Chinner ASSERT(tp->t_ticket != NULL);
8830924378aSDave Chinner
8840924378aSDave Chinner /*
8850924378aSDave Chinner * If we need to update the superblock, then do it now.
8860924378aSDave Chinner */
8870924378aSDave Chinner if (tp->t_flags & XFS_TRANS_SB_DIRTY)
8880924378aSDave Chinner xfs_trans_apply_sb_deltas(tp);
8890924378aSDave Chinner xfs_trans_apply_dquot_deltas(tp);
8900924378aSDave Chinner
8913c4cb76bSDave Chinner xlog_cil_commit(log, tp, &commit_seq, regrant);
8921da177e4SLinus Torvalds
8930244b960SChristoph Hellwig xfs_trans_free(tp);
8940244b960SChristoph Hellwig
8951da177e4SLinus Torvalds /*
8961da177e4SLinus Torvalds * If the transaction needs to be synchronous, then force the
8971da177e4SLinus Torvalds * log out now and wait for it.
8981da177e4SLinus Torvalds */
8991da177e4SLinus Torvalds if (sync) {
9005f9b4b0dSDave Chinner error = xfs_log_force_seq(mp, commit_seq, XFS_LOG_SYNC, NULL);
901ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_trans_sync);
9021da177e4SLinus Torvalds } else {
903ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_trans_async);
9041da177e4SLinus Torvalds }
9051da177e4SLinus Torvalds
906a3ccd2caSChristoph Hellwig return error;
907a3ccd2caSChristoph Hellwig
908a3ccd2caSChristoph Hellwig out_unreserve:
909a3ccd2caSChristoph Hellwig xfs_trans_unreserve_and_mod_sb(tp);
910a3ccd2caSChristoph Hellwig
911a3ccd2caSChristoph Hellwig /*
912a3ccd2caSChristoph Hellwig * It is indeed possible for the transaction to be not dirty but
913a3ccd2caSChristoph Hellwig * the dqinfo portion to be. All that means is that we have some
914a3ccd2caSChristoph Hellwig * (non-persistent) quota reservations that need to be unreserved.
915a3ccd2caSChristoph Hellwig */
916a3ccd2caSChristoph Hellwig xfs_trans_unreserve_and_mod_dquots(tp);
917a3ccd2caSChristoph Hellwig if (tp->t_ticket) {
9183c4cb76bSDave Chinner if (regrant && !xlog_is_shutdown(log))
9193c4cb76bSDave Chinner xfs_log_ticket_regrant(log, tp->t_ticket);
9208b41e3f9SChristoph Hellwig else
9213c4cb76bSDave Chinner xfs_log_ticket_ungrant(log, tp->t_ticket);
922ba18781bSDave Chinner tp->t_ticket = NULL;
923a3ccd2caSChristoph Hellwig }
924195cd83dSChristoph Hellwig xfs_trans_free_items(tp, !!error);
925a3ccd2caSChristoph Hellwig xfs_trans_free(tp);
926a3ccd2caSChristoph Hellwig
927ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_trans_empty);
928a3ccd2caSChristoph Hellwig return error;
9291da177e4SLinus Torvalds }
9301da177e4SLinus Torvalds
93170393313SChristoph Hellwig int
xfs_trans_commit(struct xfs_trans * tp)93270393313SChristoph Hellwig xfs_trans_commit(
93370393313SChristoph Hellwig struct xfs_trans *tp)
93470393313SChristoph Hellwig {
93570393313SChristoph Hellwig return __xfs_trans_commit(tp, false);
93670393313SChristoph Hellwig }
93770393313SChristoph Hellwig
9381da177e4SLinus Torvalds /*
9393c4cb76bSDave Chinner * Unlock all of the transaction's items and free the transaction. If the
9403c4cb76bSDave Chinner * transaction is dirty, we must shut down the filesystem because there is no
9413c4cb76bSDave Chinner * way to restore them to their previous state.
9421da177e4SLinus Torvalds *
9433c4cb76bSDave Chinner * If the transaction has made a log reservation, make sure to release it as
9443c4cb76bSDave Chinner * well.
9453c4cb76bSDave Chinner *
9463c4cb76bSDave Chinner * This is a high level function (equivalent to xfs_trans_commit()) and so can
9473c4cb76bSDave Chinner * be called after the transaction has effectively been aborted due to the mount
9483c4cb76bSDave Chinner * being shut down. However, if the mount has not been shut down and the
9493c4cb76bSDave Chinner * transaction is dirty we will shut the mount down and, in doing so, that
9503c4cb76bSDave Chinner * guarantees that the log is shut down, too. Hence we don't need to be as
9513c4cb76bSDave Chinner * careful with shutdown state and dirty items here as we need to be in
9523c4cb76bSDave Chinner * xfs_trans_commit().
9531da177e4SLinus Torvalds */
9541da177e4SLinus Torvalds void
xfs_trans_cancel(struct xfs_trans * tp)9551da177e4SLinus Torvalds xfs_trans_cancel(
9564906e215SChristoph Hellwig struct xfs_trans *tp)
9571da177e4SLinus Torvalds {
9584906e215SChristoph Hellwig struct xfs_mount *mp = tp->t_mountp;
9593c4cb76bSDave Chinner struct xlog *log = mp->m_log;
9604906e215SChristoph Hellwig bool dirty = (tp->t_flags & XFS_TRANS_DIRTY);
9611da177e4SLinus Torvalds
962ba18781bSDave Chinner trace_xfs_trans_cancel(tp, _RET_IP_);
963ba18781bSDave Chinner
96447a6df7cSDarrick J. Wong /*
96547a6df7cSDarrick J. Wong * It's never valid to cancel a transaction with deferred ops attached,
96647a6df7cSDarrick J. Wong * because the transaction is effectively dirty. Complain about this
96755d5c3a3SDave Chinner * loudly before freeing the in-memory defer items and shutting down the
96855d5c3a3SDave Chinner * filesystem.
96947a6df7cSDarrick J. Wong */
97047a6df7cSDarrick J. Wong if (!list_empty(&tp->t_dfops)) {
97147a6df7cSDarrick J. Wong ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
97247a6df7cSDarrick J. Wong dirty = true;
9739e28a242SBrian Foster xfs_defer_cancel(tp);
97447a6df7cSDarrick J. Wong }
975e021a2e5SBrian Foster
9761da177e4SLinus Torvalds /*
9773c4cb76bSDave Chinner * See if the caller is relying on us to shut down the filesystem. We
9783c4cb76bSDave Chinner * only want an error report if there isn't already a shutdown in
9793c4cb76bSDave Chinner * progress, so we only need to check against the mount shutdown state
9803c4cb76bSDave Chinner * here.
9811da177e4SLinus Torvalds */
98275c8c50fSDave Chinner if (dirty && !xfs_is_shutdown(mp)) {
9830733af21SRyan Hankins XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
9847d04a335SNathan Scott xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
98560a204f0SNathan Scott }
9861da177e4SLinus Torvalds #ifdef DEBUG
9873c4cb76bSDave Chinner /* Log items need to be consistent until the log is shut down. */
9883c4cb76bSDave Chinner if (!dirty && !xlog_is_shutdown(log)) {
989e6631f85SDave Chinner struct xfs_log_item *lip;
9901da177e4SLinus Torvalds
991e6631f85SDave Chinner list_for_each_entry(lip, &tp->t_items, li_trans)
992d6b8fc6cSKaixu Xia ASSERT(!xlog_item_is_intent_done(lip));
9931da177e4SLinus Torvalds }
9941da177e4SLinus Torvalds #endif
9951da177e4SLinus Torvalds xfs_trans_unreserve_and_mod_sb(tp);
9967d095257SChristoph Hellwig xfs_trans_unreserve_and_mod_dquots(tp);
9971da177e4SLinus Torvalds
998ba18781bSDave Chinner if (tp->t_ticket) {
9993c4cb76bSDave Chinner xfs_log_ticket_ungrant(log, tp->t_ticket);
1000ba18781bSDave Chinner tp->t_ticket = NULL;
1001ba18781bSDave Chinner }
10021da177e4SLinus Torvalds
1003195cd83dSChristoph Hellwig xfs_trans_free_items(tp, dirty);
10041da177e4SLinus Torvalds xfs_trans_free(tp);
10051da177e4SLinus Torvalds }
10061da177e4SLinus Torvalds
1007322ff6b8SNiv Sardi /*
1008322ff6b8SNiv Sardi * Roll from one trans in the sequence of PERMANENT transactions to
1009322ff6b8SNiv Sardi * the next: permanent transactions are only flushed out when
101070393313SChristoph Hellwig * committed with xfs_trans_commit(), but we still want as soon
1011322ff6b8SNiv Sardi * as possible to let chunks of it go to the log. So we commit the
1012322ff6b8SNiv Sardi * chunk we've been working on and get a new transaction to continue.
1013322ff6b8SNiv Sardi */
1014322ff6b8SNiv Sardi int
xfs_trans_roll(struct xfs_trans ** tpp)1015254133f5SChristoph Hellwig xfs_trans_roll(
1016411350dfSChristoph Hellwig struct xfs_trans **tpp)
1017322ff6b8SNiv Sardi {
1018411350dfSChristoph Hellwig struct xfs_trans *trans = *tpp;
10193d3c8b52SJie Liu struct xfs_trans_res tres;
1020322ff6b8SNiv Sardi int error;
1021322ff6b8SNiv Sardi
1022ba18781bSDave Chinner trace_xfs_trans_roll(trans, _RET_IP_);
1023ba18781bSDave Chinner
1024322ff6b8SNiv Sardi /*
1025322ff6b8SNiv Sardi * Copy the critical parameters from one trans to the next.
1026322ff6b8SNiv Sardi */
10273d3c8b52SJie Liu tres.tr_logres = trans->t_log_res;
10283d3c8b52SJie Liu tres.tr_logcount = trans->t_log_count;
1029411350dfSChristoph Hellwig
1030322ff6b8SNiv Sardi *tpp = xfs_trans_dup(trans);
1031322ff6b8SNiv Sardi
1032322ff6b8SNiv Sardi /*
1033322ff6b8SNiv Sardi * Commit the current transaction.
1034322ff6b8SNiv Sardi * If this commit failed, then it'd just unlock those items that
1035322ff6b8SNiv Sardi * are not marked ihold. That also means that a filesystem shutdown
1036322ff6b8SNiv Sardi * is in progress. The caller takes the responsibility to cancel
1037322ff6b8SNiv Sardi * the duplicate transaction that gets returned.
1038322ff6b8SNiv Sardi */
103970393313SChristoph Hellwig error = __xfs_trans_commit(trans, true);
1040322ff6b8SNiv Sardi if (error)
1041d99831ffSEric Sandeen return error;
1042322ff6b8SNiv Sardi
1043322ff6b8SNiv Sardi /*
1044411350dfSChristoph Hellwig * Reserve space in the log for the next transaction.
1045322ff6b8SNiv Sardi * This also pushes items in the "AIL", the list of logged items,
1046322ff6b8SNiv Sardi * out to disk if they are taking up space at the tail of the log
1047322ff6b8SNiv Sardi * that we want to use. This requires that either nothing be locked
1048322ff6b8SNiv Sardi * across this call, or that anything that is locked be logged in
1049322ff6b8SNiv Sardi * the prior and the next transactions.
1050322ff6b8SNiv Sardi */
10513d3c8b52SJie Liu tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
1052411350dfSChristoph Hellwig return xfs_trans_reserve(*tpp, &tres, 0, 0);
1053322ff6b8SNiv Sardi }
10543a1af6c3SDarrick J. Wong
10553a1af6c3SDarrick J. Wong /*
10563a1af6c3SDarrick J. Wong * Allocate an transaction, lock and join the inode to it, and reserve quota.
10573a1af6c3SDarrick J. Wong *
10583a1af6c3SDarrick J. Wong * The caller must ensure that the on-disk dquots attached to this inode have
10593a1af6c3SDarrick J. Wong * already been allocated and initialized. The caller is responsible for
10603a1af6c3SDarrick J. Wong * releasing ILOCK_EXCL if a new transaction is returned.
10613a1af6c3SDarrick J. Wong */
10623a1af6c3SDarrick J. Wong int
xfs_trans_alloc_inode(struct xfs_inode * ip,struct xfs_trans_res * resv,unsigned int dblocks,unsigned int rblocks,bool force,struct xfs_trans ** tpp)10633a1af6c3SDarrick J. Wong xfs_trans_alloc_inode(
10643a1af6c3SDarrick J. Wong struct xfs_inode *ip,
10653a1af6c3SDarrick J. Wong struct xfs_trans_res *resv,
10663a1af6c3SDarrick J. Wong unsigned int dblocks,
10673de4eb10SDarrick J. Wong unsigned int rblocks,
10683a1af6c3SDarrick J. Wong bool force,
10693a1af6c3SDarrick J. Wong struct xfs_trans **tpp)
10703a1af6c3SDarrick J. Wong {
10713a1af6c3SDarrick J. Wong struct xfs_trans *tp;
10723a1af6c3SDarrick J. Wong struct xfs_mount *mp = ip->i_mount;
1073766aabd5SDarrick J. Wong bool retried = false;
10743a1af6c3SDarrick J. Wong int error;
10753a1af6c3SDarrick J. Wong
1076766aabd5SDarrick J. Wong retry:
10773de4eb10SDarrick J. Wong error = xfs_trans_alloc(mp, resv, dblocks,
10782c2b981bSDarrick J. Wong xfs_extlen_to_rtxlen(mp, rblocks),
10793a1af6c3SDarrick J. Wong force ? XFS_TRANS_RESERVE : 0, &tp);
10803a1af6c3SDarrick J. Wong if (error)
10813a1af6c3SDarrick J. Wong return error;
10823a1af6c3SDarrick J. Wong
10833a1af6c3SDarrick J. Wong xfs_ilock(ip, XFS_ILOCK_EXCL);
10843a1af6c3SDarrick J. Wong xfs_trans_ijoin(tp, ip, 0);
10853a1af6c3SDarrick J. Wong
10863a1af6c3SDarrick J. Wong error = xfs_qm_dqattach_locked(ip, false);
10873a1af6c3SDarrick J. Wong if (error) {
10883a1af6c3SDarrick J. Wong /* Caller should have allocated the dquots! */
10893a1af6c3SDarrick J. Wong ASSERT(error != -ENOENT);
10903a1af6c3SDarrick J. Wong goto out_cancel;
10913a1af6c3SDarrick J. Wong }
10923a1af6c3SDarrick J. Wong
10933de4eb10SDarrick J. Wong error = xfs_trans_reserve_quota_nblks(tp, ip, dblocks, rblocks, force);
1094766aabd5SDarrick J. Wong if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
1095766aabd5SDarrick J. Wong xfs_trans_cancel(tp);
1096766aabd5SDarrick J. Wong xfs_iunlock(ip, XFS_ILOCK_EXCL);
1097766aabd5SDarrick J. Wong xfs_blockgc_free_quota(ip, 0);
1098766aabd5SDarrick J. Wong retried = true;
1099766aabd5SDarrick J. Wong goto retry;
1100766aabd5SDarrick J. Wong }
11013a1af6c3SDarrick J. Wong if (error)
11023a1af6c3SDarrick J. Wong goto out_cancel;
11033a1af6c3SDarrick J. Wong
11043a1af6c3SDarrick J. Wong *tpp = tp;
11053a1af6c3SDarrick J. Wong return 0;
11063a1af6c3SDarrick J. Wong
11073a1af6c3SDarrick J. Wong out_cancel:
11083a1af6c3SDarrick J. Wong xfs_trans_cancel(tp);
11093a1af6c3SDarrick J. Wong xfs_iunlock(ip, XFS_ILOCK_EXCL);
11103a1af6c3SDarrick J. Wong return error;
11113a1af6c3SDarrick J. Wong }
1112f2f7b9ffSDarrick J. Wong
1113f2f7b9ffSDarrick J. Wong /*
11148f71bedeSDarrick J. Wong * Try to reserve more blocks for a transaction.
11158f71bedeSDarrick J. Wong *
11168f71bedeSDarrick J. Wong * This is for callers that need to attach resources to a transaction, scan
11178f71bedeSDarrick J. Wong * those resources to determine the space reservation requirements, and then
11188f71bedeSDarrick J. Wong * modify the attached resources. In other words, online repair. This can
11198f71bedeSDarrick J. Wong * fail due to ENOSPC, so the caller must be able to cancel the transaction
11208f71bedeSDarrick J. Wong * without shutting down the fs.
11218f71bedeSDarrick J. Wong */
11228f71bedeSDarrick J. Wong int
xfs_trans_reserve_more(struct xfs_trans * tp,unsigned int blocks,unsigned int rtextents)11238f71bedeSDarrick J. Wong xfs_trans_reserve_more(
11248f71bedeSDarrick J. Wong struct xfs_trans *tp,
11258f71bedeSDarrick J. Wong unsigned int blocks,
11268f71bedeSDarrick J. Wong unsigned int rtextents)
11278f71bedeSDarrick J. Wong {
11288f71bedeSDarrick J. Wong struct xfs_trans_res resv = { };
11298f71bedeSDarrick J. Wong
11308f71bedeSDarrick J. Wong return xfs_trans_reserve(tp, &resv, blocks, rtextents);
11318f71bedeSDarrick J. Wong }
11328f71bedeSDarrick J. Wong
11338f71bedeSDarrick J. Wong /*
11348f71bedeSDarrick J. Wong * Try to reserve more blocks and file quota for a transaction. Same
11358f71bedeSDarrick J. Wong * conditions of usage as xfs_trans_reserve_more.
11368f71bedeSDarrick J. Wong */
11378f71bedeSDarrick J. Wong int
xfs_trans_reserve_more_inode(struct xfs_trans * tp,struct xfs_inode * ip,unsigned int dblocks,unsigned int rblocks,bool force_quota)11388f71bedeSDarrick J. Wong xfs_trans_reserve_more_inode(
11398f71bedeSDarrick J. Wong struct xfs_trans *tp,
11408f71bedeSDarrick J. Wong struct xfs_inode *ip,
11418f71bedeSDarrick J. Wong unsigned int dblocks,
11428f71bedeSDarrick J. Wong unsigned int rblocks,
11438f71bedeSDarrick J. Wong bool force_quota)
11448f71bedeSDarrick J. Wong {
11458f71bedeSDarrick J. Wong struct xfs_trans_res resv = { };
11468f71bedeSDarrick J. Wong struct xfs_mount *mp = ip->i_mount;
11478f71bedeSDarrick J. Wong unsigned int rtx = xfs_extlen_to_rtxlen(mp, rblocks);
11488f71bedeSDarrick J. Wong int error;
11498f71bedeSDarrick J. Wong
11503fed24ffSMatthew Wilcox (Oracle) xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
11518f71bedeSDarrick J. Wong
11528f71bedeSDarrick J. Wong error = xfs_trans_reserve(tp, &resv, dblocks, rtx);
11538f71bedeSDarrick J. Wong if (error)
11548f71bedeSDarrick J. Wong return error;
11558f71bedeSDarrick J. Wong
11568f71bedeSDarrick J. Wong if (!XFS_IS_QUOTA_ON(mp) || xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
11578f71bedeSDarrick J. Wong return 0;
11588f71bedeSDarrick J. Wong
11598f71bedeSDarrick J. Wong if (tp->t_flags & XFS_TRANS_RESERVE)
11608f71bedeSDarrick J. Wong force_quota = true;
11618f71bedeSDarrick J. Wong
11628f71bedeSDarrick J. Wong error = xfs_trans_reserve_quota_nblks(tp, ip, dblocks, rblocks,
11638f71bedeSDarrick J. Wong force_quota);
11648f71bedeSDarrick J. Wong if (!error)
11658f71bedeSDarrick J. Wong return 0;
11668f71bedeSDarrick J. Wong
11678f71bedeSDarrick J. Wong /* Quota failed, give back the new reservation. */
1168*f30f656eSChristoph Hellwig xfs_add_fdblocks(mp, dblocks);
11698f71bedeSDarrick J. Wong tp->t_blk_res -= dblocks;
1170*f30f656eSChristoph Hellwig xfs_add_frextents(mp, rtx);
11718f71bedeSDarrick J. Wong tp->t_rtx_res -= rtx;
11728f71bedeSDarrick J. Wong return error;
11738f71bedeSDarrick J. Wong }
11748f71bedeSDarrick J. Wong
11758f71bedeSDarrick J. Wong /*
1176f2f7b9ffSDarrick J. Wong * Allocate an transaction in preparation for inode creation by reserving quota
1177f2f7b9ffSDarrick J. Wong * against the given dquots. Callers are not required to hold any inode locks.
1178f2f7b9ffSDarrick J. Wong */
1179f2f7b9ffSDarrick J. Wong int
xfs_trans_alloc_icreate(struct xfs_mount * mp,struct xfs_trans_res * resv,struct xfs_dquot * udqp,struct xfs_dquot * gdqp,struct xfs_dquot * pdqp,unsigned int dblocks,struct xfs_trans ** tpp)1180f2f7b9ffSDarrick J. Wong xfs_trans_alloc_icreate(
1181f2f7b9ffSDarrick J. Wong struct xfs_mount *mp,
1182f2f7b9ffSDarrick J. Wong struct xfs_trans_res *resv,
1183f2f7b9ffSDarrick J. Wong struct xfs_dquot *udqp,
1184f2f7b9ffSDarrick J. Wong struct xfs_dquot *gdqp,
1185f2f7b9ffSDarrick J. Wong struct xfs_dquot *pdqp,
1186f2f7b9ffSDarrick J. Wong unsigned int dblocks,
1187f2f7b9ffSDarrick J. Wong struct xfs_trans **tpp)
1188f2f7b9ffSDarrick J. Wong {
1189f2f7b9ffSDarrick J. Wong struct xfs_trans *tp;
1190c237dd7cSDarrick J. Wong bool retried = false;
1191f2f7b9ffSDarrick J. Wong int error;
1192f2f7b9ffSDarrick J. Wong
1193c237dd7cSDarrick J. Wong retry:
1194f2f7b9ffSDarrick J. Wong error = xfs_trans_alloc(mp, resv, dblocks, 0, 0, &tp);
1195f2f7b9ffSDarrick J. Wong if (error)
1196f2f7b9ffSDarrick J. Wong return error;
1197f2f7b9ffSDarrick J. Wong
1198f2f7b9ffSDarrick J. Wong error = xfs_trans_reserve_quota_icreate(tp, udqp, gdqp, pdqp, dblocks);
1199c237dd7cSDarrick J. Wong if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
1200c237dd7cSDarrick J. Wong xfs_trans_cancel(tp);
1201c237dd7cSDarrick J. Wong xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0);
1202c237dd7cSDarrick J. Wong retried = true;
1203c237dd7cSDarrick J. Wong goto retry;
1204c237dd7cSDarrick J. Wong }
1205f2f7b9ffSDarrick J. Wong if (error) {
1206f2f7b9ffSDarrick J. Wong xfs_trans_cancel(tp);
1207f2f7b9ffSDarrick J. Wong return error;
1208f2f7b9ffSDarrick J. Wong }
1209f2f7b9ffSDarrick J. Wong
1210f2f7b9ffSDarrick J. Wong *tpp = tp;
1211f2f7b9ffSDarrick J. Wong return 0;
1212f2f7b9ffSDarrick J. Wong }
12137317a03dSDarrick J. Wong
12147317a03dSDarrick J. Wong /*
12157317a03dSDarrick J. Wong * Allocate an transaction, lock and join the inode to it, and reserve quota
12167317a03dSDarrick J. Wong * in preparation for inode attribute changes that include uid, gid, or prid
12177317a03dSDarrick J. Wong * changes.
12187317a03dSDarrick J. Wong *
12197317a03dSDarrick J. Wong * The caller must ensure that the on-disk dquots attached to this inode have
12207317a03dSDarrick J. Wong * already been allocated and initialized. The ILOCK will be dropped when the
12217317a03dSDarrick J. Wong * transaction is committed or cancelled.
12227317a03dSDarrick J. Wong */
12237317a03dSDarrick J. Wong int
xfs_trans_alloc_ichange(struct xfs_inode * ip,struct xfs_dquot * new_udqp,struct xfs_dquot * new_gdqp,struct xfs_dquot * new_pdqp,bool force,struct xfs_trans ** tpp)12247317a03dSDarrick J. Wong xfs_trans_alloc_ichange(
12257317a03dSDarrick J. Wong struct xfs_inode *ip,
1226758303d1SDarrick J. Wong struct xfs_dquot *new_udqp,
1227758303d1SDarrick J. Wong struct xfs_dquot *new_gdqp,
1228758303d1SDarrick J. Wong struct xfs_dquot *new_pdqp,
12297317a03dSDarrick J. Wong bool force,
12307317a03dSDarrick J. Wong struct xfs_trans **tpp)
12317317a03dSDarrick J. Wong {
12327317a03dSDarrick J. Wong struct xfs_trans *tp;
12337317a03dSDarrick J. Wong struct xfs_mount *mp = ip->i_mount;
1234758303d1SDarrick J. Wong struct xfs_dquot *udqp;
1235758303d1SDarrick J. Wong struct xfs_dquot *gdqp;
1236758303d1SDarrick J. Wong struct xfs_dquot *pdqp;
1237758303d1SDarrick J. Wong bool retried = false;
12387317a03dSDarrick J. Wong int error;
12397317a03dSDarrick J. Wong
1240758303d1SDarrick J. Wong retry:
12417317a03dSDarrick J. Wong error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
12427317a03dSDarrick J. Wong if (error)
12437317a03dSDarrick J. Wong return error;
12447317a03dSDarrick J. Wong
12457317a03dSDarrick J. Wong xfs_ilock(ip, XFS_ILOCK_EXCL);
12467317a03dSDarrick J. Wong xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
12477317a03dSDarrick J. Wong
12487317a03dSDarrick J. Wong error = xfs_qm_dqattach_locked(ip, false);
12497317a03dSDarrick J. Wong if (error) {
12507317a03dSDarrick J. Wong /* Caller should have allocated the dquots! */
12517317a03dSDarrick J. Wong ASSERT(error != -ENOENT);
12527317a03dSDarrick J. Wong goto out_cancel;
12537317a03dSDarrick J. Wong }
12547317a03dSDarrick J. Wong
12557317a03dSDarrick J. Wong /*
12567317a03dSDarrick J. Wong * For each quota type, skip quota reservations if the inode's dquots
12577317a03dSDarrick J. Wong * now match the ones that came from the caller, or the caller didn't
1258758303d1SDarrick J. Wong * pass one in. The inode's dquots can change if we drop the ILOCK to
1259758303d1SDarrick J. Wong * perform a blockgc scan, so we must preserve the caller's arguments.
12607317a03dSDarrick J. Wong */
1261758303d1SDarrick J. Wong udqp = (new_udqp != ip->i_udquot) ? new_udqp : NULL;
1262758303d1SDarrick J. Wong gdqp = (new_gdqp != ip->i_gdquot) ? new_gdqp : NULL;
1263758303d1SDarrick J. Wong pdqp = (new_pdqp != ip->i_pdquot) ? new_pdqp : NULL;
12647317a03dSDarrick J. Wong if (udqp || gdqp || pdqp) {
12655c615f0fSDarrick J. Wong unsigned int qflags = XFS_QMOPT_RES_REGBLKS;
12665c615f0fSDarrick J. Wong
12675c615f0fSDarrick J. Wong if (force)
12685c615f0fSDarrick J. Wong qflags |= XFS_QMOPT_FORCE_RES;
12695c615f0fSDarrick J. Wong
12705c615f0fSDarrick J. Wong /*
12715c615f0fSDarrick J. Wong * Reserve enough quota to handle blocks on disk and reserved
12725c615f0fSDarrick J. Wong * for a delayed allocation. We'll actually transfer the
12735c615f0fSDarrick J. Wong * delalloc reservation between dquots at chown time, even
12745c615f0fSDarrick J. Wong * though that part is only semi-transactional.
12755c615f0fSDarrick J. Wong */
12765c615f0fSDarrick J. Wong error = xfs_trans_reserve_quota_bydquots(tp, mp, udqp, gdqp,
12776e73a545SChristoph Hellwig pdqp, ip->i_nblocks + ip->i_delayed_blks,
12785c615f0fSDarrick J. Wong 1, qflags);
1279758303d1SDarrick J. Wong if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
1280758303d1SDarrick J. Wong xfs_trans_cancel(tp);
1281758303d1SDarrick J. Wong xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0);
1282758303d1SDarrick J. Wong retried = true;
1283758303d1SDarrick J. Wong goto retry;
1284758303d1SDarrick J. Wong }
12857317a03dSDarrick J. Wong if (error)
12867317a03dSDarrick J. Wong goto out_cancel;
12877317a03dSDarrick J. Wong }
12887317a03dSDarrick J. Wong
12897317a03dSDarrick J. Wong *tpp = tp;
12907317a03dSDarrick J. Wong return 0;
12917317a03dSDarrick J. Wong
12927317a03dSDarrick J. Wong out_cancel:
12937317a03dSDarrick J. Wong xfs_trans_cancel(tp);
12947317a03dSDarrick J. Wong return error;
12957317a03dSDarrick J. Wong }
1296871b9316SDarrick J. Wong
1297871b9316SDarrick J. Wong /*
1298871b9316SDarrick J. Wong * Allocate an transaction, lock and join the directory and child inodes to it,
1299871b9316SDarrick J. Wong * and reserve quota for a directory update. If there isn't sufficient space,
1300871b9316SDarrick J. Wong * @dblocks will be set to zero for a reservationless directory update and
1301871b9316SDarrick J. Wong * @nospace_error will be set to a negative errno describing the space
1302871b9316SDarrick J. Wong * constraint we hit.
1303871b9316SDarrick J. Wong *
1304871b9316SDarrick J. Wong * The caller must ensure that the on-disk dquots attached to this inode have
1305871b9316SDarrick J. Wong * already been allocated and initialized. The ILOCKs will be dropped when the
1306871b9316SDarrick J. Wong * transaction is committed or cancelled.
1307bd556211SAllison Henderson *
1308bd556211SAllison Henderson * Caller is responsible for unlocking the inodes manually upon return
1309871b9316SDarrick J. Wong */
1310871b9316SDarrick J. Wong int
xfs_trans_alloc_dir(struct xfs_inode * dp,struct xfs_trans_res * resv,struct xfs_inode * ip,unsigned int * dblocks,struct xfs_trans ** tpp,int * nospace_error)1311871b9316SDarrick J. Wong xfs_trans_alloc_dir(
1312871b9316SDarrick J. Wong struct xfs_inode *dp,
1313871b9316SDarrick J. Wong struct xfs_trans_res *resv,
1314871b9316SDarrick J. Wong struct xfs_inode *ip,
1315871b9316SDarrick J. Wong unsigned int *dblocks,
1316871b9316SDarrick J. Wong struct xfs_trans **tpp,
1317871b9316SDarrick J. Wong int *nospace_error)
1318871b9316SDarrick J. Wong {
1319871b9316SDarrick J. Wong struct xfs_trans *tp;
1320871b9316SDarrick J. Wong struct xfs_mount *mp = ip->i_mount;
1321871b9316SDarrick J. Wong unsigned int resblks;
1322871b9316SDarrick J. Wong bool retried = false;
1323871b9316SDarrick J. Wong int error;
1324871b9316SDarrick J. Wong
1325871b9316SDarrick J. Wong retry:
1326871b9316SDarrick J. Wong *nospace_error = 0;
1327871b9316SDarrick J. Wong resblks = *dblocks;
1328871b9316SDarrick J. Wong error = xfs_trans_alloc(mp, resv, resblks, 0, 0, &tp);
1329871b9316SDarrick J. Wong if (error == -ENOSPC) {
1330871b9316SDarrick J. Wong *nospace_error = error;
1331871b9316SDarrick J. Wong resblks = 0;
1332871b9316SDarrick J. Wong error = xfs_trans_alloc(mp, resv, resblks, 0, 0, &tp);
1333871b9316SDarrick J. Wong }
1334871b9316SDarrick J. Wong if (error)
1335871b9316SDarrick J. Wong return error;
1336871b9316SDarrick J. Wong
1337871b9316SDarrick J. Wong xfs_lock_two_inodes(dp, XFS_ILOCK_EXCL, ip, XFS_ILOCK_EXCL);
1338871b9316SDarrick J. Wong
1339bd556211SAllison Henderson xfs_trans_ijoin(tp, dp, 0);
1340bd556211SAllison Henderson xfs_trans_ijoin(tp, ip, 0);
1341871b9316SDarrick J. Wong
1342871b9316SDarrick J. Wong error = xfs_qm_dqattach_locked(dp, false);
1343871b9316SDarrick J. Wong if (error) {
1344871b9316SDarrick J. Wong /* Caller should have allocated the dquots! */
1345871b9316SDarrick J. Wong ASSERT(error != -ENOENT);
1346871b9316SDarrick J. Wong goto out_cancel;
1347871b9316SDarrick J. Wong }
1348871b9316SDarrick J. Wong
1349871b9316SDarrick J. Wong error = xfs_qm_dqattach_locked(ip, false);
1350871b9316SDarrick J. Wong if (error) {
1351871b9316SDarrick J. Wong /* Caller should have allocated the dquots! */
1352871b9316SDarrick J. Wong ASSERT(error != -ENOENT);
1353871b9316SDarrick J. Wong goto out_cancel;
1354871b9316SDarrick J. Wong }
1355871b9316SDarrick J. Wong
1356871b9316SDarrick J. Wong if (resblks == 0)
1357871b9316SDarrick J. Wong goto done;
1358871b9316SDarrick J. Wong
1359871b9316SDarrick J. Wong error = xfs_trans_reserve_quota_nblks(tp, dp, resblks, 0, false);
1360871b9316SDarrick J. Wong if (error == -EDQUOT || error == -ENOSPC) {
1361871b9316SDarrick J. Wong if (!retried) {
1362871b9316SDarrick J. Wong xfs_trans_cancel(tp);
1363bd556211SAllison Henderson xfs_iunlock(dp, XFS_ILOCK_EXCL);
1364bd556211SAllison Henderson if (dp != ip)
1365bd556211SAllison Henderson xfs_iunlock(ip, XFS_ILOCK_EXCL);
1366871b9316SDarrick J. Wong xfs_blockgc_free_quota(dp, 0);
1367871b9316SDarrick J. Wong retried = true;
1368871b9316SDarrick J. Wong goto retry;
1369871b9316SDarrick J. Wong }
1370871b9316SDarrick J. Wong
1371871b9316SDarrick J. Wong *nospace_error = error;
1372871b9316SDarrick J. Wong resblks = 0;
1373871b9316SDarrick J. Wong error = 0;
1374871b9316SDarrick J. Wong }
1375871b9316SDarrick J. Wong if (error)
1376871b9316SDarrick J. Wong goto out_cancel;
1377871b9316SDarrick J. Wong
1378871b9316SDarrick J. Wong done:
1379871b9316SDarrick J. Wong *tpp = tp;
1380871b9316SDarrick J. Wong *dblocks = resblks;
1381871b9316SDarrick J. Wong return 0;
1382871b9316SDarrick J. Wong
1383871b9316SDarrick J. Wong out_cancel:
1384871b9316SDarrick J. Wong xfs_trans_cancel(tp);
1385871b9316SDarrick J. Wong return error;
1386871b9316SDarrick J. Wong }
1387