LCOV - code coverage report
Current view: top level - fs/xfs - xfs_bmap_item.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsa @ Mon Jul 31 20:08:27 PDT 2023 Lines: 245 284 86.3 %
Date: 2023-07-31 20:08:27 Functions: 28 29 96.6 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0+
       2             : /*
       3             :  * Copyright (C) 2016 Oracle.  All Rights Reserved.
       4             :  * Author: Darrick J. Wong <darrick.wong@oracle.com>
       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_inode.h"
      16             : #include "xfs_trans.h"
      17             : #include "xfs_trans_priv.h"
      18             : #include "xfs_bmap_item.h"
      19             : #include "xfs_log.h"
      20             : #include "xfs_bmap.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"
      27             : #include "xfs_ag.h"
      28             : #include "xfs_rtgroup.h"
      29             : 
      30             : struct kmem_cache       *xfs_bui_cache;
      31             : struct kmem_cache       *xfs_bud_cache;
      32             : 
      33             : static const struct xfs_item_ops xfs_bui_item_ops;
      34             : 
      35             : static inline struct xfs_bui_log_item *BUI_ITEM(struct xfs_log_item *lip)
      36             : {
      37             :         return container_of(lip, struct xfs_bui_log_item, bui_item);
      38             : }
      39             : 
      40             : STATIC void
      41    86543735 : xfs_bui_item_free(
      42             :         struct xfs_bui_log_item *buip)
      43             : {
      44    86543735 :         kmem_free(buip->bui_item.li_lv_shadow);
      45    86543739 :         kmem_cache_free(xfs_bui_cache, buip);
      46    86543707 : }
      47             : 
      48             : /*
      49             :  * Freeing the BUI requires that we remove it from the AIL if it has already
      50             :  * been placed there. However, the BUI may not yet have been placed in the AIL
      51             :  * when called by xfs_bui_release() from BUD processing due to the ordering of
      52             :  * committed vs unpin operations in bulk insert operations. Hence the reference
      53             :  * count to ensure only the last caller frees the BUI.
      54             :  */
      55             : STATIC void
      56   173048844 : xfs_bui_release(
      57             :         struct xfs_bui_log_item *buip)
      58             : {
      59   173048844 :         ASSERT(atomic_read(&buip->bui_refcount) > 0);
      60   346124933 :         if (!atomic_dec_and_test(&buip->bui_refcount))
      61             :                 return;
      62             : 
      63    86543722 :         xfs_trans_ail_delete(&buip->bui_item, 0);
      64    86543728 :         xfs_bui_item_free(buip);
      65             : }
      66             : 
      67             : 
      68             : STATIC void
      69    86541178 : xfs_bui_item_size(
      70             :         struct xfs_log_item     *lip,
      71             :         int                     *nvecs,
      72             :         int                     *nbytes)
      73             : {
      74    86541178 :         struct xfs_bui_log_item *buip = BUI_ITEM(lip);
      75             : 
      76    86541178 :         *nvecs += 1;
      77    86541178 :         *nbytes += xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents);
      78    86541178 : }
      79             : 
      80             : /*
      81             :  * This is called to fill in the vector of log iovecs for the
      82             :  * given bui log item. We use only 1 iovec, and we point that
      83             :  * at the bui_log_format structure embedded in the bui item.
      84             :  * It is at this point that we assert that all of the extent
      85             :  * slots in the bui item have been filled.
      86             :  */
      87             : STATIC void
      88    86541014 : xfs_bui_item_format(
      89             :         struct xfs_log_item     *lip,
      90             :         struct xfs_log_vec      *lv)
      91             : {
      92    86541014 :         struct xfs_bui_log_item *buip = BUI_ITEM(lip);
      93    86541014 :         struct xfs_log_iovec    *vecp = NULL;
      94             : 
      95    86541014 :         ASSERT(atomic_read(&buip->bui_next_extent) ==
      96             :                         buip->bui_format.bui_nextents);
      97             : 
      98    86541014 :         buip->bui_format.bui_type = XFS_LI_BUI;
      99    86541014 :         buip->bui_format.bui_size = 1;
     100             : 
     101    86541014 :         xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUI_FORMAT, &buip->bui_format,
     102    86541014 :                         xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents));
     103    86541238 : }
     104             : 
     105             : /*
     106             :  * The unpin operation is the last place an BUI is manipulated in the log. It is
     107             :  * either inserted in the AIL or aborted in the event of a log I/O error. In
     108             :  * either case, the BUI transaction has been successfully committed to make it
     109             :  * this far. Therefore, we expect whoever committed the BUI to either construct
     110             :  * and commit the BUD or drop the BUD's reference in the event of error. Simply
     111             :  * drop the log's BUI reference now that the log is done with it.
     112             :  */
     113             : STATIC void
     114    86541193 : xfs_bui_item_unpin(
     115             :         struct xfs_log_item     *lip,
     116             :         int                     remove)
     117             : {
     118    86541193 :         struct xfs_bui_log_item *buip = BUI_ITEM(lip);
     119             : 
     120    86541193 :         xfs_bui_release(buip);
     121    86541255 : }
     122             : 
     123             : /*
     124             :  * The BUI has been either committed or aborted if the transaction has been
     125             :  * cancelled. If the transaction was cancelled, an BUD isn't going to be
     126             :  * constructed and thus we free the BUI here directly.
     127             :  */
     128             : STATIC void
     129        2187 : xfs_bui_item_release(
     130             :         struct xfs_log_item     *lip)
     131             : {
     132        2187 :         xfs_bui_release(BUI_ITEM(lip));
     133        2187 : }
     134             : 
     135             : /*
     136             :  * Allocate and initialize an bui item with the given number of extents.
     137             :  */
     138             : STATIC struct xfs_bui_log_item *
     139    86542185 : xfs_bui_init(
     140             :         struct xfs_mount                *mp)
     141             : 
     142             : {
     143    86542185 :         struct xfs_bui_log_item         *buip;
     144             : 
     145    86542185 :         buip = kmem_cache_zalloc(xfs_bui_cache, GFP_KERNEL | __GFP_NOFAIL);
     146             : 
     147    86542482 :         xfs_log_item_init(mp, &buip->bui_item, XFS_LI_BUI, &xfs_bui_item_ops);
     148    86542444 :         buip->bui_format.bui_nextents = XFS_BUI_MAX_FAST_EXTENTS;
     149    86542444 :         buip->bui_format.bui_id = (uintptr_t)(void *)buip;
     150    86542444 :         atomic_set(&buip->bui_next_extent, 0);
     151    86542444 :         atomic_set(&buip->bui_refcount, 2);
     152             : 
     153    86542444 :         return buip;
     154             : }
     155             : 
     156             : static inline struct xfs_bud_log_item *BUD_ITEM(struct xfs_log_item *lip)
     157             : {
     158             :         return container_of(lip, struct xfs_bud_log_item, bud_item);
     159             : }
     160             : 
     161             : STATIC void
     162    86541253 : xfs_bud_item_size(
     163             :         struct xfs_log_item     *lip,
     164             :         int                     *nvecs,
     165             :         int                     *nbytes)
     166             : {
     167    86541253 :         *nvecs += 1;
     168    86541253 :         *nbytes += sizeof(struct xfs_bud_log_format);
     169    86541253 : }
     170             : 
     171             : /*
     172             :  * This is called to fill in the vector of log iovecs for the
     173             :  * given bud log item. We use only 1 iovec, and we point that
     174             :  * at the bud_log_format structure embedded in the bud item.
     175             :  * It is at this point that we assert that all of the extent
     176             :  * slots in the bud item have been filled.
     177             :  */
     178             : STATIC void
     179       71514 : xfs_bud_item_format(
     180             :         struct xfs_log_item     *lip,
     181             :         struct xfs_log_vec      *lv)
     182             : {
     183       71514 :         struct xfs_bud_log_item *budp = BUD_ITEM(lip);
     184       71514 :         struct xfs_log_iovec    *vecp = NULL;
     185             : 
     186       71514 :         budp->bud_format.bud_type = XFS_LI_BUD;
     187       71514 :         budp->bud_format.bud_size = 1;
     188             : 
     189       71514 :         xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUD_FORMAT, &budp->bud_format,
     190             :                         sizeof(struct xfs_bud_log_format));
     191       71514 : }
     192             : 
     193             : /*
     194             :  * The BUD is either committed or aborted if the transaction is cancelled. If
     195             :  * the transaction is cancelled, drop our reference to the BUI and free the
     196             :  * BUD.
     197             :  */
     198             : STATIC void
     199    86541319 : xfs_bud_item_release(
     200             :         struct xfs_log_item     *lip)
     201             : {
     202    86541319 :         struct xfs_bud_log_item *budp = BUD_ITEM(lip);
     203             : 
     204    86541319 :         xfs_bui_release(budp->bud_buip);
     205    86541316 :         kmem_free(budp->bud_item.li_lv_shadow);
     206    86541322 :         kmem_cache_free(xfs_bud_cache, budp);
     207    86541316 : }
     208             : 
     209             : static struct xfs_log_item *
     210    86541090 : xfs_bud_item_intent(
     211             :         struct xfs_log_item     *lip)
     212             : {
     213    86541090 :         return &BUD_ITEM(lip)->bud_buip->bui_item;
     214             : }
     215             : 
     216             : static const struct xfs_item_ops xfs_bud_item_ops = {
     217             :         .flags          = XFS_ITEM_RELEASE_WHEN_COMMITTED |
     218             :                           XFS_ITEM_INTENT_DONE,
     219             :         .iop_size       = xfs_bud_item_size,
     220             :         .iop_format     = xfs_bud_item_format,
     221             :         .iop_release    = xfs_bud_item_release,
     222             :         .iop_intent     = xfs_bud_item_intent,
     223             : };
     224             : 
     225             : static struct xfs_bud_log_item *
     226    86540950 : xfs_trans_get_bud(
     227             :         struct xfs_trans                *tp,
     228             :         struct xfs_bui_log_item         *buip)
     229             : {
     230    86540950 :         struct xfs_bud_log_item         *budp;
     231             : 
     232    86540950 :         budp = kmem_cache_zalloc(xfs_bud_cache, GFP_KERNEL | __GFP_NOFAIL);
     233    86541163 :         xfs_log_item_init(tp->t_mountp, &budp->bud_item, XFS_LI_BUD,
     234             :                           &xfs_bud_item_ops);
     235    86541164 :         budp->bud_buip = buip;
     236    86541164 :         budp->bud_format.bud_bui_id = buip->bui_format.bui_id;
     237             : 
     238    86541164 :         xfs_trans_add_item(tp, &budp->bud_item);
     239    86541150 :         return budp;
     240             : }
     241             : 
     242             : /*
     243             :  * Finish an bmap update and log it to the BUD. Note that the
     244             :  * transaction is marked dirty regardless of whether the bmap update
     245             :  * succeeds or fails to support the BUI/BUD lifecycle rules.
     246             :  */
     247             : static int
     248    86540932 : xfs_trans_log_finish_bmap_update(
     249             :         struct xfs_trans                *tp,
     250             :         struct xfs_bud_log_item         *budp,
     251             :         struct xfs_bmap_intent          *bi)
     252             : {
     253    86540932 :         int                             error;
     254             : 
     255    86540932 :         error = xfs_bmap_finish_one(tp, bi);
     256             : 
     257             :         /*
     258             :          * Mark the transaction dirty, even on error. This ensures the
     259             :          * transaction is aborted, which:
     260             :          *
     261             :          * 1.) releases the BUI and frees the BUD
     262             :          * 2.) shuts down the filesystem
     263             :          */
     264    86541043 :         tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
     265    86541043 :         set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags);
     266             : 
     267    86541076 :         return error;
     268             : }
     269             : 
     270             : /* Sort bmap intents by inode. */
     271             : static int
     272           0 : xfs_bmap_update_diff_items(
     273             :         void                            *priv,
     274             :         const struct list_head          *a,
     275             :         const struct list_head          *b)
     276             : {
     277           0 :         struct xfs_bmap_intent          *ba;
     278           0 :         struct xfs_bmap_intent          *bb;
     279             : 
     280           0 :         ba = container_of(a, struct xfs_bmap_intent, bi_list);
     281           0 :         bb = container_of(b, struct xfs_bmap_intent, bi_list);
     282           0 :         return ba->bi_owner->i_ino - bb->bi_owner->i_ino;
     283             : }
     284             : 
     285             : /* Log bmap updates in the intent item. */
     286             : STATIC void
     287    86540669 : xfs_bmap_update_log_item(
     288             :         struct xfs_trans                *tp,
     289             :         struct xfs_bui_log_item         *buip,
     290             :         struct xfs_bmap_intent          *bi)
     291             : {
     292    86540669 :         uint                            next_extent;
     293    86540669 :         struct xfs_map_extent           *map;
     294             : 
     295    86540669 :         tp->t_flags |= XFS_TRANS_DIRTY;
     296    86540669 :         set_bit(XFS_LI_DIRTY, &buip->bui_item.li_flags);
     297             : 
     298             :         /*
     299             :          * atomic_inc_return gives us the value after the increment;
     300             :          * we want to use it as an array index so we need to subtract 1 from
     301             :          * it.
     302             :          */
     303    86540710 :         next_extent = atomic_inc_return(&buip->bui_next_extent) - 1;
     304    86540651 :         ASSERT(next_extent < buip->bui_format.bui_nextents);
     305    86540651 :         map = &buip->bui_format.bui_extents[next_extent];
     306    86540651 :         map->me_owner = bi->bi_owner->i_ino;
     307    86540651 :         map->me_startblock = bi->bi_bmap.br_startblock;
     308    86540651 :         map->me_startoff = bi->bi_bmap.br_startoff;
     309    86540651 :         map->me_len = bi->bi_bmap.br_blockcount;
     310             : 
     311    86540651 :         switch (bi->bi_type) {
     312    86540651 :         case XFS_BMAP_MAP:
     313             :         case XFS_BMAP_UNMAP:
     314    86540651 :                 map->me_flags = bi->bi_type;
     315    86540651 :                 break;
     316           0 :         default:
     317           0 :                 ASSERT(0);
     318             :         }
     319    86540651 :         if (bi->bi_bmap.br_state == XFS_EXT_UNWRITTEN)
     320     8477930 :                 map->me_flags |= XFS_BMAP_EXTENT_UNWRITTEN;
     321    86540651 :         if (bi->bi_whichfork == XFS_ATTR_FORK)
     322      214323 :                 map->me_flags |= XFS_BMAP_EXTENT_ATTR_FORK;
     323    86540651 :         if (xfs_ifork_is_realtime(bi->bi_owner, bi->bi_whichfork))
     324    22248131 :                 map->me_flags |= XFS_BMAP_EXTENT_REALTIME;
     325    86540673 : }
     326             : 
     327             : static struct xfs_log_item *
     328    86539317 : xfs_bmap_update_create_intent(
     329             :         struct xfs_trans                *tp,
     330             :         struct list_head                *items,
     331             :         unsigned int                    count,
     332             :         bool                            sort)
     333             : {
     334    86539317 :         struct xfs_mount                *mp = tp->t_mountp;
     335    86539317 :         struct xfs_bui_log_item         *buip = xfs_bui_init(mp);
     336    86539703 :         struct xfs_bmap_intent          *bi;
     337             : 
     338    86539703 :         ASSERT(count == XFS_BUI_MAX_FAST_EXTENTS);
     339             : 
     340    86539703 :         xfs_trans_add_item(tp, &buip->bui_item);
     341    86540654 :         if (sort)
     342    86540774 :                 list_sort(mp, items, xfs_bmap_update_diff_items);
     343   173080650 :         list_for_each_entry(bi, items, bi_list)
     344    86540170 :                 xfs_bmap_update_log_item(tp, buip, bi);
     345    86540480 :         return &buip->bui_item;
     346             : }
     347             : 
     348             : /* Get an BUD so we can process all the deferred rmap updates. */
     349             : static struct xfs_log_item *
     350    86540598 : xfs_bmap_update_create_done(
     351             :         struct xfs_trans                *tp,
     352             :         struct xfs_log_item             *intent,
     353             :         unsigned int                    count)
     354             : {
     355    86540598 :         return &xfs_trans_get_bud(tp, BUI_ITEM(intent))->bud_item;
     356             : }
     357             : 
     358             : /* Take a passive ref to the AG containing the space we're mapping. */
     359             : void
     360    86540376 : xfs_bmap_update_get_group(
     361             :         struct xfs_mount        *mp,
     362             :         struct xfs_bmap_intent  *bi)
     363             : {
     364    86540376 :         xfs_agnumber_t          agno;
     365             : 
     366    86540376 :         if (xfs_ifork_is_realtime(bi->bi_owner, bi->bi_whichfork)) {
     367    22248249 :                 if (xfs_has_rtgroups(mp)) {
     368    22248249 :                         xfs_rgnumber_t  rgno;
     369             : 
     370    22248249 :                         rgno = xfs_rtb_to_rgno(mp, bi->bi_bmap.br_startblock);
     371    22247404 :                         bi->bi_rtg = xfs_rtgroup_intent_get(mp, rgno);
     372             :                 } else {
     373           0 :                         bi->bi_rtg = NULL;
     374             :                 }
     375             : 
     376    22247215 :                 return;
     377             :         }
     378             : 
     379    64292813 :         agno = XFS_FSB_TO_AGNO(mp, bi->bi_bmap.br_startblock);
     380             : 
     381             :         /*
     382             :          * Bump the intent count on behalf of the deferred rmap and refcount
     383             :          * intent items that that we can queue when we finish this bmap work.
     384             :          * This new intent item will bump the intent count before the bmap
     385             :          * intent drops the intent count, ensuring that the intent count
     386             :          * remains nonzero across the transaction roll.
     387             :          */
     388    64292813 :         bi->bi_pag = xfs_perag_intent_get(mp, agno);
     389             : }
     390             : 
     391             : /* Release a passive AG ref after finishing mapping work. */
     392             : static inline void
     393    86541258 : xfs_bmap_update_put_group(
     394             :         struct xfs_bmap_intent  *bi)
     395             : {
     396    86541258 :         if (xfs_ifork_is_realtime(bi->bi_owner, bi->bi_whichfork)) {
     397    22248419 :                 if (xfs_has_rtgroups(bi->bi_owner->i_mount)) {
     398    22248417 :                         xfs_rtgroup_intent_put(bi->bi_rtg);
     399             :                 }
     400    22248440 :                 return;
     401             :         }
     402             : 
     403    64292836 :         xfs_perag_intent_put(bi->bi_pag);
     404             : }
     405             : 
     406             : /* Process a deferred rmap update. */
     407             : STATIC int
     408    86540739 : xfs_bmap_update_finish_item(
     409             :         struct xfs_trans                *tp,
     410             :         struct xfs_log_item             *done,
     411             :         struct list_head                *item,
     412             :         struct xfs_btree_cur            **state)
     413             : {
     414    86540739 :         struct xfs_bmap_intent          *bi;
     415    86540739 :         int                             error;
     416             : 
     417    86540739 :         bi = container_of(item, struct xfs_bmap_intent, bi_list);
     418             : 
     419    86540739 :         error = xfs_trans_log_finish_bmap_update(tp, BUD_ITEM(done), bi);
     420    86540767 :         if (!error && bi->bi_bmap.br_blockcount > 0) {
     421           0 :                 ASSERT(bi->bi_type == XFS_BMAP_UNMAP);
     422           0 :                 return -EAGAIN;
     423             :         }
     424             : 
     425    86540767 :         xfs_bmap_update_put_group(bi);
     426    86540770 :         kmem_cache_free(xfs_bmap_intent_cache, bi);
     427    86540770 :         return error;
     428             : }
     429             : 
     430             : /* Abort all pending BUIs. */
     431             : STATIC void
     432         227 : xfs_bmap_update_abort_intent(
     433             :         struct xfs_log_item             *intent)
     434             : {
     435         227 :         xfs_bui_release(BUI_ITEM(intent));
     436         227 : }
     437             : 
     438             : /* Cancel a deferred bmap update. */
     439             : STATIC void
     440         227 : xfs_bmap_update_cancel_item(
     441             :         struct list_head                *item)
     442             : {
     443         227 :         struct xfs_bmap_intent          *bi;
     444             : 
     445         227 :         bi = container_of(item, struct xfs_bmap_intent, bi_list);
     446             : 
     447         227 :         xfs_bmap_update_put_group(bi);
     448         227 :         kmem_cache_free(xfs_bmap_intent_cache, bi);
     449         227 : }
     450             : 
     451             : const struct xfs_defer_op_type xfs_bmap_update_defer_type = {
     452             :         .max_items      = XFS_BUI_MAX_FAST_EXTENTS,
     453             :         .create_intent  = xfs_bmap_update_create_intent,
     454             :         .abort_intent   = xfs_bmap_update_abort_intent,
     455             :         .create_done    = xfs_bmap_update_create_done,
     456             :         .finish_item    = xfs_bmap_update_finish_item,
     457             :         .cancel_item    = xfs_bmap_update_cancel_item,
     458             : };
     459             : 
     460             : /* Is this recovered BUI ok? */
     461             : static inline bool
     462         286 : xfs_bui_validate(
     463             :         struct xfs_mount                *mp,
     464             :         struct xfs_bui_log_item         *buip)
     465             : {
     466         286 :         struct xfs_map_extent           *map;
     467             : 
     468             :         /* Only one mapping operation per BUI... */
     469         286 :         if (buip->bui_format.bui_nextents != XFS_BUI_MAX_FAST_EXTENTS)
     470             :                 return false;
     471             : 
     472         286 :         map = &buip->bui_format.bui_extents[0];
     473             : 
     474         286 :         if (map->me_flags & ~XFS_BMAP_EXTENT_FLAGS)
     475             :                 return false;
     476             : 
     477         286 :         switch (map->me_flags & XFS_BMAP_EXTENT_TYPE_MASK) {
     478             :         case XFS_BMAP_MAP:
     479             :         case XFS_BMAP_UNMAP:
     480         286 :                 break;
     481             :         default:
     482             :                 return false;
     483             :         }
     484             : 
     485         286 :         if (!xfs_verify_ino(mp, map->me_owner))
     486             :                 return false;
     487             : 
     488         286 :         if (!xfs_verify_fileext(mp, map->me_startoff, map->me_len))
     489             :                 return false;
     490             : 
     491         286 :         if (map->me_flags & XFS_BMAP_EXTENT_REALTIME)
     492           0 :                 return xfs_verify_rtbext(mp, map->me_startblock, map->me_len);
     493             : 
     494         286 :         return xfs_verify_fsbext(mp, map->me_startblock, map->me_len);
     495             : }
     496             : 
     497             : /*
     498             :  * Process a bmap update intent item that was recovered from the log.
     499             :  * We need to update some inode's bmbt.
     500             :  */
     501             : STATIC int
     502         286 : xfs_bui_item_recover(
     503             :         struct xfs_log_item             *lip,
     504             :         struct list_head                *capture_list)
     505             : {
     506         286 :         struct xfs_bmap_intent          fake = { };
     507         286 :         struct xfs_bui_log_item         *buip = BUI_ITEM(lip);
     508         286 :         struct xfs_trans                *tp;
     509         286 :         struct xfs_inode                *ip = NULL;
     510         286 :         struct xfs_mount                *mp = lip->li_log->l_mp;
     511         286 :         struct xfs_map_extent           *map;
     512         286 :         struct xfs_bud_log_item         *budp;
     513         286 :         int                             iext_delta;
     514         286 :         int                             error = 0;
     515             : 
     516         286 :         if (!xfs_bui_validate(mp, buip)) {
     517           0 :                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     518             :                                 &buip->bui_format, sizeof(buip->bui_format));
     519           0 :                 return -EFSCORRUPTED;
     520             :         }
     521             : 
     522         286 :         map = &buip->bui_format.bui_extents[0];
     523         286 :         fake.bi_whichfork = (map->me_flags & XFS_BMAP_EXTENT_ATTR_FORK) ?
     524         286 :                         XFS_ATTR_FORK : XFS_DATA_FORK;
     525         286 :         fake.bi_type = map->me_flags & XFS_BMAP_EXTENT_TYPE_MASK;
     526             : 
     527         286 :         error = xlog_recover_iget(mp, map->me_owner, &ip);
     528         286 :         if (error)
     529             :                 return error;
     530             : 
     531             :         /* Allocate transaction and do the work. */
     532         286 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
     533         286 :                         XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK), 0, 0, &tp);
     534         286 :         if (error)
     535           0 :                 goto err_rele;
     536             : 
     537         286 :         budp = xfs_trans_get_bud(tp, buip);
     538         286 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
     539         286 :         xfs_trans_ijoin(tp, ip, 0);
     540             : 
     541         286 :         if (!!(map->me_flags & XFS_BMAP_EXTENT_REALTIME) !=
     542         286 :             xfs_ifork_is_realtime(ip, fake.bi_whichfork)) {
     543           0 :                 error = -EFSCORRUPTED;
     544           0 :                 goto err_cancel;
     545             :         }
     546             : 
     547         286 :         if (fake.bi_type == XFS_BMAP_MAP)
     548             :                 iext_delta = XFS_IEXT_ADD_NOSPLIT_CNT;
     549             :         else
     550             :                 iext_delta = XFS_IEXT_PUNCH_HOLE_CNT;
     551             : 
     552         286 :         error = xfs_iext_count_may_overflow(ip, fake.bi_whichfork, iext_delta);
     553         286 :         if (error == -EFBIG)
     554           0 :                 error = xfs_iext_count_upgrade(tp, ip, iext_delta);
     555         286 :         if (error)
     556           0 :                 goto err_cancel;
     557             : 
     558         286 :         fake.bi_owner = ip;
     559         286 :         fake.bi_bmap.br_startblock = map->me_startblock;
     560         286 :         fake.bi_bmap.br_startoff = map->me_startoff;
     561         286 :         fake.bi_bmap.br_blockcount = map->me_len;
     562         286 :         fake.bi_bmap.br_state = (map->me_flags & XFS_BMAP_EXTENT_UNWRITTEN) ?
     563         286 :                         XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
     564             : 
     565         286 :         xfs_bmap_update_get_group(mp, &fake);
     566         286 :         error = xfs_trans_log_finish_bmap_update(tp, budp, &fake);
     567         286 :         if (error == -EFSCORRUPTED)
     568           0 :                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, map,
     569             :                                 sizeof(*map));
     570         286 :         xfs_bmap_update_put_group(&fake);
     571         286 :         if (error)
     572           0 :                 goto err_cancel;
     573             : 
     574         286 :         if (fake.bi_bmap.br_blockcount > 0) {
     575           0 :                 ASSERT(fake.bi_type == XFS_BMAP_UNMAP);
     576           0 :                 xfs_bmap_unmap_extent(tp, fake.bi_owner, fake.bi_whichfork,
     577             :                                 &fake.bi_bmap);
     578             :         }
     579             : 
     580             :         /*
     581             :          * Commit transaction, which frees the transaction and saves the inode
     582             :          * for later replay activities.
     583             :          */
     584         286 :         error = xfs_defer_ops_capture_and_commit(tp, capture_list);
     585         286 :         if (error)
     586           0 :                 goto err_unlock;
     587             : 
     588         286 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     589         286 :         xfs_irele(ip);
     590         286 :         return 0;
     591             : 
     592           0 : err_cancel:
     593           0 :         xfs_trans_cancel(tp);
     594           0 : err_unlock:
     595           0 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     596           0 : err_rele:
     597           0 :         xfs_irele(ip);
     598           0 :         return error;
     599             : }
     600             : 
     601             : STATIC bool
     602        2187 : xfs_bui_item_match(
     603             :         struct xfs_log_item     *lip,
     604             :         uint64_t                intent_id)
     605             : {
     606        2187 :         return BUI_ITEM(lip)->bui_format.bui_id == intent_id;
     607             : }
     608             : 
     609             : /* Relog an intent item to push the log tail forward. */
     610             : static struct xfs_log_item *
     611         173 : xfs_bui_item_relog(
     612             :         struct xfs_log_item             *intent,
     613             :         struct xfs_trans                *tp)
     614             : {
     615         173 :         struct xfs_bud_log_item         *budp;
     616         173 :         struct xfs_bui_log_item         *buip;
     617         173 :         struct xfs_map_extent           *map;
     618         173 :         unsigned int                    count;
     619             : 
     620         173 :         count = BUI_ITEM(intent)->bui_format.bui_nextents;
     621         173 :         map = BUI_ITEM(intent)->bui_format.bui_extents;
     622             : 
     623         173 :         tp->t_flags |= XFS_TRANS_DIRTY;
     624         173 :         budp = xfs_trans_get_bud(tp, BUI_ITEM(intent));
     625         173 :         set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags);
     626             : 
     627         173 :         buip = xfs_bui_init(tp->t_mountp);
     628         346 :         memcpy(buip->bui_format.bui_extents, map, count * sizeof(*map));
     629         173 :         atomic_set(&buip->bui_next_extent, count);
     630         173 :         xfs_trans_add_item(tp, &buip->bui_item);
     631         173 :         set_bit(XFS_LI_DIRTY, &buip->bui_item.li_flags);
     632         173 :         return &buip->bui_item;
     633             : }
     634             : 
     635             : static const struct xfs_item_ops xfs_bui_item_ops = {
     636             :         .flags          = XFS_ITEM_INTENT,
     637             :         .iop_size       = xfs_bui_item_size,
     638             :         .iop_format     = xfs_bui_item_format,
     639             :         .iop_unpin      = xfs_bui_item_unpin,
     640             :         .iop_release    = xfs_bui_item_release,
     641             :         .iop_recover    = xfs_bui_item_recover,
     642             :         .iop_match      = xfs_bui_item_match,
     643             :         .iop_relog      = xfs_bui_item_relog,
     644             : };
     645             : 
     646             : static inline void
     647        2473 : xfs_bui_copy_format(
     648             :         struct xfs_bui_log_format       *dst,
     649             :         const struct xfs_bui_log_format *src)
     650             : {
     651        2473 :         unsigned int                    i;
     652             : 
     653        4946 :         memcpy(dst, src, offsetof(struct xfs_bui_log_format, bui_extents));
     654             : 
     655        4946 :         for (i = 0; i < src->bui_nextents; i++)
     656        4946 :                 memcpy(&dst->bui_extents[i], &src->bui_extents[i],
     657             :                                 sizeof(struct xfs_map_extent));
     658        2473 : }
     659             : 
     660             : /*
     661             :  * This routine is called to create an in-core extent bmap update
     662             :  * item from the bui format structure which was logged on disk.
     663             :  * It allocates an in-core bui, copies the extents from the format
     664             :  * structure into it, and adds the bui to the AIL with the given
     665             :  * LSN.
     666             :  */
     667             : STATIC int
     668        2473 : xlog_recover_bui_commit_pass2(
     669             :         struct xlog                     *log,
     670             :         struct list_head                *buffer_list,
     671             :         struct xlog_recover_item        *item,
     672             :         xfs_lsn_t                       lsn)
     673             : {
     674        2473 :         struct xfs_mount                *mp = log->l_mp;
     675        2473 :         struct xfs_bui_log_item         *buip;
     676        2473 :         struct xfs_bui_log_format       *bui_formatp;
     677        2473 :         size_t                          len;
     678             : 
     679        2473 :         bui_formatp = item->ri_buf[0].i_addr;
     680             : 
     681        2473 :         if (item->ri_buf[0].i_len < xfs_bui_log_format_sizeof(0)) {
     682           0 :                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     683             :                                 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
     684           0 :                 return -EFSCORRUPTED;
     685             :         }
     686             : 
     687        2473 :         if (bui_formatp->bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) {
     688           0 :                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     689             :                                 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
     690           0 :                 return -EFSCORRUPTED;
     691             :         }
     692             : 
     693        2473 :         len = xfs_bui_log_format_sizeof(bui_formatp->bui_nextents);
     694        2473 :         if (item->ri_buf[0].i_len != len) {
     695           0 :                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     696             :                                 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
     697           0 :                 return -EFSCORRUPTED;
     698             :         }
     699             : 
     700        2473 :         buip = xfs_bui_init(mp);
     701        2473 :         xfs_bui_copy_format(&buip->bui_format, bui_formatp);
     702        2473 :         atomic_set(&buip->bui_next_extent, bui_formatp->bui_nextents);
     703             :         /*
     704             :          * Insert the intent into the AIL directly and drop one reference so
     705             :          * that finishing or canceling the work will drop the other.
     706             :          */
     707        2473 :         xfs_trans_ail_insert(log->l_ailp, &buip->bui_item, lsn);
     708        2473 :         xfs_bui_release(buip);
     709        2473 :         return 0;
     710             : }
     711             : 
     712             : const struct xlog_recover_item_ops xlog_bui_item_ops = {
     713             :         .item_type              = XFS_LI_BUI,
     714             :         .commit_pass2           = xlog_recover_bui_commit_pass2,
     715             : };
     716             : 
     717             : /*
     718             :  * This routine is called when an BUD format structure is found in a committed
     719             :  * transaction in the log. Its purpose is to cancel the corresponding BUI if it
     720             :  * was still in the log. To do this it searches the AIL for the BUI with an id
     721             :  * equal to that in the BUD format structure. If we find it we drop the BUD
     722             :  * reference, which removes the BUI from the AIL and frees it.
     723             :  */
     724             : STATIC int
     725        2188 : xlog_recover_bud_commit_pass2(
     726             :         struct xlog                     *log,
     727             :         struct list_head                *buffer_list,
     728             :         struct xlog_recover_item        *item,
     729             :         xfs_lsn_t                       lsn)
     730             : {
     731        2188 :         struct xfs_bud_log_format       *bud_formatp;
     732             : 
     733        2188 :         bud_formatp = item->ri_buf[0].i_addr;
     734        2188 :         if (item->ri_buf[0].i_len != sizeof(struct xfs_bud_log_format)) {
     735           0 :                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
     736             :                                 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
     737           0 :                 return -EFSCORRUPTED;
     738             :         }
     739             : 
     740        2188 :         xlog_recover_release_intent(log, XFS_LI_BUI, bud_formatp->bud_bui_id);
     741        2188 :         return 0;
     742             : }
     743             : 
     744             : const struct xlog_recover_item_ops xlog_bud_item_ops = {
     745             :         .item_type              = XFS_LI_BUD,
     746             :         .commit_pass2           = xlog_recover_bud_commit_pass2,
     747             : };

Generated by: LCOV version 1.14