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-xfsx @ Mon Jul 31 20:08:34 PDT 2023 Lines: 247 284 87.0 %
Date: 2023-07-31 20:08:34 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   217977929 : xfs_bui_item_free(
      42             :         struct xfs_bui_log_item *buip)
      43             : {
      44   217977929 :         kmem_free(buip->bui_item.li_lv_shadow);
      45   217977729 :         kmem_cache_free(xfs_bui_cache, buip);
      46   217982399 : }
      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   435928551 : xfs_bui_release(
      57             :         struct xfs_bui_log_item *buip)
      58             : {
      59   435928551 :         ASSERT(atomic_read(&buip->bui_refcount) > 0);
      60   435928551 :         if (!atomic_dec_and_test(&buip->bui_refcount))
      61             :                 return;
      62             : 
      63   217976647 :         xfs_trans_ail_delete(&buip->bui_item, 0);
      64   217980524 :         xfs_bui_item_free(buip);
      65             : }
      66             : 
      67             : 
      68             : STATIC void
      69   217979743 : xfs_bui_item_size(
      70             :         struct xfs_log_item     *lip,
      71             :         int                     *nvecs,
      72             :         int                     *nbytes)
      73             : {
      74   217979743 :         struct xfs_bui_log_item *buip = BUI_ITEM(lip);
      75             : 
      76   217979743 :         *nvecs += 1;
      77   217979743 :         *nbytes += xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents);
      78   217979743 : }
      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   217979415 : xfs_bui_item_format(
      89             :         struct xfs_log_item     *lip,
      90             :         struct xfs_log_vec      *lv)
      91             : {
      92   217979415 :         struct xfs_bui_log_item *buip = BUI_ITEM(lip);
      93   217979415 :         struct xfs_log_iovec    *vecp = NULL;
      94             : 
      95   217979415 :         ASSERT(atomic_read(&buip->bui_next_extent) ==
      96             :                         buip->bui_format.bui_nextents);
      97             : 
      98   217979415 :         buip->bui_format.bui_type = XFS_LI_BUI;
      99   217979415 :         buip->bui_format.bui_size = 1;
     100             : 
     101   217979415 :         xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUI_FORMAT, &buip->bui_format,
     102   217979415 :                         xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents));
     103   217978658 : }
     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   217963942 : xfs_bui_item_unpin(
     115             :         struct xfs_log_item     *lip,
     116             :         int                     remove)
     117             : {
     118   217963942 :         struct xfs_bui_log_item *buip = BUI_ITEM(lip);
     119             : 
     120   217963942 :         xfs_bui_release(buip);
     121   217976411 : }
     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        2947 : xfs_bui_item_release(
     130             :         struct xfs_log_item     *lip)
     131             : {
     132        2947 :         xfs_bui_release(BUI_ITEM(lip));
     133        2947 : }
     134             : 
     135             : /*
     136             :  * Allocate and initialize an bui item with the given number of extents.
     137             :  */
     138             : STATIC struct xfs_bui_log_item *
     139   217978603 : xfs_bui_init(
     140             :         struct xfs_mount                *mp)
     141             : 
     142             : {
     143   217978603 :         struct xfs_bui_log_item         *buip;
     144             : 
     145   217978603 :         buip = kmem_cache_zalloc(xfs_bui_cache, GFP_KERNEL | __GFP_NOFAIL);
     146             : 
     147   217981882 :         xfs_log_item_init(mp, &buip->bui_item, XFS_LI_BUI, &xfs_bui_item_ops);
     148   217980214 :         buip->bui_format.bui_nextents = XFS_BUI_MAX_FAST_EXTENTS;
     149   217980214 :         buip->bui_format.bui_id = (uintptr_t)(void *)buip;
     150   217980214 :         atomic_set(&buip->bui_next_extent, 0);
     151   217980214 :         atomic_set(&buip->bui_refcount, 2);
     152             : 
     153   217980214 :         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   217979889 : xfs_bud_item_size(
     163             :         struct xfs_log_item     *lip,
     164             :         int                     *nvecs,
     165             :         int                     *nbytes)
     166             : {
     167   217979889 :         *nvecs += 1;
     168   217979889 :         *nbytes += sizeof(struct xfs_bud_log_format);
     169   217979889 : }
     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      267718 : xfs_bud_item_format(
     180             :         struct xfs_log_item     *lip,
     181             :         struct xfs_log_vec      *lv)
     182             : {
     183      267718 :         struct xfs_bud_log_item *budp = BUD_ITEM(lip);
     184      267718 :         struct xfs_log_iovec    *vecp = NULL;
     185             : 
     186      267718 :         budp->bud_format.bud_type = XFS_LI_BUD;
     187      267718 :         budp->bud_format.bud_size = 1;
     188             : 
     189      267718 :         xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUD_FORMAT, &budp->bud_format,
     190             :                         sizeof(struct xfs_bud_log_format));
     191      267719 : }
     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   217980006 : xfs_bud_item_release(
     200             :         struct xfs_log_item     *lip)
     201             : {
     202   217980006 :         struct xfs_bud_log_item *budp = BUD_ITEM(lip);
     203             : 
     204   217980006 :         xfs_bui_release(budp->bud_buip);
     205   217980050 :         kmem_free(budp->bud_item.li_lv_shadow);
     206   217979967 :         kmem_cache_free(xfs_bud_cache, budp);
     207   217979951 : }
     208             : 
     209             : static struct xfs_log_item *
     210   217912932 : xfs_bud_item_intent(
     211             :         struct xfs_log_item     *lip)
     212             : {
     213   217912932 :         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   217978858 : xfs_trans_get_bud(
     227             :         struct xfs_trans                *tp,
     228             :         struct xfs_bui_log_item         *buip)
     229             : {
     230   217978858 :         struct xfs_bud_log_item         *budp;
     231             : 
     232   217978858 :         budp = kmem_cache_zalloc(xfs_bud_cache, GFP_KERNEL | __GFP_NOFAIL);
     233   217979737 :         xfs_log_item_init(tp->t_mountp, &budp->bud_item, XFS_LI_BUD,
     234             :                           &xfs_bud_item_ops);
     235   217979359 :         budp->bud_buip = buip;
     236   217979359 :         budp->bud_format.bud_bui_id = buip->bui_format.bui_id;
     237             : 
     238   217979359 :         xfs_trans_add_item(tp, &budp->bud_item);
     239   217979649 :         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   217912273 : 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   217912273 :         int                             error;
     254             : 
     255   217912273 :         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   217912597 :         tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
     265   217912597 :         set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags);
     266             : 
     267   217913061 :         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   217910194 : 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   217910194 :         uint                            next_extent;
     293   217910194 :         struct xfs_map_extent           *map;
     294             : 
     295   217910194 :         tp->t_flags |= XFS_TRANS_DIRTY;
     296   217910194 :         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   217912616 :         next_extent = atomic_inc_return(&buip->bui_next_extent) - 1;
     304   217911922 :         ASSERT(next_extent < buip->bui_format.bui_nextents);
     305   217911922 :         map = &buip->bui_format.bui_extents[next_extent];
     306   217911922 :         map->me_owner = bi->bi_owner->i_ino;
     307   217911922 :         map->me_startblock = bi->bi_bmap.br_startblock;
     308   217911922 :         map->me_startoff = bi->bi_bmap.br_startoff;
     309   217911922 :         map->me_len = bi->bi_bmap.br_blockcount;
     310             : 
     311   217911922 :         switch (bi->bi_type) {
     312   217911922 :         case XFS_BMAP_MAP:
     313             :         case XFS_BMAP_UNMAP:
     314   217911922 :                 map->me_flags = bi->bi_type;
     315   217911922 :                 break;
     316           0 :         default:
     317           0 :                 ASSERT(0);
     318             :         }
     319   217911922 :         if (bi->bi_bmap.br_state == XFS_EXT_UNWRITTEN)
     320    23374994 :                 map->me_flags |= XFS_BMAP_EXTENT_UNWRITTEN;
     321   217911922 :         if (bi->bi_whichfork == XFS_ATTR_FORK)
     322      450136 :                 map->me_flags |= XFS_BMAP_EXTENT_ATTR_FORK;
     323   217911922 :         if (xfs_ifork_is_realtime(bi->bi_owner, bi->bi_whichfork))
     324    91272707 :                 map->me_flags |= XFS_BMAP_EXTENT_REALTIME;
     325   217911436 : }
     326             : 
     327             : static struct xfs_log_item *
     328   217908546 : xfs_bmap_update_create_intent(
     329             :         struct xfs_trans                *tp,
     330             :         struct list_head                *items,
     331             :         unsigned int                    count,
     332             :         bool                            sort)
     333             : {
     334   217908546 :         struct xfs_mount                *mp = tp->t_mountp;
     335   217908546 :         struct xfs_bui_log_item         *buip = xfs_bui_init(mp);
     336   217909957 :         struct xfs_bmap_intent          *bi;
     337             : 
     338   217909957 :         ASSERT(count == XFS_BUI_MAX_FAST_EXTENTS);
     339             : 
     340   217909957 :         xfs_trans_add_item(tp, &buip->bui_item);
     341   217910220 :         if (sort)
     342   217910447 :                 list_sort(mp, items, xfs_bmap_update_diff_items);
     343   435820902 :         list_for_each_entry(bi, items, bi_list)
     344   217910078 :                 xfs_bmap_update_log_item(tp, buip, bi);
     345   217910824 :         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   217911817 : xfs_bmap_update_create_done(
     351             :         struct xfs_trans                *tp,
     352             :         struct xfs_log_item             *intent,
     353             :         unsigned int                    count)
     354             : {
     355   217911817 :         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   217911808 : xfs_bmap_update_get_group(
     361             :         struct xfs_mount        *mp,
     362             :         struct xfs_bmap_intent  *bi)
     363             : {
     364   217911808 :         xfs_agnumber_t          agno;
     365             : 
     366   217911808 :         if (xfs_ifork_is_realtime(bi->bi_owner, bi->bi_whichfork)) {
     367    91272449 :                 if (xfs_has_rtgroups(mp)) {
     368    91272349 :                         xfs_rgnumber_t  rgno;
     369             : 
     370    91272349 :                         rgno = xfs_rtb_to_rgno(mp, bi->bi_bmap.br_startblock);
     371    91272285 :                         bi->bi_rtg = xfs_rtgroup_intent_get(mp, rgno);
     372             :                 } else {
     373         100 :                         bi->bi_rtg = NULL;
     374             :                 }
     375             : 
     376    91273290 :                 return;
     377             :         }
     378             : 
     379   126638690 :         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   126638690 :         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   217913336 : xfs_bmap_update_put_group(
     394             :         struct xfs_bmap_intent  *bi)
     395             : {
     396   217913336 :         if (xfs_ifork_is_realtime(bi->bi_owner, bi->bi_whichfork)) {
     397    91273609 :                 if (xfs_has_rtgroups(bi->bi_owner->i_mount)) {
     398    91273511 :                         xfs_rtgroup_intent_put(bi->bi_rtg);
     399             :                 }
     400    91273643 :                 return;
     401             :         }
     402             : 
     403   126639513 :         xfs_perag_intent_put(bi->bi_pag);
     404             : }
     405             : 
     406             : /* Process a deferred rmap update. */
     407             : STATIC int
     408   217911950 : 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   217911950 :         struct xfs_bmap_intent          *bi;
     415   217911950 :         int                             error;
     416             : 
     417   217911950 :         bi = container_of(item, struct xfs_bmap_intent, bi_list);
     418             : 
     419   217911950 :         error = xfs_trans_log_finish_bmap_update(tp, BUD_ITEM(done), bi);
     420   217912485 :         if (!error && bi->bi_bmap.br_blockcount > 0) {
     421           0 :                 ASSERT(bi->bi_type == XFS_BMAP_UNMAP);
     422           0 :                 return -EAGAIN;
     423             :         }
     424             : 
     425   217912485 :         xfs_bmap_update_put_group(bi);
     426   217912173 :         kmem_cache_free(xfs_bmap_intent_cache, bi);
     427   217912173 :         return error;
     428             : }
     429             : 
     430             : /* Abort all pending BUIs. */
     431             : STATIC void
     432         397 : xfs_bmap_update_abort_intent(
     433             :         struct xfs_log_item             *intent)
     434             : {
     435         397 :         xfs_bui_release(BUI_ITEM(intent));
     436         397 : }
     437             : 
     438             : /* Cancel a deferred bmap update. */
     439             : STATIC void
     440         397 : xfs_bmap_update_cancel_item(
     441             :         struct list_head                *item)
     442             : {
     443         397 :         struct xfs_bmap_intent          *bi;
     444             : 
     445         397 :         bi = container_of(item, struct xfs_bmap_intent, bi_list);
     446             : 
     447         397 :         xfs_bmap_update_put_group(bi);
     448         397 :         kmem_cache_free(xfs_bmap_intent_cache, bi);
     449         397 : }
     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         528 : xfs_bui_validate(
     463             :         struct xfs_mount                *mp,
     464             :         struct xfs_bui_log_item         *buip)
     465             : {
     466         528 :         struct xfs_map_extent           *map;
     467             : 
     468             :         /* Only one mapping operation per BUI... */
     469         528 :         if (buip->bui_format.bui_nextents != XFS_BUI_MAX_FAST_EXTENTS)
     470             :                 return false;
     471             : 
     472         528 :         map = &buip->bui_format.bui_extents[0];
     473             : 
     474         528 :         if (map->me_flags & ~XFS_BMAP_EXTENT_FLAGS)
     475             :                 return false;
     476             : 
     477         528 :         switch (map->me_flags & XFS_BMAP_EXTENT_TYPE_MASK) {
     478             :         case XFS_BMAP_MAP:
     479             :         case XFS_BMAP_UNMAP:
     480         528 :                 break;
     481             :         default:
     482             :                 return false;
     483             :         }
     484             : 
     485         528 :         if (!xfs_verify_ino(mp, map->me_owner))
     486             :                 return false;
     487             : 
     488         528 :         if (!xfs_verify_fileext(mp, map->me_startoff, map->me_len))
     489             :                 return false;
     490             : 
     491         528 :         if (map->me_flags & XFS_BMAP_EXTENT_REALTIME)
     492         104 :                 return xfs_verify_rtbext(mp, map->me_startblock, map->me_len);
     493             : 
     494         424 :         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         528 : xfs_bui_item_recover(
     503             :         struct xfs_log_item             *lip,
     504             :         struct list_head                *capture_list)
     505             : {
     506         528 :         struct xfs_bmap_intent          fake = { };
     507         528 :         struct xfs_bui_log_item         *buip = BUI_ITEM(lip);
     508         528 :         struct xfs_trans                *tp;
     509         528 :         struct xfs_inode                *ip = NULL;
     510         528 :         struct xfs_mount                *mp = lip->li_log->l_mp;
     511         528 :         struct xfs_map_extent           *map;
     512         528 :         struct xfs_bud_log_item         *budp;
     513         528 :         int                             iext_delta;
     514         528 :         int                             error = 0;
     515             : 
     516         528 :         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         528 :         map = &buip->bui_format.bui_extents[0];
     523         528 :         fake.bi_whichfork = (map->me_flags & XFS_BMAP_EXTENT_ATTR_FORK) ?
     524         528 :                         XFS_ATTR_FORK : XFS_DATA_FORK;
     525         528 :         fake.bi_type = map->me_flags & XFS_BMAP_EXTENT_TYPE_MASK;
     526             : 
     527         528 :         error = xlog_recover_iget(mp, map->me_owner, &ip);
     528         528 :         if (error)
     529             :                 return error;
     530             : 
     531             :         /* Allocate transaction and do the work. */
     532         528 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
     533         528 :                         XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK), 0, 0, &tp);
     534         528 :         if (error)
     535           0 :                 goto err_rele;
     536             : 
     537         528 :         budp = xfs_trans_get_bud(tp, buip);
     538         528 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
     539         528 :         xfs_trans_ijoin(tp, ip, 0);
     540             : 
     541         528 :         if (!!(map->me_flags & XFS_BMAP_EXTENT_REALTIME) !=
     542         528 :             xfs_ifork_is_realtime(ip, fake.bi_whichfork)) {
     543           0 :                 error = -EFSCORRUPTED;
     544           0 :                 goto err_cancel;
     545             :         }
     546             : 
     547         528 :         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         528 :         error = xfs_iext_count_may_overflow(ip, fake.bi_whichfork, iext_delta);
     553         528 :         if (error == -EFBIG)
     554           0 :                 error = xfs_iext_count_upgrade(tp, ip, iext_delta);
     555         528 :         if (error)
     556           0 :                 goto err_cancel;
     557             : 
     558         528 :         fake.bi_owner = ip;
     559         528 :         fake.bi_bmap.br_startblock = map->me_startblock;
     560         528 :         fake.bi_bmap.br_startoff = map->me_startoff;
     561         528 :         fake.bi_bmap.br_blockcount = map->me_len;
     562         528 :         fake.bi_bmap.br_state = (map->me_flags & XFS_BMAP_EXTENT_UNWRITTEN) ?
     563         528 :                         XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
     564             : 
     565         528 :         xfs_bmap_update_get_group(mp, &fake);
     566         528 :         error = xfs_trans_log_finish_bmap_update(tp, budp, &fake);
     567         528 :         if (error == -EFSCORRUPTED)
     568           0 :                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, map,
     569             :                                 sizeof(*map));
     570         528 :         xfs_bmap_update_put_group(&fake);
     571         528 :         if (error)
     572           0 :                 goto err_cancel;
     573             : 
     574         528 :         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         528 :         error = xfs_defer_ops_capture_and_commit(tp, capture_list);
     585         528 :         if (error)
     586           0 :                 goto err_unlock;
     587             : 
     588         528 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     589         528 :         xfs_irele(ip);
     590         528 :         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        2955 : xfs_bui_item_match(
     603             :         struct xfs_log_item     *lip,
     604             :         uint64_t                intent_id)
     605             : {
     606        2955 :         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       66941 : xfs_bui_item_relog(
     612             :         struct xfs_log_item             *intent,
     613             :         struct xfs_trans                *tp)
     614             : {
     615       66941 :         struct xfs_bud_log_item         *budp;
     616       66941 :         struct xfs_bui_log_item         *buip;
     617       66941 :         struct xfs_map_extent           *map;
     618       66941 :         unsigned int                    count;
     619             : 
     620       66941 :         count = BUI_ITEM(intent)->bui_format.bui_nextents;
     621       66941 :         map = BUI_ITEM(intent)->bui_format.bui_extents;
     622             : 
     623       66941 :         tp->t_flags |= XFS_TRANS_DIRTY;
     624       66941 :         budp = xfs_trans_get_bud(tp, BUI_ITEM(intent));
     625       66941 :         set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags);
     626             : 
     627       66941 :         buip = xfs_bui_init(tp->t_mountp);
     628      133882 :         memcpy(buip->bui_format.bui_extents, map, count * sizeof(*map));
     629       66941 :         atomic_set(&buip->bui_next_extent, count);
     630       66941 :         xfs_trans_add_item(tp, &buip->bui_item);
     631       66941 :         set_bit(XFS_LI_DIRTY, &buip->bui_item.li_flags);
     632       66941 :         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        3475 : xfs_bui_copy_format(
     648             :         struct xfs_bui_log_format       *dst,
     649             :         const struct xfs_bui_log_format *src)
     650             : {
     651        3475 :         unsigned int                    i;
     652             : 
     653        6950 :         memcpy(dst, src, offsetof(struct xfs_bui_log_format, bui_extents));
     654             : 
     655        6950 :         for (i = 0; i < src->bui_nextents; i++)
     656        6950 :                 memcpy(&dst->bui_extents[i], &src->bui_extents[i],
     657             :                                 sizeof(struct xfs_map_extent));
     658        3475 : }
     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        3475 : 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        3475 :         struct xfs_mount                *mp = log->l_mp;
     675        3475 :         struct xfs_bui_log_item         *buip;
     676        3475 :         struct xfs_bui_log_format       *bui_formatp;
     677        3475 :         size_t                          len;
     678             : 
     679        3475 :         bui_formatp = item->ri_buf[0].i_addr;
     680             : 
     681        3475 :         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        3475 :         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        3475 :         len = xfs_bui_log_format_sizeof(bui_formatp->bui_nextents);
     694        3475 :         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        3475 :         buip = xfs_bui_init(mp);
     701        3475 :         xfs_bui_copy_format(&buip->bui_format, bui_formatp);
     702        3475 :         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        3475 :         xfs_trans_ail_insert(log->l_ailp, &buip->bui_item, lsn);
     708        3475 :         xfs_bui_release(buip);
     709        3475 :         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        2954 : 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        2954 :         struct xfs_bud_log_format       *bud_formatp;
     732             : 
     733        2954 :         bud_formatp = item->ri_buf[0].i_addr;
     734        2954 :         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        2954 :         xlog_recover_release_intent(log, XFS_LI_BUI, bud_formatp->bud_bui_id);
     741        2954 :         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