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