xfs: refactor log recovery icreate item dispatch for pass2 commit functions
[linux-2.6-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"
81f40041 14#include "xfs_defer.h"
239880ef 15#include "xfs_trans.h"
1da177e4
LT
16#include "xfs_trans_priv.h"
17#include "xfs_extfree_item.h"
1234351c 18#include "xfs_log.h"
340785cc
DW
19#include "xfs_btree.h"
20#include "xfs_rmap.h"
81f40041
CH
21#include "xfs_alloc.h"
22#include "xfs_bmap.h"
23#include "xfs_trace.h"
a5155b87 24#include "xfs_error.h"
86ffa471 25#include "xfs_log_recover.h"
1da177e4
LT
26
27kmem_zone_t *xfs_efi_zone;
28kmem_zone_t *xfs_efd_zone;
29
7bfa31d8
CH
30static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
31{
32 return container_of(lip, struct xfs_efi_log_item, efi_item);
33}
1da177e4 34
7d795ca3 35void
7bfa31d8
CH
36xfs_efi_item_free(
37 struct xfs_efi_log_item *efip)
7d795ca3 38{
b1c5ebb2 39 kmem_free(efip->efi_item.li_lv_shadow);
7bfa31d8 40 if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
f0e2d93c 41 kmem_free(efip);
7bfa31d8 42 else
377bcd5f 43 kmem_cache_free(xfs_efi_zone, efip);
7d795ca3 44}
1da177e4 45
0612d116
DC
46/*
47 * Freeing the efi requires that we remove it from the AIL if it has already
48 * been placed there. However, the EFI may not yet have been placed in the AIL
49 * when called by xfs_efi_release() from EFD processing due to the ordering of
50 * committed vs unpin operations in bulk insert operations. Hence the reference
51 * count to ensure only the last caller frees the EFI.
52 */
53void
54xfs_efi_release(
55 struct xfs_efi_log_item *efip)
56{
57 ASSERT(atomic_read(&efip->efi_refcount) > 0);
58 if (atomic_dec_and_test(&efip->efi_refcount)) {
65587929 59 xfs_trans_ail_delete(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR);
0612d116
DC
60 xfs_efi_item_free(efip);
61 }
62}
63
1da177e4
LT
64/*
65 * This returns the number of iovecs needed to log the given efi item.
66 * We only need 1 iovec for an efi item. It just logs the efi_log_format
67 * structure.
68 */
166d1368
DC
69static inline int
70xfs_efi_item_sizeof(
71 struct xfs_efi_log_item *efip)
72{
73 return sizeof(struct xfs_efi_log_format) +
74 (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
75}
76
77STATIC void
7bfa31d8 78xfs_efi_item_size(
166d1368
DC
79 struct xfs_log_item *lip,
80 int *nvecs,
81 int *nbytes)
1da177e4 82{
166d1368
DC
83 *nvecs += 1;
84 *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip));
1da177e4
LT
85}
86
87/*
88 * This is called to fill in the vector of log iovecs for the
89 * given efi log item. We use only 1 iovec, and we point that
90 * at the efi_log_format structure embedded in the efi item.
91 * It is at this point that we assert that all of the extent
92 * slots in the efi item have been filled.
93 */
94STATIC void
7bfa31d8
CH
95xfs_efi_item_format(
96 struct xfs_log_item *lip,
bde7cff6 97 struct xfs_log_vec *lv)
1da177e4 98{
7bfa31d8 99 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
bde7cff6 100 struct xfs_log_iovec *vecp = NULL;
1da177e4 101
b199c8a4
DC
102 ASSERT(atomic_read(&efip->efi_next_extent) ==
103 efip->efi_format.efi_nextents);
1da177e4
LT
104
105 efip->efi_format.efi_type = XFS_LI_EFI;
1da177e4
LT
106 efip->efi_format.efi_size = 1;
107
bde7cff6 108 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
1234351c
CH
109 &efip->efi_format,
110 xfs_efi_item_sizeof(efip));
1da177e4
LT
111}
112
113
1da177e4 114/*
8d99fe92
BF
115 * The unpin operation is the last place an EFI is manipulated in the log. It is
116 * either inserted in the AIL or aborted in the event of a log I/O error. In
117 * either case, the EFI transaction has been successfully committed to make it
118 * this far. Therefore, we expect whoever committed the EFI to either construct
119 * and commit the EFD or drop the EFD's reference in the event of error. Simply
120 * drop the log's EFI reference now that the log is done with it.
1da177e4 121 */
1da177e4 122STATIC void
7bfa31d8
CH
123xfs_efi_item_unpin(
124 struct xfs_log_item *lip,
125 int remove)
1da177e4 126{
7bfa31d8 127 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
5e4b5386 128 xfs_efi_release(efip);
1da177e4
LT
129}
130
8d99fe92
BF
131/*
132 * The EFI has been either committed or aborted if the transaction has been
133 * cancelled. If the transaction was cancelled, an EFD isn't going to be
134 * constructed and thus we free the EFI here directly.
135 */
1da177e4 136STATIC void
ddf92053 137xfs_efi_item_release(
7bfa31d8 138 struct xfs_log_item *lip)
1da177e4 139{
ddf92053 140 xfs_efi_release(EFI_ITEM(lip));
1da177e4
LT
141}
142
272e42b2 143static const struct xfs_item_ops xfs_efi_item_ops = {
7bfa31d8
CH
144 .iop_size = xfs_efi_item_size,
145 .iop_format = xfs_efi_item_format,
7bfa31d8 146 .iop_unpin = xfs_efi_item_unpin,
ddf92053 147 .iop_release = xfs_efi_item_release,
1da177e4
LT
148};
149
150
151/*
152 * Allocate and initialize an efi item with the given number of extents.
153 */
7bfa31d8
CH
154struct xfs_efi_log_item *
155xfs_efi_init(
156 struct xfs_mount *mp,
157 uint nextents)
1da177e4
LT
158
159{
7bfa31d8 160 struct xfs_efi_log_item *efip;
1da177e4
LT
161 uint size;
162
163 ASSERT(nextents > 0);
164 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
82ff450b 165 size = (uint)(sizeof(struct xfs_efi_log_item) +
1da177e4 166 ((nextents - 1) * sizeof(xfs_extent_t)));
707e0dda 167 efip = kmem_zalloc(size, 0);
1da177e4 168 } else {
707e0dda 169 efip = kmem_zone_zalloc(xfs_efi_zone, 0);
1da177e4
LT
170 }
171
43f5efc5 172 xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
1da177e4 173 efip->efi_format.efi_nextents = nextents;
db9d67d6 174 efip->efi_format.efi_id = (uintptr_t)(void *)efip;
b199c8a4 175 atomic_set(&efip->efi_next_extent, 0);
666d644c 176 atomic_set(&efip->efi_refcount, 2);
1da177e4 177
7bfa31d8 178 return efip;
1da177e4
LT
179}
180
6d192a9b
TS
181/*
182 * Copy an EFI format buffer from the given buf, and into the destination
183 * EFI format structure.
184 * The given buffer can be in 32 bit or 64 bit form (which has different padding),
185 * one of which will be the native format for this kernel.
186 * It will handle the conversion of formats if necessary.
187 */
188int
189xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
190{
4e0d5f92 191 xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
6d192a9b
TS
192 uint i;
193 uint len = sizeof(xfs_efi_log_format_t) +
194 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);
195 uint len32 = sizeof(xfs_efi_log_format_32_t) +
196 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);
197 uint len64 = sizeof(xfs_efi_log_format_64_t) +
198 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);
199
200 if (buf->i_len == len) {
201 memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
202 return 0;
203 } else if (buf->i_len == len32) {
4e0d5f92 204 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
6d192a9b
TS
205
206 dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
207 dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
208 dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
209 dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
210 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
211 dst_efi_fmt->efi_extents[i].ext_start =
212 src_efi_fmt_32->efi_extents[i].ext_start;
213 dst_efi_fmt->efi_extents[i].ext_len =
214 src_efi_fmt_32->efi_extents[i].ext_len;
215 }
216 return 0;
217 } else if (buf->i_len == len64) {
4e0d5f92 218 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
6d192a9b
TS
219
220 dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
221 dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
222 dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
223 dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
224 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
225 dst_efi_fmt->efi_extents[i].ext_start =
226 src_efi_fmt_64->efi_extents[i].ext_start;
227 dst_efi_fmt->efi_extents[i].ext_len =
228 src_efi_fmt_64->efi_extents[i].ext_len;
229 }
230 return 0;
231 }
a5155b87 232 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
2451337d 233 return -EFSCORRUPTED;
6d192a9b
TS
234}
235
7bfa31d8 236static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
7d795ca3 237{
7bfa31d8
CH
238 return container_of(lip, struct xfs_efd_log_item, efd_item);
239}
1da177e4 240
7bfa31d8
CH
241STATIC void
242xfs_efd_item_free(struct xfs_efd_log_item *efdp)
243{
b1c5ebb2 244 kmem_free(efdp->efd_item.li_lv_shadow);
7bfa31d8 245 if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
f0e2d93c 246 kmem_free(efdp);
7bfa31d8 247 else
377bcd5f 248 kmem_cache_free(xfs_efd_zone, efdp);
7d795ca3 249}
1da177e4
LT
250
251/*
252 * This returns the number of iovecs needed to log the given efd item.
253 * We only need 1 iovec for an efd item. It just logs the efd_log_format
254 * structure.
255 */
166d1368
DC
256static inline int
257xfs_efd_item_sizeof(
258 struct xfs_efd_log_item *efdp)
259{
260 return sizeof(xfs_efd_log_format_t) +
261 (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
262}
263
264STATIC void
7bfa31d8 265xfs_efd_item_size(
166d1368
DC
266 struct xfs_log_item *lip,
267 int *nvecs,
268 int *nbytes)
1da177e4 269{
166d1368
DC
270 *nvecs += 1;
271 *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip));
1da177e4
LT
272}
273
274/*
275 * This is called to fill in the vector of log iovecs for the
276 * given efd log item. We use only 1 iovec, and we point that
277 * at the efd_log_format structure embedded in the efd item.
278 * It is at this point that we assert that all of the extent
279 * slots in the efd item have been filled.
280 */
281STATIC void
7bfa31d8
CH
282xfs_efd_item_format(
283 struct xfs_log_item *lip,
bde7cff6 284 struct xfs_log_vec *lv)
1da177e4 285{
7bfa31d8 286 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
bde7cff6 287 struct xfs_log_iovec *vecp = NULL;
1da177e4
LT
288
289 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
290
291 efdp->efd_format.efd_type = XFS_LI_EFD;
1da177e4
LT
292 efdp->efd_format.efd_size = 1;
293
bde7cff6 294 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
1234351c
CH
295 &efdp->efd_format,
296 xfs_efd_item_sizeof(efdp));
1da177e4
LT
297}
298
8d99fe92
BF
299/*
300 * The EFD is either committed or aborted if the transaction is cancelled. If
301 * the transaction is cancelled, drop our reference to the EFI and free the EFD.
302 */
1da177e4 303STATIC void
ddf92053 304xfs_efd_item_release(
7bfa31d8 305 struct xfs_log_item *lip)
1da177e4 306{
8d99fe92
BF
307 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
308
ddf92053
CH
309 xfs_efi_release(efdp->efd_efip);
310 xfs_efd_item_free(efdp);
1da177e4
LT
311}
312
272e42b2 313static const struct xfs_item_ops xfs_efd_item_ops = {
9ce632a2 314 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
7bfa31d8
CH
315 .iop_size = xfs_efd_item_size,
316 .iop_format = xfs_efd_item_format,
ddf92053 317 .iop_release = xfs_efd_item_release,
1da177e4
LT
318};
319
1da177e4 320/*
9c5e7c2a
CH
321 * Allocate an "extent free done" log item that will hold nextents worth of
322 * extents. The caller must use all nextents extents, because we are not
323 * flexible about this at all.
1da177e4 324 */
81f40041 325static struct xfs_efd_log_item *
9c5e7c2a
CH
326xfs_trans_get_efd(
327 struct xfs_trans *tp,
328 struct xfs_efi_log_item *efip,
329 unsigned int nextents)
1da177e4 330{
9c5e7c2a 331 struct xfs_efd_log_item *efdp;
1da177e4
LT
332
333 ASSERT(nextents > 0);
9c5e7c2a 334
1da177e4 335 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
9c5e7c2a
CH
336 efdp = kmem_zalloc(sizeof(struct xfs_efd_log_item) +
337 (nextents - 1) * sizeof(struct xfs_extent),
707e0dda 338 0);
1da177e4 339 } else {
707e0dda 340 efdp = kmem_zone_zalloc(xfs_efd_zone, 0);
1da177e4
LT
341 }
342
9c5e7c2a
CH
343 xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD,
344 &xfs_efd_item_ops);
1da177e4
LT
345 efdp->efd_efip = efip;
346 efdp->efd_format.efd_nextents = nextents;
347 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
348
9c5e7c2a 349 xfs_trans_add_item(tp, &efdp->efd_item);
7bfa31d8 350 return efdp;
1da177e4 351}
dc42375d 352
81f40041
CH
353/*
354 * Free an extent and log it to the EFD. Note that the transaction is marked
355 * dirty regardless of whether the extent free succeeds or fails to support the
356 * EFI/EFD lifecycle rules.
357 */
358static int
359xfs_trans_free_extent(
360 struct xfs_trans *tp,
361 struct xfs_efd_log_item *efdp,
362 xfs_fsblock_t start_block,
363 xfs_extlen_t ext_len,
364 const struct xfs_owner_info *oinfo,
365 bool skip_discard)
366{
367 struct xfs_mount *mp = tp->t_mountp;
368 struct xfs_extent *extp;
369 uint next_extent;
370 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, start_block);
371 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp,
372 start_block);
373 int error;
374
375 trace_xfs_bmap_free_deferred(tp->t_mountp, agno, 0, agbno, ext_len);
376
377 error = __xfs_free_extent(tp, start_block, ext_len,
378 oinfo, XFS_AG_RESV_NONE, skip_discard);
379 /*
380 * Mark the transaction dirty, even on error. This ensures the
381 * transaction is aborted, which:
382 *
383 * 1.) releases the EFI and frees the EFD
384 * 2.) shuts down the filesystem
385 */
386 tp->t_flags |= XFS_TRANS_DIRTY;
387 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
388
389 next_extent = efdp->efd_next_extent;
390 ASSERT(next_extent < efdp->efd_format.efd_nextents);
391 extp = &(efdp->efd_format.efd_extents[next_extent]);
392 extp->ext_start = start_block;
393 extp->ext_len = ext_len;
394 efdp->efd_next_extent++;
395
396 return error;
397}
398
399/* Sort bmap items by AG. */
400static int
401xfs_extent_free_diff_items(
402 void *priv,
403 struct list_head *a,
404 struct list_head *b)
405{
406 struct xfs_mount *mp = priv;
407 struct xfs_extent_free_item *ra;
408 struct xfs_extent_free_item *rb;
409
410 ra = container_of(a, struct xfs_extent_free_item, xefi_list);
411 rb = container_of(b, struct xfs_extent_free_item, xefi_list);
412 return XFS_FSB_TO_AGNO(mp, ra->xefi_startblock) -
413 XFS_FSB_TO_AGNO(mp, rb->xefi_startblock);
414}
415
81f40041
CH
416/* Log a free extent to the intent item. */
417STATIC void
418xfs_extent_free_log_item(
419 struct xfs_trans *tp,
c1f09188
CH
420 struct xfs_efi_log_item *efip,
421 struct xfs_extent_free_item *free)
81f40041 422{
81f40041
CH
423 uint next_extent;
424 struct xfs_extent *extp;
425
81f40041
CH
426 tp->t_flags |= XFS_TRANS_DIRTY;
427 set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
428
429 /*
430 * atomic_inc_return gives us the value after the increment;
431 * we want to use it as an array index so we need to subtract 1 from
432 * it.
433 */
434 next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
435 ASSERT(next_extent < efip->efi_format.efi_nextents);
436 extp = &efip->efi_format.efi_extents[next_extent];
437 extp->ext_start = free->xefi_startblock;
438 extp->ext_len = free->xefi_blockcount;
439}
440
13a83333 441static struct xfs_log_item *
c1f09188
CH
442xfs_extent_free_create_intent(
443 struct xfs_trans *tp,
444 struct list_head *items,
d367a868
CH
445 unsigned int count,
446 bool sort)
c1f09188
CH
447{
448 struct xfs_mount *mp = tp->t_mountp;
449 struct xfs_efi_log_item *efip = xfs_efi_init(mp, count);
450 struct xfs_extent_free_item *free;
451
452 ASSERT(count > 0);
453
454 xfs_trans_add_item(tp, &efip->efi_item);
d367a868
CH
455 if (sort)
456 list_sort(mp, items, xfs_extent_free_diff_items);
c1f09188
CH
457 list_for_each_entry(free, items, xefi_list)
458 xfs_extent_free_log_item(tp, efip, free);
13a83333 459 return &efip->efi_item;
c1f09188
CH
460}
461
81f40041 462/* Get an EFD so we can process all the free extents. */
f09d167c 463static struct xfs_log_item *
81f40041
CH
464xfs_extent_free_create_done(
465 struct xfs_trans *tp,
13a83333 466 struct xfs_log_item *intent,
81f40041
CH
467 unsigned int count)
468{
f09d167c 469 return &xfs_trans_get_efd(tp, EFI_ITEM(intent), count)->efd_item;
81f40041
CH
470}
471
472/* Process a free extent. */
473STATIC int
474xfs_extent_free_finish_item(
475 struct xfs_trans *tp,
f09d167c 476 struct xfs_log_item *done,
81f40041 477 struct list_head *item,
3ec1b26c 478 struct xfs_btree_cur **state)
81f40041
CH
479{
480 struct xfs_extent_free_item *free;
481 int error;
482
483 free = container_of(item, struct xfs_extent_free_item, xefi_list);
f09d167c 484 error = xfs_trans_free_extent(tp, EFD_ITEM(done),
81f40041
CH
485 free->xefi_startblock,
486 free->xefi_blockcount,
487 &free->xefi_oinfo, free->xefi_skip_discard);
488 kmem_free(free);
489 return error;
490}
491
492/* Abort all pending EFIs. */
493STATIC void
494xfs_extent_free_abort_intent(
13a83333 495 struct xfs_log_item *intent)
81f40041 496{
13a83333 497 xfs_efi_release(EFI_ITEM(intent));
81f40041
CH
498}
499
500/* Cancel a free extent. */
501STATIC void
502xfs_extent_free_cancel_item(
503 struct list_head *item)
504{
505 struct xfs_extent_free_item *free;
506
507 free = container_of(item, struct xfs_extent_free_item, xefi_list);
508 kmem_free(free);
509}
510
511const struct xfs_defer_op_type xfs_extent_free_defer_type = {
512 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
81f40041
CH
513 .create_intent = xfs_extent_free_create_intent,
514 .abort_intent = xfs_extent_free_abort_intent,
81f40041
CH
515 .create_done = xfs_extent_free_create_done,
516 .finish_item = xfs_extent_free_finish_item,
517 .cancel_item = xfs_extent_free_cancel_item,
518};
519
520/*
521 * AGFL blocks are accounted differently in the reserve pools and are not
522 * inserted into the busy extent list.
523 */
524STATIC int
525xfs_agfl_free_finish_item(
526 struct xfs_trans *tp,
f09d167c 527 struct xfs_log_item *done,
81f40041 528 struct list_head *item,
3ec1b26c 529 struct xfs_btree_cur **state)
81f40041
CH
530{
531 struct xfs_mount *mp = tp->t_mountp;
f09d167c 532 struct xfs_efd_log_item *efdp = EFD_ITEM(done);
81f40041
CH
533 struct xfs_extent_free_item *free;
534 struct xfs_extent *extp;
535 struct xfs_buf *agbp;
536 int error;
537 xfs_agnumber_t agno;
538 xfs_agblock_t agbno;
539 uint next_extent;
540
541 free = container_of(item, struct xfs_extent_free_item, xefi_list);
542 ASSERT(free->xefi_blockcount == 1);
543 agno = XFS_FSB_TO_AGNO(mp, free->xefi_startblock);
544 agbno = XFS_FSB_TO_AGBNO(mp, free->xefi_startblock);
545
546 trace_xfs_agfl_free_deferred(mp, agno, 0, agbno, free->xefi_blockcount);
547
548 error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
549 if (!error)
550 error = xfs_free_agfl_block(tp, agno, agbno, agbp,
551 &free->xefi_oinfo);
552
553 /*
554 * Mark the transaction dirty, even on error. This ensures the
555 * transaction is aborted, which:
556 *
557 * 1.) releases the EFI and frees the EFD
558 * 2.) shuts down the filesystem
559 */
560 tp->t_flags |= XFS_TRANS_DIRTY;
561 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
562
563 next_extent = efdp->efd_next_extent;
564 ASSERT(next_extent < efdp->efd_format.efd_nextents);
565 extp = &(efdp->efd_format.efd_extents[next_extent]);
566 extp->ext_start = free->xefi_startblock;
567 extp->ext_len = free->xefi_blockcount;
568 efdp->efd_next_extent++;
569
570 kmem_free(free);
571 return error;
572}
573
574/* sub-type with special handling for AGFL deferred frees */
575const struct xfs_defer_op_type xfs_agfl_free_defer_type = {
576 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
81f40041
CH
577 .create_intent = xfs_extent_free_create_intent,
578 .abort_intent = xfs_extent_free_abort_intent,
81f40041
CH
579 .create_done = xfs_extent_free_create_done,
580 .finish_item = xfs_agfl_free_finish_item,
581 .cancel_item = xfs_extent_free_cancel_item,
582};
583
dc42375d
DW
584/*
585 * Process an extent free intent item that was recovered from
586 * the log. We need to free the extents that it describes.
587 */
588int
589xfs_efi_recover(
590 struct xfs_mount *mp,
591 struct xfs_efi_log_item *efip)
592{
593 struct xfs_efd_log_item *efdp;
594 struct xfs_trans *tp;
595 int i;
596 int error = 0;
597 xfs_extent_t *extp;
598 xfs_fsblock_t startblock_fsb;
599
600 ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags));
601
602 /*
603 * First check the validity of the extents described by the
604 * EFI. If any are bad, then assume that all are bad and
605 * just toss the EFI.
606 */
607 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
e127fafd 608 extp = &efip->efi_format.efi_extents[i];
dc42375d
DW
609 startblock_fsb = XFS_BB_TO_FSB(mp,
610 XFS_FSB_TO_DADDR(mp, extp->ext_start));
e127fafd
DW
611 if (startblock_fsb == 0 ||
612 extp->ext_len == 0 ||
613 startblock_fsb >= mp->m_sb.sb_dblocks ||
614 extp->ext_len >= mp->m_sb.sb_agblocks) {
dc42375d
DW
615 /*
616 * This will pull the EFI from the AIL and
617 * free the memory associated with it.
618 */
619 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
620 xfs_efi_release(efip);
895e196f 621 return -EFSCORRUPTED;
dc42375d
DW
622 }
623 }
624
625 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
626 if (error)
627 return error;
628 efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
629
630 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
e127fafd 631 extp = &efip->efi_format.efi_extents[i];
dc42375d 632 error = xfs_trans_free_extent(tp, efdp, extp->ext_start,
7280feda
DW
633 extp->ext_len,
634 &XFS_RMAP_OINFO_ANY_OWNER, false);
dc42375d
DW
635 if (error)
636 goto abort_error;
637
638 }
639
640 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
641 error = xfs_trans_commit(tp);
642 return error;
643
644abort_error:
645 xfs_trans_cancel(tp);
646 return error;
647}
86ffa471
DW
648
649const struct xlog_recover_item_ops xlog_efi_item_ops = {
650 .item_type = XFS_LI_EFI,
651};
652
653const struct xlog_recover_item_ops xlog_efd_item_ops = {
654 .item_type = XFS_LI_EFD,
655};