1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
8 #include "xfs_format.h"
9 #include "xfs_log_format.h"
10 #include "xfs_trans_resv.h"
12 #include "xfs_shared.h"
13 #include "xfs_mount.h"
14 #include "xfs_defer.h"
15 #include "xfs_inode.h"
16 #include "xfs_trans.h"
17 #include "xfs_trans_priv.h"
18 #include "xfs_bmap_item.h"
21 #include "xfs_icache.h"
22 #include "xfs_bmap_btree.h"
23 #include "xfs_trans_space.h"
24 #include "xfs_error.h"
25 #include "xfs_log_priv.h"
26 #include "xfs_log_recover.h"
28 struct kmem_cache *xfs_bui_cache;
29 struct kmem_cache *xfs_bud_cache;
31 static const struct xfs_item_ops xfs_bui_item_ops;
33 static inline struct xfs_bui_log_item *BUI_ITEM(struct xfs_log_item *lip)
35 return container_of(lip, struct xfs_bui_log_item, bui_item);
40 struct xfs_bui_log_item *buip)
42 kmem_free(buip->bui_item.li_lv_shadow);
43 kmem_cache_free(xfs_bui_cache, buip);
47 * Freeing the BUI requires that we remove it from the AIL if it has already
48 * been placed there. However, the BUI may not yet have been placed in the AIL
49 * when called by xfs_bui_release() from BUD 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 BUI.
55 struct xfs_bui_log_item *buip)
57 ASSERT(atomic_read(&buip->bui_refcount) > 0);
58 if (!atomic_dec_and_test(&buip->bui_refcount))
61 xfs_trans_ail_delete(&buip->bui_item, 0);
62 xfs_bui_item_free(buip);
68 struct xfs_log_item *lip,
72 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
75 *nbytes += xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents);
79 * This is called to fill in the vector of log iovecs for the
80 * given bui log item. We use only 1 iovec, and we point that
81 * at the bui_log_format structure embedded in the bui item.
82 * It is at this point that we assert that all of the extent
83 * slots in the bui item have been filled.
87 struct xfs_log_item *lip,
88 struct xfs_log_vec *lv)
90 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
91 struct xfs_log_iovec *vecp = NULL;
93 ASSERT(atomic_read(&buip->bui_next_extent) ==
94 buip->bui_format.bui_nextents);
96 buip->bui_format.bui_type = XFS_LI_BUI;
97 buip->bui_format.bui_size = 1;
99 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUI_FORMAT, &buip->bui_format,
100 xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents));
104 * The unpin operation is the last place an BUI is manipulated in the log. It is
105 * either inserted in the AIL or aborted in the event of a log I/O error. In
106 * either case, the BUI transaction has been successfully committed to make it
107 * this far. Therefore, we expect whoever committed the BUI to either construct
108 * and commit the BUD or drop the BUD's reference in the event of error. Simply
109 * drop the log's BUI reference now that the log is done with it.
113 struct xfs_log_item *lip,
116 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
118 xfs_bui_release(buip);
122 * The BUI has been either committed or aborted if the transaction has been
123 * cancelled. If the transaction was cancelled, an BUD isn't going to be
124 * constructed and thus we free the BUI here directly.
127 xfs_bui_item_release(
128 struct xfs_log_item *lip)
130 xfs_bui_release(BUI_ITEM(lip));
134 * Allocate and initialize an bui item with the given number of extents.
136 STATIC struct xfs_bui_log_item *
138 struct xfs_mount *mp)
141 struct xfs_bui_log_item *buip;
143 buip = kmem_cache_zalloc(xfs_bui_cache, GFP_KERNEL | __GFP_NOFAIL);
145 xfs_log_item_init(mp, &buip->bui_item, XFS_LI_BUI, &xfs_bui_item_ops);
146 buip->bui_format.bui_nextents = XFS_BUI_MAX_FAST_EXTENTS;
147 buip->bui_format.bui_id = (uintptr_t)(void *)buip;
148 atomic_set(&buip->bui_next_extent, 0);
149 atomic_set(&buip->bui_refcount, 2);
154 static inline struct xfs_bud_log_item *BUD_ITEM(struct xfs_log_item *lip)
156 return container_of(lip, struct xfs_bud_log_item, bud_item);
161 struct xfs_log_item *lip,
166 *nbytes += sizeof(struct xfs_bud_log_format);
170 * This is called to fill in the vector of log iovecs for the
171 * given bud log item. We use only 1 iovec, and we point that
172 * at the bud_log_format structure embedded in the bud item.
173 * It is at this point that we assert that all of the extent
174 * slots in the bud item have been filled.
178 struct xfs_log_item *lip,
179 struct xfs_log_vec *lv)
181 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
182 struct xfs_log_iovec *vecp = NULL;
184 budp->bud_format.bud_type = XFS_LI_BUD;
185 budp->bud_format.bud_size = 1;
187 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUD_FORMAT, &budp->bud_format,
188 sizeof(struct xfs_bud_log_format));
192 * The BUD is either committed or aborted if the transaction is cancelled. If
193 * the transaction is cancelled, drop our reference to the BUI and free the
197 xfs_bud_item_release(
198 struct xfs_log_item *lip)
200 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
202 xfs_bui_release(budp->bud_buip);
203 kmem_free(budp->bud_item.li_lv_shadow);
204 kmem_cache_free(xfs_bud_cache, budp);
207 static struct xfs_log_item *
209 struct xfs_log_item *lip)
211 return &BUD_ITEM(lip)->bud_buip->bui_item;
214 static const struct xfs_item_ops xfs_bud_item_ops = {
215 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED |
216 XFS_ITEM_INTENT_DONE,
217 .iop_size = xfs_bud_item_size,
218 .iop_format = xfs_bud_item_format,
219 .iop_release = xfs_bud_item_release,
220 .iop_intent = xfs_bud_item_intent,
223 static struct xfs_bud_log_item *
225 struct xfs_trans *tp,
226 struct xfs_bui_log_item *buip)
228 struct xfs_bud_log_item *budp;
230 budp = kmem_cache_zalloc(xfs_bud_cache, GFP_KERNEL | __GFP_NOFAIL);
231 xfs_log_item_init(tp->t_mountp, &budp->bud_item, XFS_LI_BUD,
233 budp->bud_buip = buip;
234 budp->bud_format.bud_bui_id = buip->bui_format.bui_id;
236 xfs_trans_add_item(tp, &budp->bud_item);
241 * Finish an bmap update and log it to the BUD. Note that the
242 * transaction is marked dirty regardless of whether the bmap update
243 * succeeds or fails to support the BUI/BUD lifecycle rules.
246 xfs_trans_log_finish_bmap_update(
247 struct xfs_trans *tp,
248 struct xfs_bud_log_item *budp,
249 enum xfs_bmap_intent_type type,
250 struct xfs_inode *ip,
252 xfs_fileoff_t startoff,
253 xfs_fsblock_t startblock,
254 xfs_filblks_t *blockcount,
259 error = xfs_bmap_finish_one(tp, ip, type, whichfork, startoff,
260 startblock, blockcount, state);
263 * Mark the transaction dirty, even on error. This ensures the
264 * transaction is aborted, which:
266 * 1.) releases the BUI and frees the BUD
267 * 2.) shuts down the filesystem
269 tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
270 set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags);
275 /* Sort bmap intents by inode. */
277 xfs_bmap_update_diff_items(
279 const struct list_head *a,
280 const struct list_head *b)
282 struct xfs_bmap_intent *ba;
283 struct xfs_bmap_intent *bb;
285 ba = container_of(a, struct xfs_bmap_intent, bi_list);
286 bb = container_of(b, struct xfs_bmap_intent, bi_list);
287 return ba->bi_owner->i_ino - bb->bi_owner->i_ino;
290 /* Set the map extent flags for this mapping. */
292 xfs_trans_set_bmap_flags(
293 struct xfs_map_extent *bmap,
294 enum xfs_bmap_intent_type type,
302 bmap->me_flags = type;
307 if (state == XFS_EXT_UNWRITTEN)
308 bmap->me_flags |= XFS_BMAP_EXTENT_UNWRITTEN;
309 if (whichfork == XFS_ATTR_FORK)
310 bmap->me_flags |= XFS_BMAP_EXTENT_ATTR_FORK;
313 /* Log bmap updates in the intent item. */
315 xfs_bmap_update_log_item(
316 struct xfs_trans *tp,
317 struct xfs_bui_log_item *buip,
318 struct xfs_bmap_intent *bmap)
321 struct xfs_map_extent *map;
323 tp->t_flags |= XFS_TRANS_DIRTY;
324 set_bit(XFS_LI_DIRTY, &buip->bui_item.li_flags);
327 * atomic_inc_return gives us the value after the increment;
328 * we want to use it as an array index so we need to subtract 1 from
331 next_extent = atomic_inc_return(&buip->bui_next_extent) - 1;
332 ASSERT(next_extent < buip->bui_format.bui_nextents);
333 map = &buip->bui_format.bui_extents[next_extent];
334 map->me_owner = bmap->bi_owner->i_ino;
335 map->me_startblock = bmap->bi_bmap.br_startblock;
336 map->me_startoff = bmap->bi_bmap.br_startoff;
337 map->me_len = bmap->bi_bmap.br_blockcount;
338 xfs_trans_set_bmap_flags(map, bmap->bi_type, bmap->bi_whichfork,
339 bmap->bi_bmap.br_state);
342 static struct xfs_log_item *
343 xfs_bmap_update_create_intent(
344 struct xfs_trans *tp,
345 struct list_head *items,
349 struct xfs_mount *mp = tp->t_mountp;
350 struct xfs_bui_log_item *buip = xfs_bui_init(mp);
351 struct xfs_bmap_intent *bmap;
353 ASSERT(count == XFS_BUI_MAX_FAST_EXTENTS);
355 xfs_trans_add_item(tp, &buip->bui_item);
357 list_sort(mp, items, xfs_bmap_update_diff_items);
358 list_for_each_entry(bmap, items, bi_list)
359 xfs_bmap_update_log_item(tp, buip, bmap);
360 return &buip->bui_item;
363 /* Get an BUD so we can process all the deferred rmap updates. */
364 static struct xfs_log_item *
365 xfs_bmap_update_create_done(
366 struct xfs_trans *tp,
367 struct xfs_log_item *intent,
370 return &xfs_trans_get_bud(tp, BUI_ITEM(intent))->bud_item;
373 /* Process a deferred rmap update. */
375 xfs_bmap_update_finish_item(
376 struct xfs_trans *tp,
377 struct xfs_log_item *done,
378 struct list_head *item,
379 struct xfs_btree_cur **state)
381 struct xfs_bmap_intent *bmap;
385 bmap = container_of(item, struct xfs_bmap_intent, bi_list);
386 count = bmap->bi_bmap.br_blockcount;
387 error = xfs_trans_log_finish_bmap_update(tp, BUD_ITEM(done),
389 bmap->bi_owner, bmap->bi_whichfork,
390 bmap->bi_bmap.br_startoff,
391 bmap->bi_bmap.br_startblock,
393 bmap->bi_bmap.br_state);
394 if (!error && count > 0) {
395 ASSERT(bmap->bi_type == XFS_BMAP_UNMAP);
396 bmap->bi_bmap.br_blockcount = count;
399 kmem_cache_free(xfs_bmap_intent_cache, bmap);
403 /* Abort all pending BUIs. */
405 xfs_bmap_update_abort_intent(
406 struct xfs_log_item *intent)
408 xfs_bui_release(BUI_ITEM(intent));
411 /* Cancel a deferred rmap update. */
413 xfs_bmap_update_cancel_item(
414 struct list_head *item)
416 struct xfs_bmap_intent *bmap;
418 bmap = container_of(item, struct xfs_bmap_intent, bi_list);
419 kmem_cache_free(xfs_bmap_intent_cache, bmap);
422 const struct xfs_defer_op_type xfs_bmap_update_defer_type = {
423 .max_items = XFS_BUI_MAX_FAST_EXTENTS,
424 .create_intent = xfs_bmap_update_create_intent,
425 .abort_intent = xfs_bmap_update_abort_intent,
426 .create_done = xfs_bmap_update_create_done,
427 .finish_item = xfs_bmap_update_finish_item,
428 .cancel_item = xfs_bmap_update_cancel_item,
431 /* Is this recovered BUI ok? */
434 struct xfs_mount *mp,
435 struct xfs_bui_log_item *buip)
437 struct xfs_map_extent *bmap;
439 /* Only one mapping operation per BUI... */
440 if (buip->bui_format.bui_nextents != XFS_BUI_MAX_FAST_EXTENTS)
443 bmap = &buip->bui_format.bui_extents[0];
445 if (bmap->me_flags & ~XFS_BMAP_EXTENT_FLAGS)
448 switch (bmap->me_flags & XFS_BMAP_EXTENT_TYPE_MASK) {
456 if (!xfs_verify_ino(mp, bmap->me_owner))
459 if (!xfs_verify_fileext(mp, bmap->me_startoff, bmap->me_len))
462 return xfs_verify_fsbext(mp, bmap->me_startblock, bmap->me_len);
466 * Process a bmap update intent item that was recovered from the log.
467 * We need to update some inode's bmbt.
470 xfs_bui_item_recover(
471 struct xfs_log_item *lip,
472 struct list_head *capture_list)
474 struct xfs_bmbt_irec irec;
475 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
476 struct xfs_trans *tp;
477 struct xfs_inode *ip = NULL;
478 struct xfs_mount *mp = lip->li_log->l_mp;
479 struct xfs_map_extent *bmap;
480 struct xfs_bud_log_item *budp;
483 unsigned int bui_type;
488 if (!xfs_bui_validate(mp, buip)) {
489 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
490 &buip->bui_format, sizeof(buip->bui_format));
491 return -EFSCORRUPTED;
494 bmap = &buip->bui_format.bui_extents[0];
495 state = (bmap->me_flags & XFS_BMAP_EXTENT_UNWRITTEN) ?
496 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
497 whichfork = (bmap->me_flags & XFS_BMAP_EXTENT_ATTR_FORK) ?
498 XFS_ATTR_FORK : XFS_DATA_FORK;
499 bui_type = bmap->me_flags & XFS_BMAP_EXTENT_TYPE_MASK;
501 error = xlog_recover_iget(mp, bmap->me_owner, &ip);
505 /* Allocate transaction and do the work. */
506 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
507 XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK), 0, 0, &tp);
511 budp = xfs_trans_get_bud(tp, buip);
512 xfs_ilock(ip, XFS_ILOCK_EXCL);
513 xfs_trans_ijoin(tp, ip, 0);
515 if (bui_type == XFS_BMAP_MAP)
516 iext_delta = XFS_IEXT_ADD_NOSPLIT_CNT;
518 iext_delta = XFS_IEXT_PUNCH_HOLE_CNT;
520 error = xfs_iext_count_may_overflow(ip, whichfork, iext_delta);
522 error = xfs_iext_count_upgrade(tp, ip, iext_delta);
526 count = bmap->me_len;
527 error = xfs_trans_log_finish_bmap_update(tp, budp, bui_type, ip,
528 whichfork, bmap->me_startoff, bmap->me_startblock,
530 if (error == -EFSCORRUPTED)
531 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bmap,
537 ASSERT(bui_type == XFS_BMAP_UNMAP);
538 irec.br_startblock = bmap->me_startblock;
539 irec.br_blockcount = count;
540 irec.br_startoff = bmap->me_startoff;
541 irec.br_state = state;
542 xfs_bmap_unmap_extent(tp, ip, &irec);
546 * Commit transaction, which frees the transaction and saves the inode
547 * for later replay activities.
549 error = xfs_defer_ops_capture_and_commit(tp, capture_list);
553 xfs_iunlock(ip, XFS_ILOCK_EXCL);
558 xfs_trans_cancel(tp);
560 xfs_iunlock(ip, XFS_ILOCK_EXCL);
568 struct xfs_log_item *lip,
571 return BUI_ITEM(lip)->bui_format.bui_id == intent_id;
574 /* Relog an intent item to push the log tail forward. */
575 static struct xfs_log_item *
577 struct xfs_log_item *intent,
578 struct xfs_trans *tp)
580 struct xfs_bud_log_item *budp;
581 struct xfs_bui_log_item *buip;
582 struct xfs_map_extent *extp;
585 count = BUI_ITEM(intent)->bui_format.bui_nextents;
586 extp = BUI_ITEM(intent)->bui_format.bui_extents;
588 tp->t_flags |= XFS_TRANS_DIRTY;
589 budp = xfs_trans_get_bud(tp, BUI_ITEM(intent));
590 set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags);
592 buip = xfs_bui_init(tp->t_mountp);
593 memcpy(buip->bui_format.bui_extents, extp, count * sizeof(*extp));
594 atomic_set(&buip->bui_next_extent, count);
595 xfs_trans_add_item(tp, &buip->bui_item);
596 set_bit(XFS_LI_DIRTY, &buip->bui_item.li_flags);
597 return &buip->bui_item;
600 static const struct xfs_item_ops xfs_bui_item_ops = {
601 .flags = XFS_ITEM_INTENT,
602 .iop_size = xfs_bui_item_size,
603 .iop_format = xfs_bui_item_format,
604 .iop_unpin = xfs_bui_item_unpin,
605 .iop_release = xfs_bui_item_release,
606 .iop_recover = xfs_bui_item_recover,
607 .iop_match = xfs_bui_item_match,
608 .iop_relog = xfs_bui_item_relog,
612 * Copy an BUI format buffer from the given buf, and into the destination
613 * BUI format structure. The BUI/BUD items were designed not to need any
614 * special alignment handling.
618 struct xfs_log_iovec *buf,
619 struct xfs_bui_log_format *dst_bui_fmt)
621 struct xfs_bui_log_format *src_bui_fmt;
624 src_bui_fmt = buf->i_addr;
625 len = xfs_bui_log_format_sizeof(src_bui_fmt->bui_nextents);
627 if (buf->i_len == len) {
628 memcpy(dst_bui_fmt, src_bui_fmt, len);
631 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
632 return -EFSCORRUPTED;
636 * This routine is called to create an in-core extent bmap update
637 * item from the bui format structure which was logged on disk.
638 * It allocates an in-core bui, copies the extents from the format
639 * structure into it, and adds the bui to the AIL with the given
643 xlog_recover_bui_commit_pass2(
645 struct list_head *buffer_list,
646 struct xlog_recover_item *item,
650 struct xfs_mount *mp = log->l_mp;
651 struct xfs_bui_log_item *buip;
652 struct xfs_bui_log_format *bui_formatp;
654 bui_formatp = item->ri_buf[0].i_addr;
656 if (bui_formatp->bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) {
657 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, log->l_mp);
658 return -EFSCORRUPTED;
660 buip = xfs_bui_init(mp);
661 error = xfs_bui_copy_format(&item->ri_buf[0], &buip->bui_format);
663 xfs_bui_item_free(buip);
666 atomic_set(&buip->bui_next_extent, bui_formatp->bui_nextents);
668 * Insert the intent into the AIL directly and drop one reference so
669 * that finishing or canceling the work will drop the other.
671 xfs_trans_ail_insert(log->l_ailp, &buip->bui_item, lsn);
672 xfs_bui_release(buip);
676 const struct xlog_recover_item_ops xlog_bui_item_ops = {
677 .item_type = XFS_LI_BUI,
678 .commit_pass2 = xlog_recover_bui_commit_pass2,
682 * This routine is called when an BUD format structure is found in a committed
683 * transaction in the log. Its purpose is to cancel the corresponding BUI if it
684 * was still in the log. To do this it searches the AIL for the BUI with an id
685 * equal to that in the BUD format structure. If we find it we drop the BUD
686 * reference, which removes the BUI from the AIL and frees it.
689 xlog_recover_bud_commit_pass2(
691 struct list_head *buffer_list,
692 struct xlog_recover_item *item,
695 struct xfs_bud_log_format *bud_formatp;
697 bud_formatp = item->ri_buf[0].i_addr;
698 if (item->ri_buf[0].i_len != sizeof(struct xfs_bud_log_format)) {
699 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, log->l_mp);
700 return -EFSCORRUPTED;
703 xlog_recover_release_intent(log, XFS_LI_BUI, bud_formatp->bud_bui_id);
707 const struct xlog_recover_item_ops xlog_bud_item_ops = {
708 .item_type = XFS_LI_BUD,
709 .commit_pass2 = xlog_recover_bud_commit_pass2,