blob: 83f2032641cff56e91f84fdb764aafeaca1eab58 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
Christoph Hellwige98c4142010-06-23 18:11:15 +10003 * Copyright (C) 2010 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110021#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_format.h"
23#include "xfs_log_format.h"
24#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_inode.h"
Dave Chinnerefc27b52012-04-29 10:39:43 +000027#include "xfs_extent_busy.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_quota.h"
Dave Chinner239880e2013-10-23 10:50:10 +110029#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_trans_priv.h"
Dave Chinner239880e2013-10-23 10:50:10 +110031#include "xfs_log.h"
Dave Chinnered3b4d62010-05-21 12:07:08 +100032#include "xfs_trace.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110033#include "xfs_error.h"
Brian Fosterf8f28352018-05-07 17:38:47 -070034#include "xfs_defer.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Nathan Scott8f794052006-03-14 13:32:41 +110036kmem_zone_t *xfs_trans_zone;
Christoph Hellwige98c4142010-06-23 18:11:15 +100037kmem_zone_t *xfs_log_item_desc_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Darrick J. Wongb872af22018-01-08 10:51:26 -080039#if defined(CONFIG_TRACEPOINTS)
40static void
41xfs_trans_trace_reservations(
42 struct xfs_mount *mp)
43{
44 struct xfs_trans_res resv;
45 struct xfs_trans_res *res;
46 struct xfs_trans_res *end_res;
47 int i;
48
49 res = (struct xfs_trans_res *)M_RES(mp);
50 end_res = (struct xfs_trans_res *)(M_RES(mp) + 1);
51 for (i = 0; res < end_res; i++, res++)
52 trace_xfs_trans_resv_calc(mp, i, res);
53 xfs_log_get_max_trans_res(mp, &resv);
54 trace_xfs_trans_resv_calc(mp, -1, &resv);
55}
56#else
57# define xfs_trans_trace_reservations(mp)
58#endif
59
Jeff Liu4f3b5782013-01-28 21:25:35 +080060/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * Initialize the precomputed transaction reservation values
62 * in the mount structure.
63 */
64void
65xfs_trans_init(
Christoph Hellwig025101d2010-05-04 13:53:48 +000066 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Jie Liu3d3c8b52013-08-12 20:49:59 +100068 xfs_trans_resv_calc(mp, M_RES(mp));
Darrick J. Wongb872af22018-01-08 10:51:26 -080069 xfs_trans_trace_reservations(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
72/*
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +110073 * Free the transaction structure. If there is more clean up
74 * to do when the structure is freed, add it here.
75 */
76STATIC void
77xfs_trans_free(
Dave Chinnered3b4d62010-05-21 12:07:08 +100078 struct xfs_trans *tp)
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +110079{
Dave Chinner4ecbfe62012-04-29 10:41:10 +000080 xfs_extent_busy_sort(&tp->t_busy);
81 xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
Dave Chinnered3b4d62010-05-21 12:07:08 +100082
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +110083 atomic_dec(&tp->t_mountp->m_active_trans);
Christoph Hellwig253f4912016-04-06 09:19:55 +100084 if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
Jan Karad9457dc2012-06-12 16:20:39 +020085 sb_end_intwrite(tp->t_mountp->m_super);
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +110086 xfs_trans_free_dqinfo(tp);
87 kmem_zone_free(xfs_trans_zone, tp);
88}
89
90/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * This is called to create a new transaction which will share the
92 * permanent log reservation of the given transaction. The remaining
93 * unused block and rt extent reservations are also inherited. This
94 * implies that the original transaction is no longer allowed to allocate
95 * blocks. Locks and log items, however, are no inherited. They must
96 * be added to the new transaction explicitly.
97 */
Brian Fosterf8f28352018-05-07 17:38:47 -070098STATIC struct xfs_trans *
Linus Torvalds1da177e2005-04-16 15:20:36 -070099xfs_trans_dup(
Brian Fosterf8f28352018-05-07 17:38:47 -0700100 struct xfs_trans *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Brian Fosterf8f28352018-05-07 17:38:47 -0700102 struct xfs_trans *ntp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
105
106 /*
107 * Initialize the new transaction structure.
108 */
Dave Chinner2a3c0ac2013-08-12 20:49:28 +1000109 ntp->t_magic = XFS_TRANS_HEADER_MAGIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 ntp->t_mountp = tp->t_mountp;
Christoph Hellwige98c4142010-06-23 18:11:15 +1000111 INIT_LIST_HEAD(&ntp->t_items);
Dave Chinnered3b4d62010-05-21 12:07:08 +1000112 INIT_LIST_HEAD(&ntp->t_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 ASSERT(tp->t_ticket != NULL);
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100116
Jan Karad9457dc2012-06-12 16:20:39 +0200117 ntp->t_flags = XFS_TRANS_PERM_LOG_RES |
118 (tp->t_flags & XFS_TRANS_RESERVE) |
Christoph Hellwig253f4912016-04-06 09:19:55 +1000119 (tp->t_flags & XFS_TRANS_NO_WRITECOUNT);
Jan Karad9457dc2012-06-12 16:20:39 +0200120 /* We gave our writer reference to the new transaction */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000121 tp->t_flags |= XFS_TRANS_NO_WRITECOUNT;
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100122 ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
Brian Foster3e78b9a2018-03-09 14:01:58 -0800123
124 ASSERT(tp->t_blk_res >= tp->t_blk_res_used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
126 tp->t_blk_res = tp->t_blk_res_used;
Brian Foster3e78b9a2018-03-09 14:01:58 -0800127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
129 tp->t_rtx_res = tp->t_rtx_res_used;
Nathan Scott59c1b082006-06-09 14:59:13 +1000130 ntp->t_pflags = tp->t_pflags;
Brian Fosterf8f28352018-05-07 17:38:47 -0700131 ntp->t_agfl_dfops = tp->t_agfl_dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Christoph Hellwig7d095252009-06-08 15:33:32 +0200133 xfs_trans_dup_dqinfo(tp, ntp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 atomic_inc(&tp->t_mountp->m_active_trans);
136 return ntp;
137}
138
139/*
140 * This is called to reserve free disk blocks and log space for the
141 * given transaction. This must be done before allocating any resources
142 * within the transaction.
143 *
144 * This will return ENOSPC if there are not enough blocks available.
145 * It will sleep waiting for available log space.
146 * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
147 * is used by long running transactions. If any one of the reservations
148 * fails then they will all be backed out.
149 *
150 * This does not do quota reservations. That typically is done by the
151 * caller afterwards.
152 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000153static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154xfs_trans_reserve(
Jie Liu3d3c8b52013-08-12 20:49:59 +1000155 struct xfs_trans *tp,
156 struct xfs_trans_res *resp,
157 uint blocks,
158 uint rtextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Nathan Scott59c1b082006-06-09 14:59:13 +1000160 int error = 0;
Dave Chinner0d485ad2015-02-23 21:22:03 +1100161 bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 /* Mark this thread as being in a transaction */
Michal Hocko90707332017-05-03 14:53:12 -0700164 current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 /*
167 * Attempt to reserve the needed disk blocks by decrementing
168 * the number needed from the number available. This will
169 * fail if the count would go below zero.
170 */
171 if (blocks > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +1100172 error = xfs_mod_fdblocks(tp->t_mountp, -((int64_t)blocks), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (error != 0) {
Michal Hocko90707332017-05-03 14:53:12 -0700174 current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
Dave Chinner24513372014-06-25 14:58:08 +1000175 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177 tp->t_blk_res += blocks;
178 }
179
180 /*
181 * Reserve the log space needed for this transaction.
182 */
Jie Liu3d3c8b52013-08-12 20:49:59 +1000183 if (resp->tr_logres > 0) {
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000184 bool permanent = false;
185
Jie Liu3d3c8b52013-08-12 20:49:59 +1000186 ASSERT(tp->t_log_res == 0 ||
187 tp->t_log_res == resp->tr_logres);
188 ASSERT(tp->t_log_count == 0 ||
189 tp->t_log_count == resp->tr_logcount);
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000190
Jie Liu3d3c8b52013-08-12 20:49:59 +1000191 if (resp->tr_logflags & XFS_TRANS_PERM_LOG_RES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000193 permanent = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 } else {
195 ASSERT(tp->t_ticket == NULL);
196 ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000199 if (tp->t_ticket != NULL) {
Jie Liu3d3c8b52013-08-12 20:49:59 +1000200 ASSERT(resp->tr_logflags & XFS_TRANS_PERM_LOG_RES);
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000201 error = xfs_log_regrant(tp->t_mountp, tp->t_ticket);
202 } else {
Jie Liu3d3c8b52013-08-12 20:49:59 +1000203 error = xfs_log_reserve(tp->t_mountp,
204 resp->tr_logres,
205 resp->tr_logcount,
206 &tp->t_ticket, XFS_TRANSACTION,
Christoph Hellwig710b1e22016-04-06 09:20:36 +1000207 permanent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000209
210 if (error)
211 goto undo_blocks;
212
Jie Liu3d3c8b52013-08-12 20:49:59 +1000213 tp->t_log_res = resp->tr_logres;
214 tp->t_log_count = resp->tr_logcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
217 /*
218 * Attempt to reserve the needed realtime extents by decrementing
219 * the number needed from the number available. This will
220 * fail if the count would go below zero.
221 */
222 if (rtextents > 0) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +1100223 error = xfs_mod_frextents(tp->t_mountp, -((int64_t)rtextents));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +1000225 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 goto undo_log;
227 }
228 tp->t_rtx_res += rtextents;
229 }
230
231 return 0;
232
233 /*
234 * Error cases jump to one of these labels to undo any
235 * reservations which have already been performed.
236 */
237undo_log:
Jie Liu3d3c8b52013-08-12 20:49:59 +1000238 if (resp->tr_logres > 0) {
Christoph Hellwigf78c3902015-06-04 13:48:20 +1000239 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 tp->t_ticket = NULL;
241 tp->t_log_res = 0;
242 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
243 }
244
245undo_blocks:
246 if (blocks > 0) {
Eryu Guana27f6ef2016-09-14 07:39:07 +1000247 xfs_mod_fdblocks(tp->t_mountp, (int64_t)blocks, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 tp->t_blk_res = 0;
249 }
250
Michal Hocko90707332017-05-03 14:53:12 -0700251 current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Nathan Scott59c1b082006-06-09 14:59:13 +1000253 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Christoph Hellwig253f4912016-04-06 09:19:55 +1000256int
257xfs_trans_alloc(
258 struct xfs_mount *mp,
259 struct xfs_trans_res *resp,
260 uint blocks,
261 uint rtextents,
262 uint flags,
263 struct xfs_trans **tpp)
264{
265 struct xfs_trans *tp;
266 int error;
267
268 if (!(flags & XFS_TRANS_NO_WRITECOUNT))
269 sb_start_intwrite(mp->m_super);
270
271 WARN_ON(mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
272 atomic_inc(&mp->m_active_trans);
273
274 tp = kmem_zone_zalloc(xfs_trans_zone,
275 (flags & XFS_TRANS_NOFS) ? KM_NOFS : KM_SLEEP);
276 tp->t_magic = XFS_TRANS_HEADER_MAGIC;
277 tp->t_flags = flags;
278 tp->t_mountp = mp;
279 INIT_LIST_HEAD(&tp->t_items);
280 INIT_LIST_HEAD(&tp->t_busy);
281
282 error = xfs_trans_reserve(tp, resp, blocks, rtextents);
283 if (error) {
284 xfs_trans_cancel(tp);
285 return error;
286 }
287
288 *tpp = tp;
289 return 0;
290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*
Darrick J. Wonge89c0412017-03-28 14:56:37 -0700293 * Create an empty transaction with no reservation. This is a defensive
294 * mechanism for routines that query metadata without actually modifying
295 * them -- if the metadata being queried is somehow cross-linked (think a
296 * btree block pointer that points higher in the tree), we risk deadlock.
297 * However, blocks grabbed as part of a transaction can be re-grabbed.
298 * The verifiers will notice the corrupt block and the operation will fail
299 * back to userspace without deadlocking.
300 *
301 * Note the zero-length reservation; this transaction MUST be cancelled
302 * without any dirty data.
303 */
304int
305xfs_trans_alloc_empty(
306 struct xfs_mount *mp,
307 struct xfs_trans **tpp)
308{
309 struct xfs_trans_res resv = {0};
310
311 return xfs_trans_alloc(mp, &resv, 0, 0, XFS_TRANS_NO_WRITECOUNT, tpp);
312}
313
314/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 * Record the indicated change to the given field for application
316 * to the file system's superblock when the transaction commits.
317 * For now, just store the change in the transaction structure.
318 *
319 * Mark the transaction structure to indicate that the superblock
320 * needs to be updated before committing.
David Chinner92821e22007-05-24 15:26:31 +1000321 *
322 * Because we may not be keeping track of allocated/free inodes and
323 * used filesystem blocks in the superblock, we do not mark the
324 * superblock dirty in this transaction if we modify these fields.
325 * We still need to update the transaction deltas so that they get
326 * applied to the incore superblock, but we don't want them to
327 * cause the superblock to get locked and logged if these are the
328 * only fields in the superblock that the transaction modifies.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
330void
331xfs_trans_mod_sb(
332 xfs_trans_t *tp,
333 uint field,
David Chinner20f4ebf2007-02-10 18:36:10 +1100334 int64_t delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
David Chinner92821e22007-05-24 15:26:31 +1000336 uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
337 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 switch (field) {
340 case XFS_TRANS_SB_ICOUNT:
341 tp->t_icount_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000342 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
343 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 break;
345 case XFS_TRANS_SB_IFREE:
346 tp->t_ifree_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000347 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
348 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 break;
350 case XFS_TRANS_SB_FDBLOCKS:
351 /*
Brian Foster3e78b9a2018-03-09 14:01:58 -0800352 * Track the number of blocks allocated in the transaction.
353 * Make sure it does not exceed the number reserved. If so,
354 * shutdown as this can lead to accounting inconsistency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
356 if (delta < 0) {
357 tp->t_blk_res_used += (uint)-delta;
Brian Foster3e78b9a2018-03-09 14:01:58 -0800358 if (tp->t_blk_res_used > tp->t_blk_res)
359 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361 tp->t_fdblocks_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000362 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
363 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 break;
365 case XFS_TRANS_SB_RES_FDBLOCKS:
366 /*
367 * The allocation has already been applied to the
368 * in-core superblock's counter. This should only
369 * be applied to the on-disk superblock.
370 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 tp->t_res_fdblocks_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000372 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
373 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 break;
375 case XFS_TRANS_SB_FREXTENTS:
376 /*
377 * Track the number of blocks allocated in the
378 * transaction. Make sure it does not exceed the
379 * number reserved.
380 */
381 if (delta < 0) {
382 tp->t_rtx_res_used += (uint)-delta;
383 ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
384 }
385 tp->t_frextents_delta += delta;
386 break;
387 case XFS_TRANS_SB_RES_FREXTENTS:
388 /*
389 * The allocation has already been applied to the
Nathan Scottc41564b2006-03-29 08:55:14 +1000390 * in-core superblock's counter. This should only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 * be applied to the on-disk superblock.
392 */
393 ASSERT(delta < 0);
394 tp->t_res_frextents_delta += delta;
395 break;
396 case XFS_TRANS_SB_DBLOCKS:
397 ASSERT(delta > 0);
398 tp->t_dblocks_delta += delta;
399 break;
400 case XFS_TRANS_SB_AGCOUNT:
401 ASSERT(delta > 0);
402 tp->t_agcount_delta += delta;
403 break;
404 case XFS_TRANS_SB_IMAXPCT:
405 tp->t_imaxpct_delta += delta;
406 break;
407 case XFS_TRANS_SB_REXTSIZE:
408 tp->t_rextsize_delta += delta;
409 break;
410 case XFS_TRANS_SB_RBMBLOCKS:
411 tp->t_rbmblocks_delta += delta;
412 break;
413 case XFS_TRANS_SB_RBLOCKS:
414 tp->t_rblocks_delta += delta;
415 break;
416 case XFS_TRANS_SB_REXTENTS:
417 tp->t_rextents_delta += delta;
418 break;
419 case XFS_TRANS_SB_REXTSLOG:
420 tp->t_rextslog_delta += delta;
421 break;
422 default:
423 ASSERT(0);
424 return;
425 }
426
David Chinner210c6f12007-05-24 15:26:51 +1000427 tp->t_flags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430/*
431 * xfs_trans_apply_sb_deltas() is called from the commit code
432 * to bring the superblock buffer into the current transaction
433 * and modify it as requested by earlier calls to xfs_trans_mod_sb().
434 *
435 * For now we just look at each field allowed to change and change
436 * it if necessary.
437 */
438STATIC void
439xfs_trans_apply_sb_deltas(
440 xfs_trans_t *tp)
441{
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000442 xfs_dsb_t *sbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 xfs_buf_t *bp;
444 int whole = 0;
445
446 bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
447 sbp = XFS_BUF_TO_SBP(bp);
448
449 /*
450 * Check that superblock mods match the mods made to AGF counters.
451 */
452 ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
453 (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
454 tp->t_ag_btree_delta));
455
David Chinner92821e22007-05-24 15:26:31 +1000456 /*
457 * Only update the superblock counters if we are logging them
458 */
459 if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000460 if (tp->t_icount_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800461 be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000462 if (tp->t_ifree_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800463 be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000464 if (tp->t_fdblocks_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800465 be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000466 if (tp->t_res_fdblocks_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800467 be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000470 if (tp->t_frextents_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800471 be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000472 if (tp->t_res_frextents_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800473 be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000474
475 if (tp->t_dblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800476 be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 whole = 1;
478 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000479 if (tp->t_agcount_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800480 be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 whole = 1;
482 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000483 if (tp->t_imaxpct_delta) {
484 sbp->sb_imax_pct += tp->t_imaxpct_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 whole = 1;
486 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000487 if (tp->t_rextsize_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800488 be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 whole = 1;
490 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000491 if (tp->t_rbmblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800492 be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 whole = 1;
494 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000495 if (tp->t_rblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800496 be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 whole = 1;
498 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000499 if (tp->t_rextents_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800500 be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 whole = 1;
502 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000503 if (tp->t_rextslog_delta) {
504 sbp->sb_rextslog += tp->t_rextslog_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 whole = 1;
506 }
507
Dave Chinner3443a3b2015-01-22 09:30:23 +1100508 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (whole)
510 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000511 * Log the whole thing, the fields are noncontiguous.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000513 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 else
515 /*
516 * Since all the modifiable fields are contiguous, we
517 * can get away with this.
518 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000519 xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
520 offsetof(xfs_dsb_t, sb_frextents) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 sizeof(sbp->sb_frextents) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
Dave Chinner0bd5dded2015-02-23 21:24:11 +1100524STATIC int
525xfs_sb_mod8(
526 uint8_t *field,
527 int8_t delta)
528{
529 int8_t counter = *field;
530
531 counter += delta;
532 if (counter < 0) {
533 ASSERT(0);
534 return -EINVAL;
535 }
536 *field = counter;
537 return 0;
538}
539
540STATIC int
541xfs_sb_mod32(
542 uint32_t *field,
543 int32_t delta)
544{
545 int32_t counter = *field;
546
547 counter += delta;
548 if (counter < 0) {
549 ASSERT(0);
550 return -EINVAL;
551 }
552 *field = counter;
553 return 0;
554}
555
556STATIC int
557xfs_sb_mod64(
558 uint64_t *field,
559 int64_t delta)
560{
561 int64_t counter = *field;
562
563 counter += delta;
564 if (counter < 0) {
565 ASSERT(0);
566 return -EINVAL;
567 }
568 *field = counter;
569 return 0;
570}
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/*
David Chinner45c34142007-06-18 16:49:44 +1000573 * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
574 * and apply superblock counter changes to the in-core superblock. The
575 * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
576 * applied to the in-core superblock. The idea is that that has already been
577 * done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 *
David Chinner45c34142007-06-18 16:49:44 +1000579 * If we are not logging superblock counters, then the inode allocated/free and
580 * used block counts are not updated in the on disk superblock. In this case,
581 * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
582 * still need to update the incore superblock with the changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 */
Dave Chinner71e330b2010-05-21 14:37:18 +1000584void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585xfs_trans_unreserve_and_mod_sb(
Dave Chinner0bd5dded2015-02-23 21:24:11 +1100586 struct xfs_trans *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Dave Chinner0bd5dded2015-02-23 21:24:11 +1100588 struct xfs_mount *mp = tp->t_mountp;
589 bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
590 int64_t blkdelta = 0;
591 int64_t rtxdelta = 0;
592 int64_t idelta = 0;
593 int64_t ifreedelta = 0;
594 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Christoph Hellwig1b040712010-09-30 02:25:56 +0000596 /* calculate deltas */
David Chinner45c34142007-06-18 16:49:44 +1000597 if (tp->t_blk_res > 0)
598 blkdelta = tp->t_blk_res;
David Chinner45c34142007-06-18 16:49:44 +1000599 if ((tp->t_fdblocks_delta != 0) &&
600 (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
601 (tp->t_flags & XFS_TRANS_SB_DIRTY)))
602 blkdelta += tp->t_fdblocks_delta;
603
David Chinner45c34142007-06-18 16:49:44 +1000604 if (tp->t_rtx_res > 0)
605 rtxdelta = tp->t_rtx_res;
David Chinner45c34142007-06-18 16:49:44 +1000606 if ((tp->t_frextents_delta != 0) &&
607 (tp->t_flags & XFS_TRANS_SB_DIRTY))
608 rtxdelta += tp->t_frextents_delta;
609
Christoph Hellwig1b040712010-09-30 02:25:56 +0000610 if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
611 (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
612 idelta = tp->t_icount_delta;
613 ifreedelta = tp->t_ifree_delta;
614 }
615
616 /* apply the per-cpu counters */
617 if (blkdelta) {
Dave Chinner0d485ad2015-02-23 21:22:03 +1100618 error = xfs_mod_fdblocks(mp, blkdelta, rsvd);
Christoph Hellwig1b040712010-09-30 02:25:56 +0000619 if (error)
620 goto out;
621 }
622
623 if (idelta) {
Dave Chinner501ab322015-02-23 21:19:28 +1100624 error = xfs_mod_icount(mp, idelta);
Christoph Hellwig1b040712010-09-30 02:25:56 +0000625 if (error)
626 goto out_undo_fdblocks;
627 }
628
629 if (ifreedelta) {
Dave Chinnere88b64e2015-02-23 21:19:53 +1100630 error = xfs_mod_ifree(mp, ifreedelta);
Christoph Hellwig1b040712010-09-30 02:25:56 +0000631 if (error)
632 goto out_undo_icount;
633 }
634
Dave Chinner0bd5dded2015-02-23 21:24:11 +1100635 if (rtxdelta == 0 && !(tp->t_flags & XFS_TRANS_SB_DIRTY))
636 return;
637
Christoph Hellwig1b040712010-09-30 02:25:56 +0000638 /* apply remaining deltas */
Dave Chinner0bd5dded2015-02-23 21:24:11 +1100639 spin_lock(&mp->m_sb_lock);
Dave Chinnerbab98bb2015-02-23 21:22:54 +1100640 if (rtxdelta) {
Dave Chinner0bd5dded2015-02-23 21:24:11 +1100641 error = xfs_sb_mod64(&mp->m_sb.sb_frextents, rtxdelta);
Dave Chinnerbab98bb2015-02-23 21:22:54 +1100642 if (error)
643 goto out_undo_ifree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
Dave Chinner0bd5dded2015-02-23 21:24:11 +1100646 if (tp->t_dblocks_delta != 0) {
647 error = xfs_sb_mod64(&mp->m_sb.sb_dblocks, tp->t_dblocks_delta);
Christoph Hellwig1b040712010-09-30 02:25:56 +0000648 if (error)
Dave Chinnerbab98bb2015-02-23 21:22:54 +1100649 goto out_undo_frextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
Dave Chinner0bd5dded2015-02-23 21:24:11 +1100651 if (tp->t_agcount_delta != 0) {
652 error = xfs_sb_mod32(&mp->m_sb.sb_agcount, tp->t_agcount_delta);
653 if (error)
654 goto out_undo_dblocks;
655 }
656 if (tp->t_imaxpct_delta != 0) {
657 error = xfs_sb_mod8(&mp->m_sb.sb_imax_pct, tp->t_imaxpct_delta);
658 if (error)
659 goto out_undo_agcount;
660 }
661 if (tp->t_rextsize_delta != 0) {
662 error = xfs_sb_mod32(&mp->m_sb.sb_rextsize,
663 tp->t_rextsize_delta);
664 if (error)
665 goto out_undo_imaxpct;
666 }
667 if (tp->t_rbmblocks_delta != 0) {
668 error = xfs_sb_mod32(&mp->m_sb.sb_rbmblocks,
669 tp->t_rbmblocks_delta);
670 if (error)
671 goto out_undo_rextsize;
672 }
673 if (tp->t_rblocks_delta != 0) {
674 error = xfs_sb_mod64(&mp->m_sb.sb_rblocks, tp->t_rblocks_delta);
675 if (error)
676 goto out_undo_rbmblocks;
677 }
678 if (tp->t_rextents_delta != 0) {
679 error = xfs_sb_mod64(&mp->m_sb.sb_rextents,
680 tp->t_rextents_delta);
681 if (error)
682 goto out_undo_rblocks;
683 }
684 if (tp->t_rextslog_delta != 0) {
685 error = xfs_sb_mod8(&mp->m_sb.sb_rextslog,
686 tp->t_rextslog_delta);
687 if (error)
688 goto out_undo_rextents;
689 }
690 spin_unlock(&mp->m_sb_lock);
Christoph Hellwig1b040712010-09-30 02:25:56 +0000691 return;
692
Dave Chinner0bd5dded2015-02-23 21:24:11 +1100693out_undo_rextents:
694 if (tp->t_rextents_delta)
695 xfs_sb_mod64(&mp->m_sb.sb_rextents, -tp->t_rextents_delta);
696out_undo_rblocks:
697 if (tp->t_rblocks_delta)
698 xfs_sb_mod64(&mp->m_sb.sb_rblocks, -tp->t_rblocks_delta);
699out_undo_rbmblocks:
700 if (tp->t_rbmblocks_delta)
701 xfs_sb_mod32(&mp->m_sb.sb_rbmblocks, -tp->t_rbmblocks_delta);
702out_undo_rextsize:
703 if (tp->t_rextsize_delta)
704 xfs_sb_mod32(&mp->m_sb.sb_rextsize, -tp->t_rextsize_delta);
705out_undo_imaxpct:
706 if (tp->t_rextsize_delta)
707 xfs_sb_mod8(&mp->m_sb.sb_imax_pct, -tp->t_imaxpct_delta);
708out_undo_agcount:
709 if (tp->t_agcount_delta)
710 xfs_sb_mod32(&mp->m_sb.sb_agcount, -tp->t_agcount_delta);
711out_undo_dblocks:
712 if (tp->t_dblocks_delta)
713 xfs_sb_mod64(&mp->m_sb.sb_dblocks, -tp->t_dblocks_delta);
Dave Chinnerbab98bb2015-02-23 21:22:54 +1100714out_undo_frextents:
715 if (rtxdelta)
Dave Chinner0bd5dded2015-02-23 21:24:11 +1100716 xfs_sb_mod64(&mp->m_sb.sb_frextents, -rtxdelta);
Dave Chinnerbab98bb2015-02-23 21:22:54 +1100717out_undo_ifree:
Dave Chinner0bd5dded2015-02-23 21:24:11 +1100718 spin_unlock(&mp->m_sb_lock);
Christoph Hellwig1b040712010-09-30 02:25:56 +0000719 if (ifreedelta)
Dave Chinnere88b64e2015-02-23 21:19:53 +1100720 xfs_mod_ifree(mp, -ifreedelta);
Christoph Hellwig1b040712010-09-30 02:25:56 +0000721out_undo_icount:
722 if (idelta)
Dave Chinner501ab322015-02-23 21:19:28 +1100723 xfs_mod_icount(mp, -idelta);
Christoph Hellwig1b040712010-09-30 02:25:56 +0000724out_undo_fdblocks:
725 if (blkdelta)
Dave Chinner0d485ad2015-02-23 21:22:03 +1100726 xfs_mod_fdblocks(mp, -blkdelta, rsvd);
Christoph Hellwig1b040712010-09-30 02:25:56 +0000727out:
Jesper Juhl1884bd82010-12-25 20:14:53 +0000728 ASSERT(error == 0);
Christoph Hellwig1b040712010-09-30 02:25:56 +0000729 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Dave Chinner09243782010-03-08 11:28:28 +1100732/*
Christoph Hellwige98c4142010-06-23 18:11:15 +1000733 * Add the given log item to the transaction's list of log items.
734 *
735 * The log item will now point to its new descriptor with its li_desc field.
736 */
737void
738xfs_trans_add_item(
739 struct xfs_trans *tp,
740 struct xfs_log_item *lip)
741{
742 struct xfs_log_item_desc *lidp;
743
Jesper Juhlf65020a2012-02-13 20:51:05 +0000744 ASSERT(lip->li_mountp == tp->t_mountp);
745 ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
Christoph Hellwige98c4142010-06-23 18:11:15 +1000746
Dave Chinner43869702010-07-20 17:53:44 +1000747 lidp = kmem_zone_zalloc(xfs_log_item_desc_zone, KM_SLEEP | KM_NOFS);
Christoph Hellwige98c4142010-06-23 18:11:15 +1000748
749 lidp->lid_item = lip;
750 lidp->lid_flags = 0;
Christoph Hellwige98c4142010-06-23 18:11:15 +1000751 list_add_tail(&lidp->lid_trans, &tp->t_items);
752
753 lip->li_desc = lidp;
754}
755
756STATIC void
757xfs_trans_free_item_desc(
758 struct xfs_log_item_desc *lidp)
759{
760 list_del_init(&lidp->lid_trans);
761 kmem_zone_free(xfs_log_item_desc_zone, lidp);
762}
763
764/*
765 * Unlink and free the given descriptor.
766 */
767void
768xfs_trans_del_item(
769 struct xfs_log_item *lip)
770{
771 xfs_trans_free_item_desc(lip->li_desc);
772 lip->li_desc = NULL;
773}
774
775/*
776 * Unlock all of the items of a transaction and free all the descriptors
777 * of that transaction.
778 */
Dave Chinnerd17c7012010-08-24 11:42:52 +1000779void
Christoph Hellwige98c4142010-06-23 18:11:15 +1000780xfs_trans_free_items(
781 struct xfs_trans *tp,
782 xfs_lsn_t commit_lsn,
Christoph Hellwigeacb24e2015-06-04 13:47:43 +1000783 bool abort)
Christoph Hellwige98c4142010-06-23 18:11:15 +1000784{
785 struct xfs_log_item_desc *lidp, *next;
786
787 list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
788 struct xfs_log_item *lip = lidp->lid_item;
789
790 lip->li_desc = NULL;
791
792 if (commit_lsn != NULLCOMMITLSN)
Dave Chinner904c17e2013-08-28 21:12:03 +1000793 lip->li_ops->iop_committing(lip, commit_lsn);
Christoph Hellwigeacb24e2015-06-04 13:47:43 +1000794 if (abort)
Dave Chinner22525c12018-05-09 07:47:34 -0700795 set_bit(XFS_LI_ABORTED, &lip->li_flags);
Dave Chinner904c17e2013-08-28 21:12:03 +1000796 lip->li_ops->iop_unlock(lip);
Christoph Hellwige98c4142010-06-23 18:11:15 +1000797
798 xfs_trans_free_item_desc(lidp);
799 }
800}
801
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100802static inline void
803xfs_log_item_batch_insert(
804 struct xfs_ail *ailp,
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000805 struct xfs_ail_cursor *cur,
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100806 struct xfs_log_item **log_items,
807 int nr_items,
808 xfs_lsn_t commit_lsn)
809{
810 int i;
811
Matthew Wilcox57e80952018-03-07 14:59:39 -0800812 spin_lock(&ailp->ail_lock);
813 /* xfs_trans_ail_update_bulk drops ailp->ail_lock */
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000814 xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn);
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100815
Dave Chinner904c17e2013-08-28 21:12:03 +1000816 for (i = 0; i < nr_items; i++) {
817 struct xfs_log_item *lip = log_items[i];
818
819 lip->li_ops->iop_unpin(lip, 0);
820 }
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100821}
822
823/*
824 * Bulk operation version of xfs_trans_committed that takes a log vector of
825 * items to insert into the AIL. This uses bulk AIL insertion techniques to
826 * minimise lock traffic.
Dave Chinnere34a3142011-01-27 12:13:35 +1100827 *
828 * If we are called with the aborted flag set, it is because a log write during
829 * a CIL checkpoint commit has failed. In this case, all the items in the
Dave Chinner904c17e2013-08-28 21:12:03 +1000830 * checkpoint have already gone through iop_commited and iop_unlock, which
Dave Chinnere34a3142011-01-27 12:13:35 +1100831 * means that checkpoint commit abort handling is treated exactly the same
832 * as an iclog write error even though we haven't started any IO yet. Hence in
Dave Chinner904c17e2013-08-28 21:12:03 +1000833 * this case all we need to do is iop_committed processing, followed by an
834 * iop_unpin(aborted) call.
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000835 *
836 * The AIL cursor is used to optimise the insert process. If commit_lsn is not
837 * at the end of the AIL, the insert cursor avoids the need to walk
838 * the AIL to find the insertion point on every xfs_log_item_batch_insert()
839 * call. This saves a lot of needless list walking and is a net win, even
840 * though it slightly increases that amount of AIL lock traffic to set it up
841 * and tear it down.
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100842 */
843void
844xfs_trans_committed_bulk(
845 struct xfs_ail *ailp,
846 struct xfs_log_vec *log_vector,
847 xfs_lsn_t commit_lsn,
848 int aborted)
849{
850#define LOG_ITEM_BATCH_SIZE 32
851 struct xfs_log_item *log_items[LOG_ITEM_BATCH_SIZE];
852 struct xfs_log_vec *lv;
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000853 struct xfs_ail_cursor cur;
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100854 int i = 0;
855
Matthew Wilcox57e80952018-03-07 14:59:39 -0800856 spin_lock(&ailp->ail_lock);
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000857 xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800858 spin_unlock(&ailp->ail_lock);
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000859
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100860 /* unpin all the log items */
861 for (lv = log_vector; lv; lv = lv->lv_next ) {
862 struct xfs_log_item *lip = lv->lv_item;
863 xfs_lsn_t item_lsn;
864
865 if (aborted)
Dave Chinner22525c12018-05-09 07:47:34 -0700866 set_bit(XFS_LI_ABORTED, &lip->li_flags);
Dave Chinner904c17e2013-08-28 21:12:03 +1000867 item_lsn = lip->li_ops->iop_committed(lip, commit_lsn);
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100868
Dave Chinner1316d4d2011-07-04 05:27:36 +0000869 /* item_lsn of -1 means the item needs no further processing */
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100870 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
871 continue;
872
Dave Chinnere34a3142011-01-27 12:13:35 +1100873 /*
874 * if we are aborting the operation, no point in inserting the
875 * object into the AIL as we are in a shutdown situation.
876 */
877 if (aborted) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800878 ASSERT(XFS_FORCED_SHUTDOWN(ailp->ail_mount));
Dave Chinner904c17e2013-08-28 21:12:03 +1000879 lip->li_ops->iop_unpin(lip, 1);
Dave Chinnere34a3142011-01-27 12:13:35 +1100880 continue;
881 }
882
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100883 if (item_lsn != commit_lsn) {
884
885 /*
886 * Not a bulk update option due to unusual item_lsn.
887 * Push into AIL immediately, rechecking the lsn once
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000888 * we have the ail lock. Then unpin the item. This does
889 * not affect the AIL cursor the bulk insert path is
890 * using.
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100891 */
Matthew Wilcox57e80952018-03-07 14:59:39 -0800892 spin_lock(&ailp->ail_lock);
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100893 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
894 xfs_trans_ail_update(ailp, lip, item_lsn);
895 else
Matthew Wilcox57e80952018-03-07 14:59:39 -0800896 spin_unlock(&ailp->ail_lock);
Dave Chinner904c17e2013-08-28 21:12:03 +1000897 lip->li_ops->iop_unpin(lip, 0);
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100898 continue;
899 }
900
901 /* Item is a candidate for bulk AIL insert. */
902 log_items[i++] = lv->lv_item;
903 if (i >= LOG_ITEM_BATCH_SIZE) {
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000904 xfs_log_item_batch_insert(ailp, &cur, log_items,
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100905 LOG_ITEM_BATCH_SIZE, commit_lsn);
906 i = 0;
907 }
908 }
909
910 /* make sure we insert the remainder! */
911 if (i)
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000912 xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
913
Matthew Wilcox57e80952018-03-07 14:59:39 -0800914 spin_lock(&ailp->ail_lock);
Eric Sandeene4a1e292014-04-14 19:06:05 +1000915 xfs_trans_ail_cursor_done(&cur);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800916 spin_unlock(&ailp->ail_lock);
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100917}
918
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100919/*
Christoph Hellwigb1037052011-09-19 14:55:51 +0000920 * Commit the given transaction to the log.
Dave Chinner09243782010-03-08 11:28:28 +1100921 *
922 * XFS disk error handling mechanism is not based on a typical
923 * transaction abort mechanism. Logically after the filesystem
924 * gets marked 'SHUTDOWN', we can't let any new transactions
925 * be durable - ie. committed to disk - because some metadata might
926 * be inconsistent. In such cases, this returns an error, and the
927 * caller may assume that all locked objects joined to the transaction
928 * have already been unlocked as if the commit had succeeded.
929 * Do not reference the transaction structure after this call.
930 */
Christoph Hellwig70393312015-06-04 13:48:08 +1000931static int
932__xfs_trans_commit(
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +1100933 struct xfs_trans *tp,
Christoph Hellwig70393312015-06-04 13:48:08 +1000934 bool regrant)
Dave Chinner09243782010-03-08 11:28:28 +1100935{
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +1100936 struct xfs_mount *mp = tp->t_mountp;
Dave Chinner09243782010-03-08 11:28:28 +1100937 xfs_lsn_t commit_lsn = -1;
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +1100938 int error = 0;
Dave Chinner09243782010-03-08 11:28:28 +1100939 int sync = tp->t_flags & XFS_TRANS_SYNC;
Dave Chinner09243782010-03-08 11:28:28 +1100940
Brian Fosterf8f28352018-05-07 17:38:47 -0700941 ASSERT(!tp->t_agfl_dfops ||
942 !xfs_defer_has_unfinished_work(tp->t_agfl_dfops) || regrant);
943
Dave Chinner09243782010-03-08 11:28:28 +1100944 /*
Dave Chinner09243782010-03-08 11:28:28 +1100945 * If there is nothing to be logged by the transaction,
946 * then unlock all of the items associated with the
947 * transaction and free the transaction structure.
948 * Also make sure to return any reserved blocks to
949 * the free pool.
950 */
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +1100951 if (!(tp->t_flags & XFS_TRANS_DIRTY))
952 goto out_unreserve;
953
954 if (XFS_FORCED_SHUTDOWN(mp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000955 error = -EIO;
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +1100956 goto out_unreserve;
Dave Chinner09243782010-03-08 11:28:28 +1100957 }
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +1100958
Dave Chinner09243782010-03-08 11:28:28 +1100959 ASSERT(tp->t_ticket != NULL);
960
961 /*
962 * If we need to update the superblock, then do it now.
963 */
964 if (tp->t_flags & XFS_TRANS_SB_DIRTY)
965 xfs_trans_apply_sb_deltas(tp);
966 xfs_trans_apply_dquot_deltas(tp);
967
Christoph Hellwig70393312015-06-04 13:48:08 +1000968 xfs_log_commit_cil(mp, tp, &commit_lsn, regrant);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Michal Hocko90707332017-05-03 14:53:12 -0700970 current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
Christoph Hellwig0244b962011-12-06 21:58:08 +0000971 xfs_trans_free(tp);
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /*
974 * If the transaction needs to be synchronous, then force the
975 * log out now and wait for it.
976 */
977 if (sync) {
Christoph Hellwig656de4f2018-03-13 23:15:28 -0700978 error = xfs_log_force_lsn(mp, commit_lsn, XFS_LOG_SYNC, NULL);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100979 XFS_STATS_INC(mp, xs_trans_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 } else {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100981 XFS_STATS_INC(mp, xs_trans_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +1100984 return error;
985
986out_unreserve:
987 xfs_trans_unreserve_and_mod_sb(tp);
988
989 /*
990 * It is indeed possible for the transaction to be not dirty but
991 * the dqinfo portion to be. All that means is that we have some
992 * (non-persistent) quota reservations that need to be unreserved.
993 */
994 xfs_trans_unreserve_and_mod_dquots(tp);
995 if (tp->t_ticket) {
Christoph Hellwigf78c3902015-06-04 13:48:20 +1000996 commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, regrant);
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +1100997 if (commit_lsn == -1 && !error)
Dave Chinner24513372014-06-25 14:58:08 +1000998 error = -EIO;
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +1100999 }
Michal Hocko90707332017-05-03 14:53:12 -07001000 current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
Christoph Hellwigeacb24e2015-06-04 13:47:43 +10001001 xfs_trans_free_items(tp, NULLCOMMITLSN, !!error);
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001002 xfs_trans_free(tp);
1003
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001004 XFS_STATS_INC(mp, xs_trans_empty);
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001005 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
Christoph Hellwig70393312015-06-04 13:48:08 +10001008int
1009xfs_trans_commit(
1010 struct xfs_trans *tp)
1011{
1012 return __xfs_trans_commit(tp, false);
1013}
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 * Unlock all of the transaction's items and free the transaction.
1017 * The transaction must not have modified any of its items, because
1018 * there is no way to restore them to their previous state.
1019 *
1020 * If the transaction has made a log reservation, make sure to release
1021 * it as well.
1022 */
1023void
1024xfs_trans_cancel(
Christoph Hellwig4906e212015-06-04 13:47:56 +10001025 struct xfs_trans *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Christoph Hellwig4906e212015-06-04 13:47:56 +10001027 struct xfs_mount *mp = tp->t_mountp;
1028 bool dirty = (tp->t_flags & XFS_TRANS_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 * See if the caller is relying on us to shut down the
1032 * filesystem. This happens in paths where we detect
1033 * corruption and decide to give up.
1034 */
Christoph Hellwig4906e212015-06-04 13:47:56 +10001035 if (dirty && !XFS_FORCED_SHUTDOWN(mp)) {
Ryan Hankins0733af22006-01-11 15:36:44 +11001036 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
Nathan Scott7d04a332006-06-09 14:58:38 +10001037 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Nathan Scott60a204f2006-01-11 15:37:00 +11001038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039#ifdef DEBUG
Christoph Hellwig4906e212015-06-04 13:47:56 +10001040 if (!dirty && !XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwige98c4142010-06-23 18:11:15 +10001041 struct xfs_log_item_desc *lidp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Christoph Hellwige98c4142010-06-23 18:11:15 +10001043 list_for_each_entry(lidp, &tp->t_items, lid_trans)
1044 ASSERT(!(lidp->lid_item->li_type == XFS_LI_EFD));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046#endif
1047 xfs_trans_unreserve_and_mod_sb(tp);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001048 xfs_trans_unreserve_and_mod_dquots(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Christoph Hellwigf78c3902015-06-04 13:48:20 +10001050 if (tp->t_ticket)
1051 xfs_log_done(mp, tp->t_ticket, NULL, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 /* mark this thread as no longer being in a transaction */
Michal Hocko90707332017-05-03 14:53:12 -07001054 current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Christoph Hellwig4906e212015-06-04 13:47:56 +10001056 xfs_trans_free_items(tp, NULLCOMMITLSN, dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 xfs_trans_free(tp);
1058}
1059
Niv Sardi322ff6b2008-08-13 16:05:49 +10001060/*
1061 * Roll from one trans in the sequence of PERMANENT transactions to
1062 * the next: permanent transactions are only flushed out when
Christoph Hellwig70393312015-06-04 13:48:08 +10001063 * committed with xfs_trans_commit(), but we still want as soon
Niv Sardi322ff6b2008-08-13 16:05:49 +10001064 * as possible to let chunks of it go to the log. So we commit the
1065 * chunk we've been working on and get a new transaction to continue.
1066 */
1067int
Christoph Hellwig254133f2017-04-06 16:00:11 -07001068xfs_trans_roll(
Christoph Hellwig411350d2017-08-28 10:21:03 -07001069 struct xfs_trans **tpp)
Niv Sardi322ff6b2008-08-13 16:05:49 +10001070{
Christoph Hellwig411350d2017-08-28 10:21:03 -07001071 struct xfs_trans *trans = *tpp;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001072 struct xfs_trans_res tres;
Niv Sardi322ff6b2008-08-13 16:05:49 +10001073 int error;
1074
1075 /*
Niv Sardi322ff6b2008-08-13 16:05:49 +10001076 * Copy the critical parameters from one trans to the next.
1077 */
Jie Liu3d3c8b52013-08-12 20:49:59 +10001078 tres.tr_logres = trans->t_log_res;
1079 tres.tr_logcount = trans->t_log_count;
Christoph Hellwig411350d2017-08-28 10:21:03 -07001080
Niv Sardi322ff6b2008-08-13 16:05:49 +10001081 *tpp = xfs_trans_dup(trans);
1082
1083 /*
1084 * Commit the current transaction.
1085 * If this commit failed, then it'd just unlock those items that
1086 * are not marked ihold. That also means that a filesystem shutdown
1087 * is in progress. The caller takes the responsibility to cancel
1088 * the duplicate transaction that gets returned.
1089 */
Christoph Hellwig70393312015-06-04 13:48:08 +10001090 error = __xfs_trans_commit(trans, true);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001091 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001092 return error;
Niv Sardi322ff6b2008-08-13 16:05:49 +10001093
Niv Sardi322ff6b2008-08-13 16:05:49 +10001094 /*
Christoph Hellwig411350d2017-08-28 10:21:03 -07001095 * Reserve space in the log for the next transaction.
Niv Sardi322ff6b2008-08-13 16:05:49 +10001096 * This also pushes items in the "AIL", the list of logged items,
1097 * out to disk if they are taking up space at the tail of the log
1098 * that we want to use. This requires that either nothing be locked
1099 * across this call, or that anything that is locked be logged in
1100 * the prior and the next transactions.
1101 */
Jie Liu3d3c8b52013-08-12 20:49:59 +10001102 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
Christoph Hellwig411350d2017-08-28 10:21:03 -07001103 return xfs_trans_reserve(*tpp, &tres, 0, 0);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001104}