blob: b9c333bae0a12d28672af681069e6a0e58f5c157 [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. Wongdc423752016-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"
Christoph Hellwig81f40042019-06-28 19:28:17 -070014#include "xfs_defer.h"
Dave Chinner239880e2013-10-23 10:50:10 +110015#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "xfs_trans_priv.h"
17#include "xfs_extfree_item.h"
Christoph Hellwig12343512013-12-13 11:00:43 +110018#include "xfs_log.h"
Darrick J. Wong340785c2016-08-03 11:33:42 +100019#include "xfs_btree.h"
20#include "xfs_rmap.h"
Christoph Hellwig81f40042019-06-28 19:28:17 -070021#include "xfs_alloc.h"
22#include "xfs_bmap.h"
23#include "xfs_trace.h"
Darrick J. Wonga5155b82019-11-02 09:40:53 -070024#include "xfs_error.h"
Darrick J. Wong9817aa82020-05-01 16:00:48 -070025#include "xfs_log_priv.h"
Darrick J. Wong86ffa472020-05-01 16:00:45 -070026#include "xfs_log_recover.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28kmem_zone_t *xfs_efi_zone;
29kmem_zone_t *xfs_efd_zone;
30
Darrick J. Wong10d0c6e2020-05-01 16:00:50 -070031static const struct xfs_item_ops xfs_efi_item_ops;
32
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100033static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
34{
35 return container_of(lip, struct xfs_efi_log_item, efi_item);
36}
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Darrick J. Wong9817aa82020-05-01 16:00:48 -070038STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100039xfs_efi_item_free(
40 struct xfs_efi_log_item *efip)
Christoph Hellwig7d795ca2005-06-21 15:41:19 +100041{
Dave Chinnerb1c5ebb2016-07-22 09:52:35 +100042 kmem_free(efip->efi_item.li_lv_shadow);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100043 if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +100044 kmem_free(efip);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100045 else
Carlos Maiolino377bcd52019-11-14 12:43:04 -080046 kmem_cache_free(xfs_efi_zone, efip);
Christoph Hellwig7d795ca2005-06-21 15:41:19 +100047}
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
Dave Chinner0612d112018-04-02 20:08:27 -070050 * Freeing the efi requires that we remove it from the AIL if it has already
51 * been placed there. However, the EFI may not yet have been placed in the AIL
52 * when called by xfs_efi_release() from EFD processing due to the ordering of
53 * committed vs unpin operations in bulk insert operations. Hence the reference
54 * count to ensure only the last caller frees the EFI.
55 */
Darrick J. Wong10d0c6e2020-05-01 16:00:50 -070056STATIC void
Dave Chinner0612d112018-04-02 20:08:27 -070057xfs_efi_release(
58 struct xfs_efi_log_item *efip)
59{
60 ASSERT(atomic_read(&efip->efi_refcount) > 0);
61 if (atomic_dec_and_test(&efip->efi_refcount)) {
Brian Foster65587922020-05-06 13:25:23 -070062 xfs_trans_ail_delete(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR);
Dave Chinner0612d112018-04-02 20:08:27 -070063 xfs_efi_item_free(efip);
64 }
65}
66
67/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * This returns the number of iovecs needed to log the given efi item.
69 * We only need 1 iovec for an efi item. It just logs the efi_log_format
70 * structure.
71 */
Dave Chinner166d1362013-08-12 20:50:04 +100072static inline int
73xfs_efi_item_sizeof(
74 struct xfs_efi_log_item *efip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Dave Chinner166d1362013-08-12 20:50:04 +100076 return sizeof(struct xfs_efi_log_format) +
77 (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
78}
79
80STATIC void
81xfs_efi_item_size(
82 struct xfs_log_item *lip,
83 int *nvecs,
84 int *nbytes)
85{
86 *nvecs += 1;
87 *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90/*
91 * This is called to fill in the vector of log iovecs for the
92 * given efi log item. We use only 1 iovec, and we point that
93 * at the efi_log_format structure embedded in the efi item.
94 * It is at this point that we assert that all of the extent
95 * slots in the efi item have been filled.
96 */
97STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100098xfs_efi_item_format(
99 struct xfs_log_item *lip,
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100100 struct xfs_log_vec *lv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000102 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100103 struct xfs_log_iovec *vecp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Dave Chinnerb199c8a2010-12-20 11:59:49 +1100105 ASSERT(atomic_read(&efip->efi_next_extent) ==
106 efip->efi_format.efi_nextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 efip->efi_format.efi_type = XFS_LI_EFI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 efip->efi_format.efi_size = 1;
110
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100111 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
Christoph Hellwig12343512013-12-13 11:00:43 +1100112 &efip->efi_format,
113 xfs_efi_item_sizeof(efip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116
117/*
Brian Foster8d99fe92015-08-19 09:51:16 +1000118 * The unpin operation is the last place an EFI is manipulated in the log. It is
119 * either inserted in the AIL or aborted in the event of a log I/O error. In
120 * either case, the EFI transaction has been successfully committed to make it
121 * this far. Therefore, we expect whoever committed the EFI to either construct
122 * and commit the EFD or drop the EFD's reference in the event of error. Simply
123 * drop the log's EFI reference now that the log is done with it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000126xfs_efi_item_unpin(
127 struct xfs_log_item *lip,
128 int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000130 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
Brian Foster5e4b5382015-08-19 09:50:12 +1000131 xfs_efi_release(efip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134/*
Brian Foster8d99fe92015-08-19 09:51:16 +1000135 * The EFI has been either committed or aborted if the transaction has been
136 * cancelled. If the transaction was cancelled, an EFD isn't going to be
137 * constructed and thus we free the EFI here directly.
138 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139STATIC void
Christoph Hellwigddf92052019-06-28 19:27:32 -0700140xfs_efi_item_release(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000141 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Christoph Hellwigddf92052019-06-28 19:27:32 -0700143 xfs_efi_release(EFI_ITEM(lip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*
147 * Allocate and initialize an efi item with the given number of extents.
148 */
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700149STATIC struct xfs_efi_log_item *
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000150xfs_efi_init(
151 struct xfs_mount *mp,
152 uint nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000155 struct xfs_efi_log_item *efip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 uint size;
157
158 ASSERT(nextents > 0);
159 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
Christoph Hellwig82ff4502020-04-30 12:52:18 -0700160 size = (uint)(sizeof(struct xfs_efi_log_item) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 ((nextents - 1) * sizeof(xfs_extent_t)));
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700162 efip = kmem_zalloc(size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 } else {
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700164 efip = kmem_zone_zalloc(xfs_efi_zone, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
Dave Chinner43f5efc2010-03-23 10:10:00 +1100167 xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 efip->efi_format.efi_nextents = nextents;
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000169 efip->efi_format.efi_id = (uintptr_t)(void *)efip;
Dave Chinnerb199c8a2010-12-20 11:59:49 +1100170 atomic_set(&efip->efi_next_extent, 0);
Dave Chinner666d6442013-04-03 14:09:21 +1100171 atomic_set(&efip->efi_refcount, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000173 return efip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176/*
Tim Shimmin6d192a92006-06-09 14:55:38 +1000177 * Copy an EFI format buffer from the given buf, and into the destination
178 * EFI format structure.
179 * The given buffer can be in 32 bit or 64 bit form (which has different padding),
180 * one of which will be the native format for this kernel.
181 * It will handle the conversion of formats if necessary.
182 */
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700183STATIC int
Tim Shimmin6d192a92006-06-09 14:55:38 +1000184xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
185{
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000186 xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000187 uint i;
188 uint len = sizeof(xfs_efi_log_format_t) +
189 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);
190 uint len32 = sizeof(xfs_efi_log_format_32_t) +
191 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);
192 uint len64 = sizeof(xfs_efi_log_format_64_t) +
193 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);
194
195 if (buf->i_len == len) {
196 memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
197 return 0;
198 } else if (buf->i_len == len32) {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000199 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000200
201 dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
202 dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
203 dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
204 dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
205 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
206 dst_efi_fmt->efi_extents[i].ext_start =
207 src_efi_fmt_32->efi_extents[i].ext_start;
208 dst_efi_fmt->efi_extents[i].ext_len =
209 src_efi_fmt_32->efi_extents[i].ext_len;
210 }
211 return 0;
212 } else if (buf->i_len == len64) {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000213 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000214
215 dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
216 dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
217 dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
218 dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
219 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
220 dst_efi_fmt->efi_extents[i].ext_start =
221 src_efi_fmt_64->efi_extents[i].ext_start;
222 dst_efi_fmt->efi_extents[i].ext_len =
223 src_efi_fmt_64->efi_extents[i].ext_len;
224 }
225 return 0;
226 }
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700227 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
Dave Chinner24513372014-06-25 14:58:08 +1000228 return -EFSCORRUPTED;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000229}
230
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000231static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
Christoph Hellwig7d795ca2005-06-21 15:41:19 +1000232{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000233 return container_of(lip, struct xfs_efd_log_item, efd_item);
234}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000236STATIC void
237xfs_efd_item_free(struct xfs_efd_log_item *efdp)
238{
Dave Chinnerb1c5ebb2016-07-22 09:52:35 +1000239 kmem_free(efdp->efd_item.li_lv_shadow);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000240 if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000241 kmem_free(efdp);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000242 else
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800243 kmem_cache_free(xfs_efd_zone, efdp);
Christoph Hellwig7d795ca2005-06-21 15:41:19 +1000244}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246/*
247 * This returns the number of iovecs needed to log the given efd item.
248 * We only need 1 iovec for an efd item. It just logs the efd_log_format
249 * structure.
250 */
Dave Chinner166d1362013-08-12 20:50:04 +1000251static inline int
252xfs_efd_item_sizeof(
253 struct xfs_efd_log_item *efdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Dave Chinner166d1362013-08-12 20:50:04 +1000255 return sizeof(xfs_efd_log_format_t) +
256 (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
257}
258
259STATIC void
260xfs_efd_item_size(
261 struct xfs_log_item *lip,
262 int *nvecs,
263 int *nbytes)
264{
265 *nvecs += 1;
266 *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269/*
270 * This is called to fill in the vector of log iovecs for the
271 * given efd log item. We use only 1 iovec, and we point that
272 * at the efd_log_format structure embedded in the efd item.
273 * It is at this point that we assert that all of the extent
274 * slots in the efd item have been filled.
275 */
276STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000277xfs_efd_item_format(
278 struct xfs_log_item *lip,
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100279 struct xfs_log_vec *lv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000281 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100282 struct xfs_log_iovec *vecp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
285
286 efdp->efd_format.efd_type = XFS_LI_EFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 efdp->efd_format.efd_size = 1;
288
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100289 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
Christoph Hellwig12343512013-12-13 11:00:43 +1100290 &efdp->efd_format,
291 xfs_efd_item_sizeof(efdp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/*
Brian Foster8d99fe92015-08-19 09:51:16 +1000295 * The EFD is either committed or aborted if the transaction is cancelled. If
296 * the transaction is cancelled, drop our reference to the EFI and free the EFD.
297 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298STATIC void
Christoph Hellwigddf92052019-06-28 19:27:32 -0700299xfs_efd_item_release(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000300 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Brian Foster8d99fe92015-08-19 09:51:16 +1000302 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
303
Christoph Hellwigddf92052019-06-28 19:27:32 -0700304 xfs_efi_release(efdp->efd_efip);
305 xfs_efd_item_free(efdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000308static const struct xfs_item_ops xfs_efd_item_ops = {
Christoph Hellwig9ce632a2019-06-28 19:27:32 -0700309 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000310 .iop_size = xfs_efd_item_size,
311 .iop_format = xfs_efd_item_format,
Christoph Hellwigddf92052019-06-28 19:27:32 -0700312 .iop_release = xfs_efd_item_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313};
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/*
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700316 * Allocate an "extent free done" log item that will hold nextents worth of
317 * extents. The caller must use all nextents extents, because we are not
318 * flexible about this at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
Christoph Hellwig81f40042019-06-28 19:28:17 -0700320static struct xfs_efd_log_item *
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700321xfs_trans_get_efd(
322 struct xfs_trans *tp,
323 struct xfs_efi_log_item *efip,
324 unsigned int nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700326 struct xfs_efd_log_item *efdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 ASSERT(nextents > 0);
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700331 efdp = kmem_zalloc(sizeof(struct xfs_efd_log_item) +
332 (nextents - 1) * sizeof(struct xfs_extent),
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700333 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 } else {
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700335 efdp = kmem_zone_zalloc(xfs_efd_zone, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700338 xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD,
339 &xfs_efd_item_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 efdp->efd_efip = efip;
341 efdp->efd_format.efd_nextents = nextents;
342 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
343
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700344 xfs_trans_add_item(tp, &efdp->efd_item);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000345 return efdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
Darrick J. Wongdc423752016-08-03 11:23:49 +1000347
348/*
Christoph Hellwig81f40042019-06-28 19:28:17 -0700349 * Free an extent and log it to the EFD. Note that the transaction is marked
350 * dirty regardless of whether the extent free succeeds or fails to support the
351 * EFI/EFD lifecycle rules.
352 */
353static int
354xfs_trans_free_extent(
355 struct xfs_trans *tp,
356 struct xfs_efd_log_item *efdp,
357 xfs_fsblock_t start_block,
358 xfs_extlen_t ext_len,
359 const struct xfs_owner_info *oinfo,
360 bool skip_discard)
361{
362 struct xfs_mount *mp = tp->t_mountp;
363 struct xfs_extent *extp;
364 uint next_extent;
365 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, start_block);
366 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp,
367 start_block);
368 int error;
369
370 trace_xfs_bmap_free_deferred(tp->t_mountp, agno, 0, agbno, ext_len);
371
372 error = __xfs_free_extent(tp, start_block, ext_len,
373 oinfo, XFS_AG_RESV_NONE, skip_discard);
374 /*
375 * Mark the transaction dirty, even on error. This ensures the
376 * transaction is aborted, which:
377 *
378 * 1.) releases the EFI and frees the EFD
379 * 2.) shuts down the filesystem
380 */
381 tp->t_flags |= XFS_TRANS_DIRTY;
382 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
383
384 next_extent = efdp->efd_next_extent;
385 ASSERT(next_extent < efdp->efd_format.efd_nextents);
386 extp = &(efdp->efd_format.efd_extents[next_extent]);
387 extp->ext_start = start_block;
388 extp->ext_len = ext_len;
389 efdp->efd_next_extent++;
390
391 return error;
392}
393
394/* Sort bmap items by AG. */
395static int
396xfs_extent_free_diff_items(
397 void *priv,
398 struct list_head *a,
399 struct list_head *b)
400{
401 struct xfs_mount *mp = priv;
402 struct xfs_extent_free_item *ra;
403 struct xfs_extent_free_item *rb;
404
405 ra = container_of(a, struct xfs_extent_free_item, xefi_list);
406 rb = container_of(b, struct xfs_extent_free_item, xefi_list);
407 return XFS_FSB_TO_AGNO(mp, ra->xefi_startblock) -
408 XFS_FSB_TO_AGNO(mp, rb->xefi_startblock);
409}
410
Christoph Hellwig81f40042019-06-28 19:28:17 -0700411/* Log a free extent to the intent item. */
412STATIC void
413xfs_extent_free_log_item(
414 struct xfs_trans *tp,
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700415 struct xfs_efi_log_item *efip,
416 struct xfs_extent_free_item *free)
Christoph Hellwig81f40042019-06-28 19:28:17 -0700417{
Christoph Hellwig81f40042019-06-28 19:28:17 -0700418 uint next_extent;
419 struct xfs_extent *extp;
420
Christoph Hellwig81f40042019-06-28 19:28:17 -0700421 tp->t_flags |= XFS_TRANS_DIRTY;
422 set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
423
424 /*
425 * atomic_inc_return gives us the value after the increment;
426 * we want to use it as an array index so we need to subtract 1 from
427 * it.
428 */
429 next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
430 ASSERT(next_extent < efip->efi_format.efi_nextents);
431 extp = &efip->efi_format.efi_extents[next_extent];
432 extp->ext_start = free->xefi_startblock;
433 extp->ext_len = free->xefi_blockcount;
434}
435
Christoph Hellwig13a83332020-04-30 12:52:21 -0700436static struct xfs_log_item *
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700437xfs_extent_free_create_intent(
438 struct xfs_trans *tp,
439 struct list_head *items,
Christoph Hellwigd367a862020-04-30 12:52:20 -0700440 unsigned int count,
441 bool sort)
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700442{
443 struct xfs_mount *mp = tp->t_mountp;
444 struct xfs_efi_log_item *efip = xfs_efi_init(mp, count);
445 struct xfs_extent_free_item *free;
446
447 ASSERT(count > 0);
448
449 xfs_trans_add_item(tp, &efip->efi_item);
Christoph Hellwigd367a862020-04-30 12:52:20 -0700450 if (sort)
451 list_sort(mp, items, xfs_extent_free_diff_items);
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700452 list_for_each_entry(free, items, xefi_list)
453 xfs_extent_free_log_item(tp, efip, free);
Christoph Hellwig13a83332020-04-30 12:52:21 -0700454 return &efip->efi_item;
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700455}
456
Christoph Hellwig81f40042019-06-28 19:28:17 -0700457/* Get an EFD so we can process all the free extents. */
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700458static struct xfs_log_item *
Christoph Hellwig81f40042019-06-28 19:28:17 -0700459xfs_extent_free_create_done(
460 struct xfs_trans *tp,
Christoph Hellwig13a83332020-04-30 12:52:21 -0700461 struct xfs_log_item *intent,
Christoph Hellwig81f40042019-06-28 19:28:17 -0700462 unsigned int count)
463{
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700464 return &xfs_trans_get_efd(tp, EFI_ITEM(intent), count)->efd_item;
Christoph Hellwig81f40042019-06-28 19:28:17 -0700465}
466
467/* Process a free extent. */
468STATIC int
469xfs_extent_free_finish_item(
470 struct xfs_trans *tp,
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700471 struct xfs_log_item *done,
Christoph Hellwig81f40042019-06-28 19:28:17 -0700472 struct list_head *item,
Christoph Hellwig3ec1b262020-04-30 12:52:22 -0700473 struct xfs_btree_cur **state)
Christoph Hellwig81f40042019-06-28 19:28:17 -0700474{
475 struct xfs_extent_free_item *free;
476 int error;
477
478 free = container_of(item, struct xfs_extent_free_item, xefi_list);
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700479 error = xfs_trans_free_extent(tp, EFD_ITEM(done),
Christoph Hellwig81f40042019-06-28 19:28:17 -0700480 free->xefi_startblock,
481 free->xefi_blockcount,
482 &free->xefi_oinfo, free->xefi_skip_discard);
483 kmem_free(free);
484 return error;
485}
486
487/* Abort all pending EFIs. */
488STATIC void
489xfs_extent_free_abort_intent(
Christoph Hellwig13a83332020-04-30 12:52:21 -0700490 struct xfs_log_item *intent)
Christoph Hellwig81f40042019-06-28 19:28:17 -0700491{
Christoph Hellwig13a83332020-04-30 12:52:21 -0700492 xfs_efi_release(EFI_ITEM(intent));
Christoph Hellwig81f40042019-06-28 19:28:17 -0700493}
494
495/* Cancel a free extent. */
496STATIC void
497xfs_extent_free_cancel_item(
498 struct list_head *item)
499{
500 struct xfs_extent_free_item *free;
501
502 free = container_of(item, struct xfs_extent_free_item, xefi_list);
503 kmem_free(free);
504}
505
506const struct xfs_defer_op_type xfs_extent_free_defer_type = {
507 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
Christoph Hellwig81f40042019-06-28 19:28:17 -0700508 .create_intent = xfs_extent_free_create_intent,
509 .abort_intent = xfs_extent_free_abort_intent,
Christoph Hellwig81f40042019-06-28 19:28:17 -0700510 .create_done = xfs_extent_free_create_done,
511 .finish_item = xfs_extent_free_finish_item,
512 .cancel_item = xfs_extent_free_cancel_item,
513};
514
515/*
516 * AGFL blocks are accounted differently in the reserve pools and are not
517 * inserted into the busy extent list.
518 */
519STATIC int
520xfs_agfl_free_finish_item(
521 struct xfs_trans *tp,
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700522 struct xfs_log_item *done,
Christoph Hellwig81f40042019-06-28 19:28:17 -0700523 struct list_head *item,
Christoph Hellwig3ec1b262020-04-30 12:52:22 -0700524 struct xfs_btree_cur **state)
Christoph Hellwig81f40042019-06-28 19:28:17 -0700525{
526 struct xfs_mount *mp = tp->t_mountp;
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700527 struct xfs_efd_log_item *efdp = EFD_ITEM(done);
Christoph Hellwig81f40042019-06-28 19:28:17 -0700528 struct xfs_extent_free_item *free;
529 struct xfs_extent *extp;
530 struct xfs_buf *agbp;
531 int error;
532 xfs_agnumber_t agno;
533 xfs_agblock_t agbno;
534 uint next_extent;
535
536 free = container_of(item, struct xfs_extent_free_item, xefi_list);
537 ASSERT(free->xefi_blockcount == 1);
538 agno = XFS_FSB_TO_AGNO(mp, free->xefi_startblock);
539 agbno = XFS_FSB_TO_AGBNO(mp, free->xefi_startblock);
540
541 trace_xfs_agfl_free_deferred(mp, agno, 0, agbno, free->xefi_blockcount);
542
543 error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
544 if (!error)
545 error = xfs_free_agfl_block(tp, agno, agbno, agbp,
546 &free->xefi_oinfo);
547
548 /*
549 * Mark the transaction dirty, even on error. This ensures the
550 * transaction is aborted, which:
551 *
552 * 1.) releases the EFI and frees the EFD
553 * 2.) shuts down the filesystem
554 */
555 tp->t_flags |= XFS_TRANS_DIRTY;
556 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
557
558 next_extent = efdp->efd_next_extent;
559 ASSERT(next_extent < efdp->efd_format.efd_nextents);
560 extp = &(efdp->efd_format.efd_extents[next_extent]);
561 extp->ext_start = free->xefi_startblock;
562 extp->ext_len = free->xefi_blockcount;
563 efdp->efd_next_extent++;
564
565 kmem_free(free);
566 return error;
567}
568
569/* sub-type with special handling for AGFL deferred frees */
570const struct xfs_defer_op_type xfs_agfl_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_agfl_free_finish_item,
576 .cancel_item = xfs_extent_free_cancel_item,
577};
578
579/*
Darrick J. Wongdc423752016-08-03 11:23:49 +1000580 * Process an extent free intent item that was recovered from
581 * the log. We need to free the extents that it describes.
582 */
Darrick J. Wong10d0c6e2020-05-01 16:00:50 -0700583STATIC int
Darrick J. Wong96b60f82020-05-01 16:00:55 -0700584xfs_efi_item_recover(
585 struct xfs_log_item *lip,
586 struct xfs_trans *parent_tp)
Darrick J. Wongdc423752016-08-03 11:23:49 +1000587{
Darrick J. Wong96b60f82020-05-01 16:00:55 -0700588 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
589 struct xfs_mount *mp = parent_tp->t_mountp;
590 struct xfs_efd_log_item *efdp;
591 struct xfs_trans *tp;
592 struct xfs_extent *extp;
593 xfs_fsblock_t startblock_fsb;
594 int i;
595 int error = 0;
Darrick J. Wongdc423752016-08-03 11:23:49 +1000596
Darrick J. Wongdc423752016-08-03 11:23:49 +1000597 /*
598 * First check the validity of the extents described by the
599 * EFI. If any are bad, then assume that all are bad and
600 * just toss the EFI.
601 */
602 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
Darrick J. Wonge127faf2016-08-03 12:29:32 +1000603 extp = &efip->efi_format.efi_extents[i];
Darrick J. Wongdc423752016-08-03 11:23:49 +1000604 startblock_fsb = XFS_BB_TO_FSB(mp,
605 XFS_FSB_TO_DADDR(mp, extp->ext_start));
Darrick J. Wonge127faf2016-08-03 12:29:32 +1000606 if (startblock_fsb == 0 ||
607 extp->ext_len == 0 ||
608 startblock_fsb >= mp->m_sb.sb_dblocks ||
609 extp->ext_len >= mp->m_sb.sb_agblocks) {
Darrick J. Wongdc423752016-08-03 11:23:49 +1000610 /*
611 * This will pull the EFI from the AIL and
612 * free the memory associated with it.
613 */
Darrick J. Wongdc423752016-08-03 11:23:49 +1000614 xfs_efi_release(efip);
Darrick J. Wong895e1962019-11-06 09:17:43 -0800615 return -EFSCORRUPTED;
Darrick J. Wongdc423752016-08-03 11:23:49 +1000616 }
617 }
618
619 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
620 if (error)
621 return error;
622 efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
623
624 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
Darrick J. Wonge127faf2016-08-03 12:29:32 +1000625 extp = &efip->efi_format.efi_extents[i];
Darrick J. Wongdc423752016-08-03 11:23:49 +1000626 error = xfs_trans_free_extent(tp, efdp, extp->ext_start,
Darrick J. Wong7280fed2018-12-12 08:46:23 -0800627 extp->ext_len,
628 &XFS_RMAP_OINFO_ANY_OWNER, false);
Darrick J. Wongdc423752016-08-03 11:23:49 +1000629 if (error)
630 goto abort_error;
631
632 }
633
Darrick J. Wongdc423752016-08-03 11:23:49 +1000634 error = xfs_trans_commit(tp);
635 return error;
636
637abort_error:
638 xfs_trans_cancel(tp);
639 return error;
640}
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700641
Darrick J. Wong154c7332020-05-01 16:00:54 -0700642STATIC bool
643xfs_efi_item_match(
644 struct xfs_log_item *lip,
645 uint64_t intent_id)
646{
647 return EFI_ITEM(lip)->efi_format.efi_id == intent_id;
648}
649
Darrick J. Wong10d0c6e2020-05-01 16:00:50 -0700650static const struct xfs_item_ops xfs_efi_item_ops = {
651 .iop_size = xfs_efi_item_size,
652 .iop_format = xfs_efi_item_format,
653 .iop_unpin = xfs_efi_item_unpin,
654 .iop_release = xfs_efi_item_release,
655 .iop_recover = xfs_efi_item_recover,
Darrick J. Wong154c7332020-05-01 16:00:54 -0700656 .iop_match = xfs_efi_item_match,
Darrick J. Wong10d0c6e2020-05-01 16:00:50 -0700657};
658
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700659/*
660 * This routine is called to create an in-core extent free intent
661 * item from the efi format structure which was logged on disk.
662 * It allocates an in-core efi, copies the extents from the format
663 * structure into it, and adds the efi to the AIL with the given
664 * LSN.
665 */
666STATIC int
667xlog_recover_efi_commit_pass2(
668 struct xlog *log,
669 struct list_head *buffer_list,
670 struct xlog_recover_item *item,
671 xfs_lsn_t lsn)
672{
673 struct xfs_mount *mp = log->l_mp;
674 struct xfs_efi_log_item *efip;
675 struct xfs_efi_log_format *efi_formatp;
676 int error;
677
678 efi_formatp = item->ri_buf[0].i_addr;
679
680 efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
681 error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
682 if (error) {
683 xfs_efi_item_free(efip);
684 return error;
685 }
686 atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700687 /*
Darrick J. Wong86a37172020-05-01 16:00:54 -0700688 * Insert the intent into the AIL directly and drop one reference so
689 * that finishing or canceling the work will drop the other.
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700690 */
Darrick J. Wong86a37172020-05-01 16:00:54 -0700691 xfs_trans_ail_insert(log->l_ailp, &efip->efi_item, lsn);
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700692 xfs_efi_release(efip);
693 return 0;
694}
695
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700696const struct xlog_recover_item_ops xlog_efi_item_ops = {
697 .item_type = XFS_LI_EFI,
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700698 .commit_pass2 = xlog_recover_efi_commit_pass2,
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700699};
700
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700701/*
702 * This routine is called when an EFD format structure is found in a committed
703 * transaction in the log. Its purpose is to cancel the corresponding EFI if it
704 * was still in the log. To do this it searches the AIL for the EFI with an id
705 * equal to that in the EFD format structure. If we find it we drop the EFD
706 * reference, which removes the EFI from the AIL and frees it.
707 */
708STATIC int
709xlog_recover_efd_commit_pass2(
710 struct xlog *log,
711 struct list_head *buffer_list,
712 struct xlog_recover_item *item,
713 xfs_lsn_t lsn)
714{
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700715 struct xfs_efd_log_format *efd_formatp;
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700716
717 efd_formatp = item->ri_buf[0].i_addr;
718 ASSERT((item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_32_t) +
719 ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_32_t)))) ||
720 (item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_64_t) +
721 ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_64_t)))));
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700722
Darrick J. Wong154c7332020-05-01 16:00:54 -0700723 xlog_recover_release_intent(log, XFS_LI_EFI, efd_formatp->efd_efi_id);
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700724 return 0;
725}
726
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700727const struct xlog_recover_item_ops xlog_efd_item_ops = {
728 .item_type = XFS_LI_EFD,
Darrick J. Wong9817aa82020-05-01 16:00:48 -0700729 .commit_pass2 = xlog_recover_efd_commit_pass2,
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700730};