xref: /openbmc/linux/fs/xfs/xfs_trans.c (revision 16f6ccde74a6f8538c62f127f17207c75f4dba7a)
10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
37b718769SNathan Scott  * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4e98c414fSChristoph Hellwig  * Copyright (C) 2010 Red Hat, Inc.
57b718769SNathan Scott  * All Rights Reserved.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds #include "xfs.h"
8a844f451SNathan Scott #include "xfs_fs.h"
970a9883cSDave Chinner #include "xfs_shared.h"
10239880efSDave Chinner #include "xfs_format.h"
11239880efSDave Chinner #include "xfs_log_format.h"
12239880efSDave Chinner #include "xfs_trans_resv.h"
131da177e4SLinus Torvalds #include "xfs_mount.h"
14efc27b52SDave Chinner #include "xfs_extent_busy.h"
151da177e4SLinus Torvalds #include "xfs_quota.h"
16239880efSDave Chinner #include "xfs_trans.h"
17a844f451SNathan Scott #include "xfs_trans_priv.h"
18239880efSDave Chinner #include "xfs_log.h"
190020a190SDave Chinner #include "xfs_log_priv.h"
20ed3b4d6cSDave Chinner #include "xfs_trace.h"
21a4fbe6abSDave Chinner #include "xfs_error.h"
22f8f2835aSBrian Foster #include "xfs_defer.h"
233a1af6c3SDarrick J. Wong #include "xfs_inode.h"
24f2f7b9ffSDarrick J. Wong #include "xfs_dquot_item.h"
25f2f7b9ffSDarrick J. Wong #include "xfs_dquot.h"
26766aabd5SDarrick J. Wong #include "xfs_icache.h"
271da177e4SLinus Torvalds 
28182696fbSDarrick J. Wong struct kmem_cache	*xfs_trans_cache;
291da177e4SLinus Torvalds 
30b872af2cSDarrick J. Wong #if defined(CONFIG_TRACEPOINTS)
31b872af2cSDarrick J. Wong static void
xfs_trans_trace_reservations(struct xfs_mount * mp)32b872af2cSDarrick J. Wong xfs_trans_trace_reservations(
33b872af2cSDarrick J. Wong 	struct xfs_mount	*mp)
34b872af2cSDarrick J. Wong {
35b872af2cSDarrick J. Wong 	struct xfs_trans_res	*res;
36b872af2cSDarrick J. Wong 	struct xfs_trans_res	*end_res;
37b872af2cSDarrick J. Wong 	int			i;
38b872af2cSDarrick J. Wong 
39b872af2cSDarrick J. Wong 	res = (struct xfs_trans_res *)M_RES(mp);
40b872af2cSDarrick J. Wong 	end_res = (struct xfs_trans_res *)(M_RES(mp) + 1);
41b872af2cSDarrick J. Wong 	for (i = 0; res < end_res; i++, res++)
42b872af2cSDarrick J. Wong 		trace_xfs_trans_resv_calc(mp, i, res);
43b872af2cSDarrick J. Wong }
44b872af2cSDarrick J. Wong #else
45b872af2cSDarrick J. Wong # define xfs_trans_trace_reservations(mp)
46b872af2cSDarrick J. Wong #endif
47b872af2cSDarrick J. Wong 
484f3b5783SJeff Liu /*
491da177e4SLinus Torvalds  * Initialize the precomputed transaction reservation values
501da177e4SLinus Torvalds  * in the mount structure.
511da177e4SLinus Torvalds  */
521da177e4SLinus Torvalds void
xfs_trans_init(struct xfs_mount * mp)531da177e4SLinus Torvalds xfs_trans_init(
54025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
551da177e4SLinus Torvalds {
563d3c8b52SJie Liu 	xfs_trans_resv_calc(mp, M_RES(mp));
57b872af2cSDarrick J. Wong 	xfs_trans_trace_reservations(mp);
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds /*
61b1c1b5b6SDave Chinner  * Free the transaction structure.  If there is more clean up
62b1c1b5b6SDave Chinner  * to do when the structure is freed, add it here.
63b1c1b5b6SDave Chinner  */
64b1c1b5b6SDave Chinner STATIC void
xfs_trans_free(struct xfs_trans * tp)65b1c1b5b6SDave Chinner xfs_trans_free(
66ed3b4d6cSDave Chinner 	struct xfs_trans	*tp)
67b1c1b5b6SDave Chinner {
684ecbfe63SDave Chinner 	xfs_extent_busy_sort(&tp->t_busy);
694ecbfe63SDave Chinner 	xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
70ed3b4d6cSDave Chinner 
71ba18781bSDave Chinner 	trace_xfs_trans_free(tp, _RET_IP_);
72756b1c34SDave Chinner 	xfs_trans_clear_context(tp);
73253f4911SChristoph Hellwig 	if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
74d9457dc0SJan Kara 		sb_end_intwrite(tp->t_mountp->m_super);
75b1c1b5b6SDave Chinner 	xfs_trans_free_dqinfo(tp);
76182696fbSDarrick J. Wong 	kmem_cache_free(xfs_trans_cache, tp);
77b1c1b5b6SDave Chinner }
78b1c1b5b6SDave Chinner 
79b1c1b5b6SDave Chinner /*
801da177e4SLinus Torvalds  * This is called to create a new transaction which will share the
811da177e4SLinus Torvalds  * permanent log reservation of the given transaction.  The remaining
821da177e4SLinus Torvalds  * unused block and rt extent reservations are also inherited.  This
831da177e4SLinus Torvalds  * implies that the original transaction is no longer allowed to allocate
841da177e4SLinus Torvalds  * blocks.  Locks and log items, however, are no inherited.  They must
851da177e4SLinus Torvalds  * be added to the new transaction explicitly.
861da177e4SLinus Torvalds  */
87f8f2835aSBrian Foster STATIC struct xfs_trans *
xfs_trans_dup(struct xfs_trans * tp)881da177e4SLinus Torvalds xfs_trans_dup(
89f8f2835aSBrian Foster 	struct xfs_trans	*tp)
901da177e4SLinus Torvalds {
91f8f2835aSBrian Foster 	struct xfs_trans	*ntp;
921da177e4SLinus Torvalds 
93ba18781bSDave Chinner 	trace_xfs_trans_dup(tp, _RET_IP_);
94ba18781bSDave Chinner 
95182696fbSDarrick J. Wong 	ntp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL);
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	/*
981da177e4SLinus Torvalds 	 * Initialize the new transaction structure.
991da177e4SLinus Torvalds 	 */
1002a3c0accSDave Chinner 	ntp->t_magic = XFS_TRANS_HEADER_MAGIC;
1011da177e4SLinus Torvalds 	ntp->t_mountp = tp->t_mountp;
102e98c414fSChristoph Hellwig 	INIT_LIST_HEAD(&ntp->t_items);
103ed3b4d6cSDave Chinner 	INIT_LIST_HEAD(&ntp->t_busy);
1049d9e6233SBrian Foster 	INIT_LIST_HEAD(&ntp->t_dfops);
105692b6cddSDave Chinner 	ntp->t_highest_agno = NULLAGNUMBER;
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1081da177e4SLinus Torvalds 	ASSERT(tp->t_ticket != NULL);
109cfcbbbd0SNathan Scott 
110d9457dc0SJan Kara 	ntp->t_flags = XFS_TRANS_PERM_LOG_RES |
111d9457dc0SJan Kara 		       (tp->t_flags & XFS_TRANS_RESERVE) |
112f74681baSBrian Foster 		       (tp->t_flags & XFS_TRANS_NO_WRITECOUNT) |
113f74681baSBrian Foster 		       (tp->t_flags & XFS_TRANS_RES_FDBLKS);
114d9457dc0SJan Kara 	/* We gave our writer reference to the new transaction */
115253f4911SChristoph Hellwig 	tp->t_flags |= XFS_TRANS_NO_WRITECOUNT;
116cc09c0dcSDave Chinner 	ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
1173e78b9a4SBrian Foster 
1183e78b9a4SBrian Foster 	ASSERT(tp->t_blk_res >= tp->t_blk_res_used);
1191da177e4SLinus Torvalds 	ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
1201da177e4SLinus Torvalds 	tp->t_blk_res = tp->t_blk_res_used;
1213e78b9a4SBrian Foster 
1221da177e4SLinus Torvalds 	ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
1231da177e4SLinus Torvalds 	tp->t_rtx_res = tp->t_rtx_res_used;
124756b1c34SDave Chinner 
125756b1c34SDave Chinner 	xfs_trans_switch_context(tp, ntp);
126e021a2e5SBrian Foster 
1279d9e6233SBrian Foster 	/* move deferred ops over to the new tp */
128ce356d64SBrian Foster 	xfs_defer_move(ntp, tp);
1291da177e4SLinus Torvalds 
1307d095257SChristoph Hellwig 	xfs_trans_dup_dqinfo(tp, ntp);
1311da177e4SLinus Torvalds 	return ntp;
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds /*
1351da177e4SLinus Torvalds  * This is called to reserve free disk blocks and log space for the
1361da177e4SLinus Torvalds  * given transaction.  This must be done before allocating any resources
1371da177e4SLinus Torvalds  * within the transaction.
1381da177e4SLinus Torvalds  *
1391da177e4SLinus Torvalds  * This will return ENOSPC if there are not enough blocks available.
1401da177e4SLinus Torvalds  * It will sleep waiting for available log space.
1411da177e4SLinus Torvalds  * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
1421da177e4SLinus Torvalds  * is used by long running transactions.  If any one of the reservations
1431da177e4SLinus Torvalds  * fails then they will all be backed out.
1441da177e4SLinus Torvalds  *
1451da177e4SLinus Torvalds  * This does not do quota reservations. That typically is done by the
1461da177e4SLinus Torvalds  * caller afterwards.
1471da177e4SLinus Torvalds  */
148253f4911SChristoph Hellwig static int
xfs_trans_reserve(struct xfs_trans * tp,struct xfs_trans_res * resp,uint blocks,uint rtextents)1491da177e4SLinus Torvalds xfs_trans_reserve(
1503d3c8b52SJie Liu 	struct xfs_trans	*tp,
1513d3c8b52SJie Liu 	struct xfs_trans_res	*resp,
1521da177e4SLinus Torvalds 	uint			blocks,
1533d3c8b52SJie Liu 	uint			rtextents)
1541da177e4SLinus Torvalds {
155dd401770SDave Chinner 	struct xfs_mount	*mp = tp->t_mountp;
15659c1b082SNathan Scott 	int			error = 0;
1570d485adaSDave Chinner 	bool			rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	/*
1601da177e4SLinus Torvalds 	 * Attempt to reserve the needed disk blocks by decrementing
1611da177e4SLinus Torvalds 	 * the number needed from the number available.  This will
1621da177e4SLinus Torvalds 	 * fail if the count would go below zero.
1631da177e4SLinus Torvalds 	 */
1641da177e4SLinus Torvalds 	if (blocks > 0) {
165dd401770SDave Chinner 		error = xfs_mod_fdblocks(mp, -((int64_t)blocks), rsvd);
166756b1c34SDave Chinner 		if (error != 0)
1672451337dSDave Chinner 			return -ENOSPC;
1681da177e4SLinus Torvalds 		tp->t_blk_res += blocks;
1691da177e4SLinus Torvalds 	}
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	/*
1721da177e4SLinus Torvalds 	 * Reserve the log space needed for this transaction.
1731da177e4SLinus Torvalds 	 */
1743d3c8b52SJie Liu 	if (resp->tr_logres > 0) {
1759006fb91SChristoph Hellwig 		bool	permanent = false;
1769006fb91SChristoph Hellwig 
1773d3c8b52SJie Liu 		ASSERT(tp->t_log_res == 0 ||
1783d3c8b52SJie Liu 		       tp->t_log_res == resp->tr_logres);
1793d3c8b52SJie Liu 		ASSERT(tp->t_log_count == 0 ||
1803d3c8b52SJie Liu 		       tp->t_log_count == resp->tr_logcount);
1819006fb91SChristoph Hellwig 
1823d3c8b52SJie Liu 		if (resp->tr_logflags & XFS_TRANS_PERM_LOG_RES) {
1831da177e4SLinus Torvalds 			tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
1849006fb91SChristoph Hellwig 			permanent = true;
1851da177e4SLinus Torvalds 		} else {
1861da177e4SLinus Torvalds 			ASSERT(tp->t_ticket == NULL);
1871da177e4SLinus Torvalds 			ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
1881da177e4SLinus Torvalds 		}
1891da177e4SLinus Torvalds 
1909006fb91SChristoph Hellwig 		if (tp->t_ticket != NULL) {
1913d3c8b52SJie Liu 			ASSERT(resp->tr_logflags & XFS_TRANS_PERM_LOG_RES);
192dd401770SDave Chinner 			error = xfs_log_regrant(mp, tp->t_ticket);
1939006fb91SChristoph Hellwig 		} else {
194c7610dceSDave Chinner 			error = xfs_log_reserve(mp, resp->tr_logres,
1953d3c8b52SJie Liu 						resp->tr_logcount,
196c7610dceSDave Chinner 						&tp->t_ticket, permanent);
1971da177e4SLinus Torvalds 		}
1989006fb91SChristoph Hellwig 
1999006fb91SChristoph Hellwig 		if (error)
2009006fb91SChristoph Hellwig 			goto undo_blocks;
2019006fb91SChristoph Hellwig 
2023d3c8b52SJie Liu 		tp->t_log_res = resp->tr_logres;
2033d3c8b52SJie Liu 		tp->t_log_count = resp->tr_logcount;
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	/*
2071da177e4SLinus Torvalds 	 * Attempt to reserve the needed realtime extents by decrementing
2081da177e4SLinus Torvalds 	 * the number needed from the number available.  This will
2091da177e4SLinus Torvalds 	 * fail if the count would go below zero.
2101da177e4SLinus Torvalds 	 */
2111da177e4SLinus Torvalds 	if (rtextents > 0) {
212dd401770SDave Chinner 		error = xfs_mod_frextents(mp, -((int64_t)rtextents));
2131da177e4SLinus Torvalds 		if (error) {
2142451337dSDave Chinner 			error = -ENOSPC;
2151da177e4SLinus Torvalds 			goto undo_log;
2161da177e4SLinus Torvalds 		}
2171da177e4SLinus Torvalds 		tp->t_rtx_res += rtextents;
2181da177e4SLinus Torvalds 	}
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	return 0;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	/*
2231da177e4SLinus Torvalds 	 * Error cases jump to one of these labels to undo any
2241da177e4SLinus Torvalds 	 * reservations which have already been performed.
2251da177e4SLinus Torvalds 	 */
2261da177e4SLinus Torvalds undo_log:
2273d3c8b52SJie Liu 	if (resp->tr_logres > 0) {
2288b41e3f9SChristoph Hellwig 		xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket);
2291da177e4SLinus Torvalds 		tp->t_ticket = NULL;
2301da177e4SLinus Torvalds 		tp->t_log_res = 0;
2311da177e4SLinus Torvalds 		tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds undo_blocks:
2351da177e4SLinus Torvalds 	if (blocks > 0) {
236dd401770SDave Chinner 		xfs_mod_fdblocks(mp, (int64_t)blocks, rsvd);
2371da177e4SLinus Torvalds 		tp->t_blk_res = 0;
2381da177e4SLinus Torvalds 	}
23959c1b082SNathan Scott 	return error;
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
242253f4911SChristoph Hellwig int
xfs_trans_alloc(struct xfs_mount * mp,struct xfs_trans_res * resp,uint blocks,uint rtextents,uint flags,struct xfs_trans ** tpp)243253f4911SChristoph Hellwig xfs_trans_alloc(
244253f4911SChristoph Hellwig 	struct xfs_mount	*mp,
245253f4911SChristoph Hellwig 	struct xfs_trans_res	*resp,
246253f4911SChristoph Hellwig 	uint			blocks,
247253f4911SChristoph Hellwig 	uint			rtextents,
248253f4911SChristoph Hellwig 	uint			flags,
249253f4911SChristoph Hellwig 	struct xfs_trans	**tpp)
250253f4911SChristoph Hellwig {
251253f4911SChristoph Hellwig 	struct xfs_trans	*tp;
2529febcda6SDarrick J. Wong 	bool			want_retry = true;
253253f4911SChristoph Hellwig 	int			error;
254253f4911SChristoph Hellwig 
2558683edb7SDave Chinner 	/*
2568683edb7SDave Chinner 	 * Allocate the handle before we do our freeze accounting and setting up
2578683edb7SDave Chinner 	 * GFP_NOFS allocation context so that we avoid lockdep false positives
2588683edb7SDave Chinner 	 * by doing GFP_KERNEL allocations inside sb_start_intwrite().
2598683edb7SDave Chinner 	 */
2609febcda6SDarrick J. Wong retry:
261182696fbSDarrick J. Wong 	tp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL);
262253f4911SChristoph Hellwig 	if (!(flags & XFS_TRANS_NO_WRITECOUNT))
263253f4911SChristoph Hellwig 		sb_start_intwrite(mp->m_super);
264756b1c34SDave Chinner 	xfs_trans_set_context(tp);
265253f4911SChristoph Hellwig 
26610ee2526SDarrick J. Wong 	/*
26710ee2526SDarrick J. Wong 	 * Zero-reservation ("empty") transactions can't modify anything, so
26810ee2526SDarrick J. Wong 	 * they're allowed to run while we're frozen.
26910ee2526SDarrick J. Wong 	 */
27010ee2526SDarrick J. Wong 	WARN_ON(resp->tr_logres > 0 &&
27110ee2526SDarrick J. Wong 		mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
272f74681baSBrian Foster 	ASSERT(!(flags & XFS_TRANS_RES_FDBLKS) ||
27338c26bfdSDave Chinner 	       xfs_has_lazysbcount(mp));
274253f4911SChristoph Hellwig 
275253f4911SChristoph Hellwig 	tp->t_magic = XFS_TRANS_HEADER_MAGIC;
276253f4911SChristoph Hellwig 	tp->t_flags = flags;
277253f4911SChristoph Hellwig 	tp->t_mountp = mp;
278253f4911SChristoph Hellwig 	INIT_LIST_HEAD(&tp->t_items);
279253f4911SChristoph Hellwig 	INIT_LIST_HEAD(&tp->t_busy);
2809d9e6233SBrian Foster 	INIT_LIST_HEAD(&tp->t_dfops);
281692b6cddSDave Chinner 	tp->t_highest_agno = NULLAGNUMBER;
282253f4911SChristoph Hellwig 
283253f4911SChristoph Hellwig 	error = xfs_trans_reserve(tp, resp, blocks, rtextents);
2849febcda6SDarrick J. Wong 	if (error == -ENOSPC && want_retry) {
2859febcda6SDarrick J. Wong 		xfs_trans_cancel(tp);
2869febcda6SDarrick J. Wong 
287a1a7d05aSDarrick J. Wong 		/*
288a1a7d05aSDarrick J. Wong 		 * We weren't able to reserve enough space for the transaction.
289a1a7d05aSDarrick J. Wong 		 * Flush the other speculative space allocations to free space.
290a1a7d05aSDarrick J. Wong 		 * Do not perform a synchronous scan because callers can hold
291a1a7d05aSDarrick J. Wong 		 * other locks.
292a1a7d05aSDarrick J. Wong 		 */
293d4d12c02SDave Chinner 		error = xfs_blockgc_flush_all(mp);
294d4d12c02SDave Chinner 		if (error)
295d4d12c02SDave Chinner 			return error;
2969febcda6SDarrick J. Wong 		want_retry = false;
2979febcda6SDarrick J. Wong 		goto retry;
298a1a7d05aSDarrick J. Wong 	}
299253f4911SChristoph Hellwig 	if (error) {
300253f4911SChristoph Hellwig 		xfs_trans_cancel(tp);
301253f4911SChristoph Hellwig 		return error;
302253f4911SChristoph Hellwig 	}
303253f4911SChristoph Hellwig 
304ba18781bSDave Chinner 	trace_xfs_trans_alloc(tp, _RET_IP_);
305ba18781bSDave Chinner 
306253f4911SChristoph Hellwig 	*tpp = tp;
307253f4911SChristoph Hellwig 	return 0;
308253f4911SChristoph Hellwig }
309253f4911SChristoph Hellwig 
3101da177e4SLinus Torvalds /*
311e89c0413SDarrick J. Wong  * Create an empty transaction with no reservation.  This is a defensive
312b41b46c2SDave Chinner  * mechanism for routines that query metadata without actually modifying them --
313b41b46c2SDave Chinner  * if the metadata being queried is somehow cross-linked (think a btree block
314b41b46c2SDave Chinner  * pointer that points higher in the tree), we risk deadlock.  However, blocks
315b41b46c2SDave Chinner  * grabbed as part of a transaction can be re-grabbed.  The verifiers will
316b41b46c2SDave Chinner  * notice the corrupt block and the operation will fail back to userspace
317b41b46c2SDave Chinner  * without deadlocking.
318e89c0413SDarrick J. Wong  *
319b41b46c2SDave Chinner  * Note the zero-length reservation; this transaction MUST be cancelled without
320b41b46c2SDave Chinner  * any dirty data.
32127fb5a72SDarrick J. Wong  *
322b41b46c2SDave Chinner  * Callers should obtain freeze protection to avoid a conflict with fs freezing
323b41b46c2SDave Chinner  * where we can be grabbing buffers at the same time that freeze is trying to
324b41b46c2SDave Chinner  * drain the buffer LRU list.
325e89c0413SDarrick J. Wong  */
326e89c0413SDarrick J. Wong int
xfs_trans_alloc_empty(struct xfs_mount * mp,struct xfs_trans ** tpp)327e89c0413SDarrick J. Wong xfs_trans_alloc_empty(
328e89c0413SDarrick J. Wong 	struct xfs_mount		*mp,
329e89c0413SDarrick J. Wong 	struct xfs_trans		**tpp)
330e89c0413SDarrick J. Wong {
331e89c0413SDarrick J. Wong 	struct xfs_trans_res		resv = {0};
332e89c0413SDarrick J. Wong 
333e89c0413SDarrick J. Wong 	return xfs_trans_alloc(mp, &resv, 0, 0, XFS_TRANS_NO_WRITECOUNT, tpp);
334e89c0413SDarrick J. Wong }
335e89c0413SDarrick J. Wong 
336e89c0413SDarrick J. Wong /*
3371da177e4SLinus Torvalds  * Record the indicated change to the given field for application
3381da177e4SLinus Torvalds  * to the file system's superblock when the transaction commits.
3391da177e4SLinus Torvalds  * For now, just store the change in the transaction structure.
3401da177e4SLinus Torvalds  *
3411da177e4SLinus Torvalds  * Mark the transaction structure to indicate that the superblock
3421da177e4SLinus Torvalds  * needs to be updated before committing.
34392821e2bSDavid Chinner  *
34492821e2bSDavid Chinner  * Because we may not be keeping track of allocated/free inodes and
34592821e2bSDavid Chinner  * used filesystem blocks in the superblock, we do not mark the
34692821e2bSDavid Chinner  * superblock dirty in this transaction if we modify these fields.
34792821e2bSDavid Chinner  * We still need to update the transaction deltas so that they get
34892821e2bSDavid Chinner  * applied to the incore superblock, but we don't want them to
34992821e2bSDavid Chinner  * cause the superblock to get locked and logged if these are the
35092821e2bSDavid Chinner  * only fields in the superblock that the transaction modifies.
3511da177e4SLinus Torvalds  */
3521da177e4SLinus Torvalds void
xfs_trans_mod_sb(xfs_trans_t * tp,uint field,int64_t delta)3531da177e4SLinus Torvalds xfs_trans_mod_sb(
3541da177e4SLinus Torvalds 	xfs_trans_t	*tp,
3551da177e4SLinus Torvalds 	uint		field,
35620f4ebf2SDavid Chinner 	int64_t		delta)
3571da177e4SLinus Torvalds {
35892821e2bSDavid Chinner 	uint32_t	flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
35992821e2bSDavid Chinner 	xfs_mount_t	*mp = tp->t_mountp;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	switch (field) {
3621da177e4SLinus Torvalds 	case XFS_TRANS_SB_ICOUNT:
3631da177e4SLinus Torvalds 		tp->t_icount_delta += delta;
36438c26bfdSDave Chinner 		if (xfs_has_lazysbcount(mp))
36592821e2bSDavid Chinner 			flags &= ~XFS_TRANS_SB_DIRTY;
3661da177e4SLinus Torvalds 		break;
3671da177e4SLinus Torvalds 	case XFS_TRANS_SB_IFREE:
3681da177e4SLinus Torvalds 		tp->t_ifree_delta += delta;
36938c26bfdSDave Chinner 		if (xfs_has_lazysbcount(mp))
37092821e2bSDavid Chinner 			flags &= ~XFS_TRANS_SB_DIRTY;
3711da177e4SLinus Torvalds 		break;
3721da177e4SLinus Torvalds 	case XFS_TRANS_SB_FDBLOCKS:
3731da177e4SLinus Torvalds 		/*
3743e78b9a4SBrian Foster 		 * Track the number of blocks allocated in the transaction.
3753e78b9a4SBrian Foster 		 * Make sure it does not exceed the number reserved. If so,
3763e78b9a4SBrian Foster 		 * shutdown as this can lead to accounting inconsistency.
3771da177e4SLinus Torvalds 		 */
3781da177e4SLinus Torvalds 		if (delta < 0) {
3791da177e4SLinus Torvalds 			tp->t_blk_res_used += (uint)-delta;
3803e78b9a4SBrian Foster 			if (tp->t_blk_res_used > tp->t_blk_res)
3813e78b9a4SBrian Foster 				xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
382f74681baSBrian Foster 		} else if (delta > 0 && (tp->t_flags & XFS_TRANS_RES_FDBLKS)) {
383f74681baSBrian Foster 			int64_t	blkres_delta;
384f74681baSBrian Foster 
385f74681baSBrian Foster 			/*
386f74681baSBrian Foster 			 * Return freed blocks directly to the reservation
387f74681baSBrian Foster 			 * instead of the global pool, being careful not to
388f74681baSBrian Foster 			 * overflow the trans counter. This is used to preserve
389f74681baSBrian Foster 			 * reservation across chains of transaction rolls that
390f74681baSBrian Foster 			 * repeatedly free and allocate blocks.
391f74681baSBrian Foster 			 */
392f74681baSBrian Foster 			blkres_delta = min_t(int64_t, delta,
393f74681baSBrian Foster 					     UINT_MAX - tp->t_blk_res);
394f74681baSBrian Foster 			tp->t_blk_res += blkres_delta;
395f74681baSBrian Foster 			delta -= blkres_delta;
3961da177e4SLinus Torvalds 		}
3971da177e4SLinus Torvalds 		tp->t_fdblocks_delta += delta;
39838c26bfdSDave Chinner 		if (xfs_has_lazysbcount(mp))
39992821e2bSDavid Chinner 			flags &= ~XFS_TRANS_SB_DIRTY;
4001da177e4SLinus Torvalds 		break;
4011da177e4SLinus Torvalds 	case XFS_TRANS_SB_RES_FDBLOCKS:
4021da177e4SLinus Torvalds 		/*
4031da177e4SLinus Torvalds 		 * The allocation has already been applied to the
4041da177e4SLinus Torvalds 		 * in-core superblock's counter.  This should only
4051da177e4SLinus Torvalds 		 * be applied to the on-disk superblock.
4061da177e4SLinus Torvalds 		 */
4071da177e4SLinus Torvalds 		tp->t_res_fdblocks_delta += delta;
40838c26bfdSDave Chinner 		if (xfs_has_lazysbcount(mp))
40992821e2bSDavid Chinner 			flags &= ~XFS_TRANS_SB_DIRTY;
4101da177e4SLinus Torvalds 		break;
4111da177e4SLinus Torvalds 	case XFS_TRANS_SB_FREXTENTS:
4121da177e4SLinus Torvalds 		/*
4131da177e4SLinus Torvalds 		 * Track the number of blocks allocated in the
4141da177e4SLinus Torvalds 		 * transaction.  Make sure it does not exceed the
4151da177e4SLinus Torvalds 		 * number reserved.
4161da177e4SLinus Torvalds 		 */
4171da177e4SLinus Torvalds 		if (delta < 0) {
4181da177e4SLinus Torvalds 			tp->t_rtx_res_used += (uint)-delta;
4191da177e4SLinus Torvalds 			ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
4201da177e4SLinus Torvalds 		}
4211da177e4SLinus Torvalds 		tp->t_frextents_delta += delta;
4221da177e4SLinus Torvalds 		break;
4231da177e4SLinus Torvalds 	case XFS_TRANS_SB_RES_FREXTENTS:
4241da177e4SLinus Torvalds 		/*
4251da177e4SLinus Torvalds 		 * The allocation has already been applied to the
426c41564b5SNathan Scott 		 * in-core superblock's counter.  This should only
4271da177e4SLinus Torvalds 		 * be applied to the on-disk superblock.
4281da177e4SLinus Torvalds 		 */
4291da177e4SLinus Torvalds 		ASSERT(delta < 0);
4301da177e4SLinus Torvalds 		tp->t_res_frextents_delta += delta;
4311da177e4SLinus Torvalds 		break;
4321da177e4SLinus Torvalds 	case XFS_TRANS_SB_DBLOCKS:
4331da177e4SLinus Torvalds 		tp->t_dblocks_delta += delta;
4341da177e4SLinus Torvalds 		break;
4351da177e4SLinus Torvalds 	case XFS_TRANS_SB_AGCOUNT:
4361da177e4SLinus Torvalds 		ASSERT(delta > 0);
4371da177e4SLinus Torvalds 		tp->t_agcount_delta += delta;
4381da177e4SLinus Torvalds 		break;
4391da177e4SLinus Torvalds 	case XFS_TRANS_SB_IMAXPCT:
4401da177e4SLinus Torvalds 		tp->t_imaxpct_delta += delta;
4411da177e4SLinus Torvalds 		break;
4421da177e4SLinus Torvalds 	case XFS_TRANS_SB_REXTSIZE:
4431da177e4SLinus Torvalds 		tp->t_rextsize_delta += delta;
4441da177e4SLinus Torvalds 		break;
4451da177e4SLinus Torvalds 	case XFS_TRANS_SB_RBMBLOCKS:
4461da177e4SLinus Torvalds 		tp->t_rbmblocks_delta += delta;
4471da177e4SLinus Torvalds 		break;
4481da177e4SLinus Torvalds 	case XFS_TRANS_SB_RBLOCKS:
4491da177e4SLinus Torvalds 		tp->t_rblocks_delta += delta;
4501da177e4SLinus Torvalds 		break;
4511da177e4SLinus Torvalds 	case XFS_TRANS_SB_REXTENTS:
4521da177e4SLinus Torvalds 		tp->t_rextents_delta += delta;
4531da177e4SLinus Torvalds 		break;
4541da177e4SLinus Torvalds 	case XFS_TRANS_SB_REXTSLOG:
4551da177e4SLinus Torvalds 		tp->t_rextslog_delta += delta;
4561da177e4SLinus Torvalds 		break;
4571da177e4SLinus Torvalds 	default:
4581da177e4SLinus Torvalds 		ASSERT(0);
4591da177e4SLinus Torvalds 		return;
4601da177e4SLinus Torvalds 	}
4611da177e4SLinus Torvalds 
462210c6f1cSDavid Chinner 	tp->t_flags |= flags;
4631da177e4SLinus Torvalds }
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds /*
4661da177e4SLinus Torvalds  * xfs_trans_apply_sb_deltas() is called from the commit code
4671da177e4SLinus Torvalds  * to bring the superblock buffer into the current transaction
4681da177e4SLinus Torvalds  * and modify it as requested by earlier calls to xfs_trans_mod_sb().
4691da177e4SLinus Torvalds  *
4701da177e4SLinus Torvalds  * For now we just look at each field allowed to change and change
4711da177e4SLinus Torvalds  * it if necessary.
4721da177e4SLinus Torvalds  */
4731da177e4SLinus Torvalds STATIC void
xfs_trans_apply_sb_deltas(xfs_trans_t * tp)4741da177e4SLinus Torvalds xfs_trans_apply_sb_deltas(
4751da177e4SLinus Torvalds 	xfs_trans_t	*tp)
4761da177e4SLinus Torvalds {
477ed67ebfdSChristoph Hellwig 	struct xfs_dsb	*sbp;
478e8222613SDave Chinner 	struct xfs_buf	*bp;
4791da177e4SLinus Torvalds 	int		whole = 0;
4801da177e4SLinus Torvalds 
481cead0b10SChristoph Hellwig 	bp = xfs_trans_getsb(tp);
4823e6e8afdSChristoph Hellwig 	sbp = bp->b_addr;
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	/*
48592821e2bSDavid Chinner 	 * Only update the superblock counters if we are logging them
48692821e2bSDavid Chinner 	 */
48738c26bfdSDave Chinner 	if (!xfs_has_lazysbcount((tp->t_mountp))) {
4882bdf7cd0SChristoph Hellwig 		if (tp->t_icount_delta)
489413d57c9SMarcin Slusarz 			be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
4902bdf7cd0SChristoph Hellwig 		if (tp->t_ifree_delta)
491413d57c9SMarcin Slusarz 			be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
4922bdf7cd0SChristoph Hellwig 		if (tp->t_fdblocks_delta)
493413d57c9SMarcin Slusarz 			be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
4942bdf7cd0SChristoph Hellwig 		if (tp->t_res_fdblocks_delta)
495413d57c9SMarcin Slusarz 			be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
4961da177e4SLinus Torvalds 	}
4971da177e4SLinus Torvalds 
4982229276cSDarrick J. Wong 	/*
4992229276cSDarrick J. Wong 	 * Updating frextents requires careful handling because it does not
5002229276cSDarrick J. Wong 	 * behave like the lazysb counters because we cannot rely on log
5012229276cSDarrick J. Wong 	 * recovery in older kenels to recompute the value from the rtbitmap.
5022229276cSDarrick J. Wong 	 * This means that the ondisk frextents must be consistent with the
5032229276cSDarrick J. Wong 	 * rtbitmap.
5042229276cSDarrick J. Wong 	 *
5052229276cSDarrick J. Wong 	 * Therefore, log the frextents change to the ondisk superblock and
5062229276cSDarrick J. Wong 	 * update the incore superblock so that future calls to xfs_log_sb
5072229276cSDarrick J. Wong 	 * write the correct value ondisk.
5082229276cSDarrick J. Wong 	 *
5092229276cSDarrick J. Wong 	 * Don't touch m_frextents because it includes incore reservations,
5102229276cSDarrick J. Wong 	 * and those are handled by the unreserve function.
5112229276cSDarrick J. Wong 	 */
5122229276cSDarrick J. Wong 	if (tp->t_frextents_delta || tp->t_res_frextents_delta) {
5132229276cSDarrick J. Wong 		struct xfs_mount	*mp = tp->t_mountp;
5142229276cSDarrick J. Wong 		int64_t			rtxdelta;
5152229276cSDarrick J. Wong 
5162229276cSDarrick J. Wong 		rtxdelta = tp->t_frextents_delta + tp->t_res_frextents_delta;
5172229276cSDarrick J. Wong 
5182229276cSDarrick J. Wong 		spin_lock(&mp->m_sb_lock);
5192229276cSDarrick J. Wong 		be64_add_cpu(&sbp->sb_frextents, rtxdelta);
5202229276cSDarrick J. Wong 		mp->m_sb.sb_frextents += rtxdelta;
5212229276cSDarrick J. Wong 		spin_unlock(&mp->m_sb_lock);
5222229276cSDarrick J. Wong 	}
5231da177e4SLinus Torvalds 
5242bdf7cd0SChristoph Hellwig 	if (tp->t_dblocks_delta) {
525413d57c9SMarcin Slusarz 		be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
5261da177e4SLinus Torvalds 		whole = 1;
5271da177e4SLinus Torvalds 	}
5282bdf7cd0SChristoph Hellwig 	if (tp->t_agcount_delta) {
529413d57c9SMarcin Slusarz 		be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
5301da177e4SLinus Torvalds 		whole = 1;
5311da177e4SLinus Torvalds 	}
5322bdf7cd0SChristoph Hellwig 	if (tp->t_imaxpct_delta) {
5332bdf7cd0SChristoph Hellwig 		sbp->sb_imax_pct += tp->t_imaxpct_delta;
5341da177e4SLinus Torvalds 		whole = 1;
5351da177e4SLinus Torvalds 	}
5362bdf7cd0SChristoph Hellwig 	if (tp->t_rextsize_delta) {
537413d57c9SMarcin Slusarz 		be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
5381da177e4SLinus Torvalds 		whole = 1;
5391da177e4SLinus Torvalds 	}
5402bdf7cd0SChristoph Hellwig 	if (tp->t_rbmblocks_delta) {
541413d57c9SMarcin Slusarz 		be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
5421da177e4SLinus Torvalds 		whole = 1;
5431da177e4SLinus Torvalds 	}
5442bdf7cd0SChristoph Hellwig 	if (tp->t_rblocks_delta) {
545413d57c9SMarcin Slusarz 		be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
5461da177e4SLinus Torvalds 		whole = 1;
5471da177e4SLinus Torvalds 	}
5482bdf7cd0SChristoph Hellwig 	if (tp->t_rextents_delta) {
549413d57c9SMarcin Slusarz 		be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
5501da177e4SLinus Torvalds 		whole = 1;
5511da177e4SLinus Torvalds 	}
5522bdf7cd0SChristoph Hellwig 	if (tp->t_rextslog_delta) {
5532bdf7cd0SChristoph Hellwig 		sbp->sb_rextslog += tp->t_rextslog_delta;
5541da177e4SLinus Torvalds 		whole = 1;
5551da177e4SLinus Torvalds 	}
5561da177e4SLinus Torvalds 
5573443a3bcSDave Chinner 	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
5581da177e4SLinus Torvalds 	if (whole)
5591da177e4SLinus Torvalds 		/*
560c41564b5SNathan Scott 		 * Log the whole thing, the fields are noncontiguous.
5611da177e4SLinus Torvalds 		 */
562ed67ebfdSChristoph Hellwig 		xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1);
5631da177e4SLinus Torvalds 	else
5641da177e4SLinus Torvalds 		/*
5651da177e4SLinus Torvalds 		 * Since all the modifiable fields are contiguous, we
5661da177e4SLinus Torvalds 		 * can get away with this.
5671da177e4SLinus Torvalds 		 */
568ed67ebfdSChristoph Hellwig 		xfs_trans_log_buf(tp, bp, offsetof(struct xfs_dsb, sb_icount),
569ed67ebfdSChristoph Hellwig 				  offsetof(struct xfs_dsb, sb_frextents) +
5701da177e4SLinus Torvalds 				  sizeof(sbp->sb_frextents) - 1);
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds /*
574dc3ffbb1SDave Chinner  * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations and
575dc3ffbb1SDave Chinner  * apply superblock counter changes to the in-core superblock.  The
57645c34141SDavid Chinner  * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
57745c34141SDavid Chinner  * applied to the in-core superblock.  The idea is that that has already been
57845c34141SDavid Chinner  * done.
5791da177e4SLinus Torvalds  *
58045c34141SDavid Chinner  * If we are not logging superblock counters, then the inode allocated/free and
58145c34141SDavid Chinner  * used block counts are not updated in the on disk superblock. In this case,
58245c34141SDavid Chinner  * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
58345c34141SDavid Chinner  * still need to update the incore superblock with the changes.
584f18c9a90SDave Chinner  *
585f18c9a90SDave Chinner  * Deltas for the inode count are +/-64, hence we use a large batch size of 128
586f18c9a90SDave Chinner  * so we don't need to take the counter lock on every update.
5871da177e4SLinus Torvalds  */
588f18c9a90SDave Chinner #define XFS_ICOUNT_BATCH	128
589f18c9a90SDave Chinner 
59071e330b5SDave Chinner void
xfs_trans_unreserve_and_mod_sb(struct xfs_trans * tp)5911da177e4SLinus Torvalds xfs_trans_unreserve_and_mod_sb(
5920bd5ddedSDave Chinner 	struct xfs_trans	*tp)
5931da177e4SLinus Torvalds {
5940bd5ddedSDave Chinner 	struct xfs_mount	*mp = tp->t_mountp;
5950d485adaSDave Chinner 	bool			rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
59645c34141SDavid Chinner 	int64_t			blkdelta = 0;
59745c34141SDavid Chinner 	int64_t			rtxdelta = 0;
5981b040712SChristoph Hellwig 	int64_t			idelta = 0;
5991b040712SChristoph Hellwig 	int64_t			ifreedelta = 0;
6000bd5ddedSDave Chinner 	int			error;
6011da177e4SLinus Torvalds 
6021b040712SChristoph Hellwig 	/* calculate deltas */
60345c34141SDavid Chinner 	if (tp->t_blk_res > 0)
60445c34141SDavid Chinner 		blkdelta = tp->t_blk_res;
60545c34141SDavid Chinner 	if ((tp->t_fdblocks_delta != 0) &&
60638c26bfdSDave Chinner 	    (xfs_has_lazysbcount(mp) ||
60745c34141SDavid Chinner 	     (tp->t_flags & XFS_TRANS_SB_DIRTY)))
60845c34141SDavid Chinner 	        blkdelta += tp->t_fdblocks_delta;
60945c34141SDavid Chinner 
61045c34141SDavid Chinner 	if (tp->t_rtx_res > 0)
61145c34141SDavid Chinner 		rtxdelta = tp->t_rtx_res;
61245c34141SDavid Chinner 	if ((tp->t_frextents_delta != 0) &&
61345c34141SDavid Chinner 	    (tp->t_flags & XFS_TRANS_SB_DIRTY))
61445c34141SDavid Chinner 		rtxdelta += tp->t_frextents_delta;
61545c34141SDavid Chinner 
61638c26bfdSDave Chinner 	if (xfs_has_lazysbcount(mp) ||
6171b040712SChristoph Hellwig 	     (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
6181b040712SChristoph Hellwig 		idelta = tp->t_icount_delta;
6191b040712SChristoph Hellwig 		ifreedelta = tp->t_ifree_delta;
6201b040712SChristoph Hellwig 	}
6211b040712SChristoph Hellwig 
6221b040712SChristoph Hellwig 	/* apply the per-cpu counters */
6231b040712SChristoph Hellwig 	if (blkdelta) {
6240d485adaSDave Chinner 		error = xfs_mod_fdblocks(mp, blkdelta, rsvd);
625dc3ffbb1SDave Chinner 		ASSERT(!error);
6261b040712SChristoph Hellwig 	}
6271b040712SChristoph Hellwig 
6285825bea0SDave Chinner 	if (idelta)
629f18c9a90SDave Chinner 		percpu_counter_add_batch(&mp->m_icount, idelta,
630f18c9a90SDave Chinner 					 XFS_ICOUNT_BATCH);
6311b040712SChristoph Hellwig 
6325825bea0SDave Chinner 	if (ifreedelta)
633f18c9a90SDave Chinner 		percpu_counter_add(&mp->m_ifree, ifreedelta);
6341b040712SChristoph Hellwig 
6352229276cSDarrick J. Wong 	if (rtxdelta) {
6362229276cSDarrick J. Wong 		error = xfs_mod_frextents(mp, rtxdelta);
6372229276cSDarrick J. Wong 		ASSERT(!error);
6382229276cSDarrick J. Wong 	}
6392229276cSDarrick J. Wong 
6402229276cSDarrick J. Wong 	if (!(tp->t_flags & XFS_TRANS_SB_DIRTY))
6410bd5ddedSDave Chinner 		return;
6420bd5ddedSDave Chinner 
6431b040712SChristoph Hellwig 	/* apply remaining deltas */
6440bd5ddedSDave Chinner 	spin_lock(&mp->m_sb_lock);
6456543990aSDave Chinner 	mp->m_sb.sb_fdblocks += tp->t_fdblocks_delta + tp->t_res_fdblocks_delta;
6466543990aSDave Chinner 	mp->m_sb.sb_icount += idelta;
6476543990aSDave Chinner 	mp->m_sb.sb_ifree += ifreedelta;
6482229276cSDarrick J. Wong 	/*
6492229276cSDarrick J. Wong 	 * Do not touch sb_frextents here because we are dealing with incore
6502229276cSDarrick J. Wong 	 * reservation.  sb_frextents is not part of the lazy sb counters so it
6512229276cSDarrick J. Wong 	 * must be consistent with the ondisk rtbitmap and must never include
6522229276cSDarrick J. Wong 	 * incore reservations.
6532229276cSDarrick J. Wong 	 */
654dc3ffbb1SDave Chinner 	mp->m_sb.sb_dblocks += tp->t_dblocks_delta;
655dc3ffbb1SDave Chinner 	mp->m_sb.sb_agcount += tp->t_agcount_delta;
656dc3ffbb1SDave Chinner 	mp->m_sb.sb_imax_pct += tp->t_imaxpct_delta;
657dc3ffbb1SDave Chinner 	mp->m_sb.sb_rextsize += tp->t_rextsize_delta;
658dc3ffbb1SDave Chinner 	mp->m_sb.sb_rbmblocks += tp->t_rbmblocks_delta;
659dc3ffbb1SDave Chinner 	mp->m_sb.sb_rblocks += tp->t_rblocks_delta;
660dc3ffbb1SDave Chinner 	mp->m_sb.sb_rextents += tp->t_rextents_delta;
661dc3ffbb1SDave Chinner 	mp->m_sb.sb_rextslog += tp->t_rextslog_delta;
6620bd5ddedSDave Chinner 	spin_unlock(&mp->m_sb_lock);
6631b040712SChristoph Hellwig 
664dc3ffbb1SDave Chinner 	/*
665dc3ffbb1SDave Chinner 	 * Debug checks outside of the spinlock so they don't lock up the
666dc3ffbb1SDave Chinner 	 * machine if they fail.
667dc3ffbb1SDave Chinner 	 */
668dc3ffbb1SDave Chinner 	ASSERT(mp->m_sb.sb_imax_pct >= 0);
669dc3ffbb1SDave Chinner 	ASSERT(mp->m_sb.sb_rextslog >= 0);
6701b040712SChristoph Hellwig 	return;
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds 
673e6631f85SDave Chinner /* Add the given log item to the transaction's list of log items. */
674e98c414fSChristoph Hellwig void
xfs_trans_add_item(struct xfs_trans * tp,struct xfs_log_item * lip)675e98c414fSChristoph Hellwig xfs_trans_add_item(
676e98c414fSChristoph Hellwig 	struct xfs_trans	*tp,
677e98c414fSChristoph Hellwig 	struct xfs_log_item	*lip)
678e98c414fSChristoph Hellwig {
679d86142ddSDave Chinner 	ASSERT(lip->li_log == tp->t_mountp->m_log);
680f65020a8SJesper Juhl 	ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
681e6631f85SDave Chinner 	ASSERT(list_empty(&lip->li_trans));
682e6631f85SDave Chinner 	ASSERT(!test_bit(XFS_LI_DIRTY, &lip->li_flags));
683e98c414fSChristoph Hellwig 
684e6631f85SDave Chinner 	list_add_tail(&lip->li_trans, &tp->t_items);
685ba18781bSDave Chinner 	trace_xfs_trans_add_item(tp, _RET_IP_);
686e98c414fSChristoph Hellwig }
687e98c414fSChristoph Hellwig 
688e98c414fSChristoph Hellwig /*
689e6631f85SDave Chinner  * Unlink the log item from the transaction. the log item is no longer
690e6631f85SDave Chinner  * considered dirty in this transaction, as the linked transaction has
691e6631f85SDave Chinner  * finished, either by abort or commit completion.
692e98c414fSChristoph Hellwig  */
693e98c414fSChristoph Hellwig void
xfs_trans_del_item(struct xfs_log_item * lip)694e98c414fSChristoph Hellwig xfs_trans_del_item(
695e98c414fSChristoph Hellwig 	struct xfs_log_item	*lip)
696e98c414fSChristoph Hellwig {
697e6631f85SDave Chinner 	clear_bit(XFS_LI_DIRTY, &lip->li_flags);
698e6631f85SDave Chinner 	list_del_init(&lip->li_trans);
699e98c414fSChristoph Hellwig }
700e98c414fSChristoph Hellwig 
701e6631f85SDave Chinner /* Detach and unlock all of the items in a transaction */
702195cd83dSChristoph Hellwig static void
xfs_trans_free_items(struct xfs_trans * tp,bool abort)703e98c414fSChristoph Hellwig xfs_trans_free_items(
704e98c414fSChristoph Hellwig 	struct xfs_trans	*tp,
705eacb24e7SChristoph Hellwig 	bool			abort)
706e98c414fSChristoph Hellwig {
707e6631f85SDave Chinner 	struct xfs_log_item	*lip, *next;
708e98c414fSChristoph Hellwig 
709ba18781bSDave Chinner 	trace_xfs_trans_free_items(tp, _RET_IP_);
710ba18781bSDave Chinner 
711e6631f85SDave Chinner 	list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
712e6631f85SDave Chinner 		xfs_trans_del_item(lip);
713eacb24e7SChristoph Hellwig 		if (abort)
71422525c17SDave Chinner 			set_bit(XFS_LI_ABORTED, &lip->li_flags);
715ddf92053SChristoph Hellwig 		if (lip->li_ops->iop_release)
716ddf92053SChristoph Hellwig 			lip->li_ops->iop_release(lip);
717e98c414fSChristoph Hellwig 	}
718e98c414fSChristoph Hellwig }
719e98c414fSChristoph Hellwig 
7200e57f6a3SDave Chinner static inline void
xfs_log_item_batch_insert(struct xfs_ail * ailp,struct xfs_ail_cursor * cur,struct xfs_log_item ** log_items,int nr_items,xfs_lsn_t commit_lsn)7210e57f6a3SDave Chinner xfs_log_item_batch_insert(
7220e57f6a3SDave Chinner 	struct xfs_ail		*ailp,
7231d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
7240e57f6a3SDave Chinner 	struct xfs_log_item	**log_items,
7250e57f6a3SDave Chinner 	int			nr_items,
7260e57f6a3SDave Chinner 	xfs_lsn_t		commit_lsn)
7270e57f6a3SDave Chinner {
7280e57f6a3SDave Chinner 	int	i;
7290e57f6a3SDave Chinner 
73057e80956SMatthew Wilcox 	spin_lock(&ailp->ail_lock);
73157e80956SMatthew Wilcox 	/* xfs_trans_ail_update_bulk drops ailp->ail_lock */
7321d8c95a3SDave Chinner 	xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn);
7330e57f6a3SDave Chinner 
734904c17e6SDave Chinner 	for (i = 0; i < nr_items; i++) {
735904c17e6SDave Chinner 		struct xfs_log_item *lip = log_items[i];
736904c17e6SDave Chinner 
737e8b78db7SChristoph Hellwig 		if (lip->li_ops->iop_unpin)
738904c17e6SDave Chinner 			lip->li_ops->iop_unpin(lip, 0);
739904c17e6SDave Chinner 	}
7400e57f6a3SDave Chinner }
7410e57f6a3SDave Chinner 
7420e57f6a3SDave Chinner /*
7430e57f6a3SDave Chinner  * Bulk operation version of xfs_trans_committed that takes a log vector of
7440e57f6a3SDave Chinner  * items to insert into the AIL. This uses bulk AIL insertion techniques to
7450e57f6a3SDave Chinner  * minimise lock traffic.
746e34a314cSDave Chinner  *
747e34a314cSDave Chinner  * If we are called with the aborted flag set, it is because a log write during
748e34a314cSDave Chinner  * a CIL checkpoint commit has failed. In this case, all the items in the
749ddf92053SChristoph Hellwig  * checkpoint have already gone through iop_committed and iop_committing, which
750e34a314cSDave Chinner  * means that checkpoint commit abort handling is treated exactly the same
751e34a314cSDave Chinner  * as an iclog write error even though we haven't started any IO yet. Hence in
752904c17e6SDave Chinner  * this case all we need to do is iop_committed processing, followed by an
753904c17e6SDave Chinner  * iop_unpin(aborted) call.
7541d8c95a3SDave Chinner  *
7551d8c95a3SDave Chinner  * The AIL cursor is used to optimise the insert process. If commit_lsn is not
7561d8c95a3SDave Chinner  * at the end of the AIL, the insert cursor avoids the need to walk
7571d8c95a3SDave Chinner  * the AIL to find the insertion point on every xfs_log_item_batch_insert()
7581d8c95a3SDave Chinner  * call. This saves a lot of needless list walking and is a net win, even
7591d8c95a3SDave Chinner  * though it slightly increases that amount of AIL lock traffic to set it up
7601d8c95a3SDave Chinner  * and tear it down.
7610e57f6a3SDave Chinner  */
7620e57f6a3SDave Chinner void
xfs_trans_committed_bulk(struct xfs_ail * ailp,struct list_head * lv_chain,xfs_lsn_t commit_lsn,bool aborted)7630e57f6a3SDave Chinner xfs_trans_committed_bulk(
7640e57f6a3SDave Chinner 	struct xfs_ail		*ailp,
76516924853SDave Chinner 	struct list_head	*lv_chain,
7660e57f6a3SDave Chinner 	xfs_lsn_t		commit_lsn,
767d15cbf2fSChristoph Hellwig 	bool			aborted)
7680e57f6a3SDave Chinner {
7690e57f6a3SDave Chinner #define LOG_ITEM_BATCH_SIZE	32
7700e57f6a3SDave Chinner 	struct xfs_log_item	*log_items[LOG_ITEM_BATCH_SIZE];
7710e57f6a3SDave Chinner 	struct xfs_log_vec	*lv;
7721d8c95a3SDave Chinner 	struct xfs_ail_cursor	cur;
7730e57f6a3SDave Chinner 	int			i = 0;
7740e57f6a3SDave Chinner 
77557e80956SMatthew Wilcox 	spin_lock(&ailp->ail_lock);
7761d8c95a3SDave Chinner 	xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
77757e80956SMatthew Wilcox 	spin_unlock(&ailp->ail_lock);
7781d8c95a3SDave Chinner 
7790e57f6a3SDave Chinner 	/* unpin all the log items */
78016924853SDave Chinner 	list_for_each_entry(lv, lv_chain, lv_list) {
7810e57f6a3SDave Chinner 		struct xfs_log_item	*lip = lv->lv_item;
7820e57f6a3SDave Chinner 		xfs_lsn_t		item_lsn;
7830e57f6a3SDave Chinner 
7840e57f6a3SDave Chinner 		if (aborted)
78522525c17SDave Chinner 			set_bit(XFS_LI_ABORTED, &lip->li_flags);
7869ce632a2SChristoph Hellwig 
7879ce632a2SChristoph Hellwig 		if (lip->li_ops->flags & XFS_ITEM_RELEASE_WHEN_COMMITTED) {
7889ce632a2SChristoph Hellwig 			lip->li_ops->iop_release(lip);
7899ce632a2SChristoph Hellwig 			continue;
7909ce632a2SChristoph Hellwig 		}
7919ce632a2SChristoph Hellwig 
792e8b78db7SChristoph Hellwig 		if (lip->li_ops->iop_committed)
793904c17e6SDave Chinner 			item_lsn = lip->li_ops->iop_committed(lip, commit_lsn);
794e8b78db7SChristoph Hellwig 		else
795e8b78db7SChristoph Hellwig 			item_lsn = commit_lsn;
7960e57f6a3SDave Chinner 
7971316d4daSDave Chinner 		/* item_lsn of -1 means the item needs no further processing */
7980e57f6a3SDave Chinner 		if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
7990e57f6a3SDave Chinner 			continue;
8000e57f6a3SDave Chinner 
801e34a314cSDave Chinner 		/*
802e34a314cSDave Chinner 		 * if we are aborting the operation, no point in inserting the
803e34a314cSDave Chinner 		 * object into the AIL as we are in a shutdown situation.
804e34a314cSDave Chinner 		 */
805e34a314cSDave Chinner 		if (aborted) {
8068eda8721SDave Chinner 			ASSERT(xlog_is_shutdown(ailp->ail_log));
807e8b78db7SChristoph Hellwig 			if (lip->li_ops->iop_unpin)
808904c17e6SDave Chinner 				lip->li_ops->iop_unpin(lip, 1);
809e34a314cSDave Chinner 			continue;
810e34a314cSDave Chinner 		}
811e34a314cSDave Chinner 
8120e57f6a3SDave Chinner 		if (item_lsn != commit_lsn) {
8130e57f6a3SDave Chinner 
8140e57f6a3SDave Chinner 			/*
8150e57f6a3SDave Chinner 			 * Not a bulk update option due to unusual item_lsn.
8160e57f6a3SDave Chinner 			 * Push into AIL immediately, rechecking the lsn once
8171d8c95a3SDave Chinner 			 * we have the ail lock. Then unpin the item. This does
8181d8c95a3SDave Chinner 			 * not affect the AIL cursor the bulk insert path is
8191d8c95a3SDave Chinner 			 * using.
8200e57f6a3SDave Chinner 			 */
82157e80956SMatthew Wilcox 			spin_lock(&ailp->ail_lock);
8220e57f6a3SDave Chinner 			if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
8230e57f6a3SDave Chinner 				xfs_trans_ail_update(ailp, lip, item_lsn);
8240e57f6a3SDave Chinner 			else
82557e80956SMatthew Wilcox 				spin_unlock(&ailp->ail_lock);
826e8b78db7SChristoph Hellwig 			if (lip->li_ops->iop_unpin)
827904c17e6SDave Chinner 				lip->li_ops->iop_unpin(lip, 0);
8280e57f6a3SDave Chinner 			continue;
8290e57f6a3SDave Chinner 		}
8300e57f6a3SDave Chinner 
8310e57f6a3SDave Chinner 		/* Item is a candidate for bulk AIL insert.  */
8320e57f6a3SDave Chinner 		log_items[i++] = lv->lv_item;
8330e57f6a3SDave Chinner 		if (i >= LOG_ITEM_BATCH_SIZE) {
8341d8c95a3SDave Chinner 			xfs_log_item_batch_insert(ailp, &cur, log_items,
8350e57f6a3SDave Chinner 					LOG_ITEM_BATCH_SIZE, commit_lsn);
8360e57f6a3SDave Chinner 			i = 0;
8370e57f6a3SDave Chinner 		}
8380e57f6a3SDave Chinner 	}
8390e57f6a3SDave Chinner 
8400e57f6a3SDave Chinner 	/* make sure we insert the remainder! */
8410e57f6a3SDave Chinner 	if (i)
8421d8c95a3SDave Chinner 		xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
8431d8c95a3SDave Chinner 
84457e80956SMatthew Wilcox 	spin_lock(&ailp->ail_lock);
845e4a1e29cSEric Sandeen 	xfs_trans_ail_cursor_done(&cur);
84657e80956SMatthew Wilcox 	spin_unlock(&ailp->ail_lock);
8470e57f6a3SDave Chinner }
8480e57f6a3SDave Chinner 
849b1c1b5b6SDave Chinner /*
850fad743d7SDave Chinner  * Sort transaction items prior to running precommit operations. This will
851fad743d7SDave Chinner  * attempt to order the items such that they will always be locked in the same
852fad743d7SDave Chinner  * order. Items that have no sort function are moved to the end of the list
853fad743d7SDave Chinner  * and so are locked last.
854fad743d7SDave Chinner  *
855fad743d7SDave Chinner  * This may need refinement as different types of objects add sort functions.
856fad743d7SDave Chinner  *
857fad743d7SDave Chinner  * Function is more complex than it needs to be because we are comparing 64 bit
858fad743d7SDave Chinner  * values and the function only returns 32 bit values.
859fad743d7SDave Chinner  */
860fad743d7SDave Chinner static int
xfs_trans_precommit_sort(void * unused_arg,const struct list_head * a,const struct list_head * b)861fad743d7SDave Chinner xfs_trans_precommit_sort(
862fad743d7SDave Chinner 	void			*unused_arg,
863fad743d7SDave Chinner 	const struct list_head	*a,
864fad743d7SDave Chinner 	const struct list_head	*b)
865fad743d7SDave Chinner {
866fad743d7SDave Chinner 	struct xfs_log_item	*lia = container_of(a,
867fad743d7SDave Chinner 					struct xfs_log_item, li_trans);
868fad743d7SDave Chinner 	struct xfs_log_item	*lib = container_of(b,
869fad743d7SDave Chinner 					struct xfs_log_item, li_trans);
870fad743d7SDave Chinner 	int64_t			diff;
871fad743d7SDave Chinner 
872fad743d7SDave Chinner 	/*
873fad743d7SDave Chinner 	 * If both items are non-sortable, leave them alone. If only one is
874fad743d7SDave Chinner 	 * sortable, move the non-sortable item towards the end of the list.
875fad743d7SDave Chinner 	 */
876fad743d7SDave Chinner 	if (!lia->li_ops->iop_sort && !lib->li_ops->iop_sort)
877fad743d7SDave Chinner 		return 0;
878fad743d7SDave Chinner 	if (!lia->li_ops->iop_sort)
879fad743d7SDave Chinner 		return 1;
880fad743d7SDave Chinner 	if (!lib->li_ops->iop_sort)
881fad743d7SDave Chinner 		return -1;
882fad743d7SDave Chinner 
883fad743d7SDave Chinner 	diff = lia->li_ops->iop_sort(lia) - lib->li_ops->iop_sort(lib);
884fad743d7SDave Chinner 	if (diff < 0)
885fad743d7SDave Chinner 		return -1;
886fad743d7SDave Chinner 	if (diff > 0)
887fad743d7SDave Chinner 		return 1;
888fad743d7SDave Chinner 	return 0;
889fad743d7SDave Chinner }
890fad743d7SDave Chinner 
891fad743d7SDave Chinner /*
892fad743d7SDave Chinner  * Run transaction precommit functions.
893fad743d7SDave Chinner  *
894fad743d7SDave Chinner  * If there is an error in any of the callouts, then stop immediately and
895fad743d7SDave Chinner  * trigger a shutdown to abort the transaction. There is no recovery possible
896fad743d7SDave Chinner  * from errors at this point as the transaction is dirty....
897fad743d7SDave Chinner  */
898fad743d7SDave Chinner static int
xfs_trans_run_precommits(struct xfs_trans * tp)899fad743d7SDave Chinner xfs_trans_run_precommits(
900fad743d7SDave Chinner 	struct xfs_trans	*tp)
901fad743d7SDave Chinner {
902fad743d7SDave Chinner 	struct xfs_mount	*mp = tp->t_mountp;
903fad743d7SDave Chinner 	struct xfs_log_item	*lip, *n;
904fad743d7SDave Chinner 	int			error = 0;
905fad743d7SDave Chinner 
906fad743d7SDave Chinner 	/*
907fad743d7SDave Chinner 	 * Sort the item list to avoid ABBA deadlocks with other transactions
908fad743d7SDave Chinner 	 * running precommit operations that lock multiple shared items such as
909fad743d7SDave Chinner 	 * inode cluster buffers.
910fad743d7SDave Chinner 	 */
911fad743d7SDave Chinner 	list_sort(NULL, &tp->t_items, xfs_trans_precommit_sort);
912fad743d7SDave Chinner 
913fad743d7SDave Chinner 	/*
914fad743d7SDave Chinner 	 * Precommit operations can remove the log item from the transaction
915fad743d7SDave Chinner 	 * if the log item exists purely to delay modifications until they
916fad743d7SDave Chinner 	 * can be ordered against other operations. Hence we have to use
917fad743d7SDave Chinner 	 * list_for_each_entry_safe() here.
918fad743d7SDave Chinner 	 */
919fad743d7SDave Chinner 	list_for_each_entry_safe(lip, n, &tp->t_items, li_trans) {
920fad743d7SDave Chinner 		if (!test_bit(XFS_LI_DIRTY, &lip->li_flags))
921fad743d7SDave Chinner 			continue;
922fad743d7SDave Chinner 		if (lip->li_ops->iop_precommit) {
923fad743d7SDave Chinner 			error = lip->li_ops->iop_precommit(tp, lip);
924fad743d7SDave Chinner 			if (error)
925fad743d7SDave Chinner 				break;
926fad743d7SDave Chinner 		}
927fad743d7SDave Chinner 	}
928fad743d7SDave Chinner 	if (error)
929fad743d7SDave Chinner 		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
930fad743d7SDave Chinner 	return error;
931fad743d7SDave Chinner }
932fad743d7SDave Chinner 
933fad743d7SDave Chinner /*
934b1037058SChristoph Hellwig  * Commit the given transaction to the log.
9350924378aSDave Chinner  *
9360924378aSDave Chinner  * XFS disk error handling mechanism is not based on a typical
9370924378aSDave Chinner  * transaction abort mechanism. Logically after the filesystem
9380924378aSDave Chinner  * gets marked 'SHUTDOWN', we can't let any new transactions
9390924378aSDave Chinner  * be durable - ie. committed to disk - because some metadata might
9400924378aSDave Chinner  * be inconsistent. In such cases, this returns an error, and the
9410924378aSDave Chinner  * caller may assume that all locked objects joined to the transaction
9420924378aSDave Chinner  * have already been unlocked as if the commit had succeeded.
9430924378aSDave Chinner  * Do not reference the transaction structure after this call.
9440924378aSDave Chinner  */
94570393313SChristoph Hellwig static int
__xfs_trans_commit(struct xfs_trans * tp,bool regrant)94670393313SChristoph Hellwig __xfs_trans_commit(
947a3ccd2caSChristoph Hellwig 	struct xfs_trans	*tp,
94870393313SChristoph Hellwig 	bool			regrant)
9490924378aSDave Chinner {
950a3ccd2caSChristoph Hellwig 	struct xfs_mount	*mp = tp->t_mountp;
9513c4cb76bSDave Chinner 	struct xlog		*log = mp->m_log;
9525f9b4b0dSDave Chinner 	xfs_csn_t		commit_seq = 0;
953a3ccd2caSChristoph Hellwig 	int			error = 0;
9540924378aSDave Chinner 	int			sync = tp->t_flags & XFS_TRANS_SYNC;
9550924378aSDave Chinner 
956ba18781bSDave Chinner 	trace_xfs_trans_commit(tp, _RET_IP_);
957ba18781bSDave Chinner 
95898719051SBrian Foster 	/*
95998719051SBrian Foster 	 * Finish deferred items on final commit. Only permanent transactions
96098719051SBrian Foster 	 * should ever have deferred ops.
96198719051SBrian Foster 	 */
9629d9e6233SBrian Foster 	WARN_ON_ONCE(!list_empty(&tp->t_dfops) &&
96398719051SBrian Foster 		     !(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
96498719051SBrian Foster 	if (!regrant && (tp->t_flags & XFS_TRANS_PERM_LOG_RES)) {
965b277c37fSBrian Foster 		error = xfs_defer_finish_noroll(&tp);
9669b1f4e98SBrian Foster 		if (error)
967e021a2e5SBrian Foster 			goto out_unreserve;
968*0f2dd866SDarrick J. Wong 	}
969cb042117SDave Chinner 
970cb042117SDave Chinner 	error = xfs_trans_run_precommits(tp);
971cb042117SDave Chinner 	if (error)
972cb042117SDave Chinner 		goto out_unreserve;
973e021a2e5SBrian Foster 
9740924378aSDave Chinner 	/*
9750924378aSDave Chinner 	 * If there is nothing to be logged by the transaction,
9760924378aSDave Chinner 	 * then unlock all of the items associated with the
9770924378aSDave Chinner 	 * transaction and free the transaction structure.
9780924378aSDave Chinner 	 * Also make sure to return any reserved blocks to
9790924378aSDave Chinner 	 * the free pool.
9800924378aSDave Chinner 	 */
981a3ccd2caSChristoph Hellwig 	if (!(tp->t_flags & XFS_TRANS_DIRTY))
982a3ccd2caSChristoph Hellwig 		goto out_unreserve;
983a3ccd2caSChristoph Hellwig 
9843c4cb76bSDave Chinner 	/*
9853c4cb76bSDave Chinner 	 * We must check against log shutdown here because we cannot abort log
9863c4cb76bSDave Chinner 	 * items and leave them dirty, inconsistent and unpinned in memory while
9873c4cb76bSDave Chinner 	 * the log is active. This leaves them open to being written back to
9883c4cb76bSDave Chinner 	 * disk, and that will lead to on-disk corruption.
9893c4cb76bSDave Chinner 	 */
9903c4cb76bSDave Chinner 	if (xlog_is_shutdown(log)) {
9912451337dSDave Chinner 		error = -EIO;
992a3ccd2caSChristoph Hellwig 		goto out_unreserve;
9930924378aSDave Chinner 	}
994a3ccd2caSChristoph Hellwig 
9950924378aSDave Chinner 	ASSERT(tp->t_ticket != NULL);
9960924378aSDave Chinner 
9970924378aSDave Chinner 	/*
9980924378aSDave Chinner 	 * If we need to update the superblock, then do it now.
9990924378aSDave Chinner 	 */
10000924378aSDave Chinner 	if (tp->t_flags & XFS_TRANS_SB_DIRTY)
10010924378aSDave Chinner 		xfs_trans_apply_sb_deltas(tp);
10020924378aSDave Chinner 	xfs_trans_apply_dquot_deltas(tp);
10030924378aSDave Chinner 
10043c4cb76bSDave Chinner 	xlog_cil_commit(log, tp, &commit_seq, regrant);
10051da177e4SLinus Torvalds 
10060244b960SChristoph Hellwig 	xfs_trans_free(tp);
10070244b960SChristoph Hellwig 
10081da177e4SLinus Torvalds 	/*
10091da177e4SLinus Torvalds 	 * If the transaction needs to be synchronous, then force the
10101da177e4SLinus Torvalds 	 * log out now and wait for it.
10111da177e4SLinus Torvalds 	 */
10121da177e4SLinus Torvalds 	if (sync) {
10135f9b4b0dSDave Chinner 		error = xfs_log_force_seq(mp, commit_seq, XFS_LOG_SYNC, NULL);
1014ff6d6af2SBill O'Donnell 		XFS_STATS_INC(mp, xs_trans_sync);
10151da177e4SLinus Torvalds 	} else {
1016ff6d6af2SBill O'Donnell 		XFS_STATS_INC(mp, xs_trans_async);
10171da177e4SLinus Torvalds 	}
10181da177e4SLinus Torvalds 
1019a3ccd2caSChristoph Hellwig 	return error;
1020a3ccd2caSChristoph Hellwig 
1021a3ccd2caSChristoph Hellwig out_unreserve:
1022a3ccd2caSChristoph Hellwig 	xfs_trans_unreserve_and_mod_sb(tp);
1023a3ccd2caSChristoph Hellwig 
1024a3ccd2caSChristoph Hellwig 	/*
1025a3ccd2caSChristoph Hellwig 	 * It is indeed possible for the transaction to be not dirty but
1026a3ccd2caSChristoph Hellwig 	 * the dqinfo portion to be.  All that means is that we have some
1027a3ccd2caSChristoph Hellwig 	 * (non-persistent) quota reservations that need to be unreserved.
1028a3ccd2caSChristoph Hellwig 	 */
1029a3ccd2caSChristoph Hellwig 	xfs_trans_unreserve_and_mod_dquots(tp);
1030a3ccd2caSChristoph Hellwig 	if (tp->t_ticket) {
10313c4cb76bSDave Chinner 		if (regrant && !xlog_is_shutdown(log))
10323c4cb76bSDave Chinner 			xfs_log_ticket_regrant(log, tp->t_ticket);
10338b41e3f9SChristoph Hellwig 		else
10343c4cb76bSDave Chinner 			xfs_log_ticket_ungrant(log, tp->t_ticket);
1035ba18781bSDave Chinner 		tp->t_ticket = NULL;
1036a3ccd2caSChristoph Hellwig 	}
1037195cd83dSChristoph Hellwig 	xfs_trans_free_items(tp, !!error);
1038a3ccd2caSChristoph Hellwig 	xfs_trans_free(tp);
1039a3ccd2caSChristoph Hellwig 
1040ff6d6af2SBill O'Donnell 	XFS_STATS_INC(mp, xs_trans_empty);
1041a3ccd2caSChristoph Hellwig 	return error;
10421da177e4SLinus Torvalds }
10431da177e4SLinus Torvalds 
104470393313SChristoph Hellwig int
xfs_trans_commit(struct xfs_trans * tp)104570393313SChristoph Hellwig xfs_trans_commit(
104670393313SChristoph Hellwig 	struct xfs_trans	*tp)
104770393313SChristoph Hellwig {
104870393313SChristoph Hellwig 	return __xfs_trans_commit(tp, false);
104970393313SChristoph Hellwig }
105070393313SChristoph Hellwig 
10511da177e4SLinus Torvalds /*
10523c4cb76bSDave Chinner  * Unlock all of the transaction's items and free the transaction.  If the
10533c4cb76bSDave Chinner  * transaction is dirty, we must shut down the filesystem because there is no
10543c4cb76bSDave Chinner  * way to restore them to their previous state.
10551da177e4SLinus Torvalds  *
10563c4cb76bSDave Chinner  * If the transaction has made a log reservation, make sure to release it as
10573c4cb76bSDave Chinner  * well.
10583c4cb76bSDave Chinner  *
10593c4cb76bSDave Chinner  * This is a high level function (equivalent to xfs_trans_commit()) and so can
10603c4cb76bSDave Chinner  * be called after the transaction has effectively been aborted due to the mount
10613c4cb76bSDave Chinner  * being shut down. However, if the mount has not been shut down and the
10623c4cb76bSDave Chinner  * transaction is dirty we will shut the mount down and, in doing so, that
10633c4cb76bSDave Chinner  * guarantees that the log is shut down, too. Hence we don't need to be as
10643c4cb76bSDave Chinner  * careful with shutdown state and dirty items here as we need to be in
10653c4cb76bSDave Chinner  * xfs_trans_commit().
10661da177e4SLinus Torvalds  */
10671da177e4SLinus Torvalds void
xfs_trans_cancel(struct xfs_trans * tp)10681da177e4SLinus Torvalds xfs_trans_cancel(
10694906e215SChristoph Hellwig 	struct xfs_trans	*tp)
10701da177e4SLinus Torvalds {
10714906e215SChristoph Hellwig 	struct xfs_mount	*mp = tp->t_mountp;
10723c4cb76bSDave Chinner 	struct xlog		*log = mp->m_log;
10734906e215SChristoph Hellwig 	bool			dirty = (tp->t_flags & XFS_TRANS_DIRTY);
10741da177e4SLinus Torvalds 
1075ba18781bSDave Chinner 	trace_xfs_trans_cancel(tp, _RET_IP_);
1076ba18781bSDave Chinner 
107747a6df7cSDarrick J. Wong 	/*
107847a6df7cSDarrick J. Wong 	 * It's never valid to cancel a transaction with deferred ops attached,
107947a6df7cSDarrick J. Wong 	 * because the transaction is effectively dirty.  Complain about this
108055d5c3a3SDave Chinner 	 * loudly before freeing the in-memory defer items and shutting down the
108155d5c3a3SDave Chinner 	 * filesystem.
108247a6df7cSDarrick J. Wong 	 */
108347a6df7cSDarrick J. Wong 	if (!list_empty(&tp->t_dfops)) {
108447a6df7cSDarrick J. Wong 		ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
108547a6df7cSDarrick J. Wong 		dirty = true;
10869e28a242SBrian Foster 		xfs_defer_cancel(tp);
108747a6df7cSDarrick J. Wong 	}
1088e021a2e5SBrian Foster 
10891da177e4SLinus Torvalds 	/*
10903c4cb76bSDave Chinner 	 * See if the caller is relying on us to shut down the filesystem. We
10913c4cb76bSDave Chinner 	 * only want an error report if there isn't already a shutdown in
10923c4cb76bSDave Chinner 	 * progress, so we only need to check against the mount shutdown state
10933c4cb76bSDave Chinner 	 * here.
10941da177e4SLinus Torvalds 	 */
109575c8c50fSDave Chinner 	if (dirty && !xfs_is_shutdown(mp)) {
10960733af21SRyan Hankins 		XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
10977d04a335SNathan Scott 		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
109860a204f0SNathan Scott 	}
10991da177e4SLinus Torvalds #ifdef DEBUG
11003c4cb76bSDave Chinner 	/* Log items need to be consistent until the log is shut down. */
11013c4cb76bSDave Chinner 	if (!dirty && !xlog_is_shutdown(log)) {
1102e6631f85SDave Chinner 		struct xfs_log_item *lip;
11031da177e4SLinus Torvalds 
1104e6631f85SDave Chinner 		list_for_each_entry(lip, &tp->t_items, li_trans)
1105d6b8fc6cSKaixu Xia 			ASSERT(!xlog_item_is_intent_done(lip));
11061da177e4SLinus Torvalds 	}
11071da177e4SLinus Torvalds #endif
11081da177e4SLinus Torvalds 	xfs_trans_unreserve_and_mod_sb(tp);
11097d095257SChristoph Hellwig 	xfs_trans_unreserve_and_mod_dquots(tp);
11101da177e4SLinus Torvalds 
1111ba18781bSDave Chinner 	if (tp->t_ticket) {
11123c4cb76bSDave Chinner 		xfs_log_ticket_ungrant(log, tp->t_ticket);
1113ba18781bSDave Chinner 		tp->t_ticket = NULL;
1114ba18781bSDave Chinner 	}
11151da177e4SLinus Torvalds 
1116195cd83dSChristoph Hellwig 	xfs_trans_free_items(tp, dirty);
11171da177e4SLinus Torvalds 	xfs_trans_free(tp);
11181da177e4SLinus Torvalds }
11191da177e4SLinus Torvalds 
1120322ff6b8SNiv Sardi /*
1121322ff6b8SNiv Sardi  * Roll from one trans in the sequence of PERMANENT transactions to
1122322ff6b8SNiv Sardi  * the next: permanent transactions are only flushed out when
112370393313SChristoph Hellwig  * committed with xfs_trans_commit(), but we still want as soon
1124322ff6b8SNiv Sardi  * as possible to let chunks of it go to the log. So we commit the
1125322ff6b8SNiv Sardi  * chunk we've been working on and get a new transaction to continue.
1126322ff6b8SNiv Sardi  */
1127322ff6b8SNiv Sardi int
xfs_trans_roll(struct xfs_trans ** tpp)1128254133f5SChristoph Hellwig xfs_trans_roll(
1129411350dfSChristoph Hellwig 	struct xfs_trans	**tpp)
1130322ff6b8SNiv Sardi {
1131411350dfSChristoph Hellwig 	struct xfs_trans	*trans = *tpp;
11323d3c8b52SJie Liu 	struct xfs_trans_res	tres;
1133322ff6b8SNiv Sardi 	int			error;
1134322ff6b8SNiv Sardi 
1135ba18781bSDave Chinner 	trace_xfs_trans_roll(trans, _RET_IP_);
1136ba18781bSDave Chinner 
1137322ff6b8SNiv Sardi 	/*
1138322ff6b8SNiv Sardi 	 * Copy the critical parameters from one trans to the next.
1139322ff6b8SNiv Sardi 	 */
11403d3c8b52SJie Liu 	tres.tr_logres = trans->t_log_res;
11413d3c8b52SJie Liu 	tres.tr_logcount = trans->t_log_count;
1142411350dfSChristoph Hellwig 
1143322ff6b8SNiv Sardi 	*tpp = xfs_trans_dup(trans);
1144322ff6b8SNiv Sardi 
1145322ff6b8SNiv Sardi 	/*
1146322ff6b8SNiv Sardi 	 * Commit the current transaction.
1147322ff6b8SNiv Sardi 	 * If this commit failed, then it'd just unlock those items that
1148322ff6b8SNiv Sardi 	 * are not marked ihold. That also means that a filesystem shutdown
1149322ff6b8SNiv Sardi 	 * is in progress. The caller takes the responsibility to cancel
1150322ff6b8SNiv Sardi 	 * the duplicate transaction that gets returned.
1151322ff6b8SNiv Sardi 	 */
115270393313SChristoph Hellwig 	error = __xfs_trans_commit(trans, true);
1153322ff6b8SNiv Sardi 	if (error)
1154d99831ffSEric Sandeen 		return error;
1155322ff6b8SNiv Sardi 
1156322ff6b8SNiv Sardi 	/*
1157411350dfSChristoph Hellwig 	 * Reserve space in the log for the next transaction.
1158322ff6b8SNiv Sardi 	 * This also pushes items in the "AIL", the list of logged items,
1159322ff6b8SNiv Sardi 	 * out to disk if they are taking up space at the tail of the log
1160322ff6b8SNiv Sardi 	 * that we want to use.  This requires that either nothing be locked
1161322ff6b8SNiv Sardi 	 * across this call, or that anything that is locked be logged in
1162322ff6b8SNiv Sardi 	 * the prior and the next transactions.
1163322ff6b8SNiv Sardi 	 */
11643d3c8b52SJie Liu 	tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
1165411350dfSChristoph Hellwig 	return xfs_trans_reserve(*tpp, &tres, 0, 0);
1166322ff6b8SNiv Sardi }
11673a1af6c3SDarrick J. Wong 
11683a1af6c3SDarrick J. Wong /*
11693a1af6c3SDarrick J. Wong  * Allocate an transaction, lock and join the inode to it, and reserve quota.
11703a1af6c3SDarrick J. Wong  *
11713a1af6c3SDarrick J. Wong  * The caller must ensure that the on-disk dquots attached to this inode have
11723a1af6c3SDarrick J. Wong  * already been allocated and initialized.  The caller is responsible for
11733a1af6c3SDarrick J. Wong  * releasing ILOCK_EXCL if a new transaction is returned.
11743a1af6c3SDarrick J. Wong  */
11753a1af6c3SDarrick 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)11763a1af6c3SDarrick J. Wong xfs_trans_alloc_inode(
11773a1af6c3SDarrick J. Wong 	struct xfs_inode	*ip,
11783a1af6c3SDarrick J. Wong 	struct xfs_trans_res	*resv,
11793a1af6c3SDarrick J. Wong 	unsigned int		dblocks,
11803de4eb10SDarrick J. Wong 	unsigned int		rblocks,
11813a1af6c3SDarrick J. Wong 	bool			force,
11823a1af6c3SDarrick J. Wong 	struct xfs_trans	**tpp)
11833a1af6c3SDarrick J. Wong {
11843a1af6c3SDarrick J. Wong 	struct xfs_trans	*tp;
11853a1af6c3SDarrick J. Wong 	struct xfs_mount	*mp = ip->i_mount;
1186766aabd5SDarrick J. Wong 	bool			retried = false;
11873a1af6c3SDarrick J. Wong 	int			error;
11883a1af6c3SDarrick J. Wong 
1189766aabd5SDarrick J. Wong retry:
11903de4eb10SDarrick J. Wong 	error = xfs_trans_alloc(mp, resv, dblocks,
11913de4eb10SDarrick J. Wong 			rblocks / mp->m_sb.sb_rextsize,
11923a1af6c3SDarrick J. Wong 			force ? XFS_TRANS_RESERVE : 0, &tp);
11933a1af6c3SDarrick J. Wong 	if (error)
11943a1af6c3SDarrick J. Wong 		return error;
11953a1af6c3SDarrick J. Wong 
11963a1af6c3SDarrick J. Wong 	xfs_ilock(ip, XFS_ILOCK_EXCL);
11973a1af6c3SDarrick J. Wong 	xfs_trans_ijoin(tp, ip, 0);
11983a1af6c3SDarrick J. Wong 
11993a1af6c3SDarrick J. Wong 	error = xfs_qm_dqattach_locked(ip, false);
12003a1af6c3SDarrick J. Wong 	if (error) {
12013a1af6c3SDarrick J. Wong 		/* Caller should have allocated the dquots! */
12023a1af6c3SDarrick J. Wong 		ASSERT(error != -ENOENT);
12033a1af6c3SDarrick J. Wong 		goto out_cancel;
12043a1af6c3SDarrick J. Wong 	}
12053a1af6c3SDarrick J. Wong 
12063de4eb10SDarrick J. Wong 	error = xfs_trans_reserve_quota_nblks(tp, ip, dblocks, rblocks, force);
1207766aabd5SDarrick J. Wong 	if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
1208766aabd5SDarrick J. Wong 		xfs_trans_cancel(tp);
1209766aabd5SDarrick J. Wong 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
1210766aabd5SDarrick J. Wong 		xfs_blockgc_free_quota(ip, 0);
1211766aabd5SDarrick J. Wong 		retried = true;
1212766aabd5SDarrick J. Wong 		goto retry;
1213766aabd5SDarrick J. Wong 	}
12143a1af6c3SDarrick J. Wong 	if (error)
12153a1af6c3SDarrick J. Wong 		goto out_cancel;
12163a1af6c3SDarrick J. Wong 
12173a1af6c3SDarrick J. Wong 	*tpp = tp;
12183a1af6c3SDarrick J. Wong 	return 0;
12193a1af6c3SDarrick J. Wong 
12203a1af6c3SDarrick J. Wong out_cancel:
12213a1af6c3SDarrick J. Wong 	xfs_trans_cancel(tp);
12223a1af6c3SDarrick J. Wong 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
12233a1af6c3SDarrick J. Wong 	return error;
12243a1af6c3SDarrick J. Wong }
1225f2f7b9ffSDarrick J. Wong 
1226f2f7b9ffSDarrick J. Wong /*
1227f2f7b9ffSDarrick J. Wong  * Allocate an transaction in preparation for inode creation by reserving quota
1228f2f7b9ffSDarrick J. Wong  * against the given dquots.  Callers are not required to hold any inode locks.
1229f2f7b9ffSDarrick J. Wong  */
1230f2f7b9ffSDarrick 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)1231f2f7b9ffSDarrick J. Wong xfs_trans_alloc_icreate(
1232f2f7b9ffSDarrick J. Wong 	struct xfs_mount	*mp,
1233f2f7b9ffSDarrick J. Wong 	struct xfs_trans_res	*resv,
1234f2f7b9ffSDarrick J. Wong 	struct xfs_dquot	*udqp,
1235f2f7b9ffSDarrick J. Wong 	struct xfs_dquot	*gdqp,
1236f2f7b9ffSDarrick J. Wong 	struct xfs_dquot	*pdqp,
1237f2f7b9ffSDarrick J. Wong 	unsigned int		dblocks,
1238f2f7b9ffSDarrick J. Wong 	struct xfs_trans	**tpp)
1239f2f7b9ffSDarrick J. Wong {
1240f2f7b9ffSDarrick J. Wong 	struct xfs_trans	*tp;
1241c237dd7cSDarrick J. Wong 	bool			retried = false;
1242f2f7b9ffSDarrick J. Wong 	int			error;
1243f2f7b9ffSDarrick J. Wong 
1244c237dd7cSDarrick J. Wong retry:
1245f2f7b9ffSDarrick J. Wong 	error = xfs_trans_alloc(mp, resv, dblocks, 0, 0, &tp);
1246f2f7b9ffSDarrick J. Wong 	if (error)
1247f2f7b9ffSDarrick J. Wong 		return error;
1248f2f7b9ffSDarrick J. Wong 
1249f2f7b9ffSDarrick J. Wong 	error = xfs_trans_reserve_quota_icreate(tp, udqp, gdqp, pdqp, dblocks);
1250c237dd7cSDarrick J. Wong 	if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
1251c237dd7cSDarrick J. Wong 		xfs_trans_cancel(tp);
1252c237dd7cSDarrick J. Wong 		xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0);
1253c237dd7cSDarrick J. Wong 		retried = true;
1254c237dd7cSDarrick J. Wong 		goto retry;
1255c237dd7cSDarrick J. Wong 	}
1256f2f7b9ffSDarrick J. Wong 	if (error) {
1257f2f7b9ffSDarrick J. Wong 		xfs_trans_cancel(tp);
1258f2f7b9ffSDarrick J. Wong 		return error;
1259f2f7b9ffSDarrick J. Wong 	}
1260f2f7b9ffSDarrick J. Wong 
1261f2f7b9ffSDarrick J. Wong 	*tpp = tp;
1262f2f7b9ffSDarrick J. Wong 	return 0;
1263f2f7b9ffSDarrick J. Wong }
12647317a03dSDarrick J. Wong 
12657317a03dSDarrick J. Wong /*
12667317a03dSDarrick J. Wong  * Allocate an transaction, lock and join the inode to it, and reserve quota
12677317a03dSDarrick J. Wong  * in preparation for inode attribute changes that include uid, gid, or prid
12687317a03dSDarrick J. Wong  * changes.
12697317a03dSDarrick J. Wong  *
12707317a03dSDarrick J. Wong  * The caller must ensure that the on-disk dquots attached to this inode have
12717317a03dSDarrick J. Wong  * already been allocated and initialized.  The ILOCK will be dropped when the
12727317a03dSDarrick J. Wong  * transaction is committed or cancelled.
12737317a03dSDarrick J. Wong  */
12747317a03dSDarrick 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)12757317a03dSDarrick J. Wong xfs_trans_alloc_ichange(
12767317a03dSDarrick J. Wong 	struct xfs_inode	*ip,
1277758303d1SDarrick J. Wong 	struct xfs_dquot	*new_udqp,
1278758303d1SDarrick J. Wong 	struct xfs_dquot	*new_gdqp,
1279758303d1SDarrick J. Wong 	struct xfs_dquot	*new_pdqp,
12807317a03dSDarrick J. Wong 	bool			force,
12817317a03dSDarrick J. Wong 	struct xfs_trans	**tpp)
12827317a03dSDarrick J. Wong {
12837317a03dSDarrick J. Wong 	struct xfs_trans	*tp;
12847317a03dSDarrick J. Wong 	struct xfs_mount	*mp = ip->i_mount;
1285758303d1SDarrick J. Wong 	struct xfs_dquot	*udqp;
1286758303d1SDarrick J. Wong 	struct xfs_dquot	*gdqp;
1287758303d1SDarrick J. Wong 	struct xfs_dquot	*pdqp;
1288758303d1SDarrick J. Wong 	bool			retried = false;
12897317a03dSDarrick J. Wong 	int			error;
12907317a03dSDarrick J. Wong 
1291758303d1SDarrick J. Wong retry:
12927317a03dSDarrick J. Wong 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
12937317a03dSDarrick J. Wong 	if (error)
12947317a03dSDarrick J. Wong 		return error;
12957317a03dSDarrick J. Wong 
12967317a03dSDarrick J. Wong 	xfs_ilock(ip, XFS_ILOCK_EXCL);
12977317a03dSDarrick J. Wong 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
12987317a03dSDarrick J. Wong 
12997317a03dSDarrick J. Wong 	error = xfs_qm_dqattach_locked(ip, false);
13007317a03dSDarrick J. Wong 	if (error) {
13017317a03dSDarrick J. Wong 		/* Caller should have allocated the dquots! */
13027317a03dSDarrick J. Wong 		ASSERT(error != -ENOENT);
13037317a03dSDarrick J. Wong 		goto out_cancel;
13047317a03dSDarrick J. Wong 	}
13057317a03dSDarrick J. Wong 
13067317a03dSDarrick J. Wong 	/*
13077317a03dSDarrick J. Wong 	 * For each quota type, skip quota reservations if the inode's dquots
13087317a03dSDarrick J. Wong 	 * now match the ones that came from the caller, or the caller didn't
1309758303d1SDarrick J. Wong 	 * pass one in.  The inode's dquots can change if we drop the ILOCK to
1310758303d1SDarrick J. Wong 	 * perform a blockgc scan, so we must preserve the caller's arguments.
13117317a03dSDarrick J. Wong 	 */
1312758303d1SDarrick J. Wong 	udqp = (new_udqp != ip->i_udquot) ? new_udqp : NULL;
1313758303d1SDarrick J. Wong 	gdqp = (new_gdqp != ip->i_gdquot) ? new_gdqp : NULL;
1314758303d1SDarrick J. Wong 	pdqp = (new_pdqp != ip->i_pdquot) ? new_pdqp : NULL;
13157317a03dSDarrick J. Wong 	if (udqp || gdqp || pdqp) {
13165c615f0fSDarrick J. Wong 		unsigned int	qflags = XFS_QMOPT_RES_REGBLKS;
13175c615f0fSDarrick J. Wong 
13185c615f0fSDarrick J. Wong 		if (force)
13195c615f0fSDarrick J. Wong 			qflags |= XFS_QMOPT_FORCE_RES;
13205c615f0fSDarrick J. Wong 
13215c615f0fSDarrick J. Wong 		/*
13225c615f0fSDarrick J. Wong 		 * Reserve enough quota to handle blocks on disk and reserved
13235c615f0fSDarrick J. Wong 		 * for a delayed allocation.  We'll actually transfer the
13245c615f0fSDarrick J. Wong 		 * delalloc reservation between dquots at chown time, even
13255c615f0fSDarrick J. Wong 		 * though that part is only semi-transactional.
13265c615f0fSDarrick J. Wong 		 */
13275c615f0fSDarrick J. Wong 		error = xfs_trans_reserve_quota_bydquots(tp, mp, udqp, gdqp,
13286e73a545SChristoph Hellwig 				pdqp, ip->i_nblocks + ip->i_delayed_blks,
13295c615f0fSDarrick J. Wong 				1, qflags);
1330758303d1SDarrick J. Wong 		if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
1331758303d1SDarrick J. Wong 			xfs_trans_cancel(tp);
1332758303d1SDarrick J. Wong 			xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0);
1333758303d1SDarrick J. Wong 			retried = true;
1334758303d1SDarrick J. Wong 			goto retry;
1335758303d1SDarrick J. Wong 		}
13367317a03dSDarrick J. Wong 		if (error)
13377317a03dSDarrick J. Wong 			goto out_cancel;
13387317a03dSDarrick J. Wong 	}
13397317a03dSDarrick J. Wong 
13407317a03dSDarrick J. Wong 	*tpp = tp;
13417317a03dSDarrick J. Wong 	return 0;
13427317a03dSDarrick J. Wong 
13437317a03dSDarrick J. Wong out_cancel:
13447317a03dSDarrick J. Wong 	xfs_trans_cancel(tp);
13457317a03dSDarrick J. Wong 	return error;
13467317a03dSDarrick J. Wong }
1347871b9316SDarrick J. Wong 
1348871b9316SDarrick J. Wong /*
1349871b9316SDarrick J. Wong  * Allocate an transaction, lock and join the directory and child inodes to it,
1350871b9316SDarrick J. Wong  * and reserve quota for a directory update.  If there isn't sufficient space,
1351871b9316SDarrick J. Wong  * @dblocks will be set to zero for a reservationless directory update and
1352871b9316SDarrick J. Wong  * @nospace_error will be set to a negative errno describing the space
1353871b9316SDarrick J. Wong  * constraint we hit.
1354871b9316SDarrick J. Wong  *
1355871b9316SDarrick J. Wong  * The caller must ensure that the on-disk dquots attached to this inode have
1356871b9316SDarrick J. Wong  * already been allocated and initialized.  The ILOCKs will be dropped when the
1357871b9316SDarrick J. Wong  * transaction is committed or cancelled.
1358871b9316SDarrick J. Wong  */
1359871b9316SDarrick 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)1360871b9316SDarrick J. Wong xfs_trans_alloc_dir(
1361871b9316SDarrick J. Wong 	struct xfs_inode	*dp,
1362871b9316SDarrick J. Wong 	struct xfs_trans_res	*resv,
1363871b9316SDarrick J. Wong 	struct xfs_inode	*ip,
1364871b9316SDarrick J. Wong 	unsigned int		*dblocks,
1365871b9316SDarrick J. Wong 	struct xfs_trans	**tpp,
1366871b9316SDarrick J. Wong 	int			*nospace_error)
1367871b9316SDarrick J. Wong {
1368871b9316SDarrick J. Wong 	struct xfs_trans	*tp;
1369871b9316SDarrick J. Wong 	struct xfs_mount	*mp = ip->i_mount;
1370871b9316SDarrick J. Wong 	unsigned int		resblks;
1371871b9316SDarrick J. Wong 	bool			retried = false;
1372871b9316SDarrick J. Wong 	int			error;
1373871b9316SDarrick J. Wong 
1374871b9316SDarrick J. Wong retry:
1375871b9316SDarrick J. Wong 	*nospace_error = 0;
1376871b9316SDarrick J. Wong 	resblks = *dblocks;
1377871b9316SDarrick J. Wong 	error = xfs_trans_alloc(mp, resv, resblks, 0, 0, &tp);
1378871b9316SDarrick J. Wong 	if (error == -ENOSPC) {
1379871b9316SDarrick J. Wong 		*nospace_error = error;
1380871b9316SDarrick J. Wong 		resblks = 0;
1381871b9316SDarrick J. Wong 		error = xfs_trans_alloc(mp, resv, resblks, 0, 0, &tp);
1382871b9316SDarrick J. Wong 	}
1383871b9316SDarrick J. Wong 	if (error)
1384871b9316SDarrick J. Wong 		return error;
1385871b9316SDarrick J. Wong 
1386871b9316SDarrick J. Wong 	xfs_lock_two_inodes(dp, XFS_ILOCK_EXCL, ip, XFS_ILOCK_EXCL);
1387871b9316SDarrick J. Wong 
1388871b9316SDarrick J. Wong 	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1389871b9316SDarrick J. Wong 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1390871b9316SDarrick J. Wong 
1391871b9316SDarrick J. Wong 	error = xfs_qm_dqattach_locked(dp, false);
1392871b9316SDarrick J. Wong 	if (error) {
1393871b9316SDarrick J. Wong 		/* Caller should have allocated the dquots! */
1394871b9316SDarrick J. Wong 		ASSERT(error != -ENOENT);
1395871b9316SDarrick J. Wong 		goto out_cancel;
1396871b9316SDarrick J. Wong 	}
1397871b9316SDarrick J. Wong 
1398871b9316SDarrick J. Wong 	error = xfs_qm_dqattach_locked(ip, false);
1399871b9316SDarrick J. Wong 	if (error) {
1400871b9316SDarrick J. Wong 		/* Caller should have allocated the dquots! */
1401871b9316SDarrick J. Wong 		ASSERT(error != -ENOENT);
1402871b9316SDarrick J. Wong 		goto out_cancel;
1403871b9316SDarrick J. Wong 	}
1404871b9316SDarrick J. Wong 
1405871b9316SDarrick J. Wong 	if (resblks == 0)
1406871b9316SDarrick J. Wong 		goto done;
1407871b9316SDarrick J. Wong 
1408871b9316SDarrick J. Wong 	error = xfs_trans_reserve_quota_nblks(tp, dp, resblks, 0, false);
1409871b9316SDarrick J. Wong 	if (error == -EDQUOT || error == -ENOSPC) {
1410871b9316SDarrick J. Wong 		if (!retried) {
1411871b9316SDarrick J. Wong 			xfs_trans_cancel(tp);
1412871b9316SDarrick J. Wong 			xfs_blockgc_free_quota(dp, 0);
1413871b9316SDarrick J. Wong 			retried = true;
1414871b9316SDarrick J. Wong 			goto retry;
1415871b9316SDarrick J. Wong 		}
1416871b9316SDarrick J. Wong 
1417871b9316SDarrick J. Wong 		*nospace_error = error;
1418871b9316SDarrick J. Wong 		resblks = 0;
1419871b9316SDarrick J. Wong 		error = 0;
1420871b9316SDarrick J. Wong 	}
1421871b9316SDarrick J. Wong 	if (error)
1422871b9316SDarrick J. Wong 		goto out_cancel;
1423871b9316SDarrick J. Wong 
1424871b9316SDarrick J. Wong done:
1425871b9316SDarrick J. Wong 	*tpp = tp;
1426871b9316SDarrick J. Wong 	*dblocks = resblks;
1427871b9316SDarrick J. Wong 	return 0;
1428871b9316SDarrick J. Wong 
1429871b9316SDarrick J. Wong out_cancel:
1430871b9316SDarrick J. Wong 	xfs_trans_cancel(tp);
1431871b9316SDarrick J. Wong 	return error;
1432871b9316SDarrick J. Wong }
1433