btrfs: migrate the block group cleanup code
[linux/fpc-iii.git] / fs / xfs / xfs_extfree_item.c
blob86f6512d68643ec833b1aa5c5ef4e12036e90523
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_format.h"
9 #include "xfs_log_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_bit.h"
12 #include "xfs_shared.h"
13 #include "xfs_mount.h"
14 #include "xfs_defer.h"
15 #include "xfs_trans.h"
16 #include "xfs_trans_priv.h"
17 #include "xfs_extfree_item.h"
18 #include "xfs_log.h"
19 #include "xfs_btree.h"
20 #include "xfs_rmap.h"
21 #include "xfs_alloc.h"
22 #include "xfs_bmap.h"
23 #include "xfs_trace.h"
26 kmem_zone_t *xfs_efi_zone;
27 kmem_zone_t *xfs_efd_zone;
29 static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
31 return container_of(lip, struct xfs_efi_log_item, efi_item);
34 void
35 xfs_efi_item_free(
36 struct xfs_efi_log_item *efip)
38 kmem_free(efip->efi_item.li_lv_shadow);
39 if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
40 kmem_free(efip);
41 else
42 kmem_zone_free(xfs_efi_zone, efip);
46 * 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.
52 void
53 xfs_efi_release(
54 struct xfs_efi_log_item *efip)
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);
64 * 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.
68 static inline int
69 xfs_efi_item_sizeof(
70 struct xfs_efi_log_item *efip)
72 return sizeof(struct xfs_efi_log_format) +
73 (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
76 STATIC void
77 xfs_efi_item_size(
78 struct xfs_log_item *lip,
79 int *nvecs,
80 int *nbytes)
82 *nvecs += 1;
83 *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip));
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.
93 STATIC void
94 xfs_efi_item_format(
95 struct xfs_log_item *lip,
96 struct xfs_log_vec *lv)
98 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
99 struct xfs_log_iovec *vecp = NULL;
101 ASSERT(atomic_read(&efip->efi_next_extent) ==
102 efip->efi_format.efi_nextents);
104 efip->efi_format.efi_type = XFS_LI_EFI;
105 efip->efi_format.efi_size = 1;
107 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
108 &efip->efi_format,
109 xfs_efi_item_sizeof(efip));
114 * 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.
121 STATIC void
122 xfs_efi_item_unpin(
123 struct xfs_log_item *lip,
124 int remove)
126 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
127 xfs_efi_release(efip);
131 * 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.
135 STATIC void
136 xfs_efi_item_release(
137 struct xfs_log_item *lip)
139 xfs_efi_release(EFI_ITEM(lip));
142 static const struct xfs_item_ops xfs_efi_item_ops = {
143 .iop_size = xfs_efi_item_size,
144 .iop_format = xfs_efi_item_format,
145 .iop_unpin = xfs_efi_item_unpin,
146 .iop_release = xfs_efi_item_release,
151 * Allocate and initialize an efi item with the given number of extents.
153 struct xfs_efi_log_item *
154 xfs_efi_init(
155 struct xfs_mount *mp,
156 uint nextents)
159 struct xfs_efi_log_item *efip;
160 uint size;
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)));
166 efip = kmem_zalloc(size, KM_SLEEP);
167 } else {
168 efip = kmem_zone_zalloc(xfs_efi_zone, KM_SLEEP);
171 xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
172 efip->efi_format.efi_nextents = nextents;
173 efip->efi_format.efi_id = (uintptr_t)(void *)efip;
174 atomic_set(&efip->efi_next_extent, 0);
175 atomic_set(&efip->efi_refcount, 2);
177 return efip;
181 * 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.
188 xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
190 xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
191 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);
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) {
203 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
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;
215 return 0;
216 } else if (buf->i_len == len64) {
217 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
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;
229 return 0;
231 return -EFSCORRUPTED;
234 static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
236 return container_of(lip, struct xfs_efd_log_item, efd_item);
239 STATIC void
240 xfs_efd_item_free(struct xfs_efd_log_item *efdp)
242 kmem_free(efdp->efd_item.li_lv_shadow);
243 if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
244 kmem_free(efdp);
245 else
246 kmem_zone_free(xfs_efd_zone, efdp);
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.
254 static inline int
255 xfs_efd_item_sizeof(
256 struct xfs_efd_log_item *efdp)
258 return sizeof(xfs_efd_log_format_t) +
259 (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
262 STATIC void
263 xfs_efd_item_size(
264 struct xfs_log_item *lip,
265 int *nvecs,
266 int *nbytes)
268 *nvecs += 1;
269 *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip));
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.
279 STATIC void
280 xfs_efd_item_format(
281 struct xfs_log_item *lip,
282 struct xfs_log_vec *lv)
284 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
285 struct xfs_log_iovec *vecp = NULL;
287 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
289 efdp->efd_format.efd_type = XFS_LI_EFD;
290 efdp->efd_format.efd_size = 1;
292 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
293 &efdp->efd_format,
294 xfs_efd_item_sizeof(efdp));
298 * 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.
301 STATIC void
302 xfs_efd_item_release(
303 struct xfs_log_item *lip)
305 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
307 xfs_efi_release(efdp->efd_efip);
308 xfs_efd_item_free(efdp);
311 static const struct xfs_item_ops xfs_efd_item_ops = {
312 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
313 .iop_size = xfs_efd_item_size,
314 .iop_format = xfs_efd_item_format,
315 .iop_release = xfs_efd_item_release,
319 * 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.
323 static struct xfs_efd_log_item *
324 xfs_trans_get_efd(
325 struct xfs_trans *tp,
326 struct xfs_efi_log_item *efip,
327 unsigned int nextents)
329 struct xfs_efd_log_item *efdp;
331 ASSERT(nextents > 0);
333 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
334 efdp = kmem_zalloc(sizeof(struct xfs_efd_log_item) +
335 (nextents - 1) * sizeof(struct xfs_extent),
336 KM_SLEEP);
337 } else {
338 efdp = kmem_zone_zalloc(xfs_efd_zone, KM_SLEEP);
341 xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD,
342 &xfs_efd_item_ops);
343 efdp->efd_efip = efip;
344 efdp->efd_format.efd_nextents = nextents;
345 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
347 xfs_trans_add_item(tp, &efdp->efd_item);
348 return efdp;
352 * 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.
356 static int
357 xfs_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)
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;
373 trace_xfs_bmap_free_deferred(tp->t_mountp, agno, 0, agbno, ext_len);
375 error = __xfs_free_extent(tp, start_block, ext_len,
376 oinfo, XFS_AG_RESV_NONE, skip_discard);
378 * Mark the transaction dirty, even on error. This ensures the
379 * transaction is aborted, which:
381 * 1.) releases the EFI and frees the EFD
382 * 2.) shuts down the filesystem
384 tp->t_flags |= XFS_TRANS_DIRTY;
385 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
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++;
394 return error;
397 /* Sort bmap items by AG. */
398 static int
399 xfs_extent_free_diff_items(
400 void *priv,
401 struct list_head *a,
402 struct list_head *b)
404 struct xfs_mount *mp = priv;
405 struct xfs_extent_free_item *ra;
406 struct xfs_extent_free_item *rb;
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);
414 /* Get an EFI. */
415 STATIC void *
416 xfs_extent_free_create_intent(
417 struct xfs_trans *tp,
418 unsigned int count)
420 struct xfs_efi_log_item *efip;
422 ASSERT(tp != NULL);
423 ASSERT(count > 0);
425 efip = xfs_efi_init(tp->t_mountp, count);
426 ASSERT(efip != NULL);
429 * Get a log_item_desc to point at the new item.
431 xfs_trans_add_item(tp, &efip->efi_item);
432 return efip;
435 /* Log a free extent to the intent item. */
436 STATIC void
437 xfs_extent_free_log_item(
438 struct xfs_trans *tp,
439 void *intent,
440 struct list_head *item)
442 struct xfs_efi_log_item *efip = intent;
443 struct xfs_extent_free_item *free;
444 uint next_extent;
445 struct xfs_extent *extp;
447 free = container_of(item, struct xfs_extent_free_item, xefi_list);
449 tp->t_flags |= XFS_TRANS_DIRTY;
450 set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
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.
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;
464 /* Get an EFD so we can process all the free extents. */
465 STATIC void *
466 xfs_extent_free_create_done(
467 struct xfs_trans *tp,
468 void *intent,
469 unsigned int count)
471 return xfs_trans_get_efd(tp, intent, count);
474 /* Process a free extent. */
475 STATIC int
476 xfs_extent_free_finish_item(
477 struct xfs_trans *tp,
478 struct list_head *item,
479 void *done_item,
480 void **state)
482 struct xfs_extent_free_item *free;
483 int error;
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;
494 /* Abort all pending EFIs. */
495 STATIC void
496 xfs_extent_free_abort_intent(
497 void *intent)
499 xfs_efi_release(intent);
502 /* Cancel a free extent. */
503 STATIC void
504 xfs_extent_free_cancel_item(
505 struct list_head *item)
507 struct xfs_extent_free_item *free;
509 free = container_of(item, struct xfs_extent_free_item, xefi_list);
510 kmem_free(free);
513 const 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,
525 * AGFL blocks are accounted differently in the reserve pools and are not
526 * inserted into the busy extent list.
528 STATIC int
529 xfs_agfl_free_finish_item(
530 struct xfs_trans *tp,
531 struct list_head *item,
532 void *done_item,
533 void **state)
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;
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);
550 trace_xfs_agfl_free_deferred(mp, agno, 0, agbno, free->xefi_blockcount);
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);
558 * Mark the transaction dirty, even on error. This ensures the
559 * transaction is aborted, which:
561 * 1.) releases the EFI and frees the EFD
562 * 2.) shuts down the filesystem
564 tp->t_flags |= XFS_TRANS_DIRTY;
565 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
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++;
574 kmem_free(free);
575 return error;
578 /* sub-type with special handling for AGFL deferred frees */
579 const 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,
591 * Process an extent free intent item that was recovered from
592 * the log. We need to free the extents that it describes.
595 xfs_efi_recover(
596 struct xfs_mount *mp,
597 struct xfs_efi_log_item *efip)
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;
606 ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags));
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.
613 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
614 extp = &efip->efi_format.efi_extents[i];
615 startblock_fsb = XFS_BB_TO_FSB(mp,
616 XFS_FSB_TO_DADDR(mp, extp->ext_start));
617 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) {
622 * This will pull the EFI from the AIL and
623 * free the memory associated with it.
625 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
626 xfs_efi_release(efip);
627 return -EIO;
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);
636 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
637 extp = &efip->efi_format.efi_extents[i];
638 error = xfs_trans_free_extent(tp, efdp, extp->ext_start,
639 extp->ext_len,
640 &XFS_RMAP_OINFO_ANY_OWNER, false);
641 if (error)
642 goto abort_error;
646 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
647 error = xfs_trans_commit(tp);
648 return error;
650 abort_error:
651 xfs_trans_cancel(tp);
652 return error;