Merge tag 'kvm-s390-next-6.4-2' of https://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / fs / xfs / xfs_extfree_item.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
7b718769
NS
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
1da177e4 5 */
1da177e4 6#include "xfs.h"
a844f451 7#include "xfs_fs.h"
4fb6e8ad 8#include "xfs_format.h"
239880ef
DC
9#include "xfs_log_format.h"
10#include "xfs_trans_resv.h"
dc42375d 11#include "xfs_bit.h"
5467b34b 12#include "xfs_shared.h"
1da177e4 13#include "xfs_mount.h"
08d3e84f 14#include "xfs_ag.h"
81f40041 15#include "xfs_defer.h"
239880ef 16#include "xfs_trans.h"
1da177e4
LT
17#include "xfs_trans_priv.h"
18#include "xfs_extfree_item.h"
1234351c 19#include "xfs_log.h"
340785cc
DW
20#include "xfs_btree.h"
21#include "xfs_rmap.h"
81f40041
CH
22#include "xfs_alloc.h"
23#include "xfs_bmap.h"
24#include "xfs_trace.h"
a5155b87 25#include "xfs_error.h"
9817aa80 26#include "xfs_log_priv.h"
86ffa471 27#include "xfs_log_recover.h"
1da177e4 28
182696fb
DW
29struct kmem_cache *xfs_efi_cache;
30struct kmem_cache *xfs_efd_cache;
1da177e4 31
10d0c6e0
DW
32static const struct xfs_item_ops xfs_efi_item_ops;
33
7bfa31d8
CH
34static 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}
1da177e4 38
9817aa80 39STATIC void
7bfa31d8
CH
40xfs_efi_item_free(
41 struct xfs_efi_log_item *efip)
7d795ca3 42{
b1c5ebb2 43 kmem_free(efip->efi_item.li_lv_shadow);
7bfa31d8 44 if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
f0e2d93c 45 kmem_free(efip);
7bfa31d8 46 else
182696fb 47 kmem_cache_free(xfs_efi_cache, efip);
7d795ca3 48}
1da177e4 49
0612d116
DC
50/*
51 * 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 */
10d0c6e0 57STATIC void
0612d116
DC
58xfs_efi_release(
59 struct xfs_efi_log_item *efip)
60{
61 ASSERT(atomic_read(&efip->efi_refcount) > 0);
3512fc1e
DC
62 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);
0612d116
DC
67}
68
166d1368 69STATIC void
7bfa31d8 70xfs_efi_item_size(
166d1368
DC
71 struct xfs_log_item *lip,
72 int *nvecs,
73 int *nbytes)
1da177e4 74{
3c5aaace
DW
75 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
76
166d1368 77 *nvecs += 1;
3c5aaace 78 *nbytes += xfs_efi_log_format_sizeof(efip->efi_format.efi_nextents);
1da177e4
LT
79}
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
7bfa31d8
CH
89xfs_efi_item_format(
90 struct xfs_log_item *lip,
bde7cff6 91 struct xfs_log_vec *lv)
1da177e4 92{
7bfa31d8 93 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
bde7cff6 94 struct xfs_log_iovec *vecp = NULL;
1da177e4 95
b199c8a4
DC
96 ASSERT(atomic_read(&efip->efi_next_extent) ==
97 efip->efi_format.efi_nextents);
1da177e4
LT
98
99 efip->efi_format.efi_type = XFS_LI_EFI;
1da177e4
LT
100 efip->efi_format.efi_size = 1;
101
bde7cff6 102 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
1234351c 103 &efip->efi_format,
3c5aaace 104 xfs_efi_log_format_sizeof(efip->efi_format.efi_nextents));
1da177e4
LT
105}
106
107
1da177e4 108/*
8d99fe92
BF
109 * 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.
1da177e4 115 */
1da177e4 116STATIC void
7bfa31d8
CH
117xfs_efi_item_unpin(
118 struct xfs_log_item *lip,
119 int remove)
1da177e4 120{
7bfa31d8 121 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
5e4b5386 122 xfs_efi_release(efip);
1da177e4
LT
123}
124
8d99fe92
BF
125/*
126 * 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 */
1da177e4 130STATIC void
ddf92053 131xfs_efi_item_release(
7bfa31d8 132 struct xfs_log_item *lip)
1da177e4 133{
ddf92053 134 xfs_efi_release(EFI_ITEM(lip));
1da177e4
LT
135}
136
1da177e4
LT
137/*
138 * Allocate and initialize an efi item with the given number of extents.
139 */
9817aa80 140STATIC struct xfs_efi_log_item *
7bfa31d8
CH
141xfs_efi_init(
142 struct xfs_mount *mp,
143 uint nextents)
1da177e4
LT
144
145{
7bfa31d8 146 struct xfs_efi_log_item *efip;
1da177e4
LT
147
148 ASSERT(nextents > 0);
149 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
3c5aaace
DW
150 efip = kzalloc(xfs_efi_log_item_sizeof(nextents),
151 GFP_KERNEL | __GFP_NOFAIL);
1da177e4 152 } else {
182696fb 153 efip = kmem_cache_zalloc(xfs_efi_cache,
32a2b11f 154 GFP_KERNEL | __GFP_NOFAIL);
1da177e4
LT
155 }
156
43f5efc5 157 xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
1da177e4 158 efip->efi_format.efi_nextents = nextents;
db9d67d6 159 efip->efi_format.efi_id = (uintptr_t)(void *)efip;
b199c8a4 160 atomic_set(&efip->efi_next_extent, 0);
666d644c 161 atomic_set(&efip->efi_refcount, 2);
1da177e4 162
7bfa31d8 163 return efip;
1da177e4
LT
164}
165
6d192a9b
TS
166/*
167 * 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 */
9817aa80 173STATIC int
6d192a9b
TS
174xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
175{
4e0d5f92 176 xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
6d192a9b 177 uint i;
3c5aaace
DW
178 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);
6d192a9b
TS
181
182 if (buf->i_len == len) {
03a7485c
DW
183 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));
6d192a9b
TS
189 return 0;
190 } else if (buf->i_len == len32) {
4e0d5f92 191 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
6d192a9b
TS
192
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) {
4e0d5f92 205 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
6d192a9b
TS
206
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 }
950f0d50
DW
219 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, NULL, buf->i_addr,
220 buf->i_len);
2451337d 221 return -EFSCORRUPTED;
6d192a9b
TS
222}
223
7bfa31d8 224static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
7d795ca3 225{
7bfa31d8
CH
226 return container_of(lip, struct xfs_efd_log_item, efd_item);
227}
1da177e4 228
7bfa31d8
CH
229STATIC void
230xfs_efd_item_free(struct xfs_efd_log_item *efdp)
231{
b1c5ebb2 232 kmem_free(efdp->efd_item.li_lv_shadow);
7bfa31d8 233 if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
f0e2d93c 234 kmem_free(efdp);
7bfa31d8 235 else
182696fb 236 kmem_cache_free(xfs_efd_cache, efdp);
7d795ca3 237}
1da177e4 238
166d1368 239STATIC void
7bfa31d8 240xfs_efd_item_size(
166d1368
DC
241 struct xfs_log_item *lip,
242 int *nvecs,
243 int *nbytes)
1da177e4 244{
3c5aaace
DW
245 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
246
166d1368 247 *nvecs += 1;
3c5aaace 248 *nbytes += xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents);
1da177e4
LT
249}
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
7bfa31d8
CH
259xfs_efd_item_format(
260 struct xfs_log_item *lip,
bde7cff6 261 struct xfs_log_vec *lv)
1da177e4 262{
7bfa31d8 263 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
bde7cff6 264 struct xfs_log_iovec *vecp = NULL;
1da177e4
LT
265
266 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
267
268 efdp->efd_format.efd_type = XFS_LI_EFD;
1da177e4
LT
269 efdp->efd_format.efd_size = 1;
270
bde7cff6 271 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
1234351c 272 &efdp->efd_format,
3c5aaace 273 xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents));
1da177e4
LT
274}
275
8d99fe92
BF
276/*
277 * 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 */
1da177e4 280STATIC void
ddf92053 281xfs_efd_item_release(
7bfa31d8 282 struct xfs_log_item *lip)
1da177e4 283{
8d99fe92
BF
284 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
285
ddf92053
CH
286 xfs_efi_release(efdp->efd_efip);
287 xfs_efd_item_free(efdp);
1da177e4
LT
288}
289
c23ab603
DC
290static 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
272e42b2 297static const struct xfs_item_ops xfs_efd_item_ops = {
f5b81200
DC
298 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED |
299 XFS_ITEM_INTENT_DONE,
7bfa31d8
CH
300 .iop_size = xfs_efd_item_size,
301 .iop_format = xfs_efd_item_format,
ddf92053 302 .iop_release = xfs_efd_item_release,
c23ab603 303 .iop_intent = xfs_efd_item_intent,
1da177e4
LT
304};
305
1da177e4 306/*
9c5e7c2a
CH
307 * 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.
1da177e4 310 */
81f40041 311static struct xfs_efd_log_item *
9c5e7c2a
CH
312xfs_trans_get_efd(
313 struct xfs_trans *tp,
314 struct xfs_efi_log_item *efip,
315 unsigned int nextents)
1da177e4 316{
9c5e7c2a 317 struct xfs_efd_log_item *efdp;
1da177e4
LT
318
319 ASSERT(nextents > 0);
9c5e7c2a 320
1da177e4 321 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
3c5aaace
DW
322 efdp = kzalloc(xfs_efd_log_item_sizeof(nextents),
323 GFP_KERNEL | __GFP_NOFAIL);
1da177e4 324 } else {
182696fb 325 efdp = kmem_cache_zalloc(xfs_efd_cache,
32a2b11f 326 GFP_KERNEL | __GFP_NOFAIL);
1da177e4
LT
327 }
328
9c5e7c2a
CH
329 xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD,
330 &xfs_efd_item_ops);
1da177e4
LT
331 efdp->efd_efip = efip;
332 efdp->efd_format.efd_nextents = nextents;
333 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
334
9c5e7c2a 335 xfs_trans_add_item(tp, &efdp->efd_item);
7bfa31d8 336 return efdp;
1da177e4 337}
dc42375d 338
81f40041
CH
339/*
340 * Free an extent and log it to the EFD. Note that the transaction is marked
341 * dirty regardless of whether the extent free succeeds or fails to support the
342 * EFI/EFD lifecycle rules.
343 */
344static int
345xfs_trans_free_extent(
346 struct xfs_trans *tp,
347 struct xfs_efd_log_item *efdp,
578c714b 348 struct xfs_extent_free_item *xefi)
81f40041 349{
72ba4555 350 struct xfs_owner_info oinfo = { };
81f40041
CH
351 struct xfs_mount *mp = tp->t_mountp;
352 struct xfs_extent *extp;
353 uint next_extent;
81f40041 354 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp,
578c714b 355 xefi->xefi_startblock);
81f40041
CH
356 int error;
357
578c714b
DW
358 oinfo.oi_owner = xefi->xefi_owner;
359 if (xefi->xefi_flags & XFS_EFI_ATTR_FORK)
72ba4555 360 oinfo.oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
578c714b 361 if (xefi->xefi_flags & XFS_EFI_BMBT_BLOCK)
72ba4555
DW
362 oinfo.oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
363
f6b38463
DW
364 trace_xfs_bmap_free_deferred(tp->t_mountp, xefi->xefi_pag->pag_agno, 0,
365 agbno, xefi->xefi_blockcount);
81f40041 366
f6b38463
DW
367 error = __xfs_free_extent(tp, xefi->xefi_pag, agbno,
368 xefi->xefi_blockcount, &oinfo, XFS_AG_RESV_NONE,
578c714b 369 xefi->xefi_flags & XFS_EFI_SKIP_DISCARD);
b2ccab31 370
81f40041
CH
371 /*
372 * Mark the transaction dirty, even on error. This ensures the
373 * transaction is aborted, which:
374 *
375 * 1.) releases the EFI and frees the EFD
376 * 2.) shuts down the filesystem
377 */
bb7b1c9c 378 tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
81f40041
CH
379 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
380
381 next_extent = efdp->efd_next_extent;
382 ASSERT(next_extent < efdp->efd_format.efd_nextents);
383 extp = &(efdp->efd_format.efd_extents[next_extent]);
578c714b
DW
384 extp->ext_start = xefi->xefi_startblock;
385 extp->ext_len = xefi->xefi_blockcount;
81f40041
CH
386 efdp->efd_next_extent++;
387
388 return error;
389}
390
391/* Sort bmap items by AG. */
392static int
393xfs_extent_free_diff_items(
394 void *priv,
4f0f586b
ST
395 const struct list_head *a,
396 const struct list_head *b)
81f40041 397{
81f40041
CH
398 struct xfs_extent_free_item *ra;
399 struct xfs_extent_free_item *rb;
400
401 ra = container_of(a, struct xfs_extent_free_item, xefi_list);
402 rb = container_of(b, struct xfs_extent_free_item, xefi_list);
f6b38463
DW
403
404 return ra->xefi_pag->pag_agno - rb->xefi_pag->pag_agno;
81f40041
CH
405}
406
81f40041
CH
407/* Log a free extent to the intent item. */
408STATIC void
409xfs_extent_free_log_item(
410 struct xfs_trans *tp,
c1f09188 411 struct xfs_efi_log_item *efip,
578c714b 412 struct xfs_extent_free_item *xefi)
81f40041 413{
81f40041
CH
414 uint next_extent;
415 struct xfs_extent *extp;
416
81f40041
CH
417 tp->t_flags |= XFS_TRANS_DIRTY;
418 set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
419
420 /*
421 * atomic_inc_return gives us the value after the increment;
422 * we want to use it as an array index so we need to subtract 1 from
423 * it.
424 */
425 next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
426 ASSERT(next_extent < efip->efi_format.efi_nextents);
427 extp = &efip->efi_format.efi_extents[next_extent];
578c714b
DW
428 extp->ext_start = xefi->xefi_startblock;
429 extp->ext_len = xefi->xefi_blockcount;
81f40041
CH
430}
431
13a83333 432static struct xfs_log_item *
c1f09188
CH
433xfs_extent_free_create_intent(
434 struct xfs_trans *tp,
435 struct list_head *items,
d367a868
CH
436 unsigned int count,
437 bool sort)
c1f09188
CH
438{
439 struct xfs_mount *mp = tp->t_mountp;
440 struct xfs_efi_log_item *efip = xfs_efi_init(mp, count);
578c714b 441 struct xfs_extent_free_item *xefi;
c1f09188
CH
442
443 ASSERT(count > 0);
444
445 xfs_trans_add_item(tp, &efip->efi_item);
d367a868
CH
446 if (sort)
447 list_sort(mp, items, xfs_extent_free_diff_items);
578c714b
DW
448 list_for_each_entry(xefi, items, xefi_list)
449 xfs_extent_free_log_item(tp, efip, xefi);
13a83333 450 return &efip->efi_item;
c1f09188
CH
451}
452
81f40041 453/* Get an EFD so we can process all the free extents. */
f09d167c 454static struct xfs_log_item *
81f40041
CH
455xfs_extent_free_create_done(
456 struct xfs_trans *tp,
13a83333 457 struct xfs_log_item *intent,
81f40041
CH
458 unsigned int count)
459{
f09d167c 460 return &xfs_trans_get_efd(tp, EFI_ITEM(intent), count)->efd_item;
81f40041
CH
461}
462
f6b38463
DW
463/* Take a passive ref to the AG containing the space we're freeing. */
464void
465xfs_extent_free_get_group(
466 struct xfs_mount *mp,
467 struct xfs_extent_free_item *xefi)
468{
469 xfs_agnumber_t agno;
470
471 agno = XFS_FSB_TO_AGNO(mp, xefi->xefi_startblock);
d5c88131 472 xefi->xefi_pag = xfs_perag_intent_get(mp, agno);
f6b38463
DW
473}
474
475/* Release a passive AG ref after some freeing work. */
476static inline void
477xfs_extent_free_put_group(
478 struct xfs_extent_free_item *xefi)
479{
d5c88131 480 xfs_perag_intent_put(xefi->xefi_pag);
f6b38463
DW
481}
482
81f40041
CH
483/* Process a free extent. */
484STATIC int
485xfs_extent_free_finish_item(
486 struct xfs_trans *tp,
f09d167c 487 struct xfs_log_item *done,
81f40041 488 struct list_head *item,
3ec1b26c 489 struct xfs_btree_cur **state)
81f40041 490{
578c714b 491 struct xfs_extent_free_item *xefi;
81f40041
CH
492 int error;
493
578c714b 494 xefi = container_of(item, struct xfs_extent_free_item, xefi_list);
72ba4555 495
578c714b 496 error = xfs_trans_free_extent(tp, EFD_ITEM(done), xefi);
f6b38463
DW
497
498 xfs_extent_free_put_group(xefi);
578c714b 499 kmem_cache_free(xfs_extfree_item_cache, xefi);
81f40041
CH
500 return error;
501}
502
503/* Abort all pending EFIs. */
504STATIC void
505xfs_extent_free_abort_intent(
13a83333 506 struct xfs_log_item *intent)
81f40041 507{
13a83333 508 xfs_efi_release(EFI_ITEM(intent));
81f40041
CH
509}
510
511/* Cancel a free extent. */
512STATIC void
513xfs_extent_free_cancel_item(
514 struct list_head *item)
515{
578c714b 516 struct xfs_extent_free_item *xefi;
81f40041 517
578c714b 518 xefi = container_of(item, struct xfs_extent_free_item, xefi_list);
f6b38463
DW
519
520 xfs_extent_free_put_group(xefi);
578c714b 521 kmem_cache_free(xfs_extfree_item_cache, xefi);
81f40041
CH
522}
523
524const struct xfs_defer_op_type xfs_extent_free_defer_type = {
525 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
81f40041
CH
526 .create_intent = xfs_extent_free_create_intent,
527 .abort_intent = xfs_extent_free_abort_intent,
81f40041
CH
528 .create_done = xfs_extent_free_create_done,
529 .finish_item = xfs_extent_free_finish_item,
530 .cancel_item = xfs_extent_free_cancel_item,
531};
532
533/*
534 * AGFL blocks are accounted differently in the reserve pools and are not
535 * inserted into the busy extent list.
536 */
537STATIC int
538xfs_agfl_free_finish_item(
539 struct xfs_trans *tp,
f09d167c 540 struct xfs_log_item *done,
81f40041 541 struct list_head *item,
3ec1b26c 542 struct xfs_btree_cur **state)
81f40041 543{
b3b5ff41 544 struct xfs_owner_info oinfo = { };
81f40041 545 struct xfs_mount *mp = tp->t_mountp;
f09d167c 546 struct xfs_efd_log_item *efdp = EFD_ITEM(done);
578c714b 547 struct xfs_extent_free_item *xefi;
81f40041
CH
548 struct xfs_extent *extp;
549 struct xfs_buf *agbp;
550 int error;
81f40041
CH
551 xfs_agblock_t agbno;
552 uint next_extent;
553
578c714b
DW
554 xefi = container_of(item, struct xfs_extent_free_item, xefi_list);
555 ASSERT(xefi->xefi_blockcount == 1);
578c714b
DW
556 agbno = XFS_FSB_TO_AGBNO(mp, xefi->xefi_startblock);
557 oinfo.oi_owner = xefi->xefi_owner;
81f40041 558
f6b38463
DW
559 trace_xfs_agfl_free_deferred(mp, xefi->xefi_pag->pag_agno, 0, agbno,
560 xefi->xefi_blockcount);
81f40041 561
f6b38463 562 error = xfs_alloc_read_agf(xefi->xefi_pag, tp, 0, &agbp);
81f40041 563 if (!error)
f6b38463
DW
564 error = xfs_free_agfl_block(tp, xefi->xefi_pag->pag_agno,
565 agbno, agbp, &oinfo);
81f40041
CH
566
567 /*
568 * Mark the transaction dirty, even on error. This ensures the
569 * transaction is aborted, which:
570 *
571 * 1.) releases the EFI and frees the EFD
572 * 2.) shuts down the filesystem
573 */
574 tp->t_flags |= XFS_TRANS_DIRTY;
575 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
576
577 next_extent = efdp->efd_next_extent;
578 ASSERT(next_extent < efdp->efd_format.efd_nextents);
579 extp = &(efdp->efd_format.efd_extents[next_extent]);
578c714b
DW
580 extp->ext_start = xefi->xefi_startblock;
581 extp->ext_len = xefi->xefi_blockcount;
81f40041
CH
582 efdp->efd_next_extent++;
583
f6b38463 584 xfs_extent_free_put_group(xefi);
578c714b 585 kmem_cache_free(xfs_extfree_item_cache, xefi);
81f40041
CH
586 return error;
587}
588
589/* sub-type with special handling for AGFL deferred frees */
590const struct xfs_defer_op_type xfs_agfl_free_defer_type = {
591 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
81f40041
CH
592 .create_intent = xfs_extent_free_create_intent,
593 .abort_intent = xfs_extent_free_abort_intent,
81f40041
CH
594 .create_done = xfs_extent_free_create_done,
595 .finish_item = xfs_agfl_free_finish_item,
596 .cancel_item = xfs_extent_free_cancel_item,
597};
598
3c15df3d
DW
599/* Is this recovered EFI ok? */
600static inline bool
601xfs_efi_validate_ext(
602 struct xfs_mount *mp,
603 struct xfs_extent *extp)
604{
67457eb0 605 return xfs_verify_fsbext(mp, extp->ext_start, extp->ext_len);
3c15df3d
DW
606}
607
dc42375d
DW
608/*
609 * Process an extent free intent item that was recovered from
610 * the log. We need to free the extents that it describes.
611 */
10d0c6e0 612STATIC int
96b60f82
DW
613xfs_efi_item_recover(
614 struct xfs_log_item *lip,
e6fff81e 615 struct list_head *capture_list)
dc42375d 616{
96b60f82 617 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
d86142dd 618 struct xfs_mount *mp = lip->li_log->l_mp;
96b60f82
DW
619 struct xfs_efd_log_item *efdp;
620 struct xfs_trans *tp;
96b60f82
DW
621 int i;
622 int error = 0;
dc42375d 623
dc42375d
DW
624 /*
625 * First check the validity of the extents described by the
626 * EFI. If any are bad, then assume that all are bad and
627 * just toss the EFI.
628 */
629 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
3c15df3d
DW
630 if (!xfs_efi_validate_ext(mp,
631 &efip->efi_format.efi_extents[i])) {
632 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
633 &efip->efi_format,
634 sizeof(efip->efi_format));
895e196f 635 return -EFSCORRUPTED;
3c15df3d 636 }
dc42375d
DW
637 }
638
639 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
640 if (error)
641 return error;
642 efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
643
644 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
72ba4555
DW
645 struct xfs_extent_free_item fake = {
646 .xefi_owner = XFS_RMAP_OWN_UNKNOWN,
647 };
648 struct xfs_extent *extp;
649
e127fafd 650 extp = &efip->efi_format.efi_extents[i];
72ba4555
DW
651
652 fake.xefi_startblock = extp->ext_start;
653 fake.xefi_blockcount = extp->ext_len;
654
f6b38463 655 xfs_extent_free_get_group(mp, &fake);
72ba4555 656 error = xfs_trans_free_extent(tp, efdp, &fake);
f6b38463 657 xfs_extent_free_put_group(&fake);
43059d54
DW
658 if (error == -EFSCORRUPTED)
659 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
660 extp, sizeof(*extp));
dc42375d
DW
661 if (error)
662 goto abort_error;
663
664 }
665
512edfac 666 return xfs_defer_ops_capture_and_commit(tp, capture_list);
dc42375d
DW
667
668abort_error:
669 xfs_trans_cancel(tp);
670 return error;
671}
86ffa471 672
154c733a
DW
673STATIC bool
674xfs_efi_item_match(
675 struct xfs_log_item *lip,
676 uint64_t intent_id)
677{
678 return EFI_ITEM(lip)->efi_format.efi_id == intent_id;
679}
680
4e919af7
DW
681/* Relog an intent item to push the log tail forward. */
682static struct xfs_log_item *
683xfs_efi_item_relog(
684 struct xfs_log_item *intent,
685 struct xfs_trans *tp)
686{
687 struct xfs_efd_log_item *efdp;
688 struct xfs_efi_log_item *efip;
689 struct xfs_extent *extp;
690 unsigned int count;
691
692 count = EFI_ITEM(intent)->efi_format.efi_nextents;
693 extp = EFI_ITEM(intent)->efi_format.efi_extents;
694
695 tp->t_flags |= XFS_TRANS_DIRTY;
696 efdp = xfs_trans_get_efd(tp, EFI_ITEM(intent), count);
697 efdp->efd_next_extent = count;
698 memcpy(efdp->efd_format.efd_extents, extp, count * sizeof(*extp));
699 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
700
701 efip = xfs_efi_init(tp->t_mountp, count);
702 memcpy(efip->efi_format.efi_extents, extp, count * sizeof(*extp));
703 atomic_set(&efip->efi_next_extent, count);
704 xfs_trans_add_item(tp, &efip->efi_item);
705 set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
706 return &efip->efi_item;
707}
708
10d0c6e0 709static const struct xfs_item_ops xfs_efi_item_ops = {
f5b81200 710 .flags = XFS_ITEM_INTENT,
10d0c6e0
DW
711 .iop_size = xfs_efi_item_size,
712 .iop_format = xfs_efi_item_format,
713 .iop_unpin = xfs_efi_item_unpin,
714 .iop_release = xfs_efi_item_release,
715 .iop_recover = xfs_efi_item_recover,
154c733a 716 .iop_match = xfs_efi_item_match,
4e919af7 717 .iop_relog = xfs_efi_item_relog,
10d0c6e0
DW
718};
719
9817aa80
DW
720/*
721 * This routine is called to create an in-core extent free intent
722 * item from the efi format structure which was logged on disk.
723 * It allocates an in-core efi, copies the extents from the format
724 * structure into it, and adds the efi to the AIL with the given
725 * LSN.
726 */
727STATIC int
728xlog_recover_efi_commit_pass2(
729 struct xlog *log,
730 struct list_head *buffer_list,
731 struct xlog_recover_item *item,
732 xfs_lsn_t lsn)
733{
734 struct xfs_mount *mp = log->l_mp;
735 struct xfs_efi_log_item *efip;
736 struct xfs_efi_log_format *efi_formatp;
737 int error;
738
739 efi_formatp = item->ri_buf[0].i_addr;
740
3c5aaace 741 if (item->ri_buf[0].i_len < xfs_efi_log_format_sizeof(0)) {
950f0d50
DW
742 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
743 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
03a7485c
DW
744 return -EFSCORRUPTED;
745 }
746
9817aa80
DW
747 efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
748 error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
749 if (error) {
750 xfs_efi_item_free(efip);
751 return error;
752 }
753 atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
9817aa80 754 /*
86a37174
DW
755 * Insert the intent into the AIL directly and drop one reference so
756 * that finishing or canceling the work will drop the other.
9817aa80 757 */
86a37174 758 xfs_trans_ail_insert(log->l_ailp, &efip->efi_item, lsn);
9817aa80
DW
759 xfs_efi_release(efip);
760 return 0;
761}
762
86ffa471
DW
763const struct xlog_recover_item_ops xlog_efi_item_ops = {
764 .item_type = XFS_LI_EFI,
9817aa80 765 .commit_pass2 = xlog_recover_efi_commit_pass2,
86ffa471
DW
766};
767
9817aa80
DW
768/*
769 * This routine is called when an EFD format structure is found in a committed
770 * transaction in the log. Its purpose is to cancel the corresponding EFI if it
771 * was still in the log. To do this it searches the AIL for the EFI with an id
772 * equal to that in the EFD format structure. If we find it we drop the EFD
773 * reference, which removes the EFI from the AIL and frees it.
774 */
775STATIC int
776xlog_recover_efd_commit_pass2(
777 struct xlog *log,
778 struct list_head *buffer_list,
779 struct xlog_recover_item *item,
780 xfs_lsn_t lsn)
781{
9817aa80 782 struct xfs_efd_log_format *efd_formatp;
921ed96b 783 int buflen = item->ri_buf[0].i_len;
9817aa80
DW
784
785 efd_formatp = item->ri_buf[0].i_addr;
921ed96b
DW
786
787 if (buflen < sizeof(struct xfs_efd_log_format)) {
788 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
789 efd_formatp, buflen);
790 return -EFSCORRUPTED;
791 }
792
793 if (item->ri_buf[0].i_len != xfs_efd_log_format32_sizeof(
794 efd_formatp->efd_nextents) &&
795 item->ri_buf[0].i_len != xfs_efd_log_format64_sizeof(
796 efd_formatp->efd_nextents)) {
797 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
798 efd_formatp, buflen);
799 return -EFSCORRUPTED;
800 }
9817aa80 801
154c733a 802 xlog_recover_release_intent(log, XFS_LI_EFI, efd_formatp->efd_efi_id);
9817aa80
DW
803 return 0;
804}
805
86ffa471
DW
806const struct xlog_recover_item_ops xlog_efd_item_ops = {
807 .item_type = XFS_LI_EFD,
9817aa80 808 .commit_pass2 = xlog_recover_efd_commit_pass2,
86ffa471 809};