blob: e44efc41a04121832c60dd8a05bcb96b4666e15a [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25
26kmem_zone_t *xfs_efi_zone;
27kmem_zone_t *xfs_efd_zone;
28
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100029static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
30{
31 return container_of(lip, struct xfs_efi_log_item, efi_item);
32}
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Christoph Hellwig7d795ca2005-06-21 15:41:19 +100034void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100035xfs_efi_item_free(
36 struct xfs_efi_log_item *efip)
Christoph Hellwig7d795ca2005-06-21 15:41:19 +100037{
Dave Chinnerb1c5ebb2016-07-22 09:52:35 +100038 kmem_free(efip->efi_item.li_lv_shadow);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100039 if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +100040 kmem_free(efip);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100041 else
Christoph Hellwig7d795ca2005-06-21 15:41:19 +100042 kmem_zone_free(xfs_efi_zone, efip);
Christoph Hellwig7d795ca2005-06-21 15:41:19 +100043}
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
Dave Chinner0612d112018-04-02 20:08:27 -070046 * Freeing the efi requires that we remove it from the AIL if it has already
47 * been placed there. However, the EFI may not yet have been placed in the AIL
48 * when called by xfs_efi_release() from EFD processing due to the ordering of
49 * committed vs unpin operations in bulk insert operations. Hence the reference
50 * count to ensure only the last caller frees the EFI.
51 */
52void
53xfs_efi_release(
54 struct xfs_efi_log_item *efip)
55{
56 ASSERT(atomic_read(&efip->efi_refcount) > 0);
57 if (atomic_dec_and_test(&efip->efi_refcount)) {
58 xfs_trans_ail_remove(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR);
59 xfs_efi_item_free(efip);
60 }
61}
62
63/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * This returns the number of iovecs needed to log the given efi item.
65 * We only need 1 iovec for an efi item. It just logs the efi_log_format
66 * structure.
67 */
Dave Chinner166d1362013-08-12 20:50:04 +100068static inline int
69xfs_efi_item_sizeof(
70 struct xfs_efi_log_item *efip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Dave Chinner166d1362013-08-12 20:50:04 +100072 return sizeof(struct xfs_efi_log_format) +
73 (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
74}
75
76STATIC void
77xfs_efi_item_size(
78 struct xfs_log_item *lip,
79 int *nvecs,
80 int *nbytes)
81{
82 *nvecs += 1;
83 *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip));
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86/*
87 * This is called to fill in the vector of log iovecs for the
88 * given efi log item. We use only 1 iovec, and we point that
89 * at the efi_log_format structure embedded in the efi item.
90 * It is at this point that we assert that all of the extent
91 * slots in the efi item have been filled.
92 */
93STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100094xfs_efi_item_format(
95 struct xfs_log_item *lip,
Christoph Hellwigbde7cff2013-12-13 11:34:02 +110096 struct xfs_log_vec *lv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100098 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
Christoph Hellwigbde7cff2013-12-13 11:34:02 +110099 struct xfs_log_iovec *vecp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Dave Chinnerb199c8a2010-12-20 11:59:49 +1100101 ASSERT(atomic_read(&efip->efi_next_extent) ==
102 efip->efi_format.efi_nextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 efip->efi_format.efi_type = XFS_LI_EFI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 efip->efi_format.efi_size = 1;
106
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100107 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
Christoph Hellwig12343512013-12-13 11:00:43 +1100108 &efip->efi_format,
109 xfs_efi_item_sizeof(efip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112
113/*
Brian Foster8d99fe92015-08-19 09:51:16 +1000114 * The unpin operation is the last place an EFI is manipulated in the log. It is
115 * either inserted in the AIL or aborted in the event of a log I/O error. In
116 * either case, the EFI transaction has been successfully committed to make it
117 * this far. Therefore, we expect whoever committed the EFI to either construct
118 * and commit the EFD or drop the EFD's reference in the event of error. Simply
119 * drop the log's EFI reference now that the log is done with it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000122xfs_efi_item_unpin(
123 struct xfs_log_item *lip,
124 int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000126 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
Brian Foster5e4b5382015-08-19 09:50:12 +1000127 xfs_efi_release(efip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130/*
Brian Foster8d99fe92015-08-19 09:51:16 +1000131 * The EFI has been either committed or aborted if the transaction has been
132 * cancelled. If the transaction was cancelled, an EFD isn't going to be
133 * constructed and thus we free the EFI here directly.
134 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135STATIC void
Christoph Hellwigddf92052019-06-28 19:27:32 -0700136xfs_efi_item_release(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000137 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Christoph Hellwigddf92052019-06-28 19:27:32 -0700139 xfs_efi_release(EFI_ITEM(lip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000142static const struct xfs_item_ops xfs_efi_item_ops = {
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000143 .iop_size = xfs_efi_item_size,
144 .iop_format = xfs_efi_item_format,
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000145 .iop_unpin = xfs_efi_item_unpin,
Christoph Hellwigddf92052019-06-28 19:27:32 -0700146 .iop_release = xfs_efi_item_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
149
150/*
151 * Allocate and initialize an efi item with the given number of extents.
152 */
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000153struct xfs_efi_log_item *
154xfs_efi_init(
155 struct xfs_mount *mp,
156 uint nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000159 struct xfs_efi_log_item *efip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 uint size;
161
162 ASSERT(nextents > 0);
163 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
164 size = (uint)(sizeof(xfs_efi_log_item_t) +
165 ((nextents - 1) * sizeof(xfs_extent_t)));
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700166 efip = kmem_zalloc(size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 } else {
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700168 efip = kmem_zone_zalloc(xfs_efi_zone, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170
Dave Chinner43f5efc2010-03-23 10:10:00 +1100171 xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 efip->efi_format.efi_nextents = nextents;
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000173 efip->efi_format.efi_id = (uintptr_t)(void *)efip;
Dave Chinnerb199c8a2010-12-20 11:59:49 +1100174 atomic_set(&efip->efi_next_extent, 0);
Dave Chinner666d6442013-04-03 14:09:21 +1100175 atomic_set(&efip->efi_refcount, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000177 return efip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180/*
Tim Shimmin6d192a92006-06-09 14:55:38 +1000181 * Copy an EFI format buffer from the given buf, and into the destination
182 * EFI format structure.
183 * The given buffer can be in 32 bit or 64 bit form (which has different padding),
184 * one of which will be the native format for this kernel.
185 * It will handle the conversion of formats if necessary.
186 */
187int
188xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
189{
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000190 xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000191 uint i;
192 uint len = sizeof(xfs_efi_log_format_t) +
193 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);
194 uint len32 = sizeof(xfs_efi_log_format_32_t) +
195 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);
196 uint len64 = sizeof(xfs_efi_log_format_64_t) +
197 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);
198
199 if (buf->i_len == len) {
200 memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
201 return 0;
202 } else if (buf->i_len == len32) {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000203 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000204
205 dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
206 dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
207 dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
208 dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
209 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
210 dst_efi_fmt->efi_extents[i].ext_start =
211 src_efi_fmt_32->efi_extents[i].ext_start;
212 dst_efi_fmt->efi_extents[i].ext_len =
213 src_efi_fmt_32->efi_extents[i].ext_len;
214 }
215 return 0;
216 } else if (buf->i_len == len64) {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000217 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000218
219 dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
220 dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
221 dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
222 dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
223 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
224 dst_efi_fmt->efi_extents[i].ext_start =
225 src_efi_fmt_64->efi_extents[i].ext_start;
226 dst_efi_fmt->efi_extents[i].ext_len =
227 src_efi_fmt_64->efi_extents[i].ext_len;
228 }
229 return 0;
230 }
Dave Chinner24513372014-06-25 14:58:08 +1000231 return -EFSCORRUPTED;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000232}
233
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000234static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
Christoph Hellwig7d795ca2005-06-21 15:41:19 +1000235{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000236 return container_of(lip, struct xfs_efd_log_item, efd_item);
237}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000239STATIC void
240xfs_efd_item_free(struct xfs_efd_log_item *efdp)
241{
Dave Chinnerb1c5ebb2016-07-22 09:52:35 +1000242 kmem_free(efdp->efd_item.li_lv_shadow);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000243 if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000244 kmem_free(efdp);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000245 else
Christoph Hellwig7d795ca2005-06-21 15:41:19 +1000246 kmem_zone_free(xfs_efd_zone, efdp);
Christoph Hellwig7d795ca2005-06-21 15:41:19 +1000247}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249/*
250 * This returns the number of iovecs needed to log the given efd item.
251 * We only need 1 iovec for an efd item. It just logs the efd_log_format
252 * structure.
253 */
Dave Chinner166d1362013-08-12 20:50:04 +1000254static inline int
255xfs_efd_item_sizeof(
256 struct xfs_efd_log_item *efdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Dave Chinner166d1362013-08-12 20:50:04 +1000258 return sizeof(xfs_efd_log_format_t) +
259 (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
260}
261
262STATIC void
263xfs_efd_item_size(
264 struct xfs_log_item *lip,
265 int *nvecs,
266 int *nbytes)
267{
268 *nvecs += 1;
269 *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272/*
273 * This is called to fill in the vector of log iovecs for the
274 * given efd log item. We use only 1 iovec, and we point that
275 * at the efd_log_format structure embedded in the efd item.
276 * It is at this point that we assert that all of the extent
277 * slots in the efd item have been filled.
278 */
279STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000280xfs_efd_item_format(
281 struct xfs_log_item *lip,
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100282 struct xfs_log_vec *lv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000284 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100285 struct xfs_log_iovec *vecp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
288
289 efdp->efd_format.efd_type = XFS_LI_EFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 efdp->efd_format.efd_size = 1;
291
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100292 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
Christoph Hellwig12343512013-12-13 11:00:43 +1100293 &efdp->efd_format,
294 xfs_efd_item_sizeof(efdp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/*
Brian Foster8d99fe92015-08-19 09:51:16 +1000298 * The EFD is either committed or aborted if the transaction is cancelled. If
299 * the transaction is cancelled, drop our reference to the EFI and free the EFD.
300 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301STATIC void
Christoph Hellwigddf92052019-06-28 19:27:32 -0700302xfs_efd_item_release(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000303 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Brian Foster8d99fe92015-08-19 09:51:16 +1000305 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
306
Christoph Hellwigddf92052019-06-28 19:27:32 -0700307 xfs_efi_release(efdp->efd_efip);
308 xfs_efd_item_free(efdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000311static const struct xfs_item_ops xfs_efd_item_ops = {
Christoph Hellwig9ce632a2019-06-28 19:27:32 -0700312 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000313 .iop_size = xfs_efd_item_size,
314 .iop_format = xfs_efd_item_format,
Christoph Hellwigddf92052019-06-28 19:27:32 -0700315 .iop_release = xfs_efd_item_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316};
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/*
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700319 * Allocate an "extent free done" log item that will hold nextents worth of
320 * extents. The caller must use all nextents extents, because we are not
321 * flexible about this at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
Christoph Hellwig81f40042019-06-28 19:28:17 -0700323static struct xfs_efd_log_item *
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700324xfs_trans_get_efd(
325 struct xfs_trans *tp,
326 struct xfs_efi_log_item *efip,
327 unsigned int nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700329 struct xfs_efd_log_item *efdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 ASSERT(nextents > 0);
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700334 efdp = kmem_zalloc(sizeof(struct xfs_efd_log_item) +
335 (nextents - 1) * sizeof(struct xfs_extent),
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700336 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 } else {
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700338 efdp = kmem_zone_zalloc(xfs_efd_zone, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700341 xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD,
342 &xfs_efd_item_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 efdp->efd_efip = efip;
344 efdp->efd_format.efd_nextents = nextents;
345 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
346
Christoph Hellwig9c5e7c22019-06-28 19:27:35 -0700347 xfs_trans_add_item(tp, &efdp->efd_item);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000348 return efdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
Darrick J. Wongdc423752016-08-03 11:23:49 +1000350
351/*
Christoph Hellwig81f40042019-06-28 19:28:17 -0700352 * Free an extent and log it to the EFD. Note that the transaction is marked
353 * dirty regardless of whether the extent free succeeds or fails to support the
354 * EFI/EFD lifecycle rules.
355 */
356static int
357xfs_trans_free_extent(
358 struct xfs_trans *tp,
359 struct xfs_efd_log_item *efdp,
360 xfs_fsblock_t start_block,
361 xfs_extlen_t ext_len,
362 const struct xfs_owner_info *oinfo,
363 bool skip_discard)
364{
365 struct xfs_mount *mp = tp->t_mountp;
366 struct xfs_extent *extp;
367 uint next_extent;
368 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, start_block);
369 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp,
370 start_block);
371 int error;
372
373 trace_xfs_bmap_free_deferred(tp->t_mountp, agno, 0, agbno, ext_len);
374
375 error = __xfs_free_extent(tp, start_block, ext_len,
376 oinfo, XFS_AG_RESV_NONE, skip_discard);
377 /*
378 * Mark the transaction dirty, even on error. This ensures the
379 * transaction is aborted, which:
380 *
381 * 1.) releases the EFI and frees the EFD
382 * 2.) shuts down the filesystem
383 */
384 tp->t_flags |= XFS_TRANS_DIRTY;
385 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
386
387 next_extent = efdp->efd_next_extent;
388 ASSERT(next_extent < efdp->efd_format.efd_nextents);
389 extp = &(efdp->efd_format.efd_extents[next_extent]);
390 extp->ext_start = start_block;
391 extp->ext_len = ext_len;
392 efdp->efd_next_extent++;
393
394 return error;
395}
396
397/* Sort bmap items by AG. */
398static int
399xfs_extent_free_diff_items(
400 void *priv,
401 struct list_head *a,
402 struct list_head *b)
403{
404 struct xfs_mount *mp = priv;
405 struct xfs_extent_free_item *ra;
406 struct xfs_extent_free_item *rb;
407
408 ra = container_of(a, struct xfs_extent_free_item, xefi_list);
409 rb = container_of(b, struct xfs_extent_free_item, xefi_list);
410 return XFS_FSB_TO_AGNO(mp, ra->xefi_startblock) -
411 XFS_FSB_TO_AGNO(mp, rb->xefi_startblock);
412}
413
414/* Get an EFI. */
415STATIC void *
416xfs_extent_free_create_intent(
417 struct xfs_trans *tp,
418 unsigned int count)
419{
420 struct xfs_efi_log_item *efip;
421
422 ASSERT(tp != NULL);
423 ASSERT(count > 0);
424
425 efip = xfs_efi_init(tp->t_mountp, count);
426 ASSERT(efip != NULL);
427
428 /*
429 * Get a log_item_desc to point at the new item.
430 */
431 xfs_trans_add_item(tp, &efip->efi_item);
432 return efip;
433}
434
435/* Log a free extent to the intent item. */
436STATIC void
437xfs_extent_free_log_item(
438 struct xfs_trans *tp,
439 void *intent,
440 struct list_head *item)
441{
442 struct xfs_efi_log_item *efip = intent;
443 struct xfs_extent_free_item *free;
444 uint next_extent;
445 struct xfs_extent *extp;
446
447 free = container_of(item, struct xfs_extent_free_item, xefi_list);
448
449 tp->t_flags |= XFS_TRANS_DIRTY;
450 set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
451
452 /*
453 * atomic_inc_return gives us the value after the increment;
454 * we want to use it as an array index so we need to subtract 1 from
455 * it.
456 */
457 next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
458 ASSERT(next_extent < efip->efi_format.efi_nextents);
459 extp = &efip->efi_format.efi_extents[next_extent];
460 extp->ext_start = free->xefi_startblock;
461 extp->ext_len = free->xefi_blockcount;
462}
463
464/* Get an EFD so we can process all the free extents. */
465STATIC void *
466xfs_extent_free_create_done(
467 struct xfs_trans *tp,
468 void *intent,
469 unsigned int count)
470{
471 return xfs_trans_get_efd(tp, intent, count);
472}
473
474/* Process a free extent. */
475STATIC int
476xfs_extent_free_finish_item(
477 struct xfs_trans *tp,
478 struct list_head *item,
479 void *done_item,
480 void **state)
481{
482 struct xfs_extent_free_item *free;
483 int error;
484
485 free = container_of(item, struct xfs_extent_free_item, xefi_list);
486 error = xfs_trans_free_extent(tp, done_item,
487 free->xefi_startblock,
488 free->xefi_blockcount,
489 &free->xefi_oinfo, free->xefi_skip_discard);
490 kmem_free(free);
491 return error;
492}
493
494/* Abort all pending EFIs. */
495STATIC void
496xfs_extent_free_abort_intent(
497 void *intent)
498{
499 xfs_efi_release(intent);
500}
501
502/* Cancel a free extent. */
503STATIC void
504xfs_extent_free_cancel_item(
505 struct list_head *item)
506{
507 struct xfs_extent_free_item *free;
508
509 free = container_of(item, struct xfs_extent_free_item, xefi_list);
510 kmem_free(free);
511}
512
513const struct xfs_defer_op_type xfs_extent_free_defer_type = {
514 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
515 .diff_items = xfs_extent_free_diff_items,
516 .create_intent = xfs_extent_free_create_intent,
517 .abort_intent = xfs_extent_free_abort_intent,
518 .log_item = xfs_extent_free_log_item,
519 .create_done = xfs_extent_free_create_done,
520 .finish_item = xfs_extent_free_finish_item,
521 .cancel_item = xfs_extent_free_cancel_item,
522};
523
524/*
525 * AGFL blocks are accounted differently in the reserve pools and are not
526 * inserted into the busy extent list.
527 */
528STATIC int
529xfs_agfl_free_finish_item(
530 struct xfs_trans *tp,
531 struct list_head *item,
532 void *done_item,
533 void **state)
534{
535 struct xfs_mount *mp = tp->t_mountp;
536 struct xfs_efd_log_item *efdp = done_item;
537 struct xfs_extent_free_item *free;
538 struct xfs_extent *extp;
539 struct xfs_buf *agbp;
540 int error;
541 xfs_agnumber_t agno;
542 xfs_agblock_t agbno;
543 uint next_extent;
544
545 free = container_of(item, struct xfs_extent_free_item, xefi_list);
546 ASSERT(free->xefi_blockcount == 1);
547 agno = XFS_FSB_TO_AGNO(mp, free->xefi_startblock);
548 agbno = XFS_FSB_TO_AGBNO(mp, free->xefi_startblock);
549
550 trace_xfs_agfl_free_deferred(mp, agno, 0, agbno, free->xefi_blockcount);
551
552 error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
553 if (!error)
554 error = xfs_free_agfl_block(tp, agno, agbno, agbp,
555 &free->xefi_oinfo);
556
557 /*
558 * Mark the transaction dirty, even on error. This ensures the
559 * transaction is aborted, which:
560 *
561 * 1.) releases the EFI and frees the EFD
562 * 2.) shuts down the filesystem
563 */
564 tp->t_flags |= XFS_TRANS_DIRTY;
565 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
566
567 next_extent = efdp->efd_next_extent;
568 ASSERT(next_extent < efdp->efd_format.efd_nextents);
569 extp = &(efdp->efd_format.efd_extents[next_extent]);
570 extp->ext_start = free->xefi_startblock;
571 extp->ext_len = free->xefi_blockcount;
572 efdp->efd_next_extent++;
573
574 kmem_free(free);
575 return error;
576}
577
578/* sub-type with special handling for AGFL deferred frees */
579const struct xfs_defer_op_type xfs_agfl_free_defer_type = {
580 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
581 .diff_items = xfs_extent_free_diff_items,
582 .create_intent = xfs_extent_free_create_intent,
583 .abort_intent = xfs_extent_free_abort_intent,
584 .log_item = xfs_extent_free_log_item,
585 .create_done = xfs_extent_free_create_done,
586 .finish_item = xfs_agfl_free_finish_item,
587 .cancel_item = xfs_extent_free_cancel_item,
588};
589
590/*
Darrick J. Wongdc423752016-08-03 11:23:49 +1000591 * Process an extent free intent item that was recovered from
592 * the log. We need to free the extents that it describes.
593 */
594int
595xfs_efi_recover(
596 struct xfs_mount *mp,
597 struct xfs_efi_log_item *efip)
598{
599 struct xfs_efd_log_item *efdp;
600 struct xfs_trans *tp;
601 int i;
602 int error = 0;
603 xfs_extent_t *extp;
604 xfs_fsblock_t startblock_fsb;
605
606 ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags));
607
608 /*
609 * First check the validity of the extents described by the
610 * EFI. If any are bad, then assume that all are bad and
611 * just toss the EFI.
612 */
613 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
Darrick J. Wonge127faf2016-08-03 12:29:32 +1000614 extp = &efip->efi_format.efi_extents[i];
Darrick J. Wongdc423752016-08-03 11:23:49 +1000615 startblock_fsb = XFS_BB_TO_FSB(mp,
616 XFS_FSB_TO_DADDR(mp, extp->ext_start));
Darrick J. Wonge127faf2016-08-03 12:29:32 +1000617 if (startblock_fsb == 0 ||
618 extp->ext_len == 0 ||
619 startblock_fsb >= mp->m_sb.sb_dblocks ||
620 extp->ext_len >= mp->m_sb.sb_agblocks) {
Darrick J. Wongdc423752016-08-03 11:23:49 +1000621 /*
622 * This will pull the EFI from the AIL and
623 * free the memory associated with it.
624 */
625 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
626 xfs_efi_release(efip);
627 return -EIO;
628 }
629 }
630
631 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
632 if (error)
633 return error;
634 efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
635
636 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
Darrick J. Wonge127faf2016-08-03 12:29:32 +1000637 extp = &efip->efi_format.efi_extents[i];
Darrick J. Wongdc423752016-08-03 11:23:49 +1000638 error = xfs_trans_free_extent(tp, efdp, extp->ext_start,
Darrick J. Wong7280fed2018-12-12 08:46:23 -0800639 extp->ext_len,
640 &XFS_RMAP_OINFO_ANY_OWNER, false);
Darrick J. Wongdc423752016-08-03 11:23:49 +1000641 if (error)
642 goto abort_error;
643
644 }
645
646 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
647 error = xfs_trans_commit(tp);
648 return error;
649
650abort_error:
651 xfs_trans_cancel(tp);
652 return error;
653}