blob: 3fa8789820ad9d4e5014aa4e00b1e4c3dc3ee874 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11007#include "xfs_fs.h"
Christoph Hellwig4fb6e8a2014-11-28 14:25:04 +11008#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_log_format.h"
10#include "xfs_trans_resv.h"
Darrick J. Wongdc42375d2016-08-03 11:23:49 +100011#include "xfs_bit.h"
Darrick J. Wong5467b342019-06-28 19:25:35 -070012#include "xfs_shared.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_mount.h"
Dave Chinner08d3e842022-07-07 19:07:40 +100014#include "xfs_ag.h"
Christoph Hellwig81f40042019-06-28 19:28:17 -070015#include "xfs_defer.h"
Dave Chinner239880e2013-10-23 10:50:10 +110016#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "xfs_trans_priv.h"
18#include "xfs_extfree_item.h"
Christoph Hellwig12343512013-12-13 11:00:43 +110019#include "xfs_log.h"
Darrick J. Wong340785c2016-08-03 11:33:42 +100020#include "xfs_btree.h"
21#include "xfs_rmap.h"
Christoph Hellwig81f40042019-06-28 19:28:17 -070022#include "xfs_alloc.h"
23#include "xfs_bmap.h"
24#include "xfs_trace.h"
Darrick J. Wonga5155b82019-11-02 09:40:53 -070025#include "xfs_error.h"
Darrick J. Wong9817aa82020-05-01 16:00:48 -070026#include "xfs_log_priv.h"
Darrick J. Wong86ffa472020-05-01 16:00:45 -070027#include "xfs_log_recover.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Darrick J. Wong182696f2021-10-12 11:09:23 -070029struct kmem_cache *xfs_efi_cache;
30struct kmem_cache *xfs_efd_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Darrick J. Wong10d0c6e2020-05-01 16:00:50 -070032static const struct xfs_item_ops xfs_efi_item_ops;
33
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100034static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
35{
36 return container_of(lip, struct xfs_efi_log_item, efi_item);
37}
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Darrick J. Wong9817aa82020-05-01 16:00:48 -070039STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100040xfs_efi_item_free(
41 struct xfs_efi_log_item *efip)
Christoph Hellwig7d795ca2005-06-21 15:41:19 +100042{
Dave Chinnerb1c5ebb2016-07-22 09:52:35 +100043 kmem_free(efip->efi_item.li_lv_shadow);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100044 if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +100045 kmem_free(efip);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100046 else
Darrick J. Wong182696f2021-10-12 11:09:23 -070047 kmem_cache_free(xfs_efi_cache, efip);
Christoph Hellwig7d795ca2005-06-21 15:41:19 +100048}
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
Dave Chinner0612d112018-04-02 20:08:27 -070051 * Freeing the efi requires that we remove it from the AIL if it has already
52 * been placed there. However, the EFI may not yet have been placed in the AIL
53 * when called by xfs_efi_release() from EFD processing due to the ordering of
54 * committed vs unpin operations in bulk insert operations. Hence the reference
55 * count to ensure only the last caller frees the EFI.
56 */
Darrick J. Wong10d0c6e2020-05-01 16:00:50 -070057STATIC void
Dave Chinner0612d112018-04-02 20:08:27 -070058xfs_efi_release(
59 struct xfs_efi_log_item *efip)
60{
61 ASSERT(atomic_read(&efip->efi_refcount) > 0);
Dave Chinner3512fc12022-05-04 11:46:47 +100062 if (!atomic_dec_and_test(&efip->efi_refcount))
63 return;
64
65 xfs_trans_ail_delete(&efip->efi_item, 0);
66 xfs_efi_item_free(efip);
Dave Chinner0612d112018-04-02 20:08:27 -070067}
68
Dave Chinner166d1362013-08-12 20:50:04 +100069STATIC void
70xfs_efi_item_size(
71 struct xfs_log_item *lip,
72 int *nvecs,
73 int *nbytes)
74{
Darrick J. Wong3c5aaac2022-10-21 09:10:05 -070075 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
76
Dave Chinner166d1362013-08-12 20:50:04 +100077 *nvecs += 1;
Darrick J. Wong3c5aaac2022-10-21 09:10:05 -070078 *nbytes += xfs_efi_log_format_sizeof(efip->efi_format.efi_nextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81/*
82 * This is called to fill in the vector of log iovecs for the
83 * given efi log item. We use only 1 iovec, and we point that
84 * at the efi_log_format structure embedded in the efi item.
85 * It is at this point that we assert that all of the extent
86 * slots in the efi item have been filled.
87 */
88STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100089xfs_efi_item_format(
90 struct xfs_log_item *lip,
Christoph Hellwigbde7cff2013-12-13 11:34:02 +110091 struct xfs_log_vec *lv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100093 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
Christoph Hellwigbde7cff2013-12-13 11:34:02 +110094 struct xfs_log_iovec *vecp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Dave Chinnerb199c8a2010-12-20 11:59:49 +110096 ASSERT(atomic_read(&efip->efi_next_extent) ==
97 efip->efi_format.efi_nextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 efip->efi_format.efi_type = XFS_LI_EFI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 efip->efi_format.efi_size = 1;
101
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100102 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
Christoph Hellwig12343512013-12-13 11:00:43 +1100103 &efip->efi_format,
Darrick J. Wong3c5aaac2022-10-21 09:10:05 -0700104 xfs_efi_log_format_sizeof(efip->efi_format.efi_nextents));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107
108/*
Brian Foster8d99fe92015-08-19 09:51:16 +1000109 * The unpin operation is the last place an EFI is manipulated in the log. It is
110 * either inserted in the AIL or aborted in the event of a log I/O error. In
111 * either case, the EFI transaction has been successfully committed to make it
112 * this far. Therefore, we expect whoever committed the EFI to either construct
113 * and commit the EFD or drop the EFD's reference in the event of error. Simply
114 * drop the log's EFI reference now that the log is done with it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000117xfs_efi_item_unpin(
118 struct xfs_log_item *lip,
119 int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000121 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
Brian Foster5e4b5382015-08-19 09:50:12 +1000122 xfs_efi_release(efip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125/*
Brian Foster8d99fe92015-08-19 09:51:16 +1000126 * The EFI has been either committed or aborted if the transaction has been
127 * cancelled. If the transaction was cancelled, an EFD isn't going to be
128 * constructed and thus we free the EFI here directly.
129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130STATIC void
Christoph Hellwigddf92052019-06-28 19:27:32 -0700131xfs_efi_item_release(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000132 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Christoph Hellwigddf92052019-06-28 19:27:32 -0700134 xfs_efi_release(EFI_ITEM(lip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/*
138 * Allocate and initialize an efi item with the given number of extents.
139 */
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700140STATIC struct xfs_efi_log_item *
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000141xfs_efi_init(
142 struct xfs_mount *mp,
143 uint nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000146 struct xfs_efi_log_item *efip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 ASSERT(nextents > 0);
149 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
Darrick J. Wong3c5aaac2022-10-21 09:10:05 -0700150 efip = kzalloc(xfs_efi_log_item_sizeof(nextents),
151 GFP_KERNEL | __GFP_NOFAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 } else {
Darrick J. Wong182696f2021-10-12 11:09:23 -0700153 efip = kmem_cache_zalloc(xfs_efi_cache,
Carlos Maiolino32a2b112020-07-22 09:23:10 -0700154 GFP_KERNEL | __GFP_NOFAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156
Dave Chinner43f5efc2010-03-23 10:10:00 +1100157 xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 efip->efi_format.efi_nextents = nextents;
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000159 efip->efi_format.efi_id = (uintptr_t)(void *)efip;
Dave Chinnerb199c8a2010-12-20 11:59:49 +1100160 atomic_set(&efip->efi_next_extent, 0);
Dave Chinner666d6442013-04-03 14:09:21 +1100161 atomic_set(&efip->efi_refcount, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000163 return efip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
166/*
Tim Shimmin6d192a92006-06-09 14:55:38 +1000167 * Copy an EFI format buffer from the given buf, and into the destination
168 * EFI format structure.
169 * The given buffer can be in 32 bit or 64 bit form (which has different padding),
170 * one of which will be the native format for this kernel.
171 * It will handle the conversion of formats if necessary.
172 */
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700173STATIC int
Tim Shimmin6d192a92006-06-09 14:55:38 +1000174xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
175{
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000176 xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000177 uint i;
Darrick J. Wong3c5aaac2022-10-21 09:10:05 -0700178 uint len = xfs_efi_log_format_sizeof(src_efi_fmt->efi_nextents);
179 uint len32 = xfs_efi_log_format32_sizeof(src_efi_fmt->efi_nextents);
180 uint len64 = xfs_efi_log_format64_sizeof(src_efi_fmt->efi_nextents);
Tim Shimmin6d192a92006-06-09 14:55:38 +1000181
182 if (buf->i_len == len) {
Darrick J. Wong03a74852022-10-20 16:39:59 -0700183 memcpy(dst_efi_fmt, src_efi_fmt,
184 offsetof(struct xfs_efi_log_format, efi_extents));
185 for (i = 0; i < src_efi_fmt->efi_nextents; i++)
186 memcpy(&dst_efi_fmt->efi_extents[i],
187 &src_efi_fmt->efi_extents[i],
188 sizeof(struct xfs_extent));
Tim Shimmin6d192a92006-06-09 14:55:38 +1000189 return 0;
190 } else if (buf->i_len == len32) {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000191 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000192
193 dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
194 dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
195 dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
196 dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
197 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
198 dst_efi_fmt->efi_extents[i].ext_start =
199 src_efi_fmt_32->efi_extents[i].ext_start;
200 dst_efi_fmt->efi_extents[i].ext_len =
201 src_efi_fmt_32->efi_extents[i].ext_len;
202 }
203 return 0;
204 } else if (buf->i_len == len64) {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000205 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000206
207 dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
208 dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
209 dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
210 dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
211 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
212 dst_efi_fmt->efi_extents[i].ext_start =
213 src_efi_fmt_64->efi_extents[i].ext_start;
214 dst_efi_fmt->efi_extents[i].ext_len =
215 src_efi_fmt_64->efi_extents[i].ext_len;
216 }
217 return 0;
218 }
Darrick J. Wong950f0d52022-10-25 15:07:14 -0700219 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, NULL, buf->i_addr,
220 buf->i_len);
Dave Chinner24513372014-06-25 14:58:08 +1000221 return -EFSCORRUPTED;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000222}
223
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000224static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
Christoph Hellwig7d795ca2005-06-21 15:41:19 +1000225{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000226 return container_of(lip, struct xfs_efd_log_item, efd_item);
227}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000229STATIC void
230xfs_efd_item_free(struct xfs_efd_log_item *efdp)
231{
Dave Chinnerb1c5ebb2016-07-22 09:52:35 +1000232 kmem_free(efdp->efd_item.li_lv_shadow);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000233 if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000234 kmem_free(efdp);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000235 else
Darrick J. Wong182696f2021-10-12 11:09:23 -0700236 kmem_cache_free(xfs_efd_cache, efdp);
Christoph Hellwig7d795ca2005-06-21 15:41:19 +1000237}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Dave Chinner166d1362013-08-12 20:50:04 +1000239STATIC void
240xfs_efd_item_size(
241 struct xfs_log_item *lip,
242 int *nvecs,
243 int *nbytes)
244{
Darrick J. Wong3c5aaac2022-10-21 09:10:05 -0700245 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
246
Dave Chinner166d1362013-08-12 20:50:04 +1000247 *nvecs += 1;
Darrick J. Wong3c5aaac2022-10-21 09:10:05 -0700248 *nbytes += xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251/*
252 * This is called to fill in the vector of log iovecs for the
253 * given efd log item. We use only 1 iovec, and we point that
254 * at the efd_log_format structure embedded in the efd item.
255 * It is at this point that we assert that all of the extent
256 * slots in the efd item have been filled.
257 */
258STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000259xfs_efd_item_format(
260 struct xfs_log_item *lip,
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100261 struct xfs_log_vec *lv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000263 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100264 struct xfs_log_iovec *vecp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
267
268 efdp->efd_format.efd_type = XFS_LI_EFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 efdp->efd_format.efd_size = 1;
270
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100271 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
Christoph Hellwig12343512013-12-13 11:00:43 +1100272 &efdp->efd_format,
Darrick J. Wong3c5aaac2022-10-21 09:10:05 -0700273 xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276/*
Brian Foster8d99fe92015-08-19 09:51:16 +1000277 * The EFD is either committed or aborted if the transaction is cancelled. If
278 * the transaction is cancelled, drop our reference to the EFI and free the EFD.
279 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280STATIC void
Christoph Hellwigddf92052019-06-28 19:27:32 -0700281xfs_efd_item_release(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000282 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Brian Foster8d99fe92015-08-19 09:51:16 +1000284 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
285
Christoph Hellwigddf92052019-06-28 19:27:32 -0700286 xfs_efi_release(efdp->efd_efip);
287 xfs_efd_item_free(efdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Dave Chinnerc23ab602022-05-04 11:46:39 +1000290static struct xfs_log_item *
291xfs_efd_item_intent(
292 struct xfs_log_item *lip)
293{
294 return &EFD_ITEM(lip)->efd_efip->efi_item;
295}
296
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000297static const struct xfs_item_ops xfs_efd_item_ops = {
Dave Chinnerf5b81202022-05-04 11:46:09 +1000298 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED |
299 XFS_ITEM_INTENT_DONE,
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000300 .iop_size = xfs_efd_item_size,
301 .iop_format = xfs_efd_item_format,
Christoph Hellwigddf92052019-06-28 19:27:32 -0700302 .iop_release = xfs_efd_item_release,
Dave Chinnerc23ab602022-05-04 11:46:39 +1000303 .iop_intent = xfs_efd_item_intent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304};
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/*
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700307 * Allocate an "extent free done" log item that will hold nextents worth of
308 * extents. The caller must use all nextents extents, because we are not
309 * flexible about this at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 */
Christoph Hellwig81f40042019-06-28 19:28:17 -0700311static struct xfs_efd_log_item *
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700312xfs_trans_get_efd(
313 struct xfs_trans *tp,
314 struct xfs_efi_log_item *efip,
315 unsigned int nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700317 struct xfs_efd_log_item *efdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 ASSERT(nextents > 0);
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
Darrick J. Wong3c5aaac2022-10-21 09:10:05 -0700322 efdp = kzalloc(xfs_efd_log_item_sizeof(nextents),
323 GFP_KERNEL | __GFP_NOFAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 } else {
Darrick J. Wong182696f2021-10-12 11:09:23 -0700325 efdp = kmem_cache_zalloc(xfs_efd_cache,
Carlos Maiolino32a2b112020-07-22 09:23:10 -0700326 GFP_KERNEL | __GFP_NOFAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700329 xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD,
330 &xfs_efd_item_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 efdp->efd_efip = efip;
332 efdp->efd_format.efd_nextents = nextents;
333 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
334
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700335 xfs_trans_add_item(tp, &efdp->efd_item);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000336 return efdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
Darrick J. Wongdc42375d2016-08-03 11:23:49 +1000338
339/*
Dave Chinner0853b5de2023-06-28 11:04:33 -0700340 * Fill the EFD with all extents from the EFI when we need to roll the
341 * transaction and continue with a new EFI.
342 *
343 * This simply copies all the extents in the EFI to the EFD rather than make
344 * assumptions about which extents in the EFI have already been processed. We
345 * currently keep the xefi list in the same order as the EFI extent list, but
346 * that may not always be the case. Copying everything avoids leaving a landmine
347 * were we fail to cancel all the extents in an EFI if the xefi list is
348 * processed in a different order to the extents in the EFI.
349 */
350static void
351xfs_efd_from_efi(
352 struct xfs_efd_log_item *efdp)
353{
354 struct xfs_efi_log_item *efip = efdp->efd_efip;
355 uint i;
356
357 ASSERT(efip->efi_format.efi_nextents > 0);
358 ASSERT(efdp->efd_next_extent < efip->efi_format.efi_nextents);
359
360 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
361 efdp->efd_format.efd_extents[i] =
362 efip->efi_format.efi_extents[i];
363 }
364 efdp->efd_next_extent = efip->efi_format.efi_nextents;
365}
366
367/*
Christoph Hellwig81f40042019-06-28 19:28:17 -0700368 * Free an extent and log it to the EFD. Note that the transaction is marked
369 * dirty regardless of whether the extent free succeeds or fails to support the
370 * EFI/EFD lifecycle rules.
371 */
372static int
373xfs_trans_free_extent(
374 struct xfs_trans *tp,
375 struct xfs_efd_log_item *efdp,
Darrick J. Wong578c7142023-02-01 10:16:02 -0800376 struct xfs_extent_free_item *xefi)
Christoph Hellwig81f40042019-06-28 19:28:17 -0700377{
Darrick J. Wong72ba4552023-02-01 10:16:02 -0800378 struct xfs_owner_info oinfo = { };
Christoph Hellwig81f40042019-06-28 19:28:17 -0700379 struct xfs_mount *mp = tp->t_mountp;
380 struct xfs_extent *extp;
381 uint next_extent;
Christoph Hellwig81f40042019-06-28 19:28:17 -0700382 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp,
Darrick J. Wong578c7142023-02-01 10:16:02 -0800383 xefi->xefi_startblock);
Christoph Hellwig81f40042019-06-28 19:28:17 -0700384 int error;
385
Darrick J. Wong578c7142023-02-01 10:16:02 -0800386 oinfo.oi_owner = xefi->xefi_owner;
387 if (xefi->xefi_flags & XFS_EFI_ATTR_FORK)
Darrick J. Wong72ba4552023-02-01 10:16:02 -0800388 oinfo.oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
Darrick J. Wong578c7142023-02-01 10:16:02 -0800389 if (xefi->xefi_flags & XFS_EFI_BMBT_BLOCK)
Darrick J. Wong72ba4552023-02-01 10:16:02 -0800390 oinfo.oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
Christoph Hellwig81f40042019-06-28 19:28:17 -0700391
Darrick J. Wongf6b38462023-04-11 18:59:54 -0700392 trace_xfs_bmap_free_deferred(tp->t_mountp, xefi->xefi_pag->pag_agno, 0,
393 agbno, xefi->xefi_blockcount);
Darrick J. Wong72ba4552023-02-01 10:16:02 -0800394
Darrick J. Wongf6b38462023-04-11 18:59:54 -0700395 error = __xfs_free_extent(tp, xefi->xefi_pag, agbno,
Dave Chinnerb742d7b42023-06-28 11:04:32 -0700396 xefi->xefi_blockcount, &oinfo, xefi->xefi_agresv,
Darrick J. Wong578c7142023-02-01 10:16:02 -0800397 xefi->xefi_flags & XFS_EFI_SKIP_DISCARD);
Darrick J. Wongb2ccab312023-04-11 18:59:53 -0700398
Christoph Hellwig81f40042019-06-28 19:28:17 -0700399 /*
400 * Mark the transaction dirty, even on error. This ensures the
401 * transaction is aborted, which:
402 *
403 * 1.) releases the EFI and frees the EFD
404 * 2.) shuts down the filesystem
405 */
Dave Chinnerbb7b1c92022-05-04 11:46:21 +1000406 tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
Christoph Hellwig81f40042019-06-28 19:28:17 -0700407 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
408
Dave Chinner0853b5de2023-06-28 11:04:33 -0700409 /*
410 * If we need a new transaction to make progress, the caller will log a
411 * new EFI with the current contents. It will also log an EFD to cancel
412 * the existing EFI, and so we need to copy all the unprocessed extents
413 * in this EFI to the EFD so this works correctly.
414 */
415 if (error == -EAGAIN) {
416 xfs_efd_from_efi(efdp);
417 return error;
418 }
419
Christoph Hellwig81f40042019-06-28 19:28:17 -0700420 next_extent = efdp->efd_next_extent;
421 ASSERT(next_extent < efdp->efd_format.efd_nextents);
422 extp = &(efdp->efd_format.efd_extents[next_extent]);
Darrick J. Wong578c7142023-02-01 10:16:02 -0800423 extp->ext_start = xefi->xefi_startblock;
424 extp->ext_len = xefi->xefi_blockcount;
Christoph Hellwig81f40042019-06-28 19:28:17 -0700425 efdp->efd_next_extent++;
426
427 return error;
428}
429
430/* Sort bmap items by AG. */
431static int
432xfs_extent_free_diff_items(
433 void *priv,
Sami Tolvanen4f0f5862021-04-08 11:28:34 -0700434 const struct list_head *a,
435 const struct list_head *b)
Christoph Hellwig81f40042019-06-28 19:28:17 -0700436{
Christoph Hellwig81f40042019-06-28 19:28:17 -0700437 struct xfs_extent_free_item *ra;
438 struct xfs_extent_free_item *rb;
439
440 ra = container_of(a, struct xfs_extent_free_item, xefi_list);
441 rb = container_of(b, struct xfs_extent_free_item, xefi_list);
Darrick J. Wongf6b38462023-04-11 18:59:54 -0700442
443 return ra->xefi_pag->pag_agno - rb->xefi_pag->pag_agno;
Christoph Hellwig81f40042019-06-28 19:28:17 -0700444}
445
Christoph Hellwig81f40042019-06-28 19:28:17 -0700446/* Log a free extent to the intent item. */
447STATIC void
448xfs_extent_free_log_item(
449 struct xfs_trans *tp,
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700450 struct xfs_efi_log_item *efip,
Darrick J. Wong578c7142023-02-01 10:16:02 -0800451 struct xfs_extent_free_item *xefi)
Christoph Hellwig81f40042019-06-28 19:28:17 -0700452{
Christoph Hellwig81f40042019-06-28 19:28:17 -0700453 uint next_extent;
454 struct xfs_extent *extp;
455
Christoph Hellwig81f40042019-06-28 19:28:17 -0700456 tp->t_flags |= XFS_TRANS_DIRTY;
457 set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
458
459 /*
460 * atomic_inc_return gives us the value after the increment;
461 * we want to use it as an array index so we need to subtract 1 from
462 * it.
463 */
464 next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
465 ASSERT(next_extent < efip->efi_format.efi_nextents);
466 extp = &efip->efi_format.efi_extents[next_extent];
Darrick J. Wong578c7142023-02-01 10:16:02 -0800467 extp->ext_start = xefi->xefi_startblock;
468 extp->ext_len = xefi->xefi_blockcount;
Christoph Hellwig81f40042019-06-28 19:28:17 -0700469}
470
Christoph Hellwig13a83332020-04-30 12:52:21 -0700471static struct xfs_log_item *
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700472xfs_extent_free_create_intent(
473 struct xfs_trans *tp,
474 struct list_head *items,
Christoph Hellwigd367a862020-04-30 12:52:20 -0700475 unsigned int count,
476 bool sort)
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700477{
478 struct xfs_mount *mp = tp->t_mountp;
479 struct xfs_efi_log_item *efip = xfs_efi_init(mp, count);
Darrick J. Wong578c7142023-02-01 10:16:02 -0800480 struct xfs_extent_free_item *xefi;
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700481
482 ASSERT(count > 0);
483
484 xfs_trans_add_item(tp, &efip->efi_item);
Christoph Hellwigd367a862020-04-30 12:52:20 -0700485 if (sort)
486 list_sort(mp, items, xfs_extent_free_diff_items);
Darrick J. Wong578c7142023-02-01 10:16:02 -0800487 list_for_each_entry(xefi, items, xefi_list)
488 xfs_extent_free_log_item(tp, efip, xefi);
Christoph Hellwig13a83332020-04-30 12:52:21 -0700489 return &efip->efi_item;
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700490}
491
Christoph Hellwig81f40042019-06-28 19:28:17 -0700492/* Get an EFD so we can process all the free extents. */
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700493static struct xfs_log_item *
Christoph Hellwig81f40042019-06-28 19:28:17 -0700494xfs_extent_free_create_done(
495 struct xfs_trans *tp,
Christoph Hellwig13a83332020-04-30 12:52:21 -0700496 struct xfs_log_item *intent,
Christoph Hellwig81f40042019-06-28 19:28:17 -0700497 unsigned int count)
498{
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700499 return &xfs_trans_get_efd(tp, EFI_ITEM(intent), count)->efd_item;
Christoph Hellwig81f40042019-06-28 19:28:17 -0700500}
501
Darrick J. Wongf6b38462023-04-11 18:59:54 -0700502/* Take a passive ref to the AG containing the space we're freeing. */
503void
504xfs_extent_free_get_group(
505 struct xfs_mount *mp,
506 struct xfs_extent_free_item *xefi)
507{
508 xfs_agnumber_t agno;
509
510 agno = XFS_FSB_TO_AGNO(mp, xefi->xefi_startblock);
Darrick J. Wongd5c88132023-04-11 18:59:58 -0700511 xefi->xefi_pag = xfs_perag_intent_get(mp, agno);
Darrick J. Wongf6b38462023-04-11 18:59:54 -0700512}
513
514/* Release a passive AG ref after some freeing work. */
515static inline void
516xfs_extent_free_put_group(
517 struct xfs_extent_free_item *xefi)
518{
Darrick J. Wongd5c88132023-04-11 18:59:58 -0700519 xfs_perag_intent_put(xefi->xefi_pag);
Darrick J. Wongf6b38462023-04-11 18:59:54 -0700520}
521
Christoph Hellwig81f40042019-06-28 19:28:17 -0700522/* Process a free extent. */
523STATIC int
524xfs_extent_free_finish_item(
525 struct xfs_trans *tp,
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700526 struct xfs_log_item *done,
Christoph Hellwig81f40042019-06-28 19:28:17 -0700527 struct list_head *item,
Christoph Hellwig3ec1b262020-04-30 12:52:22 -0700528 struct xfs_btree_cur **state)
Christoph Hellwig81f40042019-06-28 19:28:17 -0700529{
Darrick J. Wong578c7142023-02-01 10:16:02 -0800530 struct xfs_extent_free_item *xefi;
Christoph Hellwig81f40042019-06-28 19:28:17 -0700531 int error;
532
Darrick J. Wong578c7142023-02-01 10:16:02 -0800533 xefi = container_of(item, struct xfs_extent_free_item, xefi_list);
Darrick J. Wong72ba4552023-02-01 10:16:02 -0800534
Darrick J. Wong578c7142023-02-01 10:16:02 -0800535 error = xfs_trans_free_extent(tp, EFD_ITEM(done), xefi);
Darrick J. Wongf6b38462023-04-11 18:59:54 -0700536
Dave Chinner0853b5de2023-06-28 11:04:33 -0700537 /*
538 * Don't free the XEFI if we need a new transaction to complete
539 * processing of it.
540 */
541 if (error == -EAGAIN)
542 return error;
543
Darrick J. Wongf6b38462023-04-11 18:59:54 -0700544 xfs_extent_free_put_group(xefi);
Darrick J. Wong578c7142023-02-01 10:16:02 -0800545 kmem_cache_free(xfs_extfree_item_cache, xefi);
Christoph Hellwig81f40042019-06-28 19:28:17 -0700546 return error;
547}
548
549/* Abort all pending EFIs. */
550STATIC void
551xfs_extent_free_abort_intent(
Christoph Hellwig13a83332020-04-30 12:52:21 -0700552 struct xfs_log_item *intent)
Christoph Hellwig81f40042019-06-28 19:28:17 -0700553{
Christoph Hellwig13a83332020-04-30 12:52:21 -0700554 xfs_efi_release(EFI_ITEM(intent));
Christoph Hellwig81f40042019-06-28 19:28:17 -0700555}
556
557/* Cancel a free extent. */
558STATIC void
559xfs_extent_free_cancel_item(
560 struct list_head *item)
561{
Darrick J. Wong578c7142023-02-01 10:16:02 -0800562 struct xfs_extent_free_item *xefi;
Christoph Hellwig81f40042019-06-28 19:28:17 -0700563
Darrick J. Wong578c7142023-02-01 10:16:02 -0800564 xefi = container_of(item, struct xfs_extent_free_item, xefi_list);
Darrick J. Wongf6b38462023-04-11 18:59:54 -0700565
566 xfs_extent_free_put_group(xefi);
Darrick J. Wong578c7142023-02-01 10:16:02 -0800567 kmem_cache_free(xfs_extfree_item_cache, xefi);
Christoph Hellwig81f40042019-06-28 19:28:17 -0700568}
569
570const struct xfs_defer_op_type xfs_extent_free_defer_type = {
571 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
Christoph Hellwig81f40042019-06-28 19:28:17 -0700572 .create_intent = xfs_extent_free_create_intent,
573 .abort_intent = xfs_extent_free_abort_intent,
Christoph Hellwig81f40042019-06-28 19:28:17 -0700574 .create_done = xfs_extent_free_create_done,
575 .finish_item = xfs_extent_free_finish_item,
576 .cancel_item = xfs_extent_free_cancel_item,
577};
578
579/*
580 * AGFL blocks are accounted differently in the reserve pools and are not
581 * inserted into the busy extent list.
582 */
583STATIC int
584xfs_agfl_free_finish_item(
585 struct xfs_trans *tp,
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700586 struct xfs_log_item *done,
Christoph Hellwig81f40042019-06-28 19:28:17 -0700587 struct list_head *item,
Christoph Hellwig3ec1b262020-04-30 12:52:22 -0700588 struct xfs_btree_cur **state)
Christoph Hellwig81f40042019-06-28 19:28:17 -0700589{
Darrick J. Wongb3b5ff42021-10-12 15:55:54 -0700590 struct xfs_owner_info oinfo = { };
Christoph Hellwig81f40042019-06-28 19:28:17 -0700591 struct xfs_mount *mp = tp->t_mountp;
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700592 struct xfs_efd_log_item *efdp = EFD_ITEM(done);
Darrick J. Wong578c7142023-02-01 10:16:02 -0800593 struct xfs_extent_free_item *xefi;
Christoph Hellwig81f40042019-06-28 19:28:17 -0700594 struct xfs_extent *extp;
595 struct xfs_buf *agbp;
596 int error;
Christoph Hellwig81f40042019-06-28 19:28:17 -0700597 xfs_agblock_t agbno;
598 uint next_extent;
599
Darrick J. Wong578c7142023-02-01 10:16:02 -0800600 xefi = container_of(item, struct xfs_extent_free_item, xefi_list);
601 ASSERT(xefi->xefi_blockcount == 1);
Darrick J. Wong578c7142023-02-01 10:16:02 -0800602 agbno = XFS_FSB_TO_AGBNO(mp, xefi->xefi_startblock);
603 oinfo.oi_owner = xefi->xefi_owner;
Christoph Hellwig81f40042019-06-28 19:28:17 -0700604
Darrick J. Wongf6b38462023-04-11 18:59:54 -0700605 trace_xfs_agfl_free_deferred(mp, xefi->xefi_pag->pag_agno, 0, agbno,
606 xefi->xefi_blockcount);
Christoph Hellwig81f40042019-06-28 19:28:17 -0700607
Darrick J. Wongf6b38462023-04-11 18:59:54 -0700608 error = xfs_alloc_read_agf(xefi->xefi_pag, tp, 0, &agbp);
Christoph Hellwig81f40042019-06-28 19:28:17 -0700609 if (!error)
Darrick J. Wongf6b38462023-04-11 18:59:54 -0700610 error = xfs_free_agfl_block(tp, xefi->xefi_pag->pag_agno,
611 agbno, agbp, &oinfo);
Christoph Hellwig81f40042019-06-28 19:28:17 -0700612
613 /*
614 * Mark the transaction dirty, even on error. This ensures the
615 * transaction is aborted, which:
616 *
617 * 1.) releases the EFI and frees the EFD
618 * 2.) shuts down the filesystem
619 */
620 tp->t_flags |= XFS_TRANS_DIRTY;
621 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
622
623 next_extent = efdp->efd_next_extent;
624 ASSERT(next_extent < efdp->efd_format.efd_nextents);
625 extp = &(efdp->efd_format.efd_extents[next_extent]);
Darrick J. Wong578c7142023-02-01 10:16:02 -0800626 extp->ext_start = xefi->xefi_startblock;
627 extp->ext_len = xefi->xefi_blockcount;
Christoph Hellwig81f40042019-06-28 19:28:17 -0700628 efdp->efd_next_extent++;
629
Darrick J. Wongf6b38462023-04-11 18:59:54 -0700630 xfs_extent_free_put_group(xefi);
Darrick J. Wong578c7142023-02-01 10:16:02 -0800631 kmem_cache_free(xfs_extfree_item_cache, xefi);
Christoph Hellwig81f40042019-06-28 19:28:17 -0700632 return error;
633}
634
635/* sub-type with special handling for AGFL deferred frees */
636const struct xfs_defer_op_type xfs_agfl_free_defer_type = {
637 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
Christoph Hellwig81f40042019-06-28 19:28:17 -0700638 .create_intent = xfs_extent_free_create_intent,
639 .abort_intent = xfs_extent_free_abort_intent,
Christoph Hellwig81f40042019-06-28 19:28:17 -0700640 .create_done = xfs_extent_free_create_done,
641 .finish_item = xfs_agfl_free_finish_item,
642 .cancel_item = xfs_extent_free_cancel_item,
643};
644
Darrick J. Wong3c15df32020-11-29 16:33:38 -0800645/* Is this recovered EFI ok? */
646static inline bool
647xfs_efi_validate_ext(
648 struct xfs_mount *mp,
649 struct xfs_extent *extp)
650{
Darrick J. Wong67457eb2020-12-04 13:20:00 -0800651 return xfs_verify_fsbext(mp, extp->ext_start, extp->ext_len);
Darrick J. Wong3c15df32020-11-29 16:33:38 -0800652}
653
Christoph Hellwig81f40042019-06-28 19:28:17 -0700654/*
Darrick J. Wongdc42375d2016-08-03 11:23:49 +1000655 * Process an extent free intent item that was recovered from
656 * the log. We need to free the extents that it describes.
657 */
Darrick J. Wong10d0c6e2020-05-01 16:00:50 -0700658STATIC int
Darrick J. Wong96b60f82020-05-01 16:00:55 -0700659xfs_efi_item_recover(
660 struct xfs_log_item *lip,
Darrick J. Wonge6fff812020-09-25 17:39:37 -0700661 struct list_head *capture_list)
Darrick J. Wongdc42375d2016-08-03 11:23:49 +1000662{
Darrick J. Wong3c919b02023-09-11 08:39:05 -0700663 struct xfs_trans_res resv;
Darrick J. Wong96b60f82020-05-01 16:00:55 -0700664 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
Dave Chinnerd86142d2022-03-17 09:09:12 -0700665 struct xfs_mount *mp = lip->li_log->l_mp;
Darrick J. Wong96b60f82020-05-01 16:00:55 -0700666 struct xfs_efd_log_item *efdp;
667 struct xfs_trans *tp;
Darrick J. Wong96b60f82020-05-01 16:00:55 -0700668 int i;
669 int error = 0;
Dave Chinner0853b5de2023-06-28 11:04:33 -0700670 bool requeue_only = false;
Darrick J. Wongdc42375d2016-08-03 11:23:49 +1000671
Darrick J. Wongdc42375d2016-08-03 11:23:49 +1000672 /*
673 * First check the validity of the extents described by the
674 * EFI. If any are bad, then assume that all are bad and
675 * just toss the EFI.
676 */
677 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
Darrick J. Wong3c15df32020-11-29 16:33:38 -0800678 if (!xfs_efi_validate_ext(mp,
679 &efip->efi_format.efi_extents[i])) {
680 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
681 &efip->efi_format,
682 sizeof(efip->efi_format));
Darrick J. Wong895e1962019-11-06 09:17:43 -0800683 return -EFSCORRUPTED;
Darrick J. Wong3c15df32020-11-29 16:33:38 -0800684 }
Darrick J. Wongdc42375d2016-08-03 11:23:49 +1000685 }
686
Darrick J. Wong3c919b02023-09-11 08:39:05 -0700687 resv = xlog_recover_resv(&M_RES(mp)->tr_itruncate);
688 error = xfs_trans_alloc(mp, &resv, 0, 0, 0, &tp);
Darrick J. Wongdc42375d2016-08-03 11:23:49 +1000689 if (error)
690 return error;
691 efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
692
693 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
Darrick J. Wong72ba4552023-02-01 10:16:02 -0800694 struct xfs_extent_free_item fake = {
695 .xefi_owner = XFS_RMAP_OWN_UNKNOWN,
Dave Chinnerb742d7b42023-06-28 11:04:32 -0700696 .xefi_agresv = XFS_AG_RESV_NONE,
Darrick J. Wong72ba4552023-02-01 10:16:02 -0800697 };
698 struct xfs_extent *extp;
699
Darrick J. Wonge127faf2016-08-03 12:29:32 +1000700 extp = &efip->efi_format.efi_extents[i];
Darrick J. Wong72ba4552023-02-01 10:16:02 -0800701
702 fake.xefi_startblock = extp->ext_start;
703 fake.xefi_blockcount = extp->ext_len;
704
Dave Chinner0853b5de2023-06-28 11:04:33 -0700705 if (!requeue_only) {
706 xfs_extent_free_get_group(mp, &fake);
707 error = xfs_trans_free_extent(tp, efdp, &fake);
708 xfs_extent_free_put_group(&fake);
709 }
710
711 /*
712 * If we can't free the extent without potentially deadlocking,
713 * requeue the rest of the extents to a new so that they get
714 * run again later with a new transaction context.
715 */
716 if (error == -EAGAIN || requeue_only) {
717 error = xfs_free_extent_later(tp, fake.xefi_startblock,
718 fake.xefi_blockcount,
719 &XFS_RMAP_OINFO_ANY_OWNER,
720 fake.xefi_agresv);
721 if (!error) {
722 requeue_only = true;
723 continue;
724 }
Yang Li34accea2023-07-03 09:38:50 -0700725 }
Dave Chinner0853b5de2023-06-28 11:04:33 -0700726
Darrick J. Wong43059d52021-08-06 11:06:35 -0700727 if (error == -EFSCORRUPTED)
728 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
729 extp, sizeof(*extp));
Darrick J. Wongdc42375d2016-08-03 11:23:49 +1000730 if (error)
731 goto abort_error;
732
733 }
734
Darrick J. Wong512edfa2021-09-16 17:28:07 -0700735 return xfs_defer_ops_capture_and_commit(tp, capture_list);
Darrick J. Wongdc42375d2016-08-03 11:23:49 +1000736
737abort_error:
738 xfs_trans_cancel(tp);
739 return error;
740}
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700741
Darrick J. Wong154c7332020-05-01 16:00:54 -0700742STATIC bool
743xfs_efi_item_match(
744 struct xfs_log_item *lip,
745 uint64_t intent_id)
746{
747 return EFI_ITEM(lip)->efi_format.efi_id == intent_id;
748}
749
Darrick J. Wong4e919af2020-09-27 16:18:13 -0700750/* Relog an intent item to push the log tail forward. */
751static struct xfs_log_item *
752xfs_efi_item_relog(
753 struct xfs_log_item *intent,
754 struct xfs_trans *tp)
755{
756 struct xfs_efd_log_item *efdp;
757 struct xfs_efi_log_item *efip;
758 struct xfs_extent *extp;
759 unsigned int count;
760
761 count = EFI_ITEM(intent)->efi_format.efi_nextents;
762 extp = EFI_ITEM(intent)->efi_format.efi_extents;
763
764 tp->t_flags |= XFS_TRANS_DIRTY;
765 efdp = xfs_trans_get_efd(tp, EFI_ITEM(intent), count);
766 efdp->efd_next_extent = count;
767 memcpy(efdp->efd_format.efd_extents, extp, count * sizeof(*extp));
768 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
769
770 efip = xfs_efi_init(tp->t_mountp, count);
771 memcpy(efip->efi_format.efi_extents, extp, count * sizeof(*extp));
772 atomic_set(&efip->efi_next_extent, count);
773 xfs_trans_add_item(tp, &efip->efi_item);
774 set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
775 return &efip->efi_item;
776}
777
Darrick J. Wong10d0c6e2020-05-01 16:00:50 -0700778static const struct xfs_item_ops xfs_efi_item_ops = {
Dave Chinnerf5b81202022-05-04 11:46:09 +1000779 .flags = XFS_ITEM_INTENT,
Darrick J. Wong10d0c6e2020-05-01 16:00:50 -0700780 .iop_size = xfs_efi_item_size,
781 .iop_format = xfs_efi_item_format,
782 .iop_unpin = xfs_efi_item_unpin,
783 .iop_release = xfs_efi_item_release,
784 .iop_recover = xfs_efi_item_recover,
Darrick J. Wong154c7332020-05-01 16:00:54 -0700785 .iop_match = xfs_efi_item_match,
Darrick J. Wong4e919af2020-09-27 16:18:13 -0700786 .iop_relog = xfs_efi_item_relog,
Darrick J. Wong10d0c6e2020-05-01 16:00:50 -0700787};
788
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700789/*
790 * This routine is called to create an in-core extent free intent
791 * item from the efi format structure which was logged on disk.
792 * It allocates an in-core efi, copies the extents from the format
793 * structure into it, and adds the efi to the AIL with the given
794 * LSN.
795 */
796STATIC int
797xlog_recover_efi_commit_pass2(
798 struct xlog *log,
799 struct list_head *buffer_list,
800 struct xlog_recover_item *item,
801 xfs_lsn_t lsn)
802{
803 struct xfs_mount *mp = log->l_mp;
804 struct xfs_efi_log_item *efip;
805 struct xfs_efi_log_format *efi_formatp;
806 int error;
807
808 efi_formatp = item->ri_buf[0].i_addr;
809
Darrick J. Wong3c5aaac2022-10-21 09:10:05 -0700810 if (item->ri_buf[0].i_len < xfs_efi_log_format_sizeof(0)) {
Darrick J. Wong950f0d52022-10-25 15:07:14 -0700811 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
812 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
Darrick J. Wong03a74852022-10-20 16:39:59 -0700813 return -EFSCORRUPTED;
814 }
815
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700816 efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
817 error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
818 if (error) {
819 xfs_efi_item_free(efip);
820 return error;
821 }
822 atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700823 /*
Darrick J. Wong86a37172020-05-01 16:00:54 -0700824 * Insert the intent into the AIL directly and drop one reference so
825 * that finishing or canceling the work will drop the other.
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700826 */
Darrick J. Wong86a37172020-05-01 16:00:54 -0700827 xfs_trans_ail_insert(log->l_ailp, &efip->efi_item, lsn);
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700828 xfs_efi_release(efip);
829 return 0;
830}
831
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700832const struct xlog_recover_item_ops xlog_efi_item_ops = {
833 .item_type = XFS_LI_EFI,
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700834 .commit_pass2 = xlog_recover_efi_commit_pass2,
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700835};
836
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700837/*
838 * This routine is called when an EFD format structure is found in a committed
839 * transaction in the log. Its purpose is to cancel the corresponding EFI if it
840 * was still in the log. To do this it searches the AIL for the EFI with an id
841 * equal to that in the EFD format structure. If we find it we drop the EFD
842 * reference, which removes the EFI from the AIL and frees it.
843 */
844STATIC int
845xlog_recover_efd_commit_pass2(
846 struct xlog *log,
847 struct list_head *buffer_list,
848 struct xlog_recover_item *item,
849 xfs_lsn_t lsn)
850{
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700851 struct xfs_efd_log_format *efd_formatp;
Darrick J. Wong921ed962022-10-25 15:14:06 -0700852 int buflen = item->ri_buf[0].i_len;
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700853
854 efd_formatp = item->ri_buf[0].i_addr;
Darrick J. Wong921ed962022-10-25 15:14:06 -0700855
856 if (buflen < sizeof(struct xfs_efd_log_format)) {
857 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
858 efd_formatp, buflen);
859 return -EFSCORRUPTED;
860 }
861
862 if (item->ri_buf[0].i_len != xfs_efd_log_format32_sizeof(
863 efd_formatp->efd_nextents) &&
864 item->ri_buf[0].i_len != xfs_efd_log_format64_sizeof(
865 efd_formatp->efd_nextents)) {
866 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
867 efd_formatp, buflen);
868 return -EFSCORRUPTED;
869 }
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700870
Darrick J. Wong154c7332020-05-01 16:00:54 -0700871 xlog_recover_release_intent(log, XFS_LI_EFI, efd_formatp->efd_efi_id);
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700872 return 0;
873}
874
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700875const struct xlog_recover_item_ops xlog_efd_item_ops = {
876 .item_type = XFS_LI_EFD,
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700877 .commit_pass2 = xlog_recover_efd_commit_pass2,
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700878};